Skip to content

Commit

Permalink
fix: fill tests
Browse files Browse the repository at this point in the history
Signed-off-by: Reinis Martinsons <[email protected]>
  • Loading branch information
Reinis-FRP committed Nov 13, 2024
1 parent 03a87dd commit cedbd26
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
25 changes: 22 additions & 3 deletions test/svm/SvmSpoke.Fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "@solana/web3.js";
import { readProgramEvents, calculateRelayHashUint8Array } from "../../src/SvmUtils";
import { common } from "./SvmSpoke.common";
import { testAcrossPlusMessage } from "./utils";
const { provider, connection, program, owner, chainId, seedBalance } = common;
const { recipient, initializeState, setCurrentTime, assertSE, assert } = common;

Expand All @@ -28,6 +29,7 @@ describe("svm_spoke.fill", () => {
const payer = (anchor.AnchorProvider.env().wallet as anchor.Wallet).payer;
const relayer = Keypair.generate();
const otherRelayer = Keypair.generate();
const { encodedMessage, remainingAccounts } = testAcrossPlusMessage();

let state: PublicKey, mint: PublicKey, relayerTA: PublicKey, recipientTA: PublicKey, otherRelayerTA: PublicKey;

Expand Down Expand Up @@ -84,7 +86,7 @@ describe("svm_spoke.fill", () => {
depositId: new BN(Math.floor(Math.random() * 1000000)), // force that we always have a new deposit id.
fillDeadline: new BN(Math.floor(Date.now() / 1000) + 60), // 1 minute from now
exclusivityDeadline: new BN(Math.floor(Date.now() / 1000) + 30), // 30 seconds from now
message: Buffer.from("Test message"),
message: encodedMessage,
};

updateRelayData(initialRelayData);
Expand All @@ -103,6 +105,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand All @@ -124,6 +127,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(420), otherRelayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand All @@ -150,6 +154,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Fill should have failed due to fill deadline passed");
Expand All @@ -167,6 +172,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([otherRelayer])
.rpc();
assert.fail("Fill should have failed due to non-exclusive relayer before exclusivity deadline");
Expand All @@ -188,6 +194,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([otherRelayer])
.rpc();

Expand Down Expand Up @@ -215,6 +222,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand All @@ -223,6 +231,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Fill should have failed due to RelayFilled error");
Expand All @@ -245,6 +254,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand Down Expand Up @@ -287,6 +297,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(Array.from(relayHash), relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand All @@ -310,6 +321,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Should not be able to fill relay when fills are paused");
Expand All @@ -334,6 +346,7 @@ describe("svm_spoke.fill", () => {
recipientTokenAccount: wrongRecipientTA,
fillStatus: wrongFillStatus,
})
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Should not be able to fill relay to wrong recipient token account");
Expand Down Expand Up @@ -362,6 +375,7 @@ describe("svm_spoke.fill", () => {
relayerTokenAccount: wrongRelayerTA,
recipientTokenAccount: wrongRecipientTA,
})
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Should not be able to process fill for inconsistent mint");
Expand All @@ -383,6 +397,7 @@ describe("svm_spoke.fill", () => {
const txSignature = await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand Down Expand Up @@ -420,6 +435,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();

Expand Down Expand Up @@ -452,6 +468,7 @@ describe("svm_spoke.fill", () => {
await program.methods
.fillV3Relay(relayHash, newRelayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.signers([relayer])
.rpc();
assert.fail("Fill should have failed due to missing ATA");
Expand All @@ -473,6 +490,7 @@ describe("svm_spoke.fill", () => {
const fillInstruction = await program.methods
.fillV3Relay(relayHash, newRelayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.instruction();

// Create and send the transaction
Expand All @@ -495,14 +513,14 @@ describe("svm_spoke.fill", () => {
const recipientAssociatedTokens = recipientAuthorities.map((authority) =>
getAssociatedTokenAddressSync(mint, authority)
);
const remainingAccounts = recipientAuthorities.flatMap((authority, index) => [
const createTAremainingAccounts = recipientAuthorities.flatMap((authority, index) => [
{ pubkey: authority, isWritable: false, isSigner: false },
{ pubkey: recipientAssociatedTokens[index], isWritable: true, isSigner: false },
]);
const createTokenAccountsInstruction = await program.methods
.createTokenAccounts()
.accounts({ signer: relayer.publicKey, mint, tokenProgram: TOKEN_PROGRAM_ID })
.remainingAccounts(remainingAccounts)
.remainingAccounts(createTAremainingAccounts)
.instruction();

// Build instructions for all fills
Expand All @@ -519,6 +537,7 @@ describe("svm_spoke.fill", () => {
const fillInstruction = await program.methods
.fillV3Relay(relayHash, newRelayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.remainingAccounts(remainingAccounts)
.instruction();
fillInstructions.push(fillInstruction);
}
Expand Down
27 changes: 25 additions & 2 deletions test/svm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { BN, Program } from "@coral-xyz/anchor";
import { Keypair, PublicKey } from "@solana/web3.js";
import { BN, Program, workspace } from "@coral-xyz/anchor";
import { AccountMeta, Keypair, PublicKey } from "@solana/web3.js";
import { BigNumber, ethers } from "ethers";
import * as crypto from "crypto";
import { SvmSpoke } from "../../target/types/svm_spoke";
import { MulticallHandler } from "../../target/types/multicall_handler";

import {
readEvents,
readProgramEvents,
calculateRelayHashUint8Array,
findProgramAddress,
LargeAccountsCoder,
MulticallHandlerCoder,
AcrossPlusMessageCoder,
} from "../../src/SvmUtils";
import { MerkleTree } from "@uma/common";
import { getParamType, keccak256 } from "../../test-utils";
Expand Down Expand Up @@ -271,3 +274,23 @@ export async function loadExecuteRelayerRefundLeafParams(
}
return instructionParams;
}

// Encodes empty list of multicall handler instructions to be used as a test message field.
export function testAcrossPlusMessage() {
const handlerProgram = workspace.MulticallHandler as Program<MulticallHandler>;
const multicallHandlerCoder = new MulticallHandlerCoder([]);
const handlerMessage = multicallHandlerCoder.encode();
const message = new AcrossPlusMessageCoder({
handler: handlerProgram.programId,
readOnlyLen: multicallHandlerCoder.readOnlyLen,
valueAmount: new BN(0),
accounts: multicallHandlerCoder.compiledMessage.accountKeys,
handlerMessage,
});
const encodedMessage = message.encode();
const remainingAccounts: AccountMeta[] = [
{ pubkey: handlerProgram.programId, isSigner: false, isWritable: false },
...multicallHandlerCoder.compiledKeyMetas,
];
return { encodedMessage, remainingAccounts };
}

0 comments on commit cedbd26

Please sign in to comment.