Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 11 additions & 25 deletions examples/relayers/solana/feeEstimate_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as solana from '@solana/web3.js';

/**
* Solana estimateFee RPC Example
*
Expand All @@ -22,10 +20,10 @@ import * as solana from '@solana/web3.js';
*/
import { Configuration, RelayersApi } from '../../../src';

import { createTokenTransfer } from './util';
import { getAssociatedTokenAddress } from '@solana/spl-token';
import { createSolanaRpc } from '@solana/kit';
import { getSerializedTokenTransfer } from './util';

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

// example dev config
const config = new Configuration({
Expand All @@ -41,33 +39,21 @@ const source = 'EYsk8PduFSAt7W9dnvL2Pt7qcVsb5wAVCYbJ5UQaUpXf';
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
const usdcToken = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr';

const usdcMint = new solana.PublicKey(usdcToken);
const sourceWalletAddress = new solana.PublicKey(source);
const destinationWalletAddress = new solana.PublicKey(destination);

async function estimateFee() {
try {
const sourceTokenAccount = await getAssociatedTokenAddress(usdcMint, sourceWalletAddress);
const destinationTokenAccount = await getAssociatedTokenAddress(usdcMint, destinationWalletAddress);
const { blockhash } = await connection.getLatestBlockhash();
console.log(`Latest blockhash: ${blockhash}`);
// Get latest blockhash
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);

const transaction = createTokenTransfer(
source,
sourceTokenAccount,
destinationTokenAccount,
// Create the serialized transaction using the util function
const serializedTransaction = await getSerializedTokenTransfer(
source,
destination,
usdcToken,
1000000, // Amount (consider token decimals)
blockhash,
latestBlockhash.blockhash,
);

const serializedTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');

// Estimate fee using the relayer
const feeEstimate = await relayersApi.rpc(relayer_id, {
method: 'feeEstimate',
Expand Down
56 changes: 14 additions & 42 deletions examples/relayers/solana/prepareTransactionToken2022_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as solana from '@solana/web3.js';

/**
* Solana prepareTransactionToken2022 RPC Example
*
* This example demonstrates how to use the OpenZeppelin Relayer SDK to prepare a Solana
* transaction for sponsored submission.
* transaction for sponsored submission using Token2022.
*
* Prepare a transaction to be signed by adding relayer-specific instructions, such as updating
* the fee payer and including relayer-specific instructions.
Expand All @@ -18,14 +16,14 @@ import * as solana from '@solana/web3.js';
* - Use https connection for production applications
*
* Usage:
* ts-node prepareTransaction_rpc.ts
* ts-node prepareTransactionToken2022_rpc.ts
*/
import { Configuration, RelayersApi } from '../../../src';
import { TOKEN_2022_PROGRAM_ID, getAssociatedTokenAddress } from '@solana/spl-token';

import { createToken2022Transfer } from './util';
import { createSolanaRpc } from '@solana/kit';
import { getSerializedToken2022Transfer } from './util';

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

// example dev config
const config = new Configuration({
Expand All @@ -41,48 +39,22 @@ const source = '';
const destination = '';
const token = ''; // Token2022 mint address

const tokenMint = new solana.PublicKey(token);
const sourceWalletAddress = new solana.PublicKey(source);
const destinationWalletAddress = new solana.PublicKey(destination);

async function prepareTransactionToken2022() {
try {
const sourceTokenAccount = await getAssociatedTokenAddress(
tokenMint,
sourceWalletAddress,
true,
TOKEN_2022_PROGRAM_ID,
);
const destinationTokenAccount = await getAssociatedTokenAddress(
tokenMint,
destinationWalletAddress,
true,
TOKEN_2022_PROGRAM_ID,
);
// Get latest blockhash
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);

console.log(`Source token account: ${sourceTokenAccount}`);
console.log(`Destination token account: ${destinationTokenAccount}`);
const { blockhash } = await connection.getLatestBlockhash();
console.log(`Latest blockhash: ${blockhash}`);

const transaction = createToken2022Transfer(
source,
sourceTokenAccount,
destinationTokenAccount,
// Create the serialized Token2022 transaction using the util function
const serializedTransaction = await getSerializedToken2022Transfer(
source,
tokenMint,
destination,
token,
1000000, // Amount (consider token decimals)
9,
blockhash,
9, // Token decimals
latestBlockhash.blockhash,
);

const serializedTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');

// Prepare transaction using the relayer
const prepareTransaction = await relayersApi.rpc(relayer_id, {
method: 'prepareTransaction',
Expand Down
39 changes: 11 additions & 28 deletions examples/relayers/solana/prepareTransaction_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as solana from '@solana/web3.js';

/**
* Solana prepareTransaction RPC Example
*
Expand All @@ -22,10 +20,10 @@ import * as solana from '@solana/web3.js';
*/
import { Configuration, RelayersApi } from '../../../src';

import { createTokenTransfer } from './util';
import { getAssociatedTokenAddress } from '@solana/spl-token';
import { createSolanaRpc } from '@solana/kit';
import { getSerializedTokenTransfer } from './util';

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

// example dev config
const config = new Configuration({
Expand All @@ -41,36 +39,21 @@ const source = 'EYsk8PduFSAt7W9dnvL2Pt7qcVsb5wAVCYbJ5UQaUpXf';
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address

const tokenMint = new solana.PublicKey(token);
const sourceWalletAddress = new solana.PublicKey(source);
const destinationWalletAddress = new solana.PublicKey(destination);

async function prepareTransaction() {
try {
const sourceTokenAccount = await getAssociatedTokenAddress(tokenMint, sourceWalletAddress);
const destinationTokenAccount = await getAssociatedTokenAddress(tokenMint, destinationWalletAddress);

console.log(`Source token account: ${sourceTokenAccount}`);
console.log(`Destination token account: ${destinationTokenAccount}`);
const { blockhash } = await connection.getLatestBlockhash();
console.log(`Latest blockhash: ${blockhash}`);
// Get latest blockhash
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);

const transaction = createTokenTransfer(
source,
sourceTokenAccount,
destinationTokenAccount,
// Create the serialized transaction using the util function
const serializedTransaction = await getSerializedTokenTransfer(
source,
destination,
token,
1000000, // Amount (consider token decimals)
blockhash,
latestBlockhash.blockhash,
);

const serializedTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');

// Prepare transaction using the relayer
const prepareTransaction = await relayersApi.rpc(relayer_id, {
method: 'prepareTransaction',
Expand Down
43 changes: 14 additions & 29 deletions examples/relayers/solana/signAndSendTransaction_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as solana from '@solana/web3.js';

/**
* Solana signAndSendTransaction RPC Example
*
Expand All @@ -22,10 +20,10 @@ import * as solana from '@solana/web3.js';
*/
import { Configuration, RelayersApi } from '../../../src';

import { createTokenTransfer } from './util';
import { getAssociatedTokenAddress } from '@solana/spl-token';
import { createSolanaRpc } from '@solana/kit';
import { getSerializedTokenTransfer } from './util';

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

// example dev config
const config = new Configuration({
Expand All @@ -37,38 +35,25 @@ const relayersApi = new RelayersApi(config);

// Replace with your actual values
const relayer_id = 'solana-example';
const source = 'C6VBV1EK2Jx7kFgCkCD5wuDeQtEH8ct2hHGUPzEhUSc8';
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address

const tokenMint = new solana.PublicKey(token);
const sourceWalletAddress = new solana.PublicKey(source);
const destinationWalletAddress = new solana.PublicKey(destination);
const source = 'DiUZ95hZn7cJCY6THuuGQUPMv4bfTuSCUraunmD5PdoZ';
const destination = '6S9v8CedUumV7qbqq37v2GfBRxWemA6zpVGjQsiVHSZ4';
const token = '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'; // USDC token mint address

async function signAndSendTransaction() {
try {
const sourceTokenAccount = await getAssociatedTokenAddress(tokenMint, sourceWalletAddress);
const destinationTokenAccount = await getAssociatedTokenAddress(tokenMint, destinationWalletAddress);
// Get the latest blockhash from devnet
const { blockhash } = await connection.getLatestBlockhash();
console.log(`Latest blockhash: ${blockhash}`);
// Get latest blockhash
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);

const transaction = createTokenTransfer(
source,
sourceTokenAccount,
destinationTokenAccount,
// Create the serialized transaction using the util function
const serializedTransaction = await getSerializedTokenTransfer(
source,
destination,
token,
1000000, // Amount (consider token decimals)
blockhash,
latestBlockhash.blockhash,
);

const serializedTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');

// Sign and send transaction using the relayer
const signAndSendTransaction = await relayersApi.rpc(relayer_id, {
method: 'signAndSendTransaction',
Expand Down
42 changes: 14 additions & 28 deletions examples/relayers/solana/signTransaction_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as solana from '@solana/web3.js';

/**
* Solana signTransaction RPC Example
*
Expand All @@ -21,10 +19,10 @@ import * as solana from '@solana/web3.js';
*/
import { Configuration, RelayersApi } from '../../../src';

import { createTokenTransfer } from './util';
import { getAssociatedTokenAddress } from '@solana/spl-token';
import { createSolanaRpc } from '@solana/kit';
import { getSerializedTokenTransfer } from './util';

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

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

// Replace with your actual values
const relayer_id = 'solana-example';
const source = 'C6VBV1EK2Jx7kFgCkCD5wuDeQtEH8ct2hHGUPzEhUSc8';
const destination = 'Gt6wiPeC3XqNZKnMcM2dbRZCkKr1PtytBxf9hhV7Hxew';
const token = 'Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr'; // USDC token mint address

const usdcMint = new solana.PublicKey(token);
const sourceWalletAddress = new solana.PublicKey(source);
const destinationWalletAddress = new solana.PublicKey(destination);
const source = 'DiUZ95hZn7cJCY6THuuGQUPMv4bfTuSCUraunmD5PdoZ';
const destination = '6S9v8CedUumV7qbqq37v2GfBRxWemA6zpVGjQsiVHSZ4';
const token = '4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU'; // USDC token mint address

async function signTransaction() {
try {
const sourceTokenAccount = await getAssociatedTokenAddress(usdcMint, sourceWalletAddress);
const destinationTokenAccount = await getAssociatedTokenAddress(usdcMint, destinationWalletAddress);
const { blockhash } = await connection.getLatestBlockhash();
console.log(`Latest blockhash: ${blockhash}`);
// Get latest blockhash
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
console.log(`Latest blockhash: ${latestBlockhash.blockhash}`);

const transaction = createTokenTransfer(
source,
sourceTokenAccount,
destinationTokenAccount,
// Create the serialized transaction using the util function
const serializedTransaction = await getSerializedTokenTransfer(
source,
destination,
token,
1000000, // Amount (consider token decimals)
blockhash,
latestBlockhash.blockhash,
);

const serializedTransaction = transaction
.serialize({
requireAllSignatures: false,
verifySignatures: false,
})
.toString('base64');

// Sign transaction using the relayer
const signTransaction = await relayersApi.rpc(relayer_id, {
method: 'signTransaction',
Expand Down
Loading
Loading