ETH BSC Swap Contracts are responsible for registering swap pairs and swapping assets between ETH and BSC.
-
Users register swap pair for erc20 token on ETH via ETHSwapAgent(
createSwapPair
) if token is not registered. -
Swap service will monitor the
SwapPairRegister
event and create swap pair on BSC:- create an BEP20 token on BSC
- record the relation between erc20 token and bep20 token.
Once swap pair is registered, users can swap tokens from ETH to BSC.
- Users call
swapBSC2ETH
via ETHSwapAgent and specify erc20 token address, amount and swap fee. - Swap service will monitor the
SwapStarted
event and callfillETH2BSCSwap
via BSCSwapAgent to mint corresponding bep20 tokens to the same address that initiate the swap.
Once swap pair is registered, users can swap tokens from BSC to ETH.
- Users call
swapBSC2ETH
via BSCSwapAgent and specify bep20 token address, amount and swap fee. Bep20 tokens will be burned. - Swap service will monitor the
SwapStarted
event and callfillBSC2ETHSwap
via BSCSwapAgent to transfer corresponding erc20 tokens to the same address that initiate the swap.
- Node v14.16.1
- Truffle v5.2.6 (core: 5.2.6)
- Solidity - ^0.6.4 (solc-js)
- Web3.js v1.2.9
- Ganache CLI v6.12.2 (ganache-core: 2.13.2) on port 8545
- Ganache GUI v2.5.4 (ganache-core: 2.13.2) on port 7545
Contracts
BSCSwapAgentImpl.sol - deploy on bsc
BSCSwapAgentUpgradeableProxy.sol
ETHSwapAgentImpl.sol - deploy on ethereum
ETHSwapAgentUpgradeableProxy.sol
...
git clone this-project-code
cd /path/to/this/project/folder/
Run command to install package dependencies;
npm install
I run Ganache GUI on port 7545, as it provides a better view;
If you use Ganache GUI too, make sure to go to "Setting", "Accounts & Keys";
If you prefer Ganache-CLI, change the port to 8545 in these files truffle-config.js and .env file's DEVELOPMENT_RPC_URL
next, launch ganache-cli with 50 accounts
ganache-cli -a 50
npm run generate
or
Generate test contracts from templates, if you want to deploy on development network please exec:
npm run generate-test
You can now compile
truffle compile
restart Ganache GUI or ganache-cli
open a new terminal
truffle migrate --reset --network <network-name>
Notice:
<network-name> value range: development, bscTestnet, bsc, rinkeby, mainnet
If you want to run the project, you should copy .env.example file and rename it to .env. And if you want to run project on other networks instead of the development network, you should fill values in .env file, 3 parts below:
- DEVELOPMENT_RPC_URL, BSC_TEST_RPC_URL, BSC_RPC_URL, RINKEBY_RPC_URL, MAINNET_RPC_URL --- rpc url, according to the NETWORK you choose
- ETH_ACCOUNT, BSC_ACCOUNT, MAINNET_ACCOUNT --- account to deploy contracts
- ETH_MNEMONIC、BSC_MNEMONIC、MAINNET_MNEMONIC --- mnemonic
truffle test --network development