Skip to content

Commit

Permalink
add pda_ata validation as constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
brewmaster012 committed Oct 7, 2024
1 parent 64e914f commit b0d3184
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion programs/protocol-contracts-solana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ pub struct WithdrawSPLToken<'info> {
#[account(mut, seeds = [b"meta"], bump)]
pub pda: Account<'info, Pda>,

#[account(mut)]
#[account(mut, token::mint = mint_account, token::authority = pda)]
pub pda_ata: Account<'info, TokenAccount>, // associated token address of PDA

pub mint_account: Account<'info, Mint>,
Expand Down
38 changes: 35 additions & 3 deletions tests/protocol-contracts-solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe("some tests", () => {
await gatewayProgram.methods.depositSplToken(new anchor.BN(1_000_000), Array.from(address)).accounts({
from: tokenAccount.address,
to: pda_ata.address,
}).rpc({commitment: 'confirmed'});
}).rpc({commitment: 'processed'});
acct = await spl.getAccount(conn, pda_ata.address);
let bal1 = acct.amount;
expect(bal1-bal0).to.be.eq(1_000_000n);
Expand Down Expand Up @@ -298,7 +298,7 @@ describe("some tests", () => {
} catch (err) {
expect(err).to.be.instanceof(anchor.AnchorError);
console.log("Error message: ", err.message);
expect(err.message).to.include("SPLAtaAndMintAddressMismatch");
expect(err.message).to.include("ConstraintTokenMint");
const account4 = await spl.getAccount(conn, pda_ata.address);
console.log("After 2nd withdraw: Account balance:", account4.amount.toString());
expect(account4.amount).to.be.eq(2_500_000n);
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("some tests", () => {
randomFillSync(newTss);
// console.log("generated new TSS address", newTss);
await gatewayProgram.methods.updateTss(Array.from(newTss)).accounts({

pda: pdaAccount,
}).rpc();
const pdaAccountData = await gatewayProgram.account.pda.fetch(pdaAccount);
// console.log("updated TSS address", pdaAccountData.tssAddress);
Expand Down Expand Up @@ -420,6 +420,38 @@ describe("some tests", () => {
}
});

it("create an account owned by the gateway program", async () => {
const gateway_id =gatewayProgram.programId;
console.log("gateway program id", gateway_id.toString());
const fake_pda = anchor.web3.Keypair.generate();
const rentExemption = await conn.getMinimumBalanceForRentExemption(100);
const instr1 = anchor.web3.SystemProgram.createAccount(
{
fromPubkey: wallet.publicKey,
newAccountPubkey: fake_pda.publicKey,
lamports: rentExemption,
space: 100,
programId: gatewayProgram.programId,
}
)
const tx = new anchor.web3.Transaction();
tx.add(instr1, );
await anchor.web3.sendAndConfirmTransaction(conn, tx, [wallet, fake_pda]);

const newTss = new Uint8Array(20);
randomFillSync(newTss);
// console.log("generated new TSS address", newTss);
try {
await gatewayProgram.methods.updateTss(Array.from(newTss)).accounts({
pda: fake_pda.publicKey,
}).rpc();
} catch (err) {
console.log("Error message: ", err.message);
expect(err).to.be.instanceof(anchor.AnchorError);
expect(err.message).to.include("AccountDiscriminatorMismatch.");
}
});


});

Expand Down

0 comments on commit b0d3184

Please sign in to comment.