UNPKG

@coinbase/cdp-sdk

Version:

SDK for interacting with the Coinbase Developer Platform Wallet API

104 lines 4.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transfer = transfer; const spl_token_1 = require("@solana/spl-token"); const web3_js_1 = require("@solana/web3.js"); const sendTransaction_js_1 = require("./sendTransaction.js"); const utils_js_1 = require("./utils.js"); /** * Transfers SOL or SPL tokens between accounts * * @param apiClient - The API client to use * @param options - The transfer options * * @returns The transfer result */ async function transfer(apiClient, options) { const connection = (0, utils_js_1.getOrCreateConnection)({ networkOrConnection: options.network }); const connectedNetwork = await (0, utils_js_1.getConnectedNetwork)(connection); const tx = options.token === "sol" ? await getNativeTransfer({ from: options.from, to: options.to, amount: options.amount, }) : await getSplTransfer({ connection, from: options.from, to: options.to, mintAddress: options.token === "usdc" ? (0, utils_js_1.getUsdcMintAddress)(connectedNetwork) : options.token, amount: options.amount, }); const serializedTx = Buffer.from(tx.serialize({ requireAllSignatures: false })).toString("base64"); const signature = await (0, sendTransaction_js_1.sendTransaction)(apiClient, { network: connectedNetwork === "mainnet" ? "solana" : "solana-devnet", transaction: serializedTx, }); return signature; } /** * Gets the transaction for a native SOL transfer * * @param options - The options for the native SOL transfer * * @param options.from - The source address * @param options.to - The destination address * @param options.amount - The amount to transfer * * @returns The native SOL transfer transaction */ async function getNativeTransfer({ from, to, amount, }) { const transaction = new web3_js_1.Transaction(); transaction.add(web3_js_1.SystemProgram.transfer({ fromPubkey: new web3_js_1.PublicKey(from), toPubkey: new web3_js_1.PublicKey(to), lamports: amount, })); transaction.recentBlockhash = web3_js_1.SYSVAR_RECENT_BLOCKHASHES_PUBKEY.toBase58(); transaction.feePayer = new web3_js_1.PublicKey(from); return transaction; } /** * Gets the transaction for a SPL token transfer * * @param options - The options for the SPL token transfer * * @param options.connection - The Solana connection * @param options.from - The source address * @param options.to - The destination address * @param options.mintAddress - The mint address of the token * @param options.amount - The amount to transfer * * @returns The SPL token transfer transaction */ async function getSplTransfer({ connection, from, to, mintAddress, amount, }) { const fromPubkey = new web3_js_1.PublicKey(from); const toPubkey = new web3_js_1.PublicKey(to); const mintPubkey = new web3_js_1.PublicKey(mintAddress); let mintInfo; try { mintInfo = await (0, spl_token_1.getMint)(connection, mintPubkey); } catch (error) { throw new Error(`Failed to fetch mint info for mint address ${mintAddress}. Error: ${error}`); } const sourceAta = await (0, spl_token_1.getAssociatedTokenAddress)(mintPubkey, fromPubkey); const destinationAta = await (0, spl_token_1.getAssociatedTokenAddress)(mintPubkey, toPubkey); const transaction = new web3_js_1.Transaction(); const sourceAccount = await (0, spl_token_1.getAccount)(connection, sourceAta); if (sourceAccount.amount < amount) { throw new Error(`Insufficient token balance. Have ${sourceAccount.amount}, need ${amount}`); } // Check if destination account exists, if not create it try { await (0, spl_token_1.getAccount)(connection, destinationAta); } catch { transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(fromPubkey, destinationAta, toPubkey, mintPubkey)); } transaction.add((0, spl_token_1.createTransferCheckedInstruction)(sourceAta, mintPubkey, destinationAta, fromPubkey, amount, mintInfo.decimals)); transaction.recentBlockhash = web3_js_1.SYSVAR_RECENT_BLOCKHASHES_PUBKEY.toBase58(); transaction.feePayer = new web3_js_1.PublicKey(from); return transaction; } //# sourceMappingURL=transfer.js.map