Hashed Timelock Contract (HTLC) demonstration for assets in daml ledger and ERC20 token.
The HTLC (Solidity) used in this demonstration is from here. Modification is made: the data type for preimage is changed from bytes32 to string.
- ganache-cli
- daml sdk 1.14.0
- python3
- python3 web3 library
We need several terminals for setting up the environment.
Daml Sandbox
./startSandbox.sh
Daml JSON-API
./startJSONAPI.sh
Ethereum (ganache-cli)
ganache-cli -m abcdef
Note: Use this mnemonic as the account information is hardcoded.
Event listener
cd demo-py3
python3 1b_launch_eventlistening.py
It is hardcoded with USD1,000 for both accounts
Open a terminal for all commands from now on.
cd demo-py3
python3 1a_deploy_eth_contracts.py
It is hardcoded with 100 shares of AAPL in Alice's account. None in Bob's.
python3 1_setup.py
python3 2_alice_lock_shares_daml.py
Alice specifies the number of shares to be locked, and a secret that only Alice knows.
Note that two pieces of information are returned
- HTLC contract in Daml ledger for Alice's asset, which will be used when Bob's withdrawal or Alice's refunding
- Hashlock value: which will be used in Step 5 when Bob locks his asset in Ethereum.
python3 3_bob_locks_USD_eth.py
When asked, use the Hashlock value obtained in Step 4. The HTLC contract ID for Bob's asset in Ethereum is returned in the event listener terminal.
python3 4_alice_withdraw_USD_eth.py
When asked, Alice first enters the HTLC contract for Bob's asset (in event listerner terminal of Step 5) and her secret. The result shows that the amount of USD is now in Alice's account.
python3 5_bob_withdraw_shares_daml.py
When asked, Bob first enters his HTLC contract (in event listener terminal of Step 5). Then he obtains the secret Alice leaves in the record. Now Bob enters Alice's HTLC in the daml ledger (Step 4). The result shows that the sahres is now in Bob's account.
Rather than swapping (Step 6 and 7), after time interval expires (2 minutes), both sides can get refund their assets.
python3 6a_alice_refund_shares_daml.py
Alice provides her HTLC in the daml ledger (Step 4) and gets back the shares.
python3 6b_bob_refund_USD_eth.py
Bob provides his HTLC in the Ethereum (in event listerner of Step 5) and gets back his USD tokens.