Skip to content

Commit

Permalink
update test with deposit and execution logic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlinaric committed Nov 22, 2023
1 parent d4e6130 commit 0c7f45e
Show file tree
Hide file tree
Showing 32 changed files with 793 additions and 276 deletions.
36 changes: 27 additions & 9 deletions test/contractBridge/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import { ethers } from "hardhat";
import { assert, expect } from "chai";
import type { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
import {
deployBridge,
deployBridgeContracts,
createResourceID,
createERCWithdrawData,
} from "../helpers";
import type {
Bridge,
Router,
ERC20Handler,
ERC20Handler__factory,
ERC20PresetMinterPauser,
ERC20PresetMinterPauser__factory,
Executor,
} from "../../typechain-types";

// This test does NOT include all getter methods, just
Expand All @@ -26,6 +28,8 @@ describe("Bridge - [admin]", () => {
const bytes32 = ethers.zeroPadValue("0x01", 32);

let bridgeInstance: Bridge;
let routerInstance: Router;
let executorInstance: Executor;
let ERC20MintableContract: ERC20PresetMinterPauser__factory;
let ERC20MintableInstance: ERC20PresetMinterPauser;
let ERC20HandlerContract: ERC20Handler__factory;
Expand All @@ -40,19 +44,21 @@ describe("Bridge - [admin]", () => {
[tokenOwnerAccount, nonAdminAccount, someAddress] =
await ethers.getSigners();

bridgeInstance = await deployBridge(domainID);
[bridgeInstance, routerInstance, executorInstance] =
await deployBridgeContracts(domainID);
ERC20MintableContract = await ethers.getContractFactory(
"ERC20PresetMinterPauser",
);
ERC20MintableInstance = await ERC20MintableContract.deploy("Token", "TOK");
ERC20HandlerContract = await ethers.getContractFactory("ERC20Handler");
ERC20HandlerInstance = await ERC20HandlerContract.deploy(
await bridgeInstance.getAddress(),
await routerInstance.getAddress(),
await executorInstance.getAddress(),
);
});

// Testing pausable methods

it("[sanity] Bridge should not be paused after deployments", async () => {
assert.isFalse(await bridgeInstance.paused());
});
Expand Down Expand Up @@ -109,6 +115,8 @@ describe("Bridge - [admin]", () => {
);
const ERC20HandlerInstance = await ERC20HandlerContract.deploy(
await bridgeInstance.getAddress(),
await routerInstance.getAddress(),
await executorInstance.getAddress(),
);

await expect(
Expand Down Expand Up @@ -180,6 +188,8 @@ describe("Bridge - [admin]", () => {
);
const ERC20HandlerInstance = await ERC20HandlerContract.deploy(
await bridgeInstance.getAddress(),
await routerInstance.getAddress(),
await executorInstance.getAddress(),
);

await expect(
Expand Down Expand Up @@ -233,6 +243,8 @@ describe("Bridge - [admin]", () => {
);
const ERC20HandlerInstance = await ERC20HandlerContract.deploy(
await bridgeInstance.getAddress(),
await routerInstance.getAddress(),
await executorInstance.getAddress(),
);

await expect(
Expand Down Expand Up @@ -289,26 +301,32 @@ describe("Bridge - [admin]", () => {

it("Should set nonce", async () => {
const nonce = 3;
await bridgeInstance.adminSetDepositNonce(domainID, nonce);
const nonceAfterSet = await bridgeInstance._depositCounts(domainID);
await routerInstance
.connect(tokenOwnerAccount)
.adminSetDepositNonce(domainID, nonce);
const nonceAfterSet = await routerInstance._depositCounts(domainID);
assert.deepEqual(nonceAfterSet, BigInt(nonce));
});

it("Should require admin role to set nonce", async () => {
await expect(
bridgeInstance.connect(nonAdminAccount).adminSetDepositNonce(1, 3),
routerInstance.connect(nonAdminAccount).adminSetDepositNonce(1, 3),
).to.be.revertedWithCustomError(
bridgeInstance,
routerInstance,
"AccessNotAllowed(address,bytes4)",
);
});

it("Should not allow for decrements of the nonce", async () => {
const currentNonce = 3;
await bridgeInstance.adminSetDepositNonce(domainID, currentNonce);
await routerInstance
.connect(tokenOwnerAccount)
.adminSetDepositNonce(domainID, currentNonce);
const newNonce = 2;
await expect(
bridgeInstance.adminSetDepositNonce(domainID, newNonce),
routerInstance
.connect(tokenOwnerAccount)
.adminSetDepositNonce(domainID, newNonce),
).to.be.revertedWith("Does not allow decrements of the nonce");
});

Expand Down
44 changes: 31 additions & 13 deletions test/contractBridge/depositERC20.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { assert, expect } from "chai";
import { ethers } from "hardhat";
import type {
Bridge,
Depositor,
ERC20Handler,
ERC20PresetMinterPauser,
Executor,
} from "../../typechain-types";
import {
createERCDepositData,
createResourceID,
deployBridge,
deployBridgeContracts,
} from "../helpers";

describe("Bridge - [deposit - ERC20]", () => {
Expand All @@ -26,6 +28,8 @@ describe("Bridge - [deposit - ERC20]", () => {
const emptySetResourceData = "0x";

let bridgeInstance: Bridge;
let routerInstance: Depositor;
let executorInstance: Executor;
let ERC20MintableInstance1: ERC20PresetMinterPauser;
let ERC20MintableInstance2: ERC20PresetMinterPauser;
let ERC20HandlerInstance: ERC20Handler;
Expand All @@ -39,7 +43,8 @@ describe("Bridge - [deposit - ERC20]", () => {
beforeEach(async () => {
[, depositorAccount, recipientAccount] = await ethers.getSigners();

bridgeInstance = await deployBridge(Number(originDomainID));
[bridgeInstance, routerInstance, executorInstance] =
await deployBridgeContracts(originDomainID);
const ERC20MintableContract = await ethers.getContractFactory(
"ERC20PresetMinterPauser",
);
Expand All @@ -55,6 +60,8 @@ describe("Bridge - [deposit - ERC20]", () => {
await ethers.getContractFactory("ERC20Handler");
ERC20HandlerInstance = await ERC20HandlerContract.deploy(
await bridgeInstance.getAddress(),
await routerInstance.getAddress(),
await executorInstance.getAddress(),
);

resourceID1 = createResourceID(
Expand Down Expand Up @@ -123,24 +130,35 @@ describe("Bridge - [deposit - ERC20]", () => {

it("ERC20 deposit can be made", async () => {
await expect(
bridgeInstance
routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData),
).not.to.be.reverted;
});

it("should revert ERC20 deposit if Bridge is paused", async () => {
assert.isFalse(await bridgeInstance.paused());
await expect(bridgeInstance.adminPauseTransfers()).not.to.be.reverted;
assert.isTrue(await bridgeInstance.paused());
await expect(
routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData),
).to.be.revertedWithCustomError(routerInstance, "BridgeIsPaused()");
});

it("_depositCounts should be increments from 0 to 1", async () => {
await bridgeInstance
await routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData);

const depositCount =
await bridgeInstance._depositCounts(destinationDomainID);
await routerInstance._depositCounts(destinationDomainID);
assert.strictEqual(depositCount, BigInt(expectedDepositNonce));
});

it("ERC20 can be deposited with correct balances", async () => {
await bridgeInstance
await routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData);

Expand All @@ -158,12 +176,12 @@ describe("Bridge - [deposit - ERC20]", () => {
});

it("Deposit event is fired with expected value", async () => {
const depositTx1 = bridgeInstance
const depositTx1 = routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData);

await expect(depositTx1)
.to.emit(bridgeInstance, "Deposit")
.to.emit(routerInstance, "Deposit")
.withArgs(
destinationDomainID,
resourceID1.toLowerCase(),
Expand All @@ -173,12 +191,12 @@ describe("Bridge - [deposit - ERC20]", () => {
"0x",
);

const depositTx2 = bridgeInstance
const depositTx2 = routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID1, depositData, feeData);

await expect(depositTx2)
.to.emit(bridgeInstance, "Deposit")
.to.emit(routerInstance, "Deposit")
.withArgs(
destinationDomainID,
resourceID1,
Expand All @@ -191,7 +209,7 @@ describe("Bridge - [deposit - ERC20]", () => {

it("deposit requires resourceID that is mapped to a handler", async () => {
await expect(
bridgeInstance
routerInstance
.connect(depositorAccount)
.deposit(
destinationDomainID,
Expand All @@ -204,15 +222,15 @@ describe("Bridge - [deposit - ERC20]", () => {

it("Deposit destination domain can not be current bridge domain ", async () => {
await expect(
bridgeInstance
routerInstance
.connect(depositorAccount)
.deposit(originDomainID, resourceID1, depositData, feeData),
).to.be.rejectedWith("DepositToCurrentDomain()");
});

it("should revert if ERC20Safe contract call fails", async () => {
await expect(
bridgeInstance
routerInstance
.connect(depositorAccount)
.deposit(destinationDomainID, resourceID2, depositData, feeData),
).to.be.revertedWith("ERC20: operation did not succeed");
Expand Down
Loading

0 comments on commit 0c7f45e

Please sign in to comment.