Skip to content

Repository files navigation

How to deploy router swap

0. compile

requires at least Go version 1.18

make all

run the above command, it will generate ./build/bin/swaprouter binary.

1. deploy AnyswapRouter

deploy a AnyswapRouter contract for each supported blockchain

eg. AnyswapV6Router.sol

2. deploy AnyswapERC20

deploy a AnyswapERC20 contract for each token on each blockchain

eg. AnyswapV6ERC20.sol

eg. AnyswapV6ERC20-NonEVM.sol

3. deploy RouterConfig

deploy a RouterConfig contract to store router bridge configs

eg. RouterConfigV2.sol

4. set router config on chain

call RouterConfig contract to set configs on blcokchain.

The following is the most used functions, please ref. the abi for more info.

4.1 set chain config

call the following contract function:

function setChainConfig(uint256chainID, stringblockChain, stringrouterContract, uint64confirmations, uint64initialHeight, stringextra)

4.2 set token config

call the following contract function:

function setTokenConfig(stringtokenID, uint256chainID, stringtokenAddr, uint8decimals, uint256version, stringrouterContract, stringextra)

4.3 set swap and fee config

we may not set swap and fee config between all fromChainIDs and toChainIDs, and we'll use the following logic to decide the actual config to use.

the actual swap config is decided by the following steps
1. if _swapConfig[tokenID][srcChainID][dstChainID] exist, then use it.
2. else if _swapConfig[tokenID][srcChainID][0] exist, then use it.
3. else if _swapConfig[tokenID][0][dstChainID] exist, then use it.
4. else use _swapConfig[tokenID][0][0].
the actual fee config is decided by the following steps
1. if _feeConfig[tokenID][srcChainID][dstChainID] exist, then use it.
2. else if _feeConfig[tokenID][srcChainID][0] exist, then use it.
3. else if _feeConfig[tokenID][0][dstChainID] exist, then use it.
4. else use _feeConfig[tokenID][0][0].
  1. set swap and fee config in batch
struct SwapConfig2 {
uint256 FromChainID;
uint256 ToChainID;
uint256 MaximumSwap;
uint256 MinimumSwap;
uint256 BigValueThreshold;
}
struct FeeConfig2 {
uint256 FromChainID;
uint256 ToChainID;
uint256 MaximumSwapFee;
uint256 MinimumSwapFee;
uint256 SwapFeeRatePerMillion;
}
function setSwapConfigs(stringmemorytokenID, SwapConfig2[] calldataconfigs)
function setFeeConfigs(stringmemorytokenID, FeeConfig2[] calldataconfigs)
  1. query swap and fee config in batch
function getAllSwapConfigs(stringmemorytokenID) externalviewreturns (SwapConfig2[] memory)
function getAllFeeConfigs(stringmemorytokenID) externalviewreturns (FeeConfig2[] memory)

4.3.1 set swap and fee config meantime

call the following contract function:

function setSwapAndFeeConfig(
stringtokenID, uint256srcChainID, uint256dstChainID,
uint256maxSwap, uint256minSwap, uint256bigSwap,
uint256maxFee, uint256minFee, uint256feeRate)

4.3.2 set swap config alone

call the following contract function to set swap config:

max/min/big value always uses decimals 18 (like precision).

function setSwapConfig(stringtokenID, uint256srcChainID, uint256dstChainID, uint256maxSwap, uint256minSwap, uint256bigSwap)

swap config is stored in a map with keys tokenID,srcChainID,dstChainID

mapping (bytes32=>mapping(uint256=>mapping(uint256=> SwapConfig))) private _swapConfig;

4.3.3 set fee config alone

call the following contract function:

rate is per million ration.

max/min value always uses decimals 18 (like precision).

function setFeeConfig(stringtokenID, uint256srcChainID, uint256dstChainID, uint256maxFee, uint256minFee, uint256feeRate)

fee config is stored in a map with keys tokenID,srcChainID,dstChainID

mapping (bytes32=>mapping(uint256=>mapping(uint256=> FeeConfig))) private _feeConfig;

4.4 set mpc address's public key

call the following contract function:

function setMPCPubkey(addressaddr, stringpubkey);

5. add local config file

please ref. config-example.toml

the following is the major config items:

config itemdescription
Identifiermust have prefix 'routerswap'
SwapTypeeg. erc20swap, nftswap, anycallswap
SwapSubTypeanycall has subtype of v5 and v6
[Server]only need by swap server
[Server.MongoDB]use mongodb database
[Server.APIServer]provide rpc service
[Oracle]only need by swap oracle
[Extra]extra configs
[OnChain]get onchain router configs in samrt contract
[Gateways]query block and tx info from full nodes' rpc
[MPC]use mpc to do threshold signing

Notation:

the MPC is a security Multi-Party threshold Computation, for more info, please refer FastMulThreshold-DSA

Because the complexity of MPC, we can use private key to sign tx for easy testing.

set the following config items in the [MPC] section:

[MPC]
# use private key instead (use for testing)SignWithPrivateKey = true# set signer's private key, key is chain ID (use for testing)
[MPC.SignerPrivateKeys]
4 = "1111111111111111111111111111111111111111111111111111111111111111"97 = "2222222222222222222222222222222222222222222222222222222222222222"

for more info, please ref. config-sign-with-privatekey-example.toml

6. run swaprouter

# for server run (add '--runserver' option)
setsid ./build/bin/swaprouter --config config.toml --log logs/routerswap.log --runserver
# for oracle run
setsid ./build/bin/swaprouter --config config.toml --log logs/routerswap.log

7. sub commands

get all sub command list and help info, run

./build/bin/swaprouter -h

sub commands:

admin is admin tool

config is tool to process and query config data

8. RPC api

please ref. server rpc api

About

No description, website, or topics provided.

Resources

Stars

68 stars

Watchers

7 watching

Forks

Releases

Packages

Used by

Contributors

Languages