From 0d71328b37462e1cbd7884c015e54d059bf6db58 Mon Sep 17 00:00:00 2001 From: gator-boi Date: Fri, 4 Aug 2023 15:43:50 -0500 Subject: [PATCH] solana: add negative outbound wrapped transfer tests --- .../solana/tests/02__wormholeGateway.ts | 144 +++++++++++++++--- 1 file changed, 127 insertions(+), 17 deletions(-) diff --git a/cross-chain/solana/tests/02__wormholeGateway.ts b/cross-chain/solana/tests/02__wormholeGateway.ts index 021dc290c..a2e3f069b 100644 --- a/cross-chain/solana/tests/02__wormholeGateway.ts +++ b/cross-chain/solana/tests/02__wormholeGateway.ts @@ -712,6 +712,35 @@ describe("wormhole-gateway", () => { await expectIxFail([ix], [commonTokenOwner], "ZeroAmount"); }); + it("cannot send tbtc to gateway (recipient is zero address)", async () => { + // Use common token account. + const sender = commonTokenOwner.publicKey; + const senderToken = getAssociatedTokenAddressSync( + tbtc.getMintPDA(), + sender + ); + + // Get destination gateway. + const recipientChain = 2; + const recipient = Array.from(Buffer.alloc(32)); // empty buffer + const nonce = 420; + + const sendAmount = BigInt(69); + const ix = await wormholeGateway.sendTbtcGatewayIx( + { + senderToken, + sender, + }, + { + amount: new anchor.BN(sendAmount.toString()), + recipientChain, + recipient, + nonce, + } + ); + await expectIxFail([ix], [commonTokenOwner], "ZeroRecipient"); + }); + it("send wrapped tbtc", async () => { // Use common token account. const sender = commonTokenOwner.publicKey; @@ -731,50 +760,131 @@ describe("wormhole-gateway", () => { const recipient = Array.from(Buffer.alloc(32, "deadbeef", "hex")); const nonce = 420; + // This should work. + const sendAmount = BigInt(2000); + const ix = await wormholeGateway.sendTbtcWrappedIx( + { + senderToken, + sender, + }, + { + amount: new anchor.BN(sendAmount.toString()), + recipientChain, + recipient, + arbiterFee: new anchor.BN(0), + nonce, + } + ); + await expectIxSuccess([ix], [commonTokenOwner]); + + // Check token accounts after sending tbtc. + const [senderTbtcAfter, gatewayAfter] = await Promise.all([ + getAccount(connection, senderToken), + getAccount(connection, gatewayWrappedTbtcToken), + ]); + + // Check balance change. + expect(senderTbtcAfter.amount).to.equal( + senderTbtcBefore.amount - sendAmount + ); + expect(gatewayAfter.amount).to.equal(gatewayBefore.amount - sendAmount); + }); + + it("cannot send wrapped tbtc (insufficient wrapped balance)", async () => { + // Use common token account. + const sender = commonTokenOwner.publicKey; + const senderToken = getAssociatedTokenAddressSync( + tbtc.getMintPDA(), + sender + ); + + // Get destination gateway. + const recipientChain = 2; + const recipient = Array.from(Buffer.alloc(32, "deadbeef", "hex")); + const nonce = 420; + + // Check token accounts. + const gatewayWrappedBalance = await getAccount( + connection, + gatewayWrappedTbtcToken + ); + // Try an amount that won't work. - const badAmount = BigInt(123000); - const badIx = await wormholeGateway.sendTbtcWrappedIx( + const sendAmount = gatewayWrappedBalance.amount + BigInt(69); + const ix = await wormholeGateway.sendTbtcWrappedIx( { senderToken, sender, }, { - amount: new anchor.BN(badAmount.toString()), + amount: new anchor.BN(sendAmount.toString()), recipientChain, recipient, arbiterFee: new anchor.BN(0), nonce, } ); - await expectIxFail([badIx], [commonTokenOwner], "NotEnoughWrappedTbtc"); + await expectIxFail([ix], [commonTokenOwner], "NotEnoughWrappedTbtc"); + }); - // This should work. - const goodAmount = BigInt(2000); + it("cannot send wrapped tbtc(zero amount)", async () => { + // Use common token account. + const sender = commonTokenOwner.publicKey; + const senderToken = getAssociatedTokenAddressSync( + tbtc.getMintPDA(), + sender + ); + + // Get destination gateway. + const recipientChain = 2; + const recipient = Array.from(Buffer.alloc(32, "deadbeef", "hex")); + const nonce = 420; + + // Try an amount that won't work. + const sendAmount = BigInt(0); const ix = await wormholeGateway.sendTbtcWrappedIx( { senderToken, sender, }, { - amount: new anchor.BN(goodAmount.toString()), + amount: new anchor.BN(sendAmount.toString()), recipientChain, recipient, arbiterFee: new anchor.BN(0), nonce, } ); - await expectIxSuccess([ix], [commonTokenOwner]); + await expectIxFail([ix], [commonTokenOwner], "ZeroAmount"); + }); - // Check token accounts after sending tbtc. - const [senderTbtcAfter, gatewayAfter] = await Promise.all([ - getAccount(connection, senderToken), - getAccount(connection, gatewayWrappedTbtcToken), - ]); + it("cannot send wrapped tbtc (recipient is zero address)", async () => { + // Use common token account. + const sender = commonTokenOwner.publicKey; + const senderToken = getAssociatedTokenAddressSync( + tbtc.getMintPDA(), + sender + ); - // Check balance change. - expect(senderTbtcAfter.amount).to.equal( - senderTbtcBefore.amount - goodAmount + // Get destination gateway. + const recipientChain = 2; + const recipient = Array.from(Buffer.alloc(32)); // empty buffer + const nonce = 420; + + const sendAmount = BigInt(69); + const ix = await wormholeGateway.sendTbtcWrappedIx( + { + senderToken, + sender, + }, + { + amount: new anchor.BN(sendAmount.toString()), + recipientChain, + recipient, + arbiterFee: new anchor.BN(0), + nonce, + } ); - expect(gatewayAfter.amount).to.equal(gatewayBefore.amount - goodAmount); + await expectIxFail([ix], [commonTokenOwner], "ZeroRecipient"); }); });