diff --git a/contracts/core/router/Router.sol b/contracts/core/router/Router.sol index 944410fea..677d91c1b 100644 --- a/contracts/core/router/Router.sol +++ b/contracts/core/router/Router.sol @@ -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( @@ -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); diff --git a/tasks/helpers/deploy/deployDummyERC20.ts b/tasks/helpers/deploy/deployDummyERC20.ts index 663522a26..34d35d64b 100644 --- a/tasks/helpers/deploy/deployDummyERC20.ts +++ b/tasks/helpers/deploy/deployDummyERC20.ts @@ -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); @@ -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]); } } diff --git a/test/core/router/Router.ts b/test/core/router/Router.ts index f8fe0807d..9e82f6988 100644 --- a/test/core/router/Router.ts +++ b/test/core/router/Router.ts @@ -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); @@ -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 () { @@ -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, "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"); @@ -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); @@ -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, "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"); @@ -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; @@ -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( @@ -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( @@ -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( @@ -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"), @@ -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; @@ -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"), @@ -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; @@ -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( @@ -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; @@ -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(