import {
createDelegatedSvmWalletClient,
delegatedSignTransaction
} from '@dynamic-labs-wallet/node-svm';
import {
Transaction,
SystemProgram,
PublicKey,
Connection,
sendAndConfirmTransaction
} from '@solana/web3.js';
const delegatedClient = createDelegatedSvmWalletClient({
environmentId: process.env.DYNAMIC_ENVIRONMENT_ID!,
apiKey: process.env.DYNAMIC_API_KEY!,
});
const connection = new Connection('https://api.mainnet-beta.solana.com');
async function sendPayment(
userId: string,
recipientAddress: string,
amount: number
) {
const credentials = await getDelegationCredentials(userId);
const senderAddress = credentials.publicKey;
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(senderAddress),
toPubkey: new PublicKey(recipientAddress),
lamports: amount,
})
);
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(senderAddress);
const signedTx = await delegatedSignTransaction(delegatedClient, {
walletId: credentials.walletId,
walletApiKey: credentials.walletApiKey,
keyShare: credentials.keyShare,
transaction,
});
const signature = await connection.sendRawTransaction(
signedTx.serialize()
);
await connection.confirmTransaction(signature);
console.log('Transaction confirmed:', signature);
return signature;
}