Skip to content

Commit

Permalink
Add ERC20 and XC20 tests for failing contract operation
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlinaric committed Apr 25, 2023
1 parent 2a746d5 commit 1214436
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 45 deletions.
59 changes: 40 additions & 19 deletions test/contractBridge/depositERC20.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const TruffleAssert = require("truffle-assertions");
const Helpers = require("../helpers");

const ERC20MintableContract = artifacts.require("ERC20PresetMinterPauser");
const ERC20MintableContractMock = artifacts.require("ERC20PresetMinterPauserMock");
const ERC20HandlerContract = artifacts.require("ERC20Handler");

contract("Bridge - [deposit - ERC20]", async (accounts) => {
Expand All @@ -25,24 +26,35 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {

let BridgeInstance;
let OriginERC20MintableInstance;
let OriginERC20MintableInstanceMock;
let OriginERC20HandlerInstance;
let depositData;
let initialResourceIDs;

beforeEach(async () => {
await Promise.all([
ERC20MintableContract.new("token", "TOK").then(
(instance) => (OriginERC20MintableInstance = instance)
),
ERC20MintableContractMock.new("token", "TOK").then(
(instance) => (OriginERC20MintableInstanceMock = instance)
),
(BridgeInstance = await Helpers.deployBridge(
originDomainID,
adminAddress
)),
]);

resourceID = Helpers.createResourceID(
const resourceID1 = Helpers.createResourceID(
OriginERC20MintableInstance.address,
originDomainID
);
const resourceID2 = Helpers.createResourceID(
OriginERC20MintableInstanceMock.address,
originDomainID
);

initialResourceIDs = [resourceID1, resourceID2];

OriginERC20HandlerInstance = await ERC20HandlerContract.new(
BridgeInstance.address
Expand All @@ -51,20 +63,35 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
await Promise.all([
BridgeInstance.adminSetResource(
OriginERC20HandlerInstance.address,
resourceID,
initialResourceIDs[0],
OriginERC20MintableInstance.address,
emptySetResourceData
),
BridgeInstance.adminSetResource(
OriginERC20HandlerInstance.address,
initialResourceIDs[1],
OriginERC20MintableInstanceMock.address,
emptySetResourceData
),
OriginERC20MintableInstance.mint(
depositorAddress,
originChainInitialTokenAmount
),
OriginERC20MintableInstanceMock.mint(
depositorAddress,
originChainInitialTokenAmount
),
]);
await OriginERC20MintableInstance.approve(
OriginERC20HandlerInstance.address,
depositAmount * 2,
{from: depositorAddress}
);
await OriginERC20MintableInstanceMock.approve(
OriginERC20HandlerInstance.address,
depositAmount * 5,
{from: depositorAddress}
);

depositData = Helpers.createERCDepositData(
depositAmount,
Expand Down Expand Up @@ -101,7 +128,7 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
await TruffleAssert.passes(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
initialResourceIDs[0],
depositData,
feeData,
{from: depositorAddress}
Expand All @@ -112,7 +139,7 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
it("_depositCounts should be increments from 0 to 1", async () => {
await BridgeInstance.deposit(
destinationDomainID,
resourceID,
initialResourceIDs[0],
depositData,
feeData,
{from: depositorAddress}
Expand All @@ -127,7 +154,7 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
it("ERC20 can be deposited with correct balances", async () => {
await BridgeInstance.deposit(
destinationDomainID,
resourceID,
initialResourceIDs[0],
depositData,
feeData,
{from: depositorAddress}
Expand All @@ -150,7 +177,7 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
it("Deposit event is fired with expected value", async () => {
let depositTx = await BridgeInstance.deposit(
destinationDomainID,
resourceID,
initialResourceIDs[0],
depositData,
feeData,
{from: depositorAddress}
Expand All @@ -159,14 +186,14 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
TruffleAssert.eventEmitted(depositTx, "Deposit", (event) => {
return (
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase() &&
event.resourceID === initialResourceIDs[0].toLowerCase() &&
event.depositNonce.toNumber() === expectedDepositNonce
);
});

depositTx = await BridgeInstance.deposit(
destinationDomainID,
resourceID,
initialResourceIDs[0],
depositData,
feeData,
{from: depositorAddress}
Expand All @@ -175,7 +202,7 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
TruffleAssert.eventEmitted(depositTx, "Deposit", (event) => {
return (
event.destinationDomainID.toNumber() === destinationDomainID &&
event.resourceID === resourceID.toLowerCase() &&
event.resourceID === initialResourceIDs[0].toLowerCase() &&
event.depositNonce.toNumber() === expectedDepositNonce + 1
);
});
Expand All @@ -199,22 +226,16 @@ contract("Bridge - [deposit - ERC20]", async (accounts) => {
);
});

it("should revert with \"FailedERC20Call\" custom error if ERC20Safe contract call fails", async () => {
const failingDepositData = Helpers.createERCDepositData(
depositAmount * 5, // deposit amount greater than allowance
20,
recipientAddress
);

it("should revert with \"FailedERC20Operation\" custom error if ERC20Safe contract call fails", async () => {
await Helpers.expectToRevertWithCustomError(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
failingDepositData,
initialResourceIDs[1],
depositData,
feeData,
{from: depositorAddress}
),
"FailedERC20Call"
"FailedERC20Operation()"
);
});
});
Loading

0 comments on commit 1214436

Please sign in to comment.