-
Notifications
You must be signed in to change notification settings - Fork 75
feat(svm): test native sol deposits #942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Reinis-FRP
merged 3 commits into
solana-march-audit-2
from
reinis-frp/native-sol-deposit
Apr 3, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| // This script is used to initiate a native token Solana deposit. useful in testing. | ||
|
|
||
| import * as anchor from "@coral-xyz/anchor"; | ||
| import { AnchorProvider, BN } from "@coral-xyz/anchor"; | ||
| import { | ||
| ASSOCIATED_TOKEN_PROGRAM_ID, | ||
| NATIVE_MINT, | ||
| TOKEN_PROGRAM_ID, | ||
| createApproveCheckedInstruction, | ||
| createAssociatedTokenAccountIdempotentInstruction, | ||
| createCloseAccountInstruction, | ||
| createSyncNativeInstruction, | ||
| getAssociatedTokenAddressSync, | ||
| getMinimumBalanceForRentExemptAccount, | ||
| getMint, | ||
| } from "@solana/spl-token"; | ||
| import { | ||
| PublicKey, | ||
| Transaction, | ||
| sendAndConfirmTransaction, | ||
| TransactionInstruction, | ||
| SystemProgram, | ||
| } from "@solana/web3.js"; | ||
| import yargs from "yargs"; | ||
| import { hideBin } from "yargs/helpers"; | ||
| import { getSpokePoolProgram, SOLANA_SPOKE_STATE_SEED } from "../../src/svm/web3-v1"; | ||
|
|
||
| // Set up the provider | ||
| const provider = AnchorProvider.env(); | ||
| anchor.setProvider(provider); | ||
| const program = getSpokePoolProgram(provider); | ||
| const programId = program.programId; | ||
| console.log("SVM-Spoke Program ID:", programId.toString()); | ||
|
|
||
| // Parse arguments | ||
| const argv = yargs(hideBin(process.argv)) | ||
| .option("recipient", { type: "string", demandOption: true, describe: "Recipient public key" }) | ||
| .option("outputToken", { type: "string", demandOption: true, describe: "Output token public key" }) | ||
| .option("inputAmount", { type: "number", demandOption: true, describe: "Input amount" }) | ||
| .option("outputAmount", { type: "number", demandOption: true, describe: "Output amount" }) | ||
| .option("destinationChainId", { type: "string", demandOption: true, describe: "Destination chain ID" }) | ||
| .option("integratorId", { type: "string", demandOption: false, describe: "integrator ID" }).argv; | ||
|
|
||
| async function nativeDeposit(): Promise<void> { | ||
| const resolvedArgv = await argv; | ||
| const seed = SOLANA_SPOKE_STATE_SEED; | ||
| const recipient = new PublicKey(resolvedArgv.recipient); | ||
| const inputToken = NATIVE_MINT; | ||
| const outputToken = new PublicKey(resolvedArgv.outputToken); | ||
| const inputAmount = new BN(resolvedArgv.inputAmount); | ||
| const outputAmount = new BN(resolvedArgv.outputAmount); | ||
| const destinationChainId = new BN(resolvedArgv.destinationChainId); | ||
| const exclusiveRelayer = PublicKey.default; | ||
| const quoteTimestamp = Math.floor(Date.now() / 1000) - 1; | ||
| const fillDeadline = quoteTimestamp + 3600; // 1 hour from now | ||
| const exclusivityDeadline = 0; | ||
| const message = Buffer.from([]); // Convert to Buffer | ||
| const integratorId = resolvedArgv.integratorId || ""; | ||
| // Define the state account PDA | ||
| const [statePda, _] = PublicKey.findProgramAddressSync( | ||
| [Buffer.from("state"), seed.toArrayLike(Buffer, "le", 8)], | ||
| programId | ||
| ); | ||
|
|
||
| // Define the route account PDA | ||
| const [routePda] = PublicKey.findProgramAddressSync( | ||
| [ | ||
| Buffer.from("route"), | ||
| inputToken.toBytes(), | ||
| seed.toArrayLike(Buffer, "le", 8), | ||
| destinationChainId.toArrayLike(Buffer, "le", 8), | ||
| ], | ||
| programId | ||
| ); | ||
|
|
||
| // Define the signer (replace with your actual signer) | ||
| const signer = (provider.wallet as anchor.Wallet).payer; | ||
|
|
||
| // Find ATA for the input token to be stored by state (vault). This was created when the route was enabled. | ||
| const vault = getAssociatedTokenAddressSync( | ||
| inputToken, | ||
| statePda, | ||
| true, | ||
| TOKEN_PROGRAM_ID, | ||
| ASSOCIATED_TOKEN_PROGRAM_ID | ||
| ); | ||
|
|
||
| const userTokenAccount = getAssociatedTokenAddressSync(inputToken, signer.publicKey); | ||
| const userTokenAccountInfo = await provider.connection.getAccountInfo(userTokenAccount); | ||
| const existingTokenAccount = userTokenAccountInfo !== null && userTokenAccountInfo.owner.equals(TOKEN_PROGRAM_ID); | ||
|
|
||
| console.log("Depositing V3..."); | ||
| console.table([ | ||
| { property: "seed", value: seed.toString() }, | ||
| { property: "recipient", value: recipient.toString() }, | ||
| { property: "inputToken", value: inputToken.toString() }, | ||
| { property: "outputToken", value: outputToken.toString() }, | ||
| { property: "inputAmount", value: inputAmount.toString() }, | ||
| { property: "outputAmount", value: outputAmount.toString() }, | ||
| { property: "destinationChainId", value: destinationChainId.toString() }, | ||
| { property: "quoteTimestamp", value: quoteTimestamp.toString() }, | ||
| { property: "fillDeadline", value: fillDeadline.toString() }, | ||
| { property: "exclusivityDeadline", value: exclusivityDeadline.toString() }, | ||
| { property: "message", value: message.toString("hex") }, | ||
| { property: "integratorId", value: integratorId }, | ||
| { property: "programId", value: programId.toString() }, | ||
| { property: "providerPublicKey", value: provider.wallet.publicKey.toString() }, | ||
| { property: "statePda", value: statePda.toString() }, | ||
| { property: "routePda", value: routePda.toString() }, | ||
| { property: "vault", value: vault.toString() }, | ||
| { property: "userTokenAccount", value: userTokenAccount.toString() }, | ||
| { property: "existingTokenAccount", value: existingTokenAccount }, | ||
| ]); | ||
|
|
||
| const tokenDecimals = (await getMint(provider.connection, inputToken, undefined, TOKEN_PROGRAM_ID)).decimals; | ||
|
|
||
| // Will need to add rent exemption to the deposit amount if the user token account does not exist. | ||
| const rentExempt = existingTokenAccount ? 0 : await getMinimumBalanceForRentExemptAccount(provider.connection); | ||
| const transferIx = SystemProgram.transfer({ | ||
| fromPubkey: signer.publicKey, | ||
| toPubkey: userTokenAccount, | ||
| lamports: BigInt(inputAmount.toString()) + BigInt(rentExempt), | ||
| }); | ||
|
|
||
| // Create wSOL user account if it doesn't exist, otherwise sync its native balance. | ||
| const syncOrCreateIx = existingTokenAccount | ||
| ? createSyncNativeInstruction(userTokenAccount) | ||
| : createAssociatedTokenAccountIdempotentInstruction( | ||
| signer.publicKey, | ||
| userTokenAccount, | ||
| signer.publicKey, | ||
| inputToken | ||
| ); | ||
|
|
||
| // Close the user token account if it did not exist before. | ||
| const lastIxs = existingTokenAccount | ||
| ? [] | ||
| : [createCloseAccountInstruction(userTokenAccount, signer.publicKey, signer.publicKey)]; | ||
|
|
||
| // Delegate state PDA to pull depositor tokens. | ||
| const approveIx = await createApproveCheckedInstruction( | ||
| userTokenAccount, | ||
| inputToken, | ||
| statePda, | ||
| signer.publicKey, | ||
| BigInt(inputAmount.toString()), | ||
| tokenDecimals, | ||
| undefined, | ||
| TOKEN_PROGRAM_ID | ||
| ); | ||
|
|
||
| const depositIx = await ( | ||
| program.methods.deposit( | ||
| signer.publicKey, | ||
| recipient, | ||
| inputToken, | ||
| outputToken, | ||
| inputAmount, | ||
| outputAmount, | ||
| destinationChainId, | ||
| exclusiveRelayer, | ||
| quoteTimestamp, | ||
| fillDeadline, | ||
| exclusivityDeadline, | ||
| message | ||
| ) as any | ||
| ) | ||
| .accounts({ | ||
| state: statePda, | ||
| route: routePda, | ||
| signer: signer.publicKey, | ||
| userTokenAccount, | ||
| vault: vault, | ||
| tokenProgram: TOKEN_PROGRAM_ID, | ||
| mint: inputToken, | ||
| }) | ||
| .instruction(); | ||
|
|
||
| // Create the deposit transaction | ||
| const depositTx = new Transaction().add(transferIx, syncOrCreateIx, approveIx, depositIx, ...lastIxs); | ||
|
|
||
| if (integratorId !== "") { | ||
| const MemoIx = new TransactionInstruction({ | ||
| keys: [{ pubkey: signer.publicKey, isSigner: true, isWritable: true }], | ||
| data: Buffer.from(integratorId, "utf-8"), | ||
| programId: new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"), // Memo program ID | ||
| }); | ||
| depositTx.add(MemoIx); | ||
| } | ||
|
|
||
| const tx = await sendAndConfirmTransaction(provider.connection, depositTx, [signer]); | ||
| console.log("Transaction signature:", tx); | ||
| } | ||
|
|
||
| // Run the nativeDeposit function | ||
| nativeDeposit(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will need to remove this once #939 is merged.