From 812ec1858c252f741df6591f3559b5901ae9ea0a Mon Sep 17 00:00:00 2001 From: brewmaster012 <88689859+brewmaster012@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:52:30 -0500 Subject: [PATCH] add: allow withdraw SOL to PDA (#37) * allow withdraw to PDA (other than system account) * add a test to withdraw SOL to a ATA address (PDA) * Update tests/protocol-contracts-solana.ts Co-authored-by: skosito * substantiated comment regarding UncheckedAccount --------- Co-authored-by: skosito --- programs/protocol-contracts-solana/src/lib.rs | 3 ++- tests/protocol-contracts-solana.ts | 11 +---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/programs/protocol-contracts-solana/src/lib.rs b/programs/protocol-contracts-solana/src/lib.rs index d49454e..0a56b58 100644 --- a/programs/protocol-contracts-solana/src/lib.rs +++ b/programs/protocol-contracts-solana/src/lib.rs @@ -350,8 +350,9 @@ pub struct Withdraw<'info> { #[account(mut)] pub pda: Account<'info, Pda>, + /// CHECK: to account is not read so no need to check its owners; the program neither knows nor cares who the owner is. #[account(mut)] - pub to: SystemAccount<'info>, + pub to: UncheckedAccount<'info>, } #[derive(Accounts)] diff --git a/tests/protocol-contracts-solana.ts b/tests/protocol-contracts-solana.ts index 33c209b..96e58f8 100644 --- a/tests/protocol-contracts-solana.ts +++ b/tests/protocol-contracts-solana.ts @@ -248,15 +248,6 @@ describe("some tests", () => { it("deposit and withdraw 0.5 SOL from Gateway with ECDSA signature", async () => { await gatewayProgram.methods.deposit(new anchor.BN(1_000_000_000), Array.from(address)).accounts({pda: pdaAccount}).rpc(); - // const transaction = new anchor.web3.Transaction(); - // transaction.add( - // web3.SystemProgram.transfer({ - // fromPubkey: wallet.publicKey, - // toPubkey: pdaAccount, - // lamports: 1_000_000_000, - // }) - // ); - // await anchor.web3.sendAndConfirmTransaction(conn, transaction, [wallet]); let bal1 = await conn.getBalance(pdaAccount); console.log("pda account balance", bal1); expect(bal1).to.be.gte(1_000_000_000); @@ -271,7 +262,7 @@ describe("some tests", () => { // ); const nonce = pdaAccountData.nonce; const amount = new anchor.BN(500000000); - const to = wallet.publicKey; + const to = pda_ata.address; const buffer = Buffer.concat([ Buffer.from("withdraw","utf-8"), chain_id_bn.toArrayLike(Buffer, 'be', 8),