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

Commit

Permalink
Fixed issues in Router, adjusted tests accordingly (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevieraykatz authored Jul 25, 2023
1 parent 3cfce77 commit ed84c17
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 78 deletions.
8 changes: 5 additions & 3 deletions contracts/core/router/Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ contract Router is IRouter, AxelarExecutable {
require(action.accountIds.length == 1, "Only one account allowed");
// deposit only
require(action.selector == IVault.deposit.selector, "Only deposit accepts tokens");
// token fwd is token expected
address tokenAddress = gateway.tokenAddresses(tokenSymbol);
require(tokenAddress == action.token, "Token mismatch");
// amt fwd equal expected amt
require(amount == (action.liqAmt + action.lockAmt), "Amount mismatch");
// check that at least one vault is expected to receive a deposit
require(action.lockAmt > 0 || action.liqAmt > 0, "No vault deposit specified");
// check that token is accepted by angel protocol
address tokenAddress = gateway.tokenAddresses(tokenSymbol);
require(registrar.isTokenAccepted(tokenAddress), "Token not accepted");
// Get parameters from registrar if approved
require(
Expand Down Expand Up @@ -408,6 +406,10 @@ contract Router is IRouter, AxelarExecutable {
// decode payload
IVault.VaultActionData memory action = RouterLib.unpackCalldata(payload);

// grab tokens sent cross-chain
address tokenAddress = gateway.tokenAddresses(tokenSymbol);
IERC20Metadata(tokenAddress).safeTransferFrom(address(gateway), address(this), amount);

// Leverage this.call() to enable try/catch logic
try this.deposit(action, tokenSymbol, amount) {
emit Deposit(action);
Expand Down
4 changes: 0 additions & 4 deletions tasks/helpers/deploy/deployDummyERC20.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers";
import {DummyERC20__factory, DummyERC20} from "typechain-types";
import {logger} from "utils";

export async function mint(token: DummyERC20, to: string, amt: number) {
await token.mint(to, amt);
Expand All @@ -12,16 +11,13 @@ export async function deployDummyERC20(
amounts?: number[],
decimals?: number
) {
logger.out("Deploying dummy ERC20...");
const Token = new DummyERC20__factory(deployer);
const decs = decimals ? decimals : 0;
const token = await Token.deploy(decs);
await token.deployed();
logger.out(`Address: ${token.address}`);

if (recipients && amounts) {
for (var i in recipients) {
logger.out(`Minting ${amounts[i]} tokens to ${recipients[i]}...`);
await mint(token, recipients[i], amounts[i]);
}
}
Expand Down
105 changes: 34 additions & 71 deletions test/core/router/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ describe("Router", function () {
});
gateway = await deployDummyGateway(owner);
gasService = await deployDummyGasService(owner);
token = await deployDummyERC20(owner);
registrar = await deployRegistrarAsProxy(owner, admin);
await gateway.setTestTokenAddress(token.address);
await registrar.setTokenAccepted(token.address, true);
Expand All @@ -257,15 +256,12 @@ describe("Router", function () {

beforeEach(async function () {
router = await deployRouterAsProxy(gateway.address, gasService.address, registrar);
token.mint(router.address, 333);
await token.mint(gateway.address, 333);
await token.approveFor(gateway.address, router.address, 333)
let collectorBal = await token.balanceOf(collector.address);
if (collectorBal.gt(0)) {
await token.connect(collector).transfer(deadAddr, collectorBal);
}
let gatewayBal = await token.balanceOf(gateway.address);
if (gatewayBal.gt(0)) {
await token.connect(collector).transfer(deadAddr, gatewayBal);
}
});

it("when more than one account is specified", async function () {
Expand Down Expand Up @@ -311,27 +307,6 @@ describe("Router", function () {
expect(gatewayAllowance).to.equal(333);
});

it("when the token designation doesn't match", async function () {
let actionData = getDefaultActionData();
actionData.selector = liquidVault.interface.getSighash("deposit");
actionData.token = token.address;
let packedData = await packActionData(actionData);
await expect(
router.executeWithToken(
ethers.utils.formatBytes32String("true"),
originatingChain,
accountsContract,
packedData,
"WRONG",
333
)
)
.to.emit(router, "ErrorLogged")
.withArgs(Array<any>, "Token mismatch");
let gatewayAllowance = await token.allowance(router.address, gateway.address);
expect(gatewayAllowance).to.equal(333);
});

it("when the payload amt doesn't match the GMP amt", async function () {
let actionData = getDefaultActionData();
actionData.selector = liquidVault.interface.getSighash("deposit");
Expand Down Expand Up @@ -460,7 +435,8 @@ describe("Router", function () {

beforeEach(async function () {
router = await deployRouterAsProxy(gateway.address, undefined, registrar); // set gas service to undef so that the sendTokens call fails
token.mint(router.address, 333);
await token.mint(gateway.address, 333);
await token.approveFor(gateway.address, router.address, 333)
let collectorBal = await token.balanceOf(collector.address);
if (collectorBal.gt(0)) {
await token.connect(collector).transfer(deadAddr, collectorBal);
Expand Down Expand Up @@ -511,27 +487,6 @@ describe("Router", function () {
expect(collectorBal).to.equal(333);
});

it("when the token designation doesn't match", async function () {
let actionData = getDefaultActionData();
actionData.selector = liquidVault.interface.getSighash("deposit");
actionData.token = token.address;
let packedData = await packActionData(actionData);
expect(
await router.executeWithToken(
ethers.utils.formatBytes32String("true"),
originatingChain,
accountsContract,
packedData,
"WRONG",
333
)
)
.to.emit(router, "ErrorLogged")
.withArgs(Array<any>, "Token mismatch");
let collectorBal = await token.balanceOf(collector.address);
expect(collectorBal).to.equal(333);
});

it("when the payload amt doesn't match the GMP amt", async function () {
let actionData = getDefaultActionData();
actionData.selector = liquidVault.interface.getSighash("deposit");
Expand Down Expand Up @@ -646,14 +601,16 @@ describe("Router", function () {
let gasService: DummyGasService;
let token: DummyERC20;
let router: Router;
const LOCKAMT = 111;
const LIQAMT = 222;
let actionData = {
destinationChain: originatingChain,
strategyId: "0xffffffff",
selector: "",
accountIds: [1],
token: "",
lockAmt: 111,
liqAmt: 222,
lockAmt: LOCKAMT,
liqAmt: LIQAMT,
status: 0,
} as IVaultHelpers.VaultActionDataStruct;

Expand Down Expand Up @@ -694,8 +651,8 @@ describe("Router", function () {
});

it("correctly calls depost", async function () {
await token.mint(router.address, actionData.liqAmt);
await token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
actionData.selector = liquidVault.interface.getSighash("deposit");
let packedData = await packActionData(actionData);
await expect(
Expand All @@ -712,8 +669,8 @@ describe("Router", function () {

it("correctly calls redeem via execute", async function () {
// Do a deposit first to update the symbol mapping
await token.mint(router.address, actionData.liqAmt);
await token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
actionData.selector = liquidVault.interface.getSighash("deposit");
let packedData = await packActionData(actionData);
await router.executeWithToken(
Expand Down Expand Up @@ -741,8 +698,8 @@ describe("Router", function () {

it("correctly calls redeemAll via execute", async function () {
// Do a deposit first to update the symbol mapping
await token.mint(router.address, actionData.liqAmt);
await token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
actionData.selector = liquidVault.interface.getSighash("deposit");
let packedData = await packActionData(actionData);
await router.executeWithToken(
Expand All @@ -758,8 +715,8 @@ describe("Router", function () {
actionData.selector = liquidVault.interface.getSighash("redeemAll");
actionData.token = token.address;
packedData = await packActionData(actionData);
await token.mint(liquidVault.address, actionData.liqAmt);
await token.mint(lockedVault.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
await expect(
router.execute(
ethers.utils.formatBytes32String("true"),
Expand Down Expand Up @@ -792,14 +749,16 @@ describe("Router", function () {
let gasService: DummyGasService;
let token: DummyERC20;
let router: Router;
const LOCKAMT = 111;
const LIQAMT = 222;
let actionData = {
destinationChain: originatingChain,
strategyId: "0xffffffff",
selector: "",
accountIds: [1],
token: "",
lockAmt: 111,
liqAmt: 222,
lockAmt: LOCKAMT,
liqAmt: LIQAMT,
status: 0,
} as IVaultHelpers.VaultActionDataStruct;

Expand Down Expand Up @@ -837,8 +796,8 @@ describe("Router", function () {
});

it("deposits the specified amounts to the specified vaults", async function () {
token.mint(router.address, actionData.liqAmt);
token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
let packedData = packActionData(actionData);
await router.executeWithToken(
ethers.utils.formatBytes32String("true"),
Expand All @@ -861,14 +820,16 @@ describe("Router", function () {
let gasService: DummyGasService;
let token: DummyERC20;
let router: Router;
const LOCKAMT = 111;
const LIQAMT = 222;
let actionData = {
destinationChain: originatingChain,
strategyId: "0xffffffff",
selector: "",
accountIds: [1],
token: "",
lockAmt: 111,
liqAmt: 222,
lockAmt: LOCKAMT,
liqAmt: LIQAMT,
status: 0,
} as IVaultHelpers.VaultActionDataStruct;

Expand Down Expand Up @@ -902,8 +863,8 @@ describe("Router", function () {

beforeEach(async function () {
router = await deployRouterAsProxy(gateway.address, gasService.address, registrar);
await token.mint(router.address, actionData.liqAmt);
await token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
actionData.selector = liquidVault.interface.getSighash("deposit");
let packedData = packActionData(actionData);
await router.executeWithToken(
Expand Down Expand Up @@ -1009,14 +970,16 @@ describe("Router", function () {
let gasService: DummyGasService;
let token: DummyERC20;
let router: Router;
const LOCKAMT = 111;
const LIQAMT = 222;
let actionData = {
destinationChain: originatingChain,
strategyId: "0xffffffff",
selector: "",
accountIds: [1],
token: "",
lockAmt: 111,
liqAmt: 222,
lockAmt: LOCKAMT,
liqAmt: LIQAMT,
status: 0,
} as IVaultHelpers.VaultActionDataStruct;

Expand Down Expand Up @@ -1051,8 +1014,8 @@ describe("Router", function () {

beforeEach(async function () {
router = await deployRouterAsProxy(gateway.address, gasService.address, registrar);
await token.mint(router.address, actionData.liqAmt);
await token.mint(router.address, actionData.lockAmt);
await token.mint(gateway.address, LOCKAMT + LIQAMT);
await token.approveFor(gateway.address, router.address, LOCKAMT + LIQAMT)
actionData.selector = liquidVault.interface.getSighash("deposit");
let packedData = packActionData(actionData);
await router.executeWithToken(
Expand Down

0 comments on commit ed84c17

Please sign in to comment.