Skip to content

Commit

Permalink
add support for quoting fees in native token
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Nov 19, 2024
1 parent f0d8dbb commit 2a358f8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
57 changes: 53 additions & 4 deletions interfaces/IIsmpModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ interface IIsmpModule {
function onGetTimeout(GetRequest memory request) external;
}


/**
* @dev Uniswap interface for estimating fees in the native token
*/
interface IUniswapV2Router02 {
function WETH() external pure returns (address);
function getAmountsIn(uint, address[] calldata) external pure returns (uint[] memory);
}

/**
* @dev Abstract contract to make implementing `IIsmpModule` easier.
*/
Expand Down Expand Up @@ -114,31 +123,71 @@ abstract contract BaseIsmpModule is IIsmpModule {
function host() public view virtual returns (address);

/**
* @dev returns the quoted fee for dispatching a POST request
* @dev returns the quoted fee in the feeToken for dispatching a POST request
*/
function quote(DispatchPost memory request) public view returns (uint256) {
uint256 len = 32 > request.body.length ? 32 : request.body.length;
return request.fee + (len * IDispatcher(host()).perByteFee(request.dest));
}

/**
* @dev returns the quoted fee for dispatching a GET request
* @dev returns the quoted fee in the native token for dispatching a POST request
*/
function quoteNative(DispatchPost memory request) public view returns (uint256) {
uint256 fee = quote(request);
address _host = host();
address _uniswap = IDispatcher(_host).uniswapV2Router();
address[] memory path = new address[](2);
path[0] = IUniswapV2Router02(_uniswap).WETH();
path[1] = IDispatcher(_host).feeToken();
return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
}

/**
* @dev returns the quoted fee in the feeToken for dispatching a GET request
*/
function quote(DispatchGet memory request) public view returns (uint256) {
uint256 pbf = IDispatcher(host()).perByteFee(IDispatcher(host()).host());
address _host = host();
uint256 pbf = IDispatcher(_host).perByteFee(IDispatcher(_host).host());
uint256 minimumFee = 32 * pbf;
uint256 totalFee = request.fee + (pbf * request.context.length);
return minimumFee > totalFee ? minimumFee : totalFee;
}

/**
* @dev returns the quoted fee for dispatching a POST response
* @dev returns the quoted fee in the native token for dispatching a GET request
*/
function quoteNative(DispatchGet memory request) public view returns (uint256) {
uint256 fee = quote(request);
address _host = host();
address _uniswap = IDispatcher(_host).uniswapV2Router();
address[] memory path = new address[](2);
path[0] = IUniswapV2Router02(_uniswap).WETH();
path[1] = IDispatcher(_host).feeToken();
return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
}

/**
* @dev returns the quoted fee in the feeToken for dispatching a POST response
*/
function quote(DispatchPostResponse memory response) public view returns (uint256) {
uint256 len = 32 > response.response.length ? 32 : response.response.length;
return response.fee + (len * IDispatcher(host()).perByteFee(response.request.source));
}

/**
* @dev returns the quoted fee in the native token for dispatching a POST response
*/
function quoteNative(DispatchPostResponse memory request) public view returns (uint256) {
uint256 fee = quote(request);
address _host = host();
address _uniswap = IDispatcher(_host).uniswapV2Router();
address[] memory path = new address[](2);
path[0] = IUniswapV2Router02(_uniswap).WETH();
path[1] = IDispatcher(_host).feeToken();
return IUniswapV2Router02(_uniswap).getAmountsIn(fee, path)[0];
}

function onAccept(IncomingPostRequest calldata) external virtual onlyHost {
revert UnexpectedCall();
}
Expand Down
10 changes: 10 additions & 0 deletions interfaces/StateMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,14 @@ library StateMachine {
function evm(uint chainid) internal pure returns (bytes memory) {
return bytes(string.concat("EVM-", Strings.toString(chainid)));
}

// @notice Address a substrate state machine
function substrate(bytes4 id) internal pure returns (bytes memory) {
return bytes(string.concat("SUBSTRATE-", string(abi.encodePacked(id))));
}

// @notice Address a tendermint state machine
function tendermint(bytes4 id) internal pure returns (bytes memory) {
return bytes(string.concat("TNDRMINT-", string(abi.encodePacked(id))));
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polytope-labs/ismp-solidity",
"version": "1.0.0",
"version": "1.0.1",
"description": "Hyperbridge Solidity SDK for the Interoperable state machine protocol",
"author": "Polytope Labs <[email protected]>",
"license": "Apache-2.0",
Expand Down

0 comments on commit 2a358f8

Please sign in to comment.