Skip to content

Commit

Permalink
solana: add negative outbound transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gator-boi committed Aug 4, 2023
1 parent 64d1dc1 commit 8fe2c50
Showing 1 changed file with 65 additions and 18 deletions.
83 changes: 65 additions & 18 deletions cross-chain/solana/tests/02__wormholeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,49 +583,96 @@ describe("wormhole-gateway", () => {
// Get destination gateway.
const recipientChain = 2;
const recipient = Array.from(Buffer.alloc(32, "deadbeef", "hex"));
const nonce = 420;
const nonce = 420;

// Try an amount that won't work.
const badAmount = BigInt(123000);
const badIx = await wormholeGateway.sendTbtcGatewayIx(
// This should work.
const sendAmount = BigInt(2000);
const ix = await wormholeGateway.sendTbtcGatewayIx(
{
senderToken,
sender,
},
{
amount: new anchor.BN(badAmount.toString()),
amount: new anchor.BN(sendAmount.toString()),
recipientChain,
recipient,
nonce,
}
);
await expectIxFail([badIx], [commonTokenOwner], "NotEnoughWrappedTbtc");
await expectIxSuccess([ix], [commonTokenOwner]);

// This should work.
const goodAmount = BigInt(2000);
// 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 tbtc to gateway (insufficient wrapped balance)", async () => {
// Use common token account.
const sender = commonTokenOwner.publicKey;
const senderToken = getAssociatedTokenAddressSync(
tbtc.getTokenPDA(),
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 sendAmount = gatewayWrappedBalance.amount + BigInt(69);
const ix = await wormholeGateway.sendTbtcGatewayIx(
{
senderToken,
sender,
},
{
amount: new anchor.BN(goodAmount.toString()),
amount: new anchor.BN(sendAmount.toString()),
recipientChain,
recipient,
nonce,
}
);
await expectIxSuccess([ix], [commonTokenOwner]);
await expectIxFail([ix], [commonTokenOwner], "NotEnoughWrappedTbtc");
});

// Check token accounts after sending tbtc.
const [senderTbtcAfter, gatewayAfter] = await Promise.all([
getAccount(connection, senderToken),
getAccount(connection, gatewayWrappedTbtcToken),
]);
it("cannot send tbtc to gateway (zero amount)", async () => {
// Use common token account.
const sender = commonTokenOwner.publicKey;
const senderToken = getAssociatedTokenAddressSync(
tbtc.getTokenPDA(),
sender
);

// Check balance change.
expect(senderTbtcAfter.amount).to.equal(senderTbtcBefore.amount - goodAmount);
expect(gatewayAfter.amount).to.equal(gatewayBefore.amount - goodAmount);
// 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.sendTbtcGatewayIx(
{
senderToken,
sender,
},
{
amount: new anchor.BN(sendAmount.toString()),
recipientChain,
recipient,
nonce,
}
);
await expectIxFail([ix], [commonTokenOwner], "ZeroAmount");
});

it("send wrapped tbtc", async () => {
Expand Down

0 comments on commit 8fe2c50

Please sign in to comment.