Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(svm): consistent anchor token constraints #694

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion programs/svm-spoke/src/instructions/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ pub struct DepositV3<'info> {
)]
pub vault: InterfaceAccount<'info, TokenAccount>,

// TODO: why are we using mint::token_program,token::token_program and associated_token::token_program?
#[account(
mint::token_program = token_program,
// IDL build fails when requiring `address = input_token` for mint, thus using a custom constraint.
Expand Down
8 changes: 4 additions & 4 deletions programs/svm-spoke/src/instructions/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ pub struct FillV3Relay<'info> {
pub signer: Signer<'info>,

#[account(
token::token_program = token_program, // TODO: consistent token imports
mint::token_program = token_program,
address = relay_data.output_token @ CustomError::InvalidMint
)]
pub mint_account: InterfaceAccount<'info, Mint>,

#[account(
mut,
associated_token::mint = mint_account, // TODO: consistent token imports
associated_token::authority = signer,
associated_token::token_program = token_program
token::mint = mint_account,
token::authority = signer,
token::token_program = token_program
)]
pub relayer_token_account: InterfaceAccount<'info, TokenAccount>,

Expand Down
2 changes: 1 addition & 1 deletion programs/svm-spoke/src/instructions/slow_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub struct ExecuteV3SlowRelayLeaf<'info> {
pub fill_status: Account<'info, FillStatusAccount>,

#[account(
token::token_program = token_program,
mint::token_program = token_program,
address = slow_fill_leaf.relay_data.output_token @ CustomError::InvalidMint
)]
pub mint: InterfaceAccount<'info, Mint>,
Expand Down
31 changes: 31 additions & 0 deletions test/svm/SvmSpoke.Fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BN } from "@coral-xyz/anchor";
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
createAccount,
createMint,
getOrCreateAssociatedTokenAccount,
mintTo,
Expand Down Expand Up @@ -390,4 +391,34 @@ describe("svm_spoke.fill", () => {
"Token Program should not be invoked"
);
});

it("Fills a V3 relay from custom relayer token account", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great!

// Create and mint to custom relayer token account
const customKeypair = Keypair.generate();
const customRelayerTA = await createAccount(connection, payer, mint, relayer.publicKey, customKeypair);
await mintTo(connection, payer, mint, customRelayerTA, owner, seedBalance);

// Save balances before the the fill
const iRelayerBal = (await getAccount(connection, customRelayerTA)).amount;
const iRecipientBal = (await getAccount(connection, recipientTA)).amount;

// Fill relay from custom relayer token account
accounts.relayerTokenAccount = customRelayerTA;
const relayHash = Array.from(calculateRelayHashUint8Array(relayData, chainId));
await program.methods
.fillV3Relay(relayHash, relayData, new BN(1), relayer.publicKey)
.accounts(accounts)
.signers([relayer])
.rpc();

// Verify balances after the fill
const fRelayerBal = (await getAccount(connection, customRelayerTA)).amount;
const fRecipientBal = (await getAccount(connection, recipientTA)).amount;
assertSE(fRelayerBal, iRelayerBal - BigInt(relayAmount), "Relayer's balance should be reduced by the relay amount");
assertSE(
fRecipientBal,
iRecipientBal + BigInt(relayAmount),
"Recipient's balance should be increased by the relay amount"
);
});
});
Loading