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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions contracts/handlers/fee/FeeHandlerGeneric.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.11;

import "./FeeHandlerWithOracle.sol";

/**
@title Handles deposit fees for generic messages based on Effective rates provided by Fee oracle.
@author ChainSafe Systems.
@notice This contract is intended to be used with the Bridge contract.
*/
contract FeeHandlerGeneric is FeeHandlerWithOracle {

/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param feeHandlerRouterAddress Contract address of previously deployed FeeHandlerRouter.
*/
constructor(address bridgeAddress, address feeHandlerRouterAddress) FeeHandlerWithOracle(bridgeAddress, feeHandlerRouterAddress) {
}

/**
@notice Calculates fee for generic messages.
This function is almost identical to the _calculateFee function in the base contract.
The difference is the txCost calculation formula.
@param sender Sender of the deposit.
@param fromDomainID ID of the source chain.
@param destinationDomainID ID of chain deposit will be bridged to.
@param resourceID ResourceID to be used when making deposits.
@param depositData Additional data to be passed to specified handler.
@param feeData Additional data to be passed to the fee handler.
@return fee Returns the fee amount.
@return tokenAddress Returns the address of the token to be used for fee.
*/
function _calculateFee(address sender, uint8 fromDomainID, uint8 destinationDomainID, bytes32 resourceID, bytes calldata depositData, bytes calldata feeData) internal view override returns(uint256 fee, address tokenAddress) {
mpetrun5 marked this conversation as resolved.
Show resolved Hide resolved
/**
Message:
ber * 10^18: uint256
ter * 10^18: uint256 (not used)
dstGasPrice: uint256
expiresAt: uint256
fromDomainID: uint8 encoded as uint256
toDomainID: uint8 encoded as uint256
resourceID: bytes32
msgGasLimit: uint256
sig: bytes(65 bytes)

total in bytes:
message:
32 * 8 = 256
message + sig:
256 + 65 = 321

amount: uint256
total: 353
*/

require(feeData.length == 353, "Incorrect feeData length");

FeeDataType memory feeDataDecoded;
uint256 txCost;

feeDataDecoded.message = bytes(feeData[: 256]);
feeDataDecoded.sig = bytes(feeData[256: 321]);
feeDataDecoded.amount = abi.decode(feeData[321:], (uint256));

OracleMessageType memory oracleMessage = abi.decode(feeDataDecoded.message, (OracleMessageType));
require(block.timestamp <= oracleMessage.expiresAt, "Obsolete oracle data");
require((oracleMessage.fromDomainID == fromDomainID)
&& (oracleMessage.toDomainID == destinationDomainID)
&& (oracleMessage.resourceID == resourceID),
"Incorrect deposit params"
);

bytes32 messageHash = keccak256(feeDataDecoded.message);

verifySig(messageHash, feeDataDecoded.sig, _oracleAddress);

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

require(oracleMessage.msgGasLimit > 0, "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;

fee = feeDataDecoded.amount * _feePercent / 1e4; // 100 for percent and 100 to avoid precision loss

if (fee < txCost) {
mpetrun5 marked this conversation as resolved.
Show resolved Hide resolved
fee = txCost;
}
return (fee, tokenAddress);
}
}
107 changes: 107 additions & 0 deletions contracts/handlers/fee/FeeHandlerSubstrate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// The Licensed Work is (c) 2022 Sygma
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.11;

import "./FeeHandlerWithOracle.sol";

/**
@title Handles deposit fees on Substrate based on Effective rates provided by Fee oracle.
@author ChainSafe Systems.
@notice This contract is intended to be used with the Bridge contract.
*/
contract FeeHandlerSubstrate is FeeHandlerWithOracle {

struct SubstrateOracleMessageType {
// Base Effective Rate - effective rate between base currencies of source and dest networks (eg. MATIC/ETH)
uint256 ber;
// Token Effective Rate - rate between base currency of destination network and token that is being trasferred (eg. MATIC/USDT)
uint256 ter;
// Final fee - resulting fee calculated by the oracle
uint256 finalFee;
uint256 expiresAt;
uint8 fromDomainID;
uint8 toDomainID;
bytes32 resourceID;
uint256 msgGasLimit;
}

/**
@param bridgeAddress Contract address of previously deployed Bridge.
@param feeHandlerRouterAddress Contract address of previously deployed FeeHandlerRouter.
*/
constructor(address bridgeAddress, address feeHandlerRouterAddress) FeeHandlerWithOracle(bridgeAddress, feeHandlerRouterAddress) {
}

/**
@notice Calculates fee for deposit for Substrate.
This function is almost identical to the _calculateFee function in the base contract.
The differences are: unpacking of the oracle message and the txCost calculation formula.
Oracle will do the calculation of the tx cost and provide the resulting fee to the contract.
The resulting calculation is:
txCost = finalFee * oracleMessage.ter / 1e18
@param sender Sender of the deposit.
@param fromDomainID ID of the source chain.
@param destinationDomainID ID of chain deposit will be bridged to.
@param resourceID ResourceID to be used when making deposits.
@param depositData Additional data to be passed to specified handler.
@param feeData Additional data to be passed to the fee handler.
@return fee Returns the fee amount.
@return tokenAddress Returns the address of the token to be used for fee.
*/
function _calculateFee(address sender, uint8 fromDomainID, uint8 destinationDomainID, bytes32 resourceID, bytes calldata depositData, bytes calldata feeData) internal view override returns(uint256 fee, address tokenAddress) {
/**
Message:
ber * 10^18: uint256 (not used)
ter * 10^18: uint256
finalFee: uint256
expiresAt: uint256
fromDomainID: uint8 encoded as uint256
toDomainID: uint8 encoded as uint256
resourceID: bytes32
msgGasLimit: uint256 (not used)
sig: bytes(65 bytes)

total in bytes:
message:
32 * 8 = 256
message + sig:
256 + 65 = 321

amount: uint256
total: 353
*/

require(feeData.length == 353, "Incorrect feeData length");

FeeDataType memory feeDataDecoded;
uint256 txCost;

feeDataDecoded.message = bytes(feeData[: 256]);
feeDataDecoded.sig = bytes(feeData[256: 321]);
feeDataDecoded.amount = abi.decode(feeData[321:], (uint256));

SubstrateOracleMessageType memory oracleMessage = abi.decode(feeDataDecoded.message, (SubstrateOracleMessageType));
require(block.timestamp <= oracleMessage.expiresAt, "Obsolete oracle data");
require((oracleMessage.fromDomainID == fromDomainID)
&& (oracleMessage.toDomainID == destinationDomainID)
&& (oracleMessage.resourceID == resourceID),
"Incorrect deposit params"
);

bytes32 messageHash = keccak256(feeDataDecoded.message);

verifySig(messageHash, feeDataDecoded.sig, _oracleAddress);

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

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

fee = feeDataDecoded.amount * _feePercent / 1e4; // 100 for percent and 100 to avoid precision loss

if (fee < txCost) {
fee = txCost;
}
return (fee, tokenAddress);
}
}
15 changes: 5 additions & 10 deletions contracts/handlers/fee/FeeHandlerWithOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ contract FeeHandlerWithOracle is IFeeHandler, AccessControl, ERC20Safe {
return _calculateFee(sender, fromDomainID, destinationDomainID, resourceID, depositData, feeData);
}

function _calculateFee(address sender, uint8 fromDomainID, uint8 destinationDomainID, bytes32 resourceID, bytes calldata depositData, bytes calldata feeData) internal view returns(uint256 fee, address tokenAddress) {
function _calculateFee(address sender, uint8 fromDomainID, uint8 destinationDomainID, bytes32 resourceID, bytes calldata depositData, bytes calldata feeData) internal view virtual returns(uint256 fee, address tokenAddress) {
/**
Message:
ber * 10^18: uint256
ber * 10^18: uint256 (not used)
ter * 10^18: uint256
dstGasPrice: uint256
expiresAt: uint256
fromDomainID: uint8 encoded as uint256
toDomainID: uint8 encoded as uint256
resourceID: bytes32
msgGasLimit: uint256
msgGasLimit: uint256 (not used)
sig: bytes(65 bytes)

total in bytes:
Expand Down Expand Up @@ -180,13 +180,8 @@ contract FeeHandlerWithOracle is IFeeHandler, AccessControl, ERC20Safe {
address tokenHandler = IBridge(_bridgeAddress)._resourceIDToHandlerAddress(resourceID);
address tokenAddress = IERCHandler(tokenHandler)._resourceIDToTokenContractAddress(resourceID);

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;
}
// txCost = dstGasPrice * _gasUsed * Token Effective Rate (rate of dest base currency to token)
txCost = oracleMessage.dstGasPrice * _gasUsed * oracleMessage.ter / 1e18;

fee = feeDataDecoded.amount * _feePercent / 1e4; // 100 for percent and 100 to avoid precision loss

Expand Down
Loading