Skip to content

Commit

Permalink
quote get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Aug 21, 2024
1 parent f99e879 commit 9bc80c8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
29 changes: 22 additions & 7 deletions interfaces/IIsmpModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
pragma solidity 0.8.17;

import {PostRequest, PostResponse, GetResponse, GetRequest} from "./Message.sol";
import {DispatchPost, DispatchPostResponse} from "./IDispatcher.sol";
import {DispatchPost, DispatchPostResponse, DispatchGet} from "./IDispatcher.sol";
import {IIsmpHost} from "./IIsmpHost.sol";
import {Context} from "openzeppelin/utils/Context.sol";
import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol";
import {SafeERC20} from "openzeppelin/token/ERC20/utils/SafeERC20.sol";

struct IncomingPostRequest {
// The Post request
Expand Down Expand Up @@ -66,6 +69,8 @@ interface IIsmpModule {

// @notice Abstract contract to make implementing `IIsmpModule` easier.
abstract contract BaseIsmpModule is IIsmpModule {
using SafeERC20 for IERC20;

// @notice Chain is not supported
error UnsupportedChain();

Expand All @@ -77,13 +82,18 @@ abstract contract BaseIsmpModule is IIsmpModule {

// @dev restricts caller to the local `IsmpHost`
modifier onlyHost() {
if (msg.sender != hostAddr()) revert UnauthorizedAccount();
if (msg.sender != host()) revert UnauthorizedAccount();
_;
}

constructor() {
// approve the host infintely
IERC20(IIsmpHost(host()).feeToken()).safeIncreaseAllowance(host(), type(uint256).max);
}

// @dev Returns the `IsmpHost` address for the current chain.
// The `IsmpHost` is an immutable contract that will never change.
function hostAddr() internal view returns (address h) {
function host() public view returns (address h) {
assembly {
switch chainid()
// Ethereum Sepolia
Expand Down Expand Up @@ -112,13 +122,18 @@ abstract contract BaseIsmpModule is IIsmpModule {
}

// @dev returns the quoted fee for a dispatch
function quoteFee(DispatchPost memory post) internal view returns (uint256) {
return post.body.length * IIsmpHost(hostAddr()).perByteFee();
function quote(DispatchPost memory post) internal view returns (uint256) {
return post.body.length * IIsmpHost(host()).perByteFee();
}

// @dev returns the quoted fee for a dispatch
function quote(DispatchPostResponse memory res) internal view returns (uint256) {
return res.response.length * IIsmpHost(host()).perByteFee();
}

// @dev returns the quoted fee for a dispatch
function quoteFee(DispatchPostResponse memory res) internal view returns (uint256) {
return res.response.length * IIsmpHost(hostAddr()).perByteFee();
function quote(DispatchGet memory get) internal view returns (uint256) {
return get.fee + (get.context.length * IIsmpHost(host()).perByteFee());
}

function onAccept(IncomingPostRequest calldata) external virtual onlyHost {
Expand Down
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": "0.5.1",
"version": "0.5.2",
"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 9bc80c8

Please sign in to comment.