Skip to content

Commit

Permalink
test(bridge): test TokenVault sendERC20 function (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerLamTd authored Jan 4, 2023
1 parent c513734 commit d5ca2a4
Showing 1 changed file with 195 additions and 22 deletions.
217 changes: 195 additions & 22 deletions packages/protocol/test/bridge/TokenVault.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
/* eslint-disable camelcase */
import { expect } from "chai";
import { AddressManager, TokenVault } from "../../typechain";
import {
AddressManager,
AddressManager__factory,
BridgedERC20,
BridgedERC20__factory,
TestMessageSender__factory,
TokenVault,
TokenVault__factory,
} from "../../typechain";
import { ethers } from "hardhat";
import { BigNumber, BigNumberish } from "ethers";
import { ADDRESS_RESOLVER_DENIED } from "../constants/errors";
import { MockContract, smock } from "@defi-wonderland/smock";

type CanonicalERC20 = {
chainId: BigNumberish;
Expand All @@ -23,46 +33,86 @@ const weth: CanonicalERC20 = {
describe("TokenVault", function () {
let owner: any;
let nonOwner: any;
let tokenVault: TokenVault;
let L1TokenVault: MockContract<TokenVault>;
let tokenVaultAddressManager: AddressManager;
let destChainTokenVault: TokenVault;
const defaultProcessingFee = 10;
const destChainId = 167001;
let bridgedToken: MockContract<BridgedERC20>;

before(async function () {
[owner, nonOwner] = await ethers.getSigners();
});

beforeEach(async function () {
const tokenVaultAddressManager: AddressManager = await (
await ethers.getContractFactory("AddressManager")
).deploy();
const network = await ethers.provider.getNetwork();
const addressManagerFactory: AddressManager__factory =
await ethers.getContractFactory("AddressManager");
const tokenVaultFactory: TokenVault__factory =
await ethers.getContractFactory("TokenVault");

tokenVaultAddressManager = await addressManagerFactory.deploy();
await tokenVaultAddressManager.init();

tokenVault = await (await ethers.getContractFactory("TokenVault"))
.connect(owner)
.deploy();
const mockTokenVaultFactory = await smock.mock<TokenVault__factory>(
"TokenVault"
);

await tokenVault.init(tokenVaultAddressManager.address);
L1TokenVault = await mockTokenVaultFactory.connect(owner).deploy();
await L1TokenVault.init(tokenVaultAddressManager.address);

const network = await ethers.provider.getNetwork();
destChainTokenVault = await tokenVaultFactory.connect(owner).deploy();
await destChainTokenVault.init(tokenVaultAddressManager.address);

const TestMessageSenderFactory = await ethers.getContractFactory(
"TestMessageSender"
);
const TestMessageSenderFactory: TestMessageSender__factory =
await ethers.getContractFactory("TestMessageSender");

const testMessageSender = await TestMessageSenderFactory.deploy();
const testMessageSender2 = await TestMessageSenderFactory.deploy();

await tokenVaultAddressManager.setAddress(
`${network.chainId}.bridge`,
testMessageSender.address
);
await tokenVaultAddressManager.setAddress(
`${destChainId}.bridge`,
testMessageSender2.address
);
await tokenVaultAddressManager.setAddress(
`${network.chainId}.token_vault`,
L1TokenVault.address
);
await tokenVaultAddressManager.setAddress(
`${destChainId}.token_vault`,
destChainTokenVault.address
);

const bridgedTokenFactory = await smock.mock<BridgedERC20__factory>(
"BridgedERC20"
);

bridgedToken = await bridgedTokenFactory.deploy();

await bridgedToken.init(
tokenVaultAddressManager.address,
weth.addr,
destChainId,
18,
weth.symbol,
weth.name
);

await bridgedToken.setVariable("_totalSupply", 1000000);
await bridgedToken.approve(owner.address, 1000);
await bridgedToken.setVariable("_balances", { [owner.address]: 10 });
});

describe("receiveERC20()", async () => {
it("throws when named 'bridge' is not the caller", async () => {
const amount = BigNumber.from(1);

await expect(
tokenVault.receiveERC20(
L1TokenVault.receiveERC20(
weth,
owner.address,
nonOwner.address,
Expand All @@ -75,7 +125,7 @@ describe("TokenVault", function () {
describe("sendEther()", async () => {
it("throws when msg.value is 0", async () => {
await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
owner.address,
10000,
Expand All @@ -88,7 +138,7 @@ describe("TokenVault", function () {

it("throws when msg.value - processing fee is 0", async () => {
await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
owner.address,
10000,
Expand All @@ -104,7 +154,7 @@ describe("TokenVault", function () {

it("throws when msg.value is < processingFee", async () => {
await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
owner.address,
10000,
Expand All @@ -120,7 +170,7 @@ describe("TokenVault", function () {

it("throws when to is 0", async () => {
await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
ethers.constants.AddressZero,
10000,
Expand All @@ -141,7 +191,7 @@ describe("TokenVault", function () {
"0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab";

await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
owner.address,
10000,
Expand All @@ -153,7 +203,7 @@ describe("TokenVault", function () {
}
)
)
.to.emit(tokenVault, "EtherSent")
.to.emit(L1TokenVault, "EtherSent")
.withArgs(
owner.address,
destChainId,
Expand All @@ -169,7 +219,7 @@ describe("TokenVault", function () {
"0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab";

await expect(
tokenVault.sendEther(
L1TokenVault.sendEther(
destChainId,
owner.address,
10000,
Expand All @@ -181,7 +231,7 @@ describe("TokenVault", function () {
}
)
)
.to.emit(tokenVault, "EtherSent")
.to.emit(L1TokenVault, "EtherSent")
.withArgs(
owner.address,
destChainId,
Expand All @@ -190,4 +240,127 @@ describe("TokenVault", function () {
);
});
});

describe("sendERC20()", async () => {
it("should throw if to == address(0)", async function () {
await expect(
L1TokenVault.sendERC20(
destChainId,
ethers.constants.AddressZero,
weth.addr,
1,
20000000,
1000,
owner.address,
"",
{
value: 1,
}
)
).to.be.revertedWith("V:to");
});

it("should throw if to == destChainId.token_vault", async function () {
await expect(
L1TokenVault.sendERC20(
destChainId,
destChainTokenVault.address,
weth.addr,
1,
20000000,
1000,
owner.address,
"",
{
value: 1,
}
)
).to.be.revertedWith("V:to");
});

it("should throw if token == address(0)", async function () {
await expect(
L1TokenVault.sendERC20(
destChainId,
nonOwner.address,
ethers.constants.AddressZero,
1,
20000000,
1000,
owner.address,
"",
{
value: 1,
}
)
).to.be.revertedWith("V:token");
});

it("should throw if amount <= 0", async function () {
await expect(
L1TokenVault.sendERC20(
destChainId,
nonOwner.address,
weth.addr,
0,
20000000,
1000,
owner.address,
"",
{
value: 1,
}
)
).to.be.revertedWith("V:amount");
});

it("should throw if isBridgedToken, and canonicalToken.addr == address(0)", async function () {
await L1TokenVault.setVariable("isBridgedToken", {
[bridgedToken.address]: true,
});
// don't need to manually set bridgedToCanonical since default value is addressZero

await expect(
L1TokenVault.connect(owner).sendERC20(
destChainId,
nonOwner.address,
bridgedToken.address,
1,
20000000,
1000,
owner.address,
"",
{
value: 1,
}
)
).to.be.revertedWith("V:canonicalToken");
});

it("should pass and emit ERC20Sent Event", async function () {
await L1TokenVault.setVariable("isBridgedToken", {
[bridgedToken.address]: true,
});

await L1TokenVault.setVariable("bridgedToCanonical", {
[bridgedToken.address]: weth,
});

await expect(
L1TokenVault.connect(owner).sendERC20(
destChainId,
nonOwner.address,
bridgedToken.address,
1,
20000000,
1000,
owner.address,
"",
{
value: 1000,
}
)
).to.emit(L1TokenVault, "ERC20Sent");
});
});
});

0 comments on commit d5ca2a4

Please sign in to comment.