Skip to content

Commit

Permalink
fix: add zero check for price (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersonlee111 authored May 21, 2024
1 parent 6d51e86 commit f1fc767
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
5 changes: 3 additions & 2 deletions contracts/handlers/fee/V2/DynamicERC20FeeHandlerEVMV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ contract DynamicERC20FeeHandlerEVMV2 is DynamicFeeHandlerV2 {
@return tokenAddress Returns the address of the token to be used for fee.
*/
function _calculateFee(address, uint8, uint8 destinationDomainID, bytes32, bytes calldata, bytes calldata) internal view override returns (uint256 fee, address tokenAddress) {
address desintationCoin = destinationNativeCoinWrap[destinationDomainID];
uint256 txCost = destinationGasPrice[destinationDomainID] * _gasUsed * twapOracle.getPrice(desintationCoin) / 1e18;
uint256 desintationCoinPrice = twapOracle.getPrice(destinationNativeCoinWrap[destinationDomainID]);
if (desintationCoinPrice == 0) revert IncorrectPrice();
uint256 txCost = destinationGasPrice[destinationDomainID] * _gasUsed * desintationCoinPrice / 1e18;
return (txCost, address(0));
}
}
1 change: 1 addition & 0 deletions contracts/handlers/fee/V2/DynamicFeeHandlerV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract contract DynamicFeeHandlerV2 is IFeeHandler, AccessControl {
event WrapTokenAddressSet(uint8 destinationDomainID, address wrapTokenAddress);

error IncorrectFeeSupplied(uint256);
error IncorrectPrice();

modifier onlyAdmin() {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "sender doesn't have admin role");
Expand Down
5 changes: 3 additions & 2 deletions contracts/handlers/fee/V2/DynamicGenericFeeHandlerEVMV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ contract DynamicGenericFeeHandlerEVMV2 is DynamicFeeHandlerV2 {
*/
function _calculateFee(address, uint8, uint8 destinationDomainID, bytes32, bytes calldata depositData, bytes calldata) internal view override returns (uint256 fee, address tokenAddress) {
uint256 maxFee = uint256(bytes32(depositData[:32]));
address desintationCoin = destinationNativeCoinWrap[destinationDomainID];
uint256 txCost = destinationGasPrice[destinationDomainID] * maxFee * twapOracle.getPrice(desintationCoin) / 1e18;
uint256 desintationCoinPrice = twapOracle.getPrice(destinationNativeCoinWrap[destinationDomainID]);
if (desintationCoinPrice == 0) revert IncorrectPrice();
uint256 txCost = destinationGasPrice[destinationDomainID] * maxFee * desintationCoinPrice / 1e18;
return (txCost, address(0));
}
}
19 changes: 19 additions & 0 deletions testUnderForked/collectFeeERC20EVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ contract("DynamicERC20FeeHandlerEVMV2 - [collectFee]", async (accounts) => {
assert.equal(errorValues[0].toString(), fee.toString());
});

it("deposit should revert if the destination coin's price is 0", async () => {
const fee = Ethers.utils.parseEther("1.0");
await TwapOracleInstance.setPrice(MATIC_ADDRESS, 0);

await Helpers.expectToRevertWithCustomError(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: fee,
}
),
"IncorrectPrice()"
);
});

it("deposit should not revert if exceed fee (msg.value) amount supplied", async () => {
const exceedFee = Ethers.utils.parseEther("1.0");

Expand Down
19 changes: 19 additions & 0 deletions testUnderForked/collectFeeGenericEVM.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ contract("DynamicGenericFeeHandlerEVMV2 - [collectFee]", async (accounts) => {
assert.equal(errorValues[0].toString(), fee.toString());
});

it("deposit should revert if the destination coin's price is 0", async () => {
const fee = Ethers.utils.parseEther("1.0");
await TwapOracleInstance.setPrice(MATIC_ADDRESS, 0);

await Helpers.expectToRevertWithCustomError(
BridgeInstance.deposit(
destinationDomainID,
resourceID,
depositData,
"0x00",
{
from: depositorAddress,
value: fee,
}
),
"IncorrectPrice()"
);
});

it("deposit should not revert if exceed fee (msg.value) amount supplied", async () => {
const exceedFee = Ethers.utils.parseEther("1.0");

Expand Down

0 comments on commit f1fc767

Please sign in to comment.