Skip to content

Commit

Permalink
fix(svm): consistent anchor token constraints (#694)
Browse files Browse the repository at this point in the history
* fix(svm): consistent anchor token constraints

Signed-off-by: Reinis Martinsons <reinis@umaproject.org>

* test: custom relayer token account

Signed-off-by: Reinis Martinsons <reinis@umaproject.org>

* fix: test balances

Signed-off-by: Reinis Martinsons <reinis@umaproject.org>

---------

Signed-off-by: Reinis Martinsons <reinis@umaproject.org>
  • Loading branch information
Reinis-FRP authored Oct 28, 2024
1 parent 4238084 commit ad00ab2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
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 @@ -53,7 +53,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 state: Account<'info, State>,

#[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 @@ -146,7 +146,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 () => {
// 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"
);
});
});

0 comments on commit ad00ab2

Please sign in to comment.