-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsimpleDeposit.ts
148 lines (133 loc) · 5.69 KB
/
simpleDeposit.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
// This script is used to initiate a Solana deposit. useful in testing.
import * as anchor from "@coral-xyz/anchor";
import { AnchorProvider, BN } from "@coral-xyz/anchor";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
createApproveCheckedInstruction,
getAssociatedTokenAddressSync,
getMint,
} from "@solana/spl-token";
import { PublicKey, Transaction, sendAndConfirmTransaction, TransactionInstruction } from "@solana/web3.js";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getSpokePoolProgram } 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("seed", { type: "string", demandOption: true, describe: "Seed for the state account PDA" })
.option("recipient", { type: "string", demandOption: true, describe: "Recipient public key" })
.option("inputToken", { type: "string", demandOption: true, describe: "Input token 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 deposit(): Promise<void> {
const resolvedArgv = await argv;
const seed = new BN(resolvedArgv.seed);
const recipient = new PublicKey(resolvedArgv.recipient);
const inputToken = new PublicKey(resolvedArgv.inputToken);
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
console.log("integratorId", resolvedArgv.integratorId);
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). This should have been created before the deposit is attempted.
const vault = getAssociatedTokenAddressSync(
inputToken,
statePda,
true,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
console.log("Depositing...");
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() },
]);
const userTokenAccount = getAssociatedTokenAddressSync(inputToken, signer.publicKey);
const tokenDecimals = (await getMint(provider.connection, inputToken, undefined, TOKEN_PROGRAM_ID)).decimals;
// 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 a custom instruction with arbitrary data
const depositTx = new Transaction().add(approveIx, depositIx);
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 deposit function
deposit();