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): remove redundant accounts #689

Merged
merged 3 commits into from
Oct 25, 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
2 changes: 0 additions & 2 deletions programs/svm-spoke/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ pub enum CustomError {
ExceededPendingBridgeAmount,
#[msg("Deposits are currently paused!")]
DepositsArePaused,
#[msg("Invalid fill recipient!")]
InvalidFillRecipient,
#[msg("Invalid quote timestamp!")]
InvalidQuoteTimestamp,
#[msg("Ivalid fill deadline!")]
Expand Down
11 changes: 2 additions & 9 deletions programs/svm-spoke/src/instructions/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ pub struct FillV3Relay<'info> {
#[account(mut)]
pub signer: Signer<'info>,

pub relayer: SystemAccount<'info>, // TODO: should this be the same as signer?

#[account(
address = relay_data.recipient @ CustomError::InvalidFillRecipient
)]
pub recipient: SystemAccount<'info>, // TODO: this might be redundant.

#[account(
token::token_program = token_program, // TODO: consistent token imports
address = relay_data.output_token @ CustomError::InvalidMint
Expand All @@ -43,15 +36,15 @@ pub struct FillV3Relay<'info> {
#[account(
mut,
associated_token::mint = mint_account, // TODO: consistent token imports
associated_token::authority = relayer,
associated_token::authority = signer,
associated_token::token_program = token_program
)]
pub relayer_token_account: InterfaceAccount<'info, TokenAccount>,

#[account(
mut,
associated_token::mint = mint_account,
associated_token::authority = recipient, // TODO: use relay_data.recipient
associated_token::authority = relay_data.recipient,
associated_token::token_program = token_program
)]
pub recipient_token_account: InterfaceAccount<'info, TokenAccount>,
Expand Down
7 changes: 1 addition & 6 deletions programs/svm-spoke/src/instructions/slow_fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ pub struct ExecuteV3SlowRelayLeaf<'info> {
)]
pub fill_status: Account<'info, FillStatusAccount>,

#[account(
address = slow_fill_leaf.relay_data.recipient @ CustomError::InvalidFillRecipient
)]
pub recipient: SystemAccount<'info>,

#[account(
token::token_program = token_program,
address = slow_fill_leaf.relay_data.output_token @ CustomError::InvalidMint
Expand All @@ -162,7 +157,7 @@ pub struct ExecuteV3SlowRelayLeaf<'info> {
#[account(
mut,
associated_token::mint = mint,
associated_token::authority = recipient,
associated_token::authority = slow_fill_leaf.relay_data.recipient,
associated_token::token_program = token_program
)]
pub recipient_token_account: InterfaceAccount<'info, TokenAccount>,
Expand Down
2 changes: 0 additions & 2 deletions scripts/svm/simpleFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ async function fillV3Relay(): Promise<void> {
.accounts({
state: statePda,
signer: signer,
relayer: signer,
recipient: recipient,
mintAccount: outputToken,
relayerTokenAccount: relayerTokenAccount,
recipientTokenAccount: recipientTokenAccount,
Expand Down
24 changes: 9 additions & 15 deletions test/svm/SvmSpoke.Fill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ describe("svm_spoke.fill", () => {
accounts = {
state,
signer: relayer.publicKey,
relayer: relayer.publicKey,
recipient: recipient,
mintAccount: mint,
relayerTA: relayerTA,
recipientTA: recipientTA,
relayerTokenAccount: relayerTA,
recipientTokenAccount: recipientTA,
fillStatus: fillStatusPDA,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -138,8 +136,7 @@ describe("svm_spoke.fill", () => {

it("Fails to fill a V3 relay by non-exclusive relayer before exclusivity deadline", async () => {
accounts.signer = otherRelayer.publicKey;
accounts.relayer = otherRelayer.publicKey;
accounts.relayerTA = otherRelayerTA;
accounts.relayerTokenAccount = otherRelayerTA;

const relayHash = Array.from(calculateRelayHashUint8Array(relayData, chainId));
try {
Expand All @@ -158,8 +155,7 @@ describe("svm_spoke.fill", () => {
updateRelayData({ ...relayData, exclusivityDeadline: new BN(Math.floor(Date.now() / 1000) - 100) });

accounts.signer = otherRelayer.publicKey;
accounts.relayer = otherRelayer.publicKey;
accounts.relayerTA = otherRelayerTA;
accounts.relayerTokenAccount = otherRelayerTA;

const recipientAccountBefore = await getAccount(connection, recipientTA);
const relayerAccountBefore = await getAccount(connection, otherRelayerTA);
Expand Down Expand Up @@ -274,7 +270,7 @@ describe("svm_spoke.fill", () => {
}
});

it("Fails to fill a relay to wrong recipient", async () => {
it("Fails to fill a relay to wrong recipient token account", async () => {
const relayHash = calculateRelayHashUint8Array(relayData, chainId);

// Create new accounts as derived from wrong recipient.
Expand All @@ -287,16 +283,15 @@ describe("svm_spoke.fill", () => {
.fillV3Relay(Array.from(relayHash), relayData, new BN(1))
.accounts({
...accounts,
recipient: wrongRecipient,
recipientTA: wrongRecipientTA,
recipientTokenAccount: wrongRecipientTA,
fillStatus: wrongFillStatus,
})
.signers([relayer])
.rpc();
assert.fail("Should not be able to fill relay to wrong recipient");
assert.fail("Should not be able to fill relay to wrong recipient token account");
} catch (err: any) {
assert.instanceOf(err, anchor.AnchorError);
assert.strictEqual(err.error.errorCode.code, "InvalidFillRecipient", "Expected error code InvalidFillRecipient");
assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner", "Expected error code ConstraintTokenOwner");
}
});

Expand Down Expand Up @@ -331,8 +326,7 @@ describe("svm_spoke.fill", () => {
it("Self-relay does not invoke token transfer", async () => {
// Set recipient to be the same as relayer.
updateRelayData({ ...relayData, depositor: relayer.publicKey, recipient: relayer.publicKey });
accounts.recipient = relayer.publicKey;
accounts.recipientTA = relayerTA;
accounts.recipientTokenAccount = relayerTA;

// Store relayer's balance before the fill
const iRelayerBalance = (await getAccount(connection, relayerTA)).amount;
Expand Down
18 changes: 5 additions & 13 deletions test/svm/SvmSpoke.SlowFill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ describe("svm_spoke.slow_fill", () => {
fillAccounts = {
state,
signer: relayer.publicKey,
relayer: relayer.publicKey,
recipient: relayData.recipient, // This could be different from global recipient.
mintAccount: mint,
relayerTA: relayerTA,
recipientTA: recipientTA,
relayerTokenAccount: relayerTA,
Copy link
Member

Choose a reason for hiding this comment

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

feels a bit messay that we mix TA and TokenAccount in names of things but we can make it more consistant later.

recipientTokenAccount: recipientTA,
fillStatus,
tokenProgram: TOKEN_PROGRAM_ID,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -325,7 +323,6 @@ describe("svm_spoke.slow_fill", () => {
vault: vault,
tokenProgram: TOKEN_PROGRAM_ID,
mint: mint,
recipient,
recipientTokenAccount: recipientTA,
program: program.programId,
};
Expand Down Expand Up @@ -413,7 +410,7 @@ describe("svm_spoke.slow_fill", () => {
.signers([relayer])
.rpc();

// Try to execute V3 slow relay leaf with wrong recipient should fail.
// Try to execute V3 slow relay leaf with wrong recipient token account should fail.
const wrongRecipient = Keypair.generate().publicKey;
const wrongRecipientTA = (await getOrCreateAssociatedTokenAccount(connection, payer, mint, wrongRecipient)).address;
try {
Expand All @@ -425,7 +422,6 @@ describe("svm_spoke.slow_fill", () => {
vault: vault,
tokenProgram: TOKEN_PROGRAM_ID,
mint: mint,
recipient: wrongRecipient,
recipientTokenAccount: wrongRecipientTA,
program: program.programId,
};
Expand All @@ -438,10 +434,10 @@ describe("svm_spoke.slow_fill", () => {
)
.accounts(executeSlowRelayLeafAccounts)
.rpc();
assert.fail("Execution should have failed due to wrong recipient");
assert.fail("Execution should have failed due to wrong recipient token account");
} catch (err: any) {
assert.instanceOf(err, anchor.AnchorError);
assert.strictEqual(err.error.errorCode.code, "InvalidFillRecipient", "Expected error code InvalidFillRecipient");
assert.strictEqual(err.error.errorCode.code, "ConstraintTokenOwner", "Expected error code ConstraintTokenOwner");
}
});

Expand Down Expand Up @@ -485,7 +481,6 @@ describe("svm_spoke.slow_fill", () => {
vault,
tokenProgram: TOKEN_PROGRAM_ID,
mint,
recipient: firstRecipient,
recipientTokenAccount: firstRecipientTA,
program: program.programId,
};
Expand Down Expand Up @@ -516,7 +511,6 @@ describe("svm_spoke.slow_fill", () => {
vault,
tokenProgram: TOKEN_PROGRAM_ID,
mint,
recipient: firstRecipient,
recipientTokenAccount: firstRecipientTA,
program: program.programId,
};
Expand Down Expand Up @@ -561,7 +555,6 @@ describe("svm_spoke.slow_fill", () => {
vault: wrongVault,
tokenProgram: TOKEN_PROGRAM_ID,
mint: wrongMint,
recipient,
recipientTokenAccount: wrongRecipientTA,
program: program.programId,
};
Expand Down Expand Up @@ -605,7 +598,6 @@ describe("svm_spoke.slow_fill", () => {
vault,
tokenProgram: TOKEN_PROGRAM_ID,
mint,
recipient,
recipientTokenAccount: recipientTA,
program: program.programId,
};
Expand Down
Loading