this is an example of creating a swap asset (ERC20) contract with required Swapin
and Swapout
methods for cross chain bridge.
please ref. to CrossChain-Bridge.
npm install
This has been already done. Mention here only for re-flatten if modified later.
mpm install -g truffle-flattener
truffle-flattener internal/BtcSwapAsset.sol | sed '/SPDX-License-Identifier:/d' | sed 1i'// SPDX-License-Identifier: MIT' > contracts/BtcSwapAsset.sol
truffle-flattener internal/EthSwapAsset.sol | sed '/SPDX-License-Identifier:/d' | sed 1i'// SPDX-License-Identifier: MIT' > contracts/EthSwapAsset.sol
truffle-flattener internal/UsdtSwapAsset.sol | sed '/SPDX-License-Identifier:/d' | sed 1i'// SPDX-License-Identifier: MIT' > contracts/UsdtSwapAsset.sol
truffle-flattener internal/Erc20SwapAsset.sol | sed '/SPDX-License-Identifier:/d' | sed 1i'// SPDX-License-Identifier: MIT' > contracts/Erc20SwapAsset.sol
Normally, you may meed to modify the following items:
host
: the rpc host of the node
port
: the rpc port of the node
network_id
: the chain id
Add and modify contracts in contracts
directory.
like our example: BtcSwapAsset.sol.
Add an js
file to depoly contract in migrations
directory.
like our example: 2_deploy_contracts.js.
Use the following command to compile contracts:
truffle compile
or re-compile all contracts:
truffle compile --all
Use the following command to deploy contract:
truffle migrate
or re-deploy contract:
truffle migrate --reset
We may want to perform a test or 'dry run' migration firstly:
truffle migrate --dry-run
Because we need to build a raw contract creation transaction and sign this raw transaction using DCRM technology,
So we need the bytecode
as the transaction input data to build a raw contract cteation transaction.
bytecode
can be found in ./build/contracts/BtcSwapAsset.json
The bytecode files in bytecode/solc
directory are generated by the following command,
with solc version of 0.5.4+commit.9549d8ff.Emscripten.clang
solcjs --bin --output-dir solc --evm-version byzantium --optimize --runs=1500 contracts/*.sol
the bytecode
in truffle built contracts is runtime bytecode
which has not the constructor argument info.
we should append the packed constructor arguments data (like abi encoding) to the end of the runtime bytecode
to generage the creation bytecode
which is used as contract creation transaction's input data.
the following is a way by attaching to full node:
abi = ...
runtime_bytecode = ...
creation_bytecode = eth.contract(abi).getData(param1,param2,...lastparam,{from:eth.coinbase,data:runtime_bytecode})
We can access the above deployed contract in truffle console terminal.
Firstly log into truffle console terminal:
truffle console
Then assign an variable app
to the deployed contract instance:
BtcSwapAsset.deployed().then(function(instance){ app = instance; })
For convenience, we can also assign all local accounts to accounts
variable:
web3.eth.getAccounts().then(function(result){ accounts = result })
Now we can access the methods and attributes of the deployed contract.
For example,
app.Swapout("45678","2MxQaHFhqKgYyTgcJj8foYHbPJQLQxvXQjp",{from:accounts[1]})
after attached to the running node, we can access the contract by the following infos:
app = eth.contract(abi).at(contractAddress)
abi
can be found in ./build/contracts/BtcSwapAsset.json
(after building)