Skip to content

Commit

Permalink
solana: add negative outbound wrapped transfer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gator-boi committed Aug 4, 2023
1 parent 67dad30 commit 0d71328
Showing 1 changed file with 127 additions and 17 deletions.
144 changes: 127 additions & 17 deletions cross-chain/solana/tests/02__wormholeGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
});
});

0 comments on commit 0d71328

Please sign in to comment.