Skip to content

Commit

Permalink
fix: revert with custom error for 0 conversion amounts (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmlinaric authored Mar 13, 2024
1 parent 37517c1 commit cb2d7ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/contracts/handlers/ERCHandlerHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract contract ERCHandlerHelpers is IERCHandler {
}

error ContractAddressNotWhitelisted(address contractAddress);
error DepositAmountTooSmall(uint256 depositAmount);

// resourceID => token contract address
mapping(bytes32 => address) public _resourceIDToTokenContractAddress;
Expand Down Expand Up @@ -130,6 +131,10 @@ abstract contract ERCHandlerHelpers is IERCHandler {
convertedBalance = amount * (10 ** (DEFAULT_DECIMALS - decimals.externalDecimals));
}

if (convertedBalance == 0) {
revert DepositAmountTooSmall(amount);
}

return convertedBalance;
}
}
35 changes: 35 additions & 0 deletions test/e2e/erc20/decimals/roundingLoss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,39 @@ describe("E2E ERC20 - Two EVM Chains both with decimal places != 18 with roundin
await originERC20MintableInstance.balanceOf(depositorAccount);
assert.strictEqual(depositorBalance, initialTokenAmount - roundingLoss);
});

it(`should revert if deposit amount would result in converted zero amount`, async () => {
const depositAmount = "50";

const originDepositData = createERCDepositData(
depositAmount,
20,
await recipientAccount.getAddress(),
);

// hardhat locally deploys contract to predictable address, so
// ERCHandlerHelpers locally is always deployed to this address
const ercHandlerHelpersAddress =
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
const ercHandlerHelpersInstance = await ethers.getContractAt(
"ERCHandlerHelpers",
ercHandlerHelpersAddress,
);

// depositorAccount makes initial deposit of depositAmount
await expect(
originRouterInstance
.connect(depositorAccount)
.deposit(
destinationDomainID,
originResourceID,
securityModel,
originDepositData,
feeData,
),
).to.be.revertedWithCustomError(
ercHandlerHelpersInstance,
"DepositAmountTooSmall(uint256)",
);
});
});

0 comments on commit cb2d7ae

Please sign in to comment.