Skip to content

Commit 1616f8f

Browse files
committed
refactor: replace solana deps
1 parent dd6b4c6 commit 1616f8f

File tree

9 files changed

+1539
-1338
lines changed

9 files changed

+1539
-1338
lines changed

examples/relayers/solana/feeEstimate_rpc.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as solana from '@solana/web3.js';
2-
31
/**
42
* Solana estimateFee RPC Example
53
*
@@ -22,15 +20,15 @@ import * as solana from '@solana/web3.js';
2220
*/
2321
import { Configuration, RelayersApi } from '../../../src';
2422

25-
import { createTokenTransfer } from './util';
26-
import { getAssociatedTokenAddress } from '@solana/spl-token';
23+
import { createSolanaRpc } from '@solana/kit';
24+
import { getSerializedTokenTransfer } from './util';
2725

28-
const connection = new solana.Connection(solana.clusterApiUrl('devnet'));
26+
const rpc = createSolanaRpc('https://api.devnet.solana.com');
2927

3028
// example dev config
3129
const config = new Configuration({
3230
basePath: 'http://localhost:8080',
33-
accessToken: '', // replace with your actual api key
31+
accessToken: '9df4bf0d-9b38-457f-9673-0afffd5d411a', // replace with your actual api key
3432
});
3533

3634
const relayersApi = new RelayersApi(config);
@@ -41,33 +39,21 @@ const source = 'EYsk8PduFSAt7W9dnvL2Pt7qcVsb5wAVCYbJ5UQaUpXf';
4139
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
4240
const usdcToken = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr';
4341

44-
const usdcMint = new solana.PublicKey(usdcToken);
45-
const sourceWalletAddress = new solana.PublicKey(source);
46-
const destinationWalletAddress = new solana.PublicKey(destination);
47-
4842
async function estimateFee() {
4943
try {
50-
const sourceTokenAccount = await getAssociatedTokenAddress(usdcMint, sourceWalletAddress);
51-
const destinationTokenAccount = await getAssociatedTokenAddress(usdcMint, destinationWalletAddress);
52-
const { blockhash } = await connection.getLatestBlockhash();
53-
console.log(`Latest blockhash: ${blockhash}`);
44+
// Get latest blockhash
45+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
46+
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);
5447

55-
const transaction = createTokenTransfer(
56-
source,
57-
sourceTokenAccount,
58-
destinationTokenAccount,
48+
// Create the serialized transaction using the util function
49+
const serializedTransaction = await getSerializedTokenTransfer(
5950
source,
51+
destination,
52+
usdcToken,
6053
1000000, // Amount (consider token decimals)
61-
blockhash,
54+
latestBlockhash.blockhash,
6255
);
6356

64-
const serializedTransaction = transaction
65-
.serialize({
66-
requireAllSignatures: false,
67-
verifySignatures: false,
68-
})
69-
.toString('base64');
70-
7157
// Estimate fee using the relayer
7258
const feeEstimate = await relayersApi.rpc(relayer_id, {
7359
method: 'feeEstimate',

examples/relayers/solana/prepareTransactionToken2022_rpc.ts

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import * as solana from '@solana/web3.js';
2-
31
/**
42
* Solana prepareTransactionToken2022 RPC Example
53
*
64
* This example demonstrates how to use the OpenZeppelin Relayer SDK to prepare a Solana
7-
* transaction for sponsored submission.
5+
* transaction for sponsored submission using Token2022.
86
*
97
* Prepare a transaction to be signed by adding relayer-specific instructions, such as updating
108
* the fee payer and including relayer-specific instructions.
@@ -18,14 +16,14 @@ import * as solana from '@solana/web3.js';
1816
* - Use https connection for production applications
1917
*
2018
* Usage:
21-
* ts-node prepareTransaction_rpc.ts
19+
* ts-node prepareTransactionToken2022_rpc.ts
2220
*/
2321
import { Configuration, RelayersApi } from '../../../src';
24-
import { TOKEN_2022_PROGRAM_ID, getAssociatedTokenAddress } from '@solana/spl-token';
2522

26-
import { createToken2022Transfer } from './util';
23+
import { createSolanaRpc } from '@solana/kit';
24+
import { getSerializedToken2022Transfer } from './util';
2725

28-
const connection = new solana.Connection(solana.clusterApiUrl('devnet'));
26+
const rpc = createSolanaRpc('https://api.devnet.solana.com');
2927

3028
// example dev config
3129
const config = new Configuration({
@@ -36,53 +34,27 @@ const config = new Configuration({
3634
const relayersApi = new RelayersApi(config);
3735

3836
// Replace with your actual values
39-
const relayer_id = '';
37+
const relayer_id = 'solana-example';
4038
const source = '';
4139
const destination = '';
4240
const token = ''; // Token2022 mint address
4341

44-
const tokenMint = new solana.PublicKey(token);
45-
const sourceWalletAddress = new solana.PublicKey(source);
46-
const destinationWalletAddress = new solana.PublicKey(destination);
47-
4842
async function prepareTransactionToken2022() {
4943
try {
50-
const sourceTokenAccount = await getAssociatedTokenAddress(
51-
tokenMint,
52-
sourceWalletAddress,
53-
true,
54-
TOKEN_2022_PROGRAM_ID,
55-
);
56-
const destinationTokenAccount = await getAssociatedTokenAddress(
57-
tokenMint,
58-
destinationWalletAddress,
59-
true,
60-
TOKEN_2022_PROGRAM_ID,
61-
);
44+
// Get latest blockhash
45+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
46+
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);
6247

63-
console.log(`Source token account: ${sourceTokenAccount}`);
64-
console.log(`Destination token account: ${destinationTokenAccount}`);
65-
const { blockhash } = await connection.getLatestBlockhash();
66-
console.log(`Latest blockhash: ${blockhash}`);
67-
68-
const transaction = createToken2022Transfer(
69-
source,
70-
sourceTokenAccount,
71-
destinationTokenAccount,
48+
// Create the serialized Token2022 transaction using the util function
49+
const serializedTransaction = await getSerializedToken2022Transfer(
7250
source,
73-
tokenMint,
51+
destination,
52+
token,
7453
1000000, // Amount (consider token decimals)
75-
9,
76-
blockhash,
54+
9, // Token decimals
55+
latestBlockhash.blockhash,
7756
);
7857

79-
const serializedTransaction = transaction
80-
.serialize({
81-
requireAllSignatures: false,
82-
verifySignatures: false,
83-
})
84-
.toString('base64');
85-
8658
// Prepare transaction using the relayer
8759
const prepareTransaction = await relayersApi.rpc(relayer_id, {
8860
method: 'prepareTransaction',

examples/relayers/solana/prepareTransaction_rpc.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as solana from '@solana/web3.js';
2-
31
/**
42
* Solana prepareTransaction RPC Example
53
*
@@ -22,15 +20,15 @@ import * as solana from '@solana/web3.js';
2220
*/
2321
import { Configuration, RelayersApi } from '../../../src';
2422

25-
import { createTokenTransfer } from './util';
26-
import { getAssociatedTokenAddress } from '@solana/spl-token';
23+
import { createSolanaRpc } from '@solana/kit';
24+
import { getSerializedTokenTransfer } from './util';
2725

28-
const connection = new solana.Connection(solana.clusterApiUrl('devnet'));
26+
const rpc = createSolanaRpc('https://api.devnet.solana.com');
2927

3028
// example dev config
3129
const config = new Configuration({
3230
basePath: 'http://localhost:8080',
33-
accessToken: '', // replace with your actual api key
31+
accessToken: '9df4bf0d-9b38-457f-9673-0afffd5d411a', // replace with your actual api key
3432
});
3533

3634
const relayersApi = new RelayersApi(config);
@@ -41,36 +39,21 @@ const source = 'EYsk8PduFSAt7W9dnvL2Pt7qcVsb5wAVCYbJ5UQaUpXf';
4139
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
4240
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address
4341

44-
const tokenMint = new solana.PublicKey(token);
45-
const sourceWalletAddress = new solana.PublicKey(source);
46-
const destinationWalletAddress = new solana.PublicKey(destination);
47-
4842
async function prepareTransaction() {
4943
try {
50-
const sourceTokenAccount = await getAssociatedTokenAddress(tokenMint, sourceWalletAddress);
51-
const destinationTokenAccount = await getAssociatedTokenAddress(tokenMint, destinationWalletAddress);
52-
53-
console.log(`Source token account: ${sourceTokenAccount}`);
54-
console.log(`Destination token account: ${destinationTokenAccount}`);
55-
const { blockhash } = await connection.getLatestBlockhash();
56-
console.log(`Latest blockhash: ${blockhash}`);
44+
// Get latest blockhash
45+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
46+
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);
5747

58-
const transaction = createTokenTransfer(
59-
source,
60-
sourceTokenAccount,
61-
destinationTokenAccount,
48+
// Create the serialized transaction using the util function
49+
const serializedTransaction = await getSerializedTokenTransfer(
6250
source,
51+
destination,
52+
token,
6353
1000000, // Amount (consider token decimals)
64-
blockhash,
54+
latestBlockhash.blockhash,
6555
);
6656

67-
const serializedTransaction = transaction
68-
.serialize({
69-
requireAllSignatures: false,
70-
verifySignatures: false,
71-
})
72-
.toString('base64');
73-
7457
// Prepare transaction using the relayer
7558
const prepareTransaction = await relayersApi.rpc(relayer_id, {
7659
method: 'prepareTransaction',

examples/relayers/solana/signAndSendTransaction_rpc.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as solana from '@solana/web3.js';
2-
31
/**
42
* Solana signAndSendTransaction RPC Example
53
*
@@ -22,53 +20,40 @@ import * as solana from '@solana/web3.js';
2220
*/
2321
import { Configuration, RelayersApi } from '../../../src';
2422

25-
import { createTokenTransfer } from './util';
26-
import { getAssociatedTokenAddress } from '@solana/spl-token';
23+
import { createSolanaRpc } from '@solana/kit';
24+
import { getSerializedTokenTransfer } from './util';
2725

28-
const connection = new solana.Connection(solana.clusterApiUrl('devnet'));
26+
const rpc = createSolanaRpc('https://api.devnet.solana.com');
2927

3028
// example dev config
3129
const config = new Configuration({
3230
basePath: 'http://localhost:8080',
33-
accessToken: '', // replace with your actual api key
31+
accessToken: '9df4bf0d-9b38-457f-9673-0afffd5d411a', // replace with your actual api key
3432
});
3533

3634
const relayersApi = new RelayersApi(config);
3735

3836
// Replace with your actual values
3937
const relayer_id = 'solana-example';
40-
const source = 'C6VBV1EK2Jx7kFgCkCD5wuDeQtEH8ct2hHGUPzEhUSc8';
41-
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
42-
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address
43-
44-
const tokenMint = new solana.PublicKey(token);
45-
const sourceWalletAddress = new solana.PublicKey(source);
46-
const destinationWalletAddress = new solana.PublicKey(destination);
38+
const source = 'DiUZ95hZn7cJCY6THuuGQUPMv4bfTuSCUraunmD5PdoZ';
39+
const destination = '6S9v8CedUumV7qbqq37v2GfBRxWemA6zpVGjQsiVHSZ4';
40+
const token = '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'; // USDC token mint address
4741

4842
async function signAndSendTransaction() {
4943
try {
50-
const sourceTokenAccount = await getAssociatedTokenAddress(tokenMint, sourceWalletAddress);
51-
const destinationTokenAccount = await getAssociatedTokenAddress(tokenMint, destinationWalletAddress);
52-
// Get the latest blockhash from devnet
53-
const { blockhash } = await connection.getLatestBlockhash();
54-
console.log(`Latest blockhash: ${blockhash}`);
44+
// Get latest blockhash
45+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
46+
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);
5547

56-
const transaction = createTokenTransfer(
57-
source,
58-
sourceTokenAccount,
59-
destinationTokenAccount,
48+
// Create the serialized transaction using the util function
49+
const serializedTransaction = await getSerializedTokenTransfer(
6050
source,
51+
destination,
52+
token,
6153
1000000, // Amount (consider token decimals)
62-
blockhash,
54+
latestBlockhash.blockhash,
6355
);
6456

65-
const serializedTransaction = transaction
66-
.serialize({
67-
requireAllSignatures: false,
68-
verifySignatures: false,
69-
})
70-
.toString('base64');
71-
7257
// Sign and send transaction using the relayer
7358
const signAndSendTransaction = await relayersApi.rpc(relayer_id, {
7459
method: 'signAndSendTransaction',

examples/relayers/solana/signTransaction_rpc.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as solana from '@solana/web3.js';
2-
31
/**
42
* Solana signTransaction RPC Example
53
*
@@ -21,10 +19,10 @@ import * as solana from '@solana/web3.js';
2119
*/
2220
import { Configuration, RelayersApi } from '../../../src';
2321

24-
import { createTokenTransfer } from './util';
25-
import { getAssociatedTokenAddress } from '@solana/spl-token';
22+
import { createSolanaRpc } from '@solana/kit';
23+
import { getSerializedTokenTransfer } from './util';
2624

27-
const connection = new solana.Connection(solana.clusterApiUrl('devnet'));
25+
const rpc = createSolanaRpc('https://api.devnet.solana.com');
2826

2927
// example dev config
3028
const config = new Configuration({
@@ -36,37 +34,25 @@ const relayersApi = new RelayersApi(config);
3634

3735
// Replace with your actual values
3836
const relayer_id = 'solana-example';
39-
const source = 'C6VBV1EK2Jx7kFgCkCD5wuDeQtEH8ct2hHGUPzEhUSc8';
40-
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
41-
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address
42-
43-
const usdcMint = new solana.PublicKey(token);
44-
const sourceWalletAddress = new solana.PublicKey(source);
45-
const destinationWalletAddress = new solana.PublicKey(destination);
37+
const source = 'DiUZ95hZn7cJCY6THuuGQUPMv4bfTuSCUraunmD5PdoZ';
38+
const destination = '6S9v8CedUumV7qbqq37v2GfBRxWemA6zpVGjQsiVHSZ4';
39+
const token = '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'; // USDC token mint address
4640

4741
async function signTransaction() {
4842
try {
49-
const sourceTokenAccount = await getAssociatedTokenAddress(usdcMint, sourceWalletAddress);
50-
const destinationTokenAccount = await getAssociatedTokenAddress(usdcMint, destinationWalletAddress);
51-
const { blockhash } = await connection.getLatestBlockhash();
52-
console.log(`Latest blockhash: ${blockhash}`);
43+
// Get latest blockhash
44+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
45+
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);
5346

54-
const transaction = createTokenTransfer(
55-
source,
56-
sourceTokenAccount,
57-
destinationTokenAccount,
47+
// Create the serialized transaction using the util function
48+
const serializedTransaction = await getSerializedTokenTransfer(
5849
source,
50+
destination,
51+
token,
5952
1000000, // Amount (consider token decimals)
60-
blockhash,
53+
latestBlockhash.blockhash,
6154
);
6255

63-
const serializedTransaction = transaction
64-
.serialize({
65-
requireAllSignatures: false,
66-
verifySignatures: false,
67-
})
68-
.toString('base64');
69-
7056
// Sign transaction using the relayer
7157
const signTransaction = await relayersApi.rpc(relayer_id, {
7258
method: 'signTransaction',

0 commit comments

Comments
 (0)