-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathfakeFillWithRandomDistribution.ts
232 lines (208 loc) · 9.76 KB
/
fakeFillWithRandomDistribution.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This script implements a fill where relayed tokens are distributed to random recipients via the message handler.
// Note that this should be run only on devnet as this is fake fill and all filled tokens are sent to random recipients.
import * as anchor from "@coral-xyz/anchor";
import { AnchorProvider, BN } from "@coral-xyz/anchor";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
createApproveCheckedInstruction,
createTransferCheckedInstruction,
getAssociatedTokenAddressSync,
getMint,
getOrCreateAssociatedTokenAccount,
} from "@solana/spl-token";
import { AccountMeta, Keypair, PublicKey, SystemProgram, TransactionInstruction } from "@solana/web3.js";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import {
AcrossPlusMessageCoder,
MulticallHandlerCoder,
calculateRelayHashUint8Array,
getSpokePoolProgram,
loadFillRelayParams,
sendTransactionWithLookupTable,
} from "../../src/svm/web3-v1";
import { FillDataParams, FillDataValues } from "../../src/types/svm";
// Set up the provider and signer.
const provider = AnchorProvider.env();
anchor.setProvider(provider);
const signer = (anchor.AnchorProvider.env().wallet as anchor.Wallet).payer;
const program = getSpokePoolProgram(provider);
const programId = program.programId;
// Parse arguments
const argv = yargs(hideBin(process.argv))
.option("depositor", { type: "string", demandOption: true, describe: "Depositor public key" })
.option("handler", { type: "string", demandOption: true, describe: "Handler program ID" })
.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: "array", demandOption: true, describe: "Deposit ID" })
.option("fillDeadline", { type: "number", demandOption: false, describe: "Fill deadline" })
.option("exclusivityDeadline", { type: "number", demandOption: false, describe: "Exclusivity deadline" })
.option("repaymentChain", { type: "number", demandOption: false, description: "Repayment chain ID" })
.option("repaymentAddress", { type: "string", demandOption: false, description: "Repayment address" })
.option("distributionCount", { type: "number", demandOption: false, describe: "Distribution count" })
.option("bufferParams", { type: "boolean", demandOption: false, describe: "Use buffer account for params" }).argv;
async function fillRelayToRandom(): Promise<void> {
const resolvedArgv = await argv;
const depositor = new PublicKey(resolvedArgv.depositor);
const handler = new PublicKey(resolvedArgv.handler);
const exclusiveRelayer = new PublicKey(resolvedArgv.exclusiveRelayer || PublicKey.default.toString());
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 = (resolvedArgv.depositId as number[]).map((id) => id); // Ensure depositId is an array of BN
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 repaymentChain = new BN(resolvedArgv.repaymentChain || 1);
const repaymentAddress = new PublicKey(resolvedArgv.repaymentAddress || signer.publicKey.toString());
const seed = new BN(0);
const distributionCount = resolvedArgv.distributionCount || 1;
const bufferParams = resolvedArgv.bufferParams || false;
const tokenDecimals = (await getMint(provider.connection, outputToken)).decimals;
// Filled relay will first send tokens to the handler ATA.
const [handlerSigner] = PublicKey.findProgramAddressSync([Buffer.from("handler_signer")], handler);
const handlerATA = (
await getOrCreateAssociatedTokenAccount(provider.connection, signer, outputToken, handlerSigner, true)
).address;
// Populate random final recipients and transfer instructions.
const recipientAccounts: PublicKey[] = [];
const transferInstructions: TransactionInstruction[] = [];
for (let i = 0; i < distributionCount; i++) {
const recipient = Keypair.generate().publicKey;
const recipientATA = (await getOrCreateAssociatedTokenAccount(provider.connection, signer, outputToken, recipient))
.address;
recipientAccounts.push(recipientATA);
// Construct ix to transfer tokens from handler to the recipient in equal distribution (except the last one getting any rounding remainder).
const distributionAmount =
i !== distributionCount - 1
? outputAmount.div(new BN(distributionCount))
: outputAmount.sub(outputAmount.div(new BN(distributionCount)).mul(new BN(distributionCount - 1)));
const transferInstruction = createTransferCheckedInstruction(
handlerATA,
outputToken,
recipientATA,
handlerSigner,
BigInt(distributionAmount.toString()),
tokenDecimals
);
transferInstructions.push(transferInstruction);
}
// Encode handler message for the token distribution.
const multicallHandlerCoder = new MulticallHandlerCoder(transferInstructions);
const handlerMessage = multicallHandlerCoder.encode();
const message = new AcrossPlusMessageCoder({
handler,
readOnlyLen: multicallHandlerCoder.readOnlyLen,
valueAmount: new BN(0),
accounts: multicallHandlerCoder.compiledMessage.accountKeys,
handlerMessage,
});
const encodedMessage = message.encode();
const relayData = {
depositor,
recipient: handlerSigner,
exclusiveRelayer,
inputToken,
outputToken,
inputAmount,
outputAmount,
originChainId,
depositId,
fillDeadline,
exclusivityDeadline,
message: encodedMessage,
};
console.log("Filling Relay with handler...");
// 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);
const relayHash = Array.from(relayHashUint8Array);
// Define the fill status account PDA
const [fillStatusPda] = PublicKey.findProgramAddressSync([Buffer.from("fills"), relayHashUint8Array], programId);
// Get ATA for the relayer token account
const relayerTokenAccount = getAssociatedTokenAddressSync(outputToken, signer.publicKey);
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: handlerATA.toString() },
{ property: "seed", value: seed.toString() },
]);
console.log("Relay Data:");
console.table(
Object.entries(relayData).map(([key, value]) => ({
key,
value: key === "message" ? (value as Buffer).toString("hex") : value.toString(),
}))
);
// Delegate state PDA to pull relayer tokens.
const approveInstruction = await createApproveCheckedInstruction(
relayerTokenAccount,
outputToken,
statePda,
signer.publicKey,
BigInt(relayData.outputAmount.toString()),
tokenDecimals,
undefined,
TOKEN_PROGRAM_ID
);
// Prepare fill instruction as we will need to use Address Lookup Table (ALT).
const fillRelayValues: FillDataValues = [relayHash, relayData, repaymentChain, repaymentAddress];
if (bufferParams) {
await loadFillRelayParams(program, signer, fillRelayValues[1], fillRelayValues[2], fillRelayValues[3]);
}
const fillRelayParams: FillDataParams = bufferParams ? [fillRelayValues[0], null, null, null] : fillRelayValues;
const [instructionParams] = bufferParams
? PublicKey.findProgramAddressSync(
[Buffer.from("instruction_params"), signer.publicKey.toBuffer()],
program.programId
)
: [program.programId];
const fillAccounts = {
state: statePda,
signer: signer.publicKey,
instructionParams,
mint: outputToken,
relayerTokenAccount,
recipientTokenAccount: handlerATA,
fillStatus: fillStatusPda,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
program: programId,
};
const remainingAccounts: AccountMeta[] = [
{ pubkey: handler, isSigner: false, isWritable: false },
...multicallHandlerCoder.compiledKeyMetas,
];
const fillInstruction = await program.methods
.fillRelay(...fillRelayParams)
.accounts(fillAccounts)
.remainingAccounts(remainingAccounts)
.instruction();
// Fill using the ALT.
const { txSignature } = await sendTransactionWithLookupTable(
provider.connection,
[approveInstruction, fillInstruction],
signer
);
console.log("Transaction signature:", txSignature);
}
// Run the fillRelayToRandom function
fillRelayToRandom();