|
| 1 | +// This script is used to initiate a native token Solana deposit. useful in testing. |
| 2 | + |
| 3 | +import * as anchor from "@coral-xyz/anchor"; |
| 4 | +import { AnchorProvider, BN } from "@coral-xyz/anchor"; |
| 5 | +import { |
| 6 | + ASSOCIATED_TOKEN_PROGRAM_ID, |
| 7 | + NATIVE_MINT, |
| 8 | + TOKEN_PROGRAM_ID, |
| 9 | + createApproveCheckedInstruction, |
| 10 | + createAssociatedTokenAccountIdempotentInstruction, |
| 11 | + createCloseAccountInstruction, |
| 12 | + createSyncNativeInstruction, |
| 13 | + getAssociatedTokenAddressSync, |
| 14 | + getMinimumBalanceForRentExemptAccount, |
| 15 | + getMint, |
| 16 | +} from "@solana/spl-token"; |
| 17 | +import { |
| 18 | + PublicKey, |
| 19 | + Transaction, |
| 20 | + sendAndConfirmTransaction, |
| 21 | + TransactionInstruction, |
| 22 | + SystemProgram, |
| 23 | +} from "@solana/web3.js"; |
| 24 | +import yargs from "yargs"; |
| 25 | +import { hideBin } from "yargs/helpers"; |
| 26 | +import { getSpokePoolProgram, SOLANA_SPOKE_STATE_SEED } from "../../src/svm/web3-v1"; |
| 27 | + |
| 28 | +// Set up the provider |
| 29 | +const provider = AnchorProvider.env(); |
| 30 | +anchor.setProvider(provider); |
| 31 | +const program = getSpokePoolProgram(provider); |
| 32 | +const programId = program.programId; |
| 33 | +console.log("SVM-Spoke Program ID:", programId.toString()); |
| 34 | + |
| 35 | +// Parse arguments |
| 36 | +const argv = yargs(hideBin(process.argv)) |
| 37 | + .option("recipient", { type: "string", demandOption: true, describe: "Recipient public key" }) |
| 38 | + .option("outputToken", { type: "string", demandOption: true, describe: "Output token public key" }) |
| 39 | + .option("inputAmount", { type: "number", demandOption: true, describe: "Input amount" }) |
| 40 | + .option("outputAmount", { type: "number", demandOption: true, describe: "Output amount" }) |
| 41 | + .option("destinationChainId", { type: "string", demandOption: true, describe: "Destination chain ID" }) |
| 42 | + .option("integratorId", { type: "string", demandOption: false, describe: "integrator ID" }).argv; |
| 43 | + |
| 44 | +async function nativeDeposit(): Promise<void> { |
| 45 | + const resolvedArgv = await argv; |
| 46 | + const seed = SOLANA_SPOKE_STATE_SEED; |
| 47 | + const recipient = new PublicKey(resolvedArgv.recipient); |
| 48 | + const inputToken = NATIVE_MINT; |
| 49 | + const outputToken = new PublicKey(resolvedArgv.outputToken); |
| 50 | + const inputAmount = new BN(resolvedArgv.inputAmount); |
| 51 | + const outputAmount = new BN(resolvedArgv.outputAmount); |
| 52 | + const destinationChainId = new BN(resolvedArgv.destinationChainId); |
| 53 | + const exclusiveRelayer = PublicKey.default; |
| 54 | + const quoteTimestamp = Math.floor(Date.now() / 1000) - 1; |
| 55 | + const fillDeadline = quoteTimestamp + 3600; // 1 hour from now |
| 56 | + const exclusivityDeadline = 0; |
| 57 | + const message = Buffer.from([]); // Convert to Buffer |
| 58 | + const integratorId = resolvedArgv.integratorId || ""; |
| 59 | + // Define the state account PDA |
| 60 | + const [statePda, _] = PublicKey.findProgramAddressSync( |
| 61 | + [Buffer.from("state"), seed.toArrayLike(Buffer, "le", 8)], |
| 62 | + programId |
| 63 | + ); |
| 64 | + |
| 65 | + // Define the signer (replace with your actual signer) |
| 66 | + const signer = (provider.wallet as anchor.Wallet).payer; |
| 67 | + |
| 68 | + // Find ATA for the input token to be stored by state (vault). This was created when the route was enabled. |
| 69 | + const vault = getAssociatedTokenAddressSync( |
| 70 | + inputToken, |
| 71 | + statePda, |
| 72 | + true, |
| 73 | + TOKEN_PROGRAM_ID, |
| 74 | + ASSOCIATED_TOKEN_PROGRAM_ID |
| 75 | + ); |
| 76 | + |
| 77 | + const userTokenAccount = getAssociatedTokenAddressSync(inputToken, signer.publicKey); |
| 78 | + const userTokenAccountInfo = await provider.connection.getAccountInfo(userTokenAccount); |
| 79 | + const existingTokenAccount = userTokenAccountInfo !== null && userTokenAccountInfo.owner.equals(TOKEN_PROGRAM_ID); |
| 80 | + |
| 81 | + console.log("Depositing V3..."); |
| 82 | + console.table([ |
| 83 | + { property: "seed", value: seed.toString() }, |
| 84 | + { property: "recipient", value: recipient.toString() }, |
| 85 | + { property: "inputToken", value: inputToken.toString() }, |
| 86 | + { property: "outputToken", value: outputToken.toString() }, |
| 87 | + { property: "inputAmount", value: inputAmount.toString() }, |
| 88 | + { property: "outputAmount", value: outputAmount.toString() }, |
| 89 | + { property: "destinationChainId", value: destinationChainId.toString() }, |
| 90 | + { property: "quoteTimestamp", value: quoteTimestamp.toString() }, |
| 91 | + { property: "fillDeadline", value: fillDeadline.toString() }, |
| 92 | + { property: "exclusivityDeadline", value: exclusivityDeadline.toString() }, |
| 93 | + { property: "message", value: message.toString("hex") }, |
| 94 | + { property: "integratorId", value: integratorId }, |
| 95 | + { property: "programId", value: programId.toString() }, |
| 96 | + { property: "providerPublicKey", value: provider.wallet.publicKey.toString() }, |
| 97 | + { property: "statePda", value: statePda.toString() }, |
| 98 | + { property: "vault", value: vault.toString() }, |
| 99 | + { property: "userTokenAccount", value: userTokenAccount.toString() }, |
| 100 | + { property: "existingTokenAccount", value: existingTokenAccount }, |
| 101 | + ]); |
| 102 | + |
| 103 | + const tokenDecimals = (await getMint(provider.connection, inputToken, undefined, TOKEN_PROGRAM_ID)).decimals; |
| 104 | + |
| 105 | + // Will need to add rent exemption to the deposit amount if the user token account does not exist. |
| 106 | + const rentExempt = existingTokenAccount ? 0 : await getMinimumBalanceForRentExemptAccount(provider.connection); |
| 107 | + const transferIx = SystemProgram.transfer({ |
| 108 | + fromPubkey: signer.publicKey, |
| 109 | + toPubkey: userTokenAccount, |
| 110 | + lamports: BigInt(inputAmount.toString()) + BigInt(rentExempt), |
| 111 | + }); |
| 112 | + |
| 113 | + // Create wSOL user account if it doesn't exist, otherwise sync its native balance. |
| 114 | + const syncOrCreateIx = existingTokenAccount |
| 115 | + ? createSyncNativeInstruction(userTokenAccount) |
| 116 | + : createAssociatedTokenAccountIdempotentInstruction( |
| 117 | + signer.publicKey, |
| 118 | + userTokenAccount, |
| 119 | + signer.publicKey, |
| 120 | + inputToken |
| 121 | + ); |
| 122 | + |
| 123 | + // Close the user token account if it did not exist before. |
| 124 | + const lastIxs = existingTokenAccount |
| 125 | + ? [] |
| 126 | + : [createCloseAccountInstruction(userTokenAccount, signer.publicKey, signer.publicKey)]; |
| 127 | + |
| 128 | + // Delegate state PDA to pull depositor tokens. |
| 129 | + const approveIx = await createApproveCheckedInstruction( |
| 130 | + userTokenAccount, |
| 131 | + inputToken, |
| 132 | + statePda, |
| 133 | + signer.publicKey, |
| 134 | + BigInt(inputAmount.toString()), |
| 135 | + tokenDecimals, |
| 136 | + undefined, |
| 137 | + TOKEN_PROGRAM_ID |
| 138 | + ); |
| 139 | + |
| 140 | + const depositIx = await ( |
| 141 | + program.methods.deposit( |
| 142 | + signer.publicKey, |
| 143 | + recipient, |
| 144 | + inputToken, |
| 145 | + outputToken, |
| 146 | + inputAmount, |
| 147 | + outputAmount, |
| 148 | + destinationChainId, |
| 149 | + exclusiveRelayer, |
| 150 | + quoteTimestamp, |
| 151 | + fillDeadline, |
| 152 | + exclusivityDeadline, |
| 153 | + message |
| 154 | + ) as any |
| 155 | + ) |
| 156 | + .accounts({ |
| 157 | + state: statePda, |
| 158 | + signer: signer.publicKey, |
| 159 | + userTokenAccount, |
| 160 | + vault: vault, |
| 161 | + tokenProgram: TOKEN_PROGRAM_ID, |
| 162 | + mint: inputToken, |
| 163 | + }) |
| 164 | + .instruction(); |
| 165 | + |
| 166 | + // Create the deposit transaction |
| 167 | + const depositTx = new Transaction().add(transferIx, syncOrCreateIx, approveIx, depositIx, ...lastIxs); |
| 168 | + |
| 169 | + if (integratorId !== "") { |
| 170 | + const MemoIx = new TransactionInstruction({ |
| 171 | + keys: [{ pubkey: signer.publicKey, isSigner: true, isWritable: true }], |
| 172 | + data: Buffer.from(integratorId, "utf-8"), |
| 173 | + programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"), // Memo program ID |
| 174 | + }); |
| 175 | + depositTx.add(MemoIx); |
| 176 | + } |
| 177 | + |
| 178 | + const tx = await sendAndConfirmTransaction(provider.connection, depositTx, [signer]); |
| 179 | + console.log("Transaction signature:", tx); |
| 180 | +} |
| 181 | + |
| 182 | +// Run the nativeDeposit function |
| 183 | +nativeDeposit(); |
0 commit comments