-
Notifications
You must be signed in to change notification settings - Fork 87
How to write anytoken wrapper contract to support Multichain Bridge Router
For a token to support Multichain Bridge, we should support the following interface
interface IBridge {
function Swapin(bytes32 txhash, address account, uint256 amount) external returns (bool);
function Swapout(uint256 amount, address bindaddr) external returns (bool);
event LogSwapin(bytes32 indexed txhash, address indexed account, uint256 amount);
event LogSwapout(address indexed account, address indexed bindaddr, uint256 amount);
}
For a token to support Multichain Router, we should support the following interface
interface IRouter {
function mint(address to, uint256 amount) external returns (bool);
function burn(address from, uint256 amount) external returns (bool);
}
Theorectically we only need to implement the above IRouter
interface to support Multichain Router.
But For the frontend interaction, we must distinguish the following three things:
-
call which swapout method of the Router contract
function anySwapOut(address token, address to, uint amount, uint toChainID) external
function anySwapOutUnderlying(address token, address to, uint amount, uint toChainID) external
function anySwapOutNative(address token, address to, uint toChainID) external payable
-
how to approve token spending
- no need to approve
- need approve to the router contract
- need approve to the anytoken/wrapper contract
-
should we provide liquidity methods (deposit/withdraw)
To distinguish these, we have the following two processing logics.
if anytoken/wrapper contract has accessable underlying
method,
then use the underlying logic
function underlying() external view returns (address);
function underlyingIsMinted() external view returns (bool);
-
if
underlying
is thezero
address, then- call
anySwapOut
- no need to approve
- no need to provide liquidity
- call
-
if
underlying
isnon-zero
address andunderlyingIsMinted
istrue
, then- call
anySwapOut
- need approve to the
anytoken contract
- no need to provide liquidity
- call
-
if
underlying
isnon-zero
address andunderlyingIsMinted
isfalse
, then- call
anySwapOutNative
if the underlying is thewNative
address, otherwise callanySwapOutUnderlying
- need approve to the
router contract
need provide liquidity
- call
Examples:
function tokenType() external view returns (uint8);
function token() external view returns (address);
the following is the knowable TokenType
enumerables (value start from 0):
- MintBurnAny (0)
-
token
should supportmint
andburn(from, amount)
- no need to approve
- no need to provide liquidity
-
- MintBurnFrom (1)
-
token
should supportmint
andburnFrom(from, amount)
- need approve to the
wrapper contract
- no need to provide liquidity
-
- MintBurnSelf (2)
-
token
should supportmint
andburn(amount)
, calledtransferFrom
- need approve to the
wrapper contract
- no need to provide liquidity
-
- Transfer (3)
- use
token
transfer and transferFrom - need approve to the
wrapper contract
- no need to provide liquidity
- use
- TransferDeposit (4)
- use
token
transfer and transferFrom - need approve to the
wrapper contract
-
need to provide liquidity
(block when lack of liquidity)
- use
- TransferDeposit2 (5)
- use
token
transfer and transferFrom - need approve to the
wrapper contract
-
no need to provide liquidity
(when lack of liquidity, don't block and borrow or swap from other party)
- use
Examples: