-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathnativeDeposit.ts
183 lines (165 loc) · 6.9 KB
/
nativeDeposit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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 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).
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: "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,
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();