-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
daf41a7
commit a9a964f
Showing
8 changed files
with
110 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule multi-chain-tokens
added at
01a46a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
ismp/=lib/ismp-solidity/src/ | ||
openzeppelin/=lib/openzeppelin-contracts/contracts/ | ||
solidity-merkle-trees/=lib/solidity-merkle-trees/src/ | ||
multichain-token/=lib/multichain-native-tokens/src/ | ||
multi-chain-tokens/=lib/multi-chain-tokens/src/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,87 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity 0.8.17; | ||
// | ||
//// import "../IISMPRouter.sol"; | ||
//import "../interfaces/IISMPModule.sol"; | ||
//import "openzeppelin/utils/introspection/IERC165.sol"; | ||
//import "multichain-token/interfaces/IERC6160Ext20.sol"; | ||
//import {IERC_ACL_CORE} from "multichain-token/interfaces/IERCAclCore.sol"; | ||
//import "../interfaces/IIsmp.sol"; | ||
//import "../interfaces/IIsmpHost.sol"; | ||
// | ||
//// | ||
//// Add supports interface for custom bride token | ||
//// | ||
//error LengthMismatch(); | ||
//error ZeroAddress(); | ||
//error TokenNotMultiChainNative(); | ||
//error BurnerRoleMissing(); | ||
//error MinterRoleMissing(); | ||
//error AuthFailed(); | ||
//error NotDispatcher(); | ||
// | ||
//contract TokenGateway is IIsmpModule { | ||
// address admin; | ||
// address host; | ||
// bytes4 constant IERC6160Ext20ID = 0xbbb8b47e; | ||
// | ||
// bytes32 constant MINTER_ROLE = keccak256("MINTER ROLE"); | ||
// bytes32 constant BURNER_ROLE = keccak256("BURNER ROLE"); | ||
// | ||
// mapping(uint256 => address) public chains; | ||
// mapping(uint256 => address) public tokenIds; | ||
// | ||
// // auth modifier | ||
// modifier auth() { | ||
// if (msg.sender != admin) { | ||
// revert AuthFailed(); | ||
// } | ||
// _; | ||
// } | ||
// | ||
// // restricts call to `dispatcher` | ||
// modifier onlyDispatcher() { | ||
// if (msg.sender != host) { | ||
// revert NotDispatcher(); | ||
// } | ||
// _; | ||
// } | ||
// | ||
// constructor( | ||
// address _host, | ||
// uint256[] memory _SMids, | ||
// address[] memory _SMaddresses, | ||
// uint256[] memory _Tids, | ||
// address[] memory _Taddresses | ||
// ) { | ||
// admin = msg.sender; | ||
// host = _host; | ||
// setStateMachineIds(_SMids, _SMaddresses); | ||
// setTokenIds(_Tids, _Taddresses); | ||
// } | ||
// | ||
// // sets the addresses for a given StateMachineId | ||
// function setStateMachineIds(uint256[] memory _ids, address[] memory _addresses) public auth { | ||
// if (_ids.length != _addresses.length) revert LengthMismatch(); | ||
// for (uint256 i = 0; i < _ids.length;) { | ||
// address _address = _addresses[i]; | ||
// if (_address == address(0)) continue; | ||
// chains[_ids[i]] = _addresses[i]; | ||
// unchecked { | ||
// ++i; | ||
// } | ||
// } | ||
// } | ||
// | ||
// // sets the Id for a bridge compatible token | ||
// function setTokenIds(uint256[] memory _tokenIds, address[] memory _addresses) public auth { | ||
// if (_tokenIds.length != _addresses.length) revert LengthMismatch(); | ||
// for (uint256 i = 0; i < _tokenIds.length;) { | ||
// address _tokenAddress = _addresses[i]; | ||
// if (_tokenAddress == address(0)) revert ZeroAddress(); | ||
// if (!IERC_ACL_CORE(_tokenAddress).hasRole(BURNER_ROLE, address(this))) revert BurnerRoleMissing(); | ||
// if (!IERC_ACL_CORE(_tokenAddress).hasRole(MINTER_ROLE, address(this))) revert MinterRoleMissing(); | ||
// if (!IERC165(_tokenAddress).supportsInterface(IERC6160Ext20ID)) revert TokenNotMultiChainNative(); | ||
// tokenIds[_tokenIds[i]] = _tokenAddress; | ||
// unchecked { | ||
// ++i; | ||
// } | ||
// } | ||
// } | ||
// | ||
// // The Gateway contract has to have the roles `MINTER` and `BURNER`. | ||
// function send( | ||
// bytes memory stateMachine, | ||
// uint256 tokenId, | ||
// uint256 amount, | ||
// address to, | ||
// bytes memory module, | ||
// uint64 timestamp, | ||
// uint64 gasLimit | ||
// ) public { | ||
// // USDC -> HyperUSDC(ERC6160) | ||
// address tokenAddress = tokenIds[tokenId]; | ||
// // check permision at set token. | ||
// IERC6160Ext20(tokenAddress).burn(msg.sender, amount, ""); | ||
// bytes memory data = abi.encodePacked(to, amount, tokenId); | ||
// bytes memory source = IIsmpHost(host).host(); | ||
// DispatchPost memory postRequest = DispatchPost({ | ||
// destChain: stateMachine, | ||
// from: source, | ||
// to: module, | ||
// body: data, | ||
// timeoutTimestamp: timestamp, | ||
// gaslimit: gasLimit | ||
// }); | ||
// IIsmp(host).dispatch(postRequest); | ||
// } | ||
// | ||
// function onAccept(PostRequest memory request) public onlyDispatcher { | ||
// (address to, uint256 amount, uint256 tokenId) = _decodePackedData(request.body); | ||
// address tokenAddress = tokenIds[tokenId]; | ||
// | ||
// IERC6160Ext20(tokenAddress).mint(to, amount, ""); | ||
// } | ||
// | ||
// function onPostResponse(PostResponse memory response) public view onlyDispatcher { | ||
// revert("Token gateway doesn't emit responses"); | ||
// } | ||
// | ||
// function onPostTimeout(PostRequest memory request) public onlyDispatcher { | ||
// (address to, uint256 amount, uint256 tokenId) = _decodePackedData(request.body); | ||
// address tokenAddress = tokenIds[tokenId]; | ||
// | ||
// if (tokenAddress == address(0)) revert ZeroAddress(); | ||
// | ||
// IERC6160Ext20(tokenAddress).mint(to, amount, ""); | ||
// } | ||
// | ||
// function onGetResponse(GetResponse memory response) public view onlyDispatcher { | ||
// revert("Not implemented"); | ||
// } | ||
// | ||
// function onGetTimeout(GetRequest memory request) public view onlyDispatcher { | ||
// revert("Not implemented"); | ||
// } | ||
// | ||
// function _decodePackedData(bytes memory data) | ||
// internal | ||
// pure | ||
// returns (address to_, uint256 amount_, uint256 tokenId_) | ||
// { | ||
// assembly { | ||
// to_ := div(mload(add(data, 32)), 0x1000000000000000000000000) // hex slicing to get first 20-bytes. | ||
// amount_ := mload(add(data, 52)) | ||
// tokenId_ := mload(add(data, 84)) | ||
// } | ||
// } | ||
//} | ||
|
||
import "ismp/interfaces/IIsmpModule.sol"; | ||
import "ismp/interfaces/IIsmp.sol"; | ||
import "multi-chain-tokens/interfaces/IERC6160Ext20.sol"; | ||
|
||
error ZeroAddress(); | ||
|
||
contract TokenGateway is IIsmpModule { | ||
address private host; | ||
|
||
mapping(uint256 => address) public chains; | ||
mapping(uint256 => address) public tokenIds; | ||
|
||
// restricts call to `dispatcher` | ||
modifier onlyIsmpHost() { | ||
if (msg.sender != host) { | ||
revert("Unauthorized call"); | ||
} | ||
_; | ||
} | ||
|
||
constructor(address _host) { | ||
host = _host; | ||
} | ||
|
||
// The Gateway contract has to have the roles `MINTER` and `BURNER`. | ||
function send( | ||
uint256 amount, | ||
address to, | ||
bytes memory dest, | ||
address tokenContract, | ||
address gateway, | ||
uint64 gasLimit | ||
) public { | ||
address from = msg.sender; | ||
IERC6160Ext20(tokenContract).burn(from, amount, ""); | ||
bytes memory data = abi.encodePacked(from, to, amount, tokenContract); | ||
DispatchPost memory postRequest = DispatchPost({ | ||
dest: dest, | ||
to: abi.encodePacked(gateway), | ||
body: data, | ||
timeout: 60 * 60, // seconds | ||
gaslimit: gasLimit | ||
}); | ||
IIsmp(host).dispatch(postRequest); | ||
} | ||
|
||
function onAccept(PostRequest memory request) public onlyIsmpHost { | ||
(address _from, address to, uint256 amount, address tokenContract) = _decodePackedData(request.body); | ||
|
||
IERC6160Ext20(tokenContract).mint(to, amount, ""); | ||
} | ||
|
||
function onPostTimeout(PostRequest memory request) public onlyIsmpHost { | ||
(address from, address _to, uint256 amount, address tokenContract) = _decodePackedData(request.body); | ||
|
||
IERC6160Ext20(tokenContract).mint(from, amount, ""); | ||
} | ||
|
||
function onPostResponse(PostResponse memory response) public view onlyIsmpHost { | ||
revert("Token gateway doesn't emit responses"); | ||
} | ||
|
||
function onGetResponse(GetResponse memory response) public view onlyIsmpHost { | ||
revert("Token gateway doesn't emit Get Requests"); | ||
} | ||
|
||
function onGetTimeout(GetRequest memory request) public view onlyIsmpHost { | ||
revert("Token gateway doesn't emit Get Requests"); | ||
} | ||
|
||
function _decodePackedData(bytes memory data) | ||
internal | ||
pure | ||
returns (address from_, address to_, uint256 amount_, address tokenContract_) | ||
{ | ||
// todo: | ||
assembly { | ||
from_ := div(mload(add(data, 32)), 0x1000000000000000000000000) // hex slicing to get first 20-bytes. | ||
to_ := div(mload(add(data, 32)), 0x1000000000000000000000000) // hex slicing to get first 20-bytes. | ||
amount_ := mload(add(data, 52)) | ||
tokenContract_ := mload(add(data, 84)) | ||
} | ||
} | ||
} |