Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Add snapshot tests + refactor to Router
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Sep 18, 2023
1 parent 109ec42 commit 00875bd
Showing 1 changed file with 39 additions and 132 deletions.
171 changes: 39 additions & 132 deletions test/core/router/Router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {FakeContract, smock} from "@defi-wonderland/smock";
import {SnapshotRestorer, takeSnapshot} from "@nomicfoundation/hardhat-network-helpers";
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {expect, use} from "chai";
import {Wallet} from "ethers";
Expand Down Expand Up @@ -77,33 +78,44 @@ describe("Router", function () {
RouterProxy.upgradeTo(RouterImpl.address);
}

describe("Deployment", function () {
let registrar: FakeContract<Registrar>;
let router: Router;
beforeEach(async function () {
registrar = await smock.fake<Registrar>(new Registrar__factory());
router = await deployRouterAsProxy(registrar.address);
});
let router: Router;

let liquidVault: FakeContract<DummyVault>;
let lockedVault: FakeContract<DummyVault>;
let gasService: FakeContract<DummyGasService>;
let gateway: FakeContract<DummyGateway>;
let registrar: FakeContract<Registrar>;
let token: FakeContract<DummyERC20>;

before(async () => {
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
token = await smock.fake<DummyERC20>(new DummyERC20__factory());

router = await deployRouterAsProxy(registrar.address);
});

let snapshot: SnapshotRestorer;

beforeEach(async () => {
snapshot = await takeSnapshot();
});

afterEach(async () => {
await snapshot.restore();
});

describe("Deployment", function () {
it("Should successfully deploy the contract as an upgradable proxy", async function () {
expect(router.address);
expect(await upgradeProxy(admin, router.address));
await expect(upgradeProxy(admin, router.address)).to.not.be.reverted;
});
});

describe("Protected methods", function () {
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand All @@ -120,7 +132,6 @@ describe("Router", function () {
.returns(accountsContract);
registrar.getAccountsContractAddressByChain.whenCalledWith(localChain).returns(owner.address);
registrar.thisChain.returns(localChain);
router = await deployRouterAsProxy(registrar.address);
});

it("Does not allow a non-accounts contract address on another chain to call executeWithToken via GMP", async function () {
Expand Down Expand Up @@ -195,17 +206,11 @@ describe("Router", function () {
});

describe("Correctly triggers the refund process on failed Deposit", function () {
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;

const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultActionData = () => ({
...DEFAULT_ACTION_DATA,
accountId: 1,
Expand All @@ -214,12 +219,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand All @@ -243,7 +242,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

describe("and the refund call is successful back through axelar", function () {
Expand Down Expand Up @@ -370,16 +368,11 @@ describe("Router", function () {
});

describe("and the refund call fails through axelar and falls back to the refund collector", async function () {
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultActionData = () => ({
...DEFAULT_ACTION_DATA,
accountId: 1,
Expand All @@ -388,12 +381,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand All @@ -419,7 +406,6 @@ describe("Router", function () {
token.approve.returns(false);
token.approveFor.returns(false);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("when an action other than deposit is called", async function () {
Expand Down Expand Up @@ -542,17 +528,11 @@ describe("Router", function () {
});

describe("Routes messages according to payload instructions", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultActionData = () => ({
...DEFAULT_ACTION_DATA,
accountId: 1,
Expand All @@ -561,13 +541,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand Down Expand Up @@ -600,7 +573,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("correctly calls depost", async function () {
Expand Down Expand Up @@ -678,17 +650,11 @@ describe("Router", function () {
});

describe("Deposit", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultActionData = () => ({
...DEFAULT_ACTION_DATA,
accountId: 1,
Expand All @@ -697,13 +663,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand Down Expand Up @@ -734,7 +693,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("deposits the specified amounts to the specified vaults", async function () {
Expand All @@ -756,13 +714,6 @@ describe("Router", function () {
});

describe("Redeem", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
Expand All @@ -775,13 +726,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand Down Expand Up @@ -814,7 +758,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("Redeems the amounts specified back to the router", async function () {
Expand Down Expand Up @@ -905,17 +848,11 @@ describe("Router", function () {
});

describe("RedeemAll", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let gasService: FakeContract<DummyGasService>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const GAS_COST = 5;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultActionData = () => ({
...DEFAULT_ACTION_DATA,
accountId: 1,
Expand All @@ -924,13 +861,6 @@ describe("Router", function () {
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
gasService = await smock.fake<DummyGasService>(new DummyGasService__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand Down Expand Up @@ -963,7 +893,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("Redeems the amounts specified back to the router", async function () {
Expand Down Expand Up @@ -1054,27 +983,16 @@ describe("Router", function () {
});

describe("Harvest cross-chain", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let gateway: FakeContract<DummyGateway>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultHarvestRequest = () => ({
...DEFAULT_HARVEST_REQUEST,
accountIds: [1],
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
gateway = await smock.fake<DummyGateway>(new DummyGateway__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
axelarGateway: gateway.address,
Expand Down Expand Up @@ -1106,7 +1024,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("Harvests the targeted accounts and forwards tokens cross-chain", async function () {
Expand All @@ -1131,25 +1048,16 @@ describe("Router", function () {
});

describe("Harvest locally", function () {
let lockedVault: FakeContract<DummyVault>;
let liquidVault: FakeContract<DummyVault>;
let registrar: FakeContract<Registrar>;
let token: FakeContract<DummyERC20>;
let router: Router;
const LOCK_AMT = 111;
const LIQ_AMT = 222;
const TOTAL_AMT = LOCK_AMT + LIQ_AMT;

const getDefaultHarvestRequest = () => ({
...DEFAULT_HARVEST_REQUEST,
accountIds: [1],
});

beforeEach(async function () {
token = await smock.fake<DummyERC20>(new DummyERC20__factory());
registrar = await smock.fake<Registrar>(new Registrar__factory());
lockedVault = await smock.fake<DummyVault>(new DummyVault__factory());
liquidVault = await smock.fake<DummyVault>(new DummyVault__factory());

const networkParams = {
...DEFAULT_NETWORK_INFO,
};
Expand All @@ -1175,7 +1083,6 @@ describe("Router", function () {
token.approve.returns(true);
token.approveFor.returns(true);
token.symbol.returns("TKN");
router = await deployRouterAsProxy(registrar.address);
});

it("Reverts if the caller is not a valid operator", async function () {
Expand Down

0 comments on commit 00875bd

Please sign in to comment.