Skip to content

Instantly share code, notes, and snippets.

@cagodoy
Created March 15, 2022 21:38
Show Gist options
  • Save cagodoy/1e33ee074a290e3f34cfd4cc04d68ad1 to your computer and use it in GitHub Desktop.
Save cagodoy/1e33ee074a290e3f34cfd4cc04d68ad1 to your computer and use it in GitHub Desktop.
Bridge example testing
import { ethers } from "hardhat";
import { use, expect } from "chai";
import { solidity } from "ethereum-waffle";
import { TransactionKind, TransactionStatus } from "../scripts/types";
use(solidity);
describe("Bridge Contract", () => {
let tokenContract, ownerBridgeContract, walletBridgeContract;
let wallet, walletTo;
beforeEach(async () => {
const name = "Token";
const symbol = "TK";
// prepare randoms wallets
wallet = ethers.Wallet.createRandom().connect(ethers.provider);
walletTo = ethers.Wallet.createRandom().connect(ethers.provider);
// send 1 eth from signer(0) to random wallet
{
const signer = ethers.provider.getSigner(0);
await signer.sendTransaction({
to: wallet.address,
value: ethers.utils.parseEther("1.0"),
});
}
// deploy token contract
tokenContract = await (await ethers.getContractFactory("Token")).deploy(name, symbol);
// deploy bridge contract
ownerBridgeContract = await (await ethers.getContractFactory("Bridge")).deploy(tokenContract.address);
walletBridgeContract = await ownerBridgeContract.connect(wallet);
});
it("Should return error when call the outbound method and the wallet don't have enough balance", async () => {
// deploy brige contract
const ownerBridgeContract = await (await ethers.getContractFactory("Bridge")).deploy(tokenContract.address);
const walletBridgeContract = await ownerBridgeContract.connect(wallet);
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// check error message
await expect(walletBridgeContract.outbound(wallet.address, ethers.utils.parseUnits("10.0", 18))).to.be.revertedWith(
"the current balance is not enough"
);
// check contract count value
const count = await walletBridgeContract.outboundCount();
expect(count).to.equal(ethers.constants.Zero);
// check outbound transaction using count value by index
const [_count, from, to, amount, status] = await walletBridgeContract.getOutboundTransactions(count);
expect(_count).to.equal(count);
expect(from).to.equal(ethers.constants.AddressZero);
expect(to).to.equal(ethers.constants.AddressZero);
expect(amount).to.equal(ethers.constants.Zero);
expect(status).to.equal(TransactionStatus.CREATING);
});
it("Should return success when call the outbound method", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// check contract count value
const count = await walletBridgeContract.outboundCount();
expect(count).to.equal(ethers.constants.Zero);
await expect(walletBridgeContract.outbound(walletTo.address, ethers.constants.Two))
.to.emit(walletBridgeContract, "Transaction")
.withArgs(
TransactionKind.OUTBOUND,
count,
wallet.address,
walletTo.address,
ethers.constants.Two,
TransactionStatus.MINTING
);
// check contract next count value
const nextCount = await walletBridgeContract.outboundCount();
expect(nextCount).to.equal(ethers.constants.One);
// check outbound transaction using count value by index
const [_count, from, to, amount, status] = await walletBridgeContract.getOutboundTransactions(count);
expect(_count).to.equal(count);
expect(from).to.equal(wallet.address);
expect(to).to.equal(walletTo.address);
expect(amount).to.equal(ethers.constants.Two);
expect(status).to.equal(TransactionStatus.MINTING);
});
it("Should fail when call the done method with invalid count", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// testing outbound contract method with invalid status
const randomCount = Math.floor(Math.random() * 10);
await expect(walletBridgeContract.done(randomCount, TransactionStatus.CREATING)).to.be.revertedWith(
"transaction not found"
);
});
it("Should fail when call the done method with invalid status", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// get contract count value
const count = await walletBridgeContract.outboundCount();
expect(count).to.equal(ethers.constants.Zero);
// call outbound contract method
await walletBridgeContract.outbound(walletTo.address, ethers.constants.Two);
// get and test next count contract value
const nextCount = await walletBridgeContract.outboundCount();
expect(nextCount).to.equal(ethers.constants.One);
// testing outbound contract method with invalid status
await expect(walletBridgeContract.done(count, TransactionStatus.CREATING)).to.be.revertedWith("invalid status");
await expect(walletBridgeContract.done(count, TransactionStatus.MINTING)).to.be.revertedWith("invalid status");
});
it("Should success when call the done method with valid ready status", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// get contract count value
const count = await walletBridgeContract.outboundCount();
// call outbound contract method and testing
await walletBridgeContract.outbound(walletTo.address, ethers.constants.Two);
await expect(walletBridgeContract.done(count, TransactionStatus.READY))
.to.emit(walletBridgeContract, "Transaction")
.withArgs(
TransactionKind.OUTBOUND,
count,
wallet.address,
walletTo.address,
ethers.constants.Two,
TransactionStatus.READY
);
// check outbound transaction using count value by index
const [_count, from, to, amount, status] = await walletBridgeContract.getOutboundTransactions(count);
expect(_count).to.equal(count);
expect(from).to.equal(wallet.address);
expect(to).to.equal(walletTo.address);
expect(amount).to.equal(ethers.constants.Two);
expect(status).to.equal(TransactionStatus.READY);
await expect(walletBridgeContract.done(count, TransactionStatus.READY)).to.be.revertedWith("transaction done");
});
it("Should success when call the done method with valid failed status", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// get contract count value
const count = await walletBridgeContract.outboundCount();
// call outbound contract method and testing
await walletBridgeContract.outbound(walletTo.address, ethers.constants.Two);
await expect(walletBridgeContract.done(count, TransactionStatus.FAILED))
.to.emit(walletBridgeContract, "Transaction")
.withArgs(
TransactionKind.OUTBOUND,
count,
wallet.address,
walletTo.address,
ethers.constants.Two,
TransactionStatus.FAILED
);
// check outbound transaction using count value by index
const [_count, from, to, amount, status] = await walletBridgeContract.getOutboundTransactions(count);
expect(_count).to.equal(count);
expect(from).to.equal(wallet.address);
expect(to).to.equal(walletTo.address);
expect(amount).to.equal(ethers.constants.Two);
expect(status).to.equal(TransactionStatus.FAILED);
await expect(walletBridgeContract.done(count, TransactionStatus.FAILED)).to.be.revertedWith("transaction done");
});
it("Should success when call the inbound method", async () => {
// mint tokens to random wallet
await tokenContract.mint(wallet.address, ethers.constants.Two);
// get contract count value
const count = await walletBridgeContract.inboundCount();
expect(count).to.equal(ethers.constants.Zero);
// call outbound contract method
await walletBridgeContract.outbound(walletTo.address, ethers.constants.Two);
// call inbound method and testing
await expect(walletBridgeContract.inbound(count, walletTo.address, ethers.constants.Two))
.to.emit(walletBridgeContract, "Transaction")
.withArgs(
TransactionKind.INBOUND,
count,
wallet.address,
walletTo.address,
ethers.constants.Two,
TransactionStatus.READY
);
// get and test next count contract value
const nextCount = await walletBridgeContract.inboundCount();
expect(nextCount).to.equal(ethers.constants.One);
await expect(walletBridgeContract.inbound(count, walletTo.address, ethers.constants.Two)).to.be.revertedWith(
"transaction done"
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment