Skip to content

ICX Atomic Swap

U-Zyn Chua edited this page Aug 18, 2021 · 1 revision

ICX Atomic Swap

For Tech Talk on 2021-08-12

Process

  1. Maker: make order
  2. Taker: make offer
  3. Maker: accept offer, generates seed (password) and sends to HTLC
  4. Taker: sends to HTLC with the same hash
  5. Maker: claims, reveals seed
  6. Taker: claims with the revealed seed

Fees

  • Taker
    • 0.3% of the BTC transaction amount
    • 0.003 * BTC * DFI/BTC (rate on DEX)
  • Maker
    • If it's a parity trade (1 BTC for 1 dBTC), receives 75% of taker fee (DFI)
    • Else, receives 25% of taker fee (DFI)
  • Remaining taker fee is burned (25% or 75%)
  • Fee needs to be paid in token,

Maker bot

We are running a parity maker bot.

https://github.com/DeFiCh/icx-makerbot

Parity maker bot address: 8cDSPjDe7HqvzmSL33xCrcrvBbUcmkTSpg

Taking

Prep

  1. Get DFI address and Bitcoin pubkey

    • HTLC (BIP 199) uses pubkey, not address:
      • defi-cli spv_getaddresspubkey BTC_ADDRESS
    • DFI address
      • from UI
  2. icx_listorders

    • type
      • INTERNAL: Maker is going OUT of DeFiChain (Maker has dBTC)
      • EXTERNAL: Maker is going INTO of DeFiChain (Maker has BTC)
    • Take note of orderPrice

Taker wants to get into DeFiChain

Reference: https://github.com/DeFiCh/app/discussions/996

Taker has BTC

  1. Make offer

    • Pick an INTERNAL one. Take node of ownerAddress.
    • orderTx: from icx_listorders
    • amount
    • ownerAddress: for DFI and dBTC (receive and refund)
    • receivePubkey: for BTC
    • expiry: number of DeFiChain blocks
    • icx_makeoffer '{"orderTx":"670f177dd1e1ca81412790688197b529863fd0ee148f05911e9661228593f7d7","amount":0.00102,"ownerAddress":"dSdXC5bEXxLbeoLCHUgh6jVQiLbgp83Aiq","receivePubkey":"0332a00d8328b2bcec0d73f75900498a4b24c4c11aaa8c88a5534f4781bfcf39a1","expiry":240}'
  2. Check for status

    • Check if the maker has locked up the BTC at an HTLC
    • icx_listhtlcs '{"offerTx":"f07275d4bf45191b035e5d7bb61cbead451ad37fa53471791cae438e76ac1221"}'
    • Or icx_listhtlcs '{"offerTx":"f07275d4bf45191b035e5d7bb61cbead451ad37fa53471791cae438e76ac1221", "closed":true}'
    • Ensure that the response is correct
  3. Lock in BTC to SPV

    • spc_createhtlc
    • timeout in Bitcoin should be
    • spv_createhtlc 03923fb45dab6b1c2aa87d59341337016d4e37c025b22ff6f78061d18c568b78d5 022432051c0559558807977ae4fce2617383b23e08c3990ac9d36cbe67c8e6d4a1 9 9a77b7e5db814ae87d050efd00a313e41a2270d381a10ed59111305dbae03613
  4. Fund HTLC

    • spv_sendtoaddress
    • spv_sendtoaddress 3HLRXfaZQSMSQ8B61bTgJq9iYrUJqcsSYR 0.00012
  5. Inform DeFiChain of HTLC

    • icx_submitexthtlc '{"offerTx":"5906ea3e568d375bc8f9f66edab804b030dd77498d268953c375b86ea63ec2e2","hash":"9a77b7e5db814ae87d050efd00a313e41a2270d381a10ed59111305dbae03613","amount":"0.00012","htlcScriptAddress":"3HLRXfaZQSMSQ8B61bTgJq9iYrUJqcsSYR","ownerPubkey":"022432051c0559558807977ae4fce2617383b23e08c3990ac9d36cbe67c8e6d4a1","timeout":9}'
  6. Check for fund claim

    • spv_gethtlcseed "3HLRXfaZQSMSQ8B61bTgJq9iYrUJqcsSYR"
  7. Claim dBTC

    • offerTx, seed
    • icx_claimdfchtlc '{"dfchtlcTx":"2afa1a7c935548ffd6be9d550bb9c7f59da31b9a461bd6c3ad8ee11a50f8ac41","seed":"26f5d9f68086e0bb3458d6f102a6b3bb66a1f70a4a439a3276362017bc1d5540"}'

Taker wants to out of DeFiChain to Bitcoin

Reference: https://github.com/DeFiCh/app/discussions/998

Taker has dBTC

  1. Make offer

    • Pick an EXTERNAL one. Take node of ownerAddress.
    • orderTx: from icx_listorders
    • amount
    • ownerAddress: for DFI and dBTC (receive and refund)
    • receivePubkey: for BTC
    • expiry: number of DeFiChain blocks
    • icx_makeoffer '{"orderTx":"670f177dd1e1ca81412790688197b529863fd0ee148f05911e9661228593f7d7","amount":0.00102,"ownerAddress":"dSdXC5bEXxLbeoLCHUgh6jVQiLbgp83Aiq","receivePubkey":"0332a00d8328b2bcec0d73f75900498a4b24c4c11aaa8c88a5534f4781bfcf39a1","expiry":240}'
  2. Check for status

    • Check if the maker has locked up the BTC at an HTLC
    • icx_listhtlcs '{"offerTx":"f07275d4bf45191b035e5d7bb61cbead451ad37fa53471791cae438e76ac1221"}'
    • Or icx_listhtlcs '{"offerTx":"f07275d4bf45191b035e5d7bb61cbead451ad37fa53471791cae438e76ac1221", "closed":true}'
    • Ensure that the response is correct, esp. that token is locked up.
  3. Create BTC HTLC to check for funding

    • spc_createhtlc
    • timeout in Bitcoin should be
    • spv_createhtlc 03923fb45dab6b1c2aa87d59341337016d4e37c025b22ff6f78061d18c568b78d5 022432051c0559558807977ae4fce2617383b23e08c3990ac9d36cbe67c8e6d4a1 9 9a77b7e5db814ae87d050efd00a313e41a2270d381a10ed59111305dbae03613
  4. Check that SPV is funded correctly

    • spv_listreceivedbyaddress 1 htlcAddress
  5. Lock in DST via icx_submitdfchtlc

    • defi-cli icx_submitdfchtlc '{"offerTx":"95873cdf935157b9c0395d307c9b40a9bab30a3057a548b3df2678373479437a","amount":0.0001,"hash":"ded4a16fde0a2dbab8db98c55d29733f40506068aaad84387a201ce53657465d"}'
  6. Check for DST claim by Maker

    • This would reveal seed (password)
    • `defi-cli icx_listhtlcs '{"offerTx":"95873cdf935157b9c0395d307c9b40a9bab30a3057a548b3df2678373479437a"}'
  7. Claim BTC

    • offerTx, seed
    • spv_claimhtlc SCRIPT_ADDRESS TAKER_SPV_ADDRESS SEED

Refund

  • Refund is automatically carried out for DST side when offer or HTLC expires
  • For BTC side, claim is needed, use spv_refundhtlc

Make

  1. Make order: icx_createorder
  2. Wait for offer: icx_listorders
  3. When there's offer, accept the offer. Maker can accept less offer.
    • Maker generates the seed & hash
    • Submit hash to HTLC