Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fee handler for substrate #120

Merged
merged 4 commits into from
Feb 20, 2023
Merged

Conversation

viatrix
Copy link
Contributor

@viatrix viatrix commented Feb 9, 2023

Added a fee handler for Substrate.
Removed feeData.amount from txCost calculations.
Renamed fee handlers.

Description

  1. Added a fee handler contract for Substrate based on FeeHandlerWithOracle (now renamed to DynamicFeeHandler).

  2. Replaced feeData.amount with depositData.amount in txCost calculations. feeData.amount is not used at the moment but kept for possible uses in the future.

  3. Renamed fee handlers:
    Created abstract contract DynamicFeeHandler
    Other fee handlers with oracle (dynamic fee handlers) inherit from DynamicFeeHandler:
    FeeHandlerGeneric -> DynamicGenericFeeHandlerEVM
    FeeHandlerSubstrate -> DynamicERC20FeeHandlerSubstrate
    FeeHandlerWithOracle -> DynamicERC20FeeHandlerEVM

Related Issue Or Context

Closes: #60, #122, #124

How Has This Been Tested? Testing details.

Added a unit test.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation

Checklist:

  • I have commented my code, particularly in hard-to-understand areas.
  • I have ensured that all acceptance criteria (or expected behavior) from issue are met
  • I have updated the documentation locally and in chainbridge-docs.
  • I have added tests to cover my changes.
  • I have ensured that all the checks are passing and green, I've signed the CLA bot

@viatrix viatrix changed the title Add fee handler for substrate feat: add fee handler for substrate Feb 9, 2023
@viatrix viatrix force-pushed the viatrix/fee-handler-substrate branch from 1704808 to f90106c Compare February 9, 2023 22:10
@MakMuftic
Copy link
Contributor

Hey @viatrix, my idea here was to also separate fee handler contracts for evm chains and generic message passing as separate implementations.

Currently, we have this logic inside FeeHandlerWithOracle:

if(oracleMessage.msgGasLimit > 0) {
    // txCost = dstGasPrice * oracleMessage.msgGasLimit * Base Effective Rate (rate between base currencies of source and dest)
    txCost = oracleMessage.dstGasPrice * oracleMessage.msgGasLimit * oracleMessage.ber / 1e18;
} else {
    // txCost = dstGasPrice * _gasUsed * Token Effective Rate (rate of dest base currency to token)
    txCost = oracleMessage.dstGasPrice * _gasUsed * oracleMessage.ter / 1e18;
}

that determines if the processed message (deposit request) is just a basic token transfer or a generic message (msgGasLimit > 0).

This approach allows users to do funky things like set this msgGasLimit when requesting fee data from fee oracle, but then execute erc20 token bridging with this fee data. I don't know how exactly this can be used for malicious behavior, but we should split this into two different implementations of FeeHandlerWithOracle. With this approach, we can map exact implementation based on resourceID and domains inside fee router and not even worry about how this can be used for malicious activity.

cc: @P1sar @nmlinaric @mpetrun5

address tokenHandler = IBridge(_bridgeAddress)._resourceIDToHandlerAddress(resourceID);
address tokenAddress = IERCHandler(tokenHandler)._resourceIDToTokenContractAddress(resourceID);

txCost= oracleMessage.finalFee * oracleMessage.ter / 1e18;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
txCost= oracleMessage.finalFee * oracleMessage.ter / 1e18;
txCost = oracleMessage.finalFee * oracleMessage.ter / 1e18;

depositData,
feeData
);
assert.equal(Ethers.utils.formatUnits(res.fee.toString(), "wei"), "49180200000");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why formatting if it already has the same value?

Copy link
Contributor

@lastperson lastperson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting needs a bit of a fix.

Co-authored-by: Oleksii Matiiasevych <[email protected]>
Copy link
Contributor

@P1sar P1sar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also should update names. I think next should works better:
FeeHandlerGeneric -> DynamicGenericFeeHandlerEVM
FeeHandlerSubstrate -> DynamicERC20FeeHandlerSubstrate
FeeHandlerWithOracle -> DynamicERC20FeeHandlerEVM
or something like that. I am open for proposals

@mpetrun5 mpetrun5 merged commit 97bcba1 into master Feb 20, 2023
@mpetrun5 mpetrun5 deleted the viatrix/fee-handler-substrate branch February 20, 2023 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement FeeHandlerWithOracle for targeting Substrate chain
6 participants