-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsimpleFill.ts
188 lines (164 loc) · 7.15 KB
/
simpleFill.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
184
185
186
187
188
// This script implements a simple relayer fill against known deposit props. Note that if the deposit data is done wrong
// this script can easily create invalid fills.
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,
getOrCreateAssociatedTokenAccount,
} from "@solana/spl-token";
import { PublicKey, SystemProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { calculateRelayHashUint8Array, getSpokePoolProgram, intToU8Array32 } from "../../src/svm/web3-v1";
import { FillDataValues } from "../../src/types/svm";
// Set up the provider
const provider = AnchorProvider.env();
anchor.setProvider(provider);
const program = getSpokePoolProgram(provider);
const programId = program.programId;
// Parse arguments
const argv = yargs(hideBin(process.argv))
.option("seed", { type: "string", demandOption: true, describe: "Seed for the state account PDA" })
.option("depositor", { type: "string", demandOption: true, describe: "Depositor public key" })
.option("recipient", { type: "string", demandOption: true, describe: "Recipient public key" })
.option("exclusiveRelayer", { type: "string", demandOption: false, describe: "Exclusive relayer 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("originChainId", { type: "string", demandOption: true, describe: "Origin chain ID" })
.option("depositId", { type: "string", demandOption: true, describe: "Deposit ID" })
.option("fillDeadline", { type: "number", demandOption: false, describe: "Fill deadline" })
.option("exclusivityDeadline", { type: "number", demandOption: false, describe: "Exclusivity deadline" }).argv;
async function fillRelay(): Promise<void> {
const resolvedArgv = await argv;
const depositor = new PublicKey(resolvedArgv.depositor);
const recipient = new PublicKey(resolvedArgv.recipient);
const exclusiveRelayer = new PublicKey(resolvedArgv.exclusiveRelayer || "11111111111111111111111111111111");
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 originChainId = new BN(resolvedArgv.originChainId);
const depositId = intToU8Array32(new BN(resolvedArgv.depositId));
const fillDeadline = resolvedArgv.fillDeadline || Math.floor(Date.now() / 1000) + 60; // Current time + 1 minute
const exclusivityDeadline = resolvedArgv.exclusivityDeadline || Math.floor(Date.now() / 1000) + 30; // Current time + 30 seconds
const message = Buffer.from("");
const seed = new BN(resolvedArgv.seed);
const relayData = {
depositor,
recipient,
exclusiveRelayer,
inputToken,
outputToken,
inputAmount,
outputAmount,
originChainId,
depositId,
fillDeadline,
exclusivityDeadline,
message,
};
// Define the signer (replace with your actual signer)
const signer = (provider.wallet as anchor.Wallet).payer;
console.log("Filling Relay...");
// Define the state account PDA
const [statePda, _] = PublicKey.findProgramAddressSync(
[Buffer.from("state"), seed.toArrayLike(Buffer, "le", 8)],
programId
);
// Fetch the state from the on-chain program to get chainId
const state = await program.account.state.fetch(statePda);
const chainId = new BN(state.chainId);
const relayHashUint8Array = calculateRelayHashUint8Array(relayData, chainId);
// Define the fill status account PDA
const [fillStatusPda] = PublicKey.findProgramAddressSync([Buffer.from("fills"), relayHashUint8Array], programId);
// Create ATA for the relayer and recipient token accounts
const relayerTokenAccount = getAssociatedTokenAddressSync(
outputToken,
signer.publicKey,
true,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
);
const recipientTokenAccount = (
await getOrCreateAssociatedTokenAccount(
provider.connection,
signer,
outputToken,
recipient,
true,
undefined,
undefined,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
)
).address;
console.table([
{ property: "relayHash", value: Buffer.from(relayHashUint8Array).toString("hex") },
{ property: "chainId", value: chainId.toString() },
{ property: "programId", value: programId.toString() },
{ property: "providerPublicKey", value: provider.wallet.publicKey.toString() },
{ property: "statePda", value: statePda.toString() },
{ property: "fillStatusPda", value: fillStatusPda.toString() },
{ property: "relayerTokenAccount", value: relayerTokenAccount.toString() },
{ property: "recipientTokenAccount", value: recipientTokenAccount.toString() },
{ property: "seed", value: seed.toString() },
]);
console.log("Relay Data:");
console.table(
Object.entries(relayData).map(([key, value]) => ({
key,
value: value.toString(),
}))
);
const tokenDecimals = (await getMint(provider.connection, outputToken, undefined, TOKEN_PROGRAM_ID)).decimals;
// Create the ATA using the create_token_accounts method
const createTokenAccountsIx = await program.methods
.createTokenAccounts()
.accounts({ signer: signer.publicKey, mint: outputToken, tokenProgram: TOKEN_PROGRAM_ID })
.remainingAccounts([
{ pubkey: recipient, isWritable: false, isSigner: false },
{ pubkey: recipientTokenAccount, isWritable: true, isSigner: false },
])
.instruction();
// Delegate state PDA to pull relayer tokens.
const approveIx = await createApproveCheckedInstruction(
relayerTokenAccount,
outputToken,
statePda,
signer.publicKey,
BigInt(relayData.outputAmount.toString()),
tokenDecimals,
undefined,
TOKEN_PROGRAM_ID
);
const fillDataValues: FillDataValues = [Array.from(relayHashUint8Array), relayData, chainId, signer.publicKey];
const fillAccounts = {
state: statePda,
signer: signer.publicKey,
instructionParams: program.programId,
mint: outputToken,
relayerTokenAccount: relayerTokenAccount,
recipientTokenAccount: recipientTokenAccount,
fillStatus: fillStatusPda,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
programId: programId,
program: program.programId,
};
const fillIx = await program.methods
.fillRelay(...fillDataValues)
.accounts(fillAccounts)
.instruction();
const fillTx = new Transaction().add(createTokenAccountsIx, approveIx, fillIx);
const tx = await sendAndConfirmTransaction(provider.connection, fillTx, [signer]);
console.log("Transaction signature:", tx);
}
// Run the fillRelay function
fillRelay();