Skip to content

Interchain Exchange Tutorial

Mihailo Milenkovic edited this page Jun 15, 2021 · 11 revisions

Warning: Interchain Exchange is in testing phase and is an experimental feature.

Performing an interchain exchange between DeFiChain and Bitcoin requires several steps. Please see the pinkpaper for an overview to understand the flow required: Interchain Exchange Pinkpaper

This tutorial will teach you how to use the Interchain Exchange through the command line interface. It will demonstrate actions on both sides, maker and taker (seller and buyer). In order to test you can issue commands for both sides, it is recommended to do it on two separate nodes.

Selling DFI for Bitcoin

Prerequisites

  • Maker having DFI token balance for order amount plus makerFee on single address
  • Maker have and BTC address to receive BTC from atomic swap
    • ./defi-cli spv_getnewaddress
    • ./defi-cli spv_getaddresspubkey ADDRESS
  • Taker having DFI token balance for takerFee on single address
  • Taker having BTC balance for offer amount

Creating an order (maker)

Create order for selling 100 DFI for BTC on price of 1DFI = 0.0001 BTC (1 BTC = 10000 DFI)

./defi-cli icx_createorder '{"ownerAddress":"75zwRPznMN6De5tDyKgetBETet2KmDZBKN","tokenFrom":"DFI","chainTo":"BTC","amountFrom":"100","orderPrice":"0.0001","receivePubkey":"0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5"}'

327de66afa00a5b9f75beb058daa7f594f550d9a973b2cb20e2584092d3564dc

Make an offer for order (taker)

Check open orders

./defi-cli icx_listorders

Make offer for 100 DFI with 0.01 BTC to order 327de66afa00a5b9f75beb058daa7f594f550d9a973b2cb20e2584092d3564dc

./defi-cli icx_makeoffer '{"orderTx":"327de66afa00a5b9f75beb058daa7f594f550d9a973b2cb20e2584092d3564dc","amount":0.01,"ownerAddress":"7EwwohgHcKtzxR818A1D3aqh8sVnjvmguH","expiry":200}'

cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0

Check offers on order

./defi-cli icx_listorders '{"orderTx":"327de66afa00a5b9f75beb058daa7f594f550d9a973b2cb20e2584092d3564dc"}'

Create DFI HTLC for offer (maker)

Firstly, the DeFiChain HTLC creator will need to generate a seed/hash pair, here is some Javascript code to generate a seed/hash pair:

import { createHash } from "https://deno.land/[email protected]/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/[email protected]/mod.ts"
import { decodeString } from "https://deno.land/std/encoding/hex.ts"

export const createSeedHashPair = () => {
    const seed = cryptoRandomString({length: 64});
    let hash = createHash('sha256');
    hash.update(decodeString(seed));
    
    return { seed: seed, hash: hash.toString('hex')};
}

Put 100 DFI in HTLC for offer cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0

./defi-cli icx_submitdfchtlc '{"offerTx":"cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0","hash":"957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220","amount":100,"timeout":500}'

37d35b1760c17c3775aedd66ad6f16fd57082d8a8a28cb299e267b1b9e2af191

Verify that DFI HTLC exists and amount and timeout is correct (taker)

./defi-cli icx_listhtlcs '{"offerTx":"cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0"}'

Create BTC HTLC (taker)

Create BTC HTLC with hash from DFC HTLC, 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 is taker pubkey (owner of BTC), 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 is maker pubkey (claiming pubkey)

./defi-cli spv_createhtlc 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 15 957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220

{
"address": "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n",
"redeemScript": "63a820957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220882103d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843675fb275210348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a568ac"
}

Fund the HTLC address with 0.01 BTC

./defi-cli spv_sendtoaddress "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n" 0.01

1adc8b32e16899425ba9ce98b50fa430af8afaf1de16ab1a8d072af0cdb56920 (NOTE: this is txid on BTC chain)

Submit the BTC HTLC to Defichain (taker)

Submit data about the BTC HTLC to Defichain so that maker can see and verify the BTC HTLC

./defi-cli icx_submitexthtlc '{"offerTx":"cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0","hash":"957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220","amount":"0.01","htlcScriptAddress":"2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n","ownerPubkey":"03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843","timeout":15}'
        
b9b8a629d3c733e2ee21af4e448c95619b4f67e2863b56353a38aa33d59afb37

Check and claim BTC HTLC (maker)

Check BTC HTLC

./defi-cli icx_listhtlcs '{"offerTx":"cbda7b214fc46b87a4c60fdf96ff32149fd9c6ef354e91ef0c42454cd469afa0"}'

In order to claim BTC HTLC maker must create same BTC HTLC on his node

./defi-cli spv_createhtlc 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 15 957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220
        
{
"address": "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n",
"redeemScript": "63a820957fc0fd643f605b2938e0631a61529fd70bd35b2162a21d978c41e5241a5220882103d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843675fb275210348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a568ac"
}

Check if balance on BTC HTLC address is same as in offer

./defi-cli spv_listreceivedbyaddress 1 "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n"

Claim BTC to address bc1qk65huxcrt54uptsa890rua9fcn34gq09zumzry

./defi-cli spv_claimhtlc "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n" "bc1qk65huxcrt54uptsa890rua9fcn34gq09zumzry" "f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef"

6d1fad6ff38f77bb2fa2bc41882cdb9be6b299318ad0896cd911f26353533aff (NOTE: BTC txid)

Get seed from claimed BTC HTLC and claim DFI HTLC (taker)

Get seed

./defi-cli spv_gethtlcseed "2N79hz6UZ1rH2J3eMpQomRbkMw71ZtvTD1n"
        
f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef

Claim DFI HTLC to address given in offer

./defi-cli icx_claimdfchtlc '{"dfchtlcTx":"37d35b1760c17c3775aedd66ad6f16fd57082d8a8a28cb299e267b1b9e2af191","seed":"f75a61ad8f7a6e0ab701d5be1f5d4523a9b534571e4e92e0c4610c6a6784ccef"}'
        
e4da71e5dc326068c97c6f623db9895368b1295d788e28ad9bfda85f91b3719a

Atomic swap ended - summary

  • Maker balance

    • -100 DFI
    • +takerFee * 0.25 DFI (makerIncentive)
    • +0.01 BTC - txfee
  • Taker balance

    • +100 DFI
    • -takerFee DFI
    • -0.01 BTC

Selling BTC for DFI

Prerequisites

  • Maker having DFI token balance for makerDeposit on single address
  • Maker having BTC balance for order amount
  • Taker having DFI token balance for offer amount and takerFee on single address
  • Taker have BTC address to receive BTC from atomic swap
    • ./defi-cli spv_getnewaddress
    • ./defi-cli spv_getaddresspubkey ADDRESS

Creating an order (maker)

Create order for selling 0.01 BTC on price of 1DFI = 0.0001 BTC (1 BTC = 10000 DFI)

./defi-cli icx_createorder '{"ownerAddress":"75zwRPznMN6De5tDyKgetBETet2KmDZBKN","chainFrom":"BTC","tokenTo":"DFI","amountFrom":"0.01","orderPrice":"10000"}'
        
6a57f2ef99431964bcaaec8c957c14cb161e3a44f9840dbb9c490b23a82e44ff

Make an offer on order (taker)

Check open orders

./defi-cli icx_listorders

Make offer for 10 DFI with 0.001 BTC to order 6a57f2ef99431964bcaaec8c957c14cb161e3a44f9840dbb9c490b23a82e44ff

./defi-cli icx_makeoffer '{"orderTx":"6a57f2ef99431964bcaaec8c957c14cb161e3a44f9840dbb9c490b23a82e44ff","amount":10,"ownerAddress":"7EwwohgHcKtzxR818A1D3aqh8sVnjvmguH","receivePubkey":"03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843","expiry":200}'
        
0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6

Check offers on order

./defi-cli icx_listorders '{"orderTx":"6a57f2ef99431964bcaaec8c957c14cb161e3a44f9840dbb9c490b23a82e44ff"}'

Create BTC HTLC for offer (maker)

Create BTC HTLC with hash from DFC HTLC, 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 is maker pubkey (owner of BTC), 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 is taker pubkey (claiming pubkey)

./defi-cli spv_createhtlc 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 40
        
{        
"address": "2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ",
"redeemScript": "63a820f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd8444888210348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5670128b2752103d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc84368ac",
"seed": "39ff9fafe5c2f27fd438a7e665de677221a7e515c8b3a3140b80ce53e4b45ee2",
"seedhash": "f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd84448"
}

Fund the HTLC address with 0.001 BTC

./defi-cli spv_sendtoaddress "2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ" 0.001
        
da299d7f25c2a450cc9b3759d1903572fd9331e38ea0b566d32bc7adb7d1e205 (NOTE: BTC txid)

Submit the BTC HTLC to Defichain (maker)

Submit data about the BTC HTLC to Defichain so that maker can see and verify the BTC HTLC

./defi-cli icx_submitexthtlc '{"offerTx":"0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6","hash":"f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd84448","amount":0.001,"htlcScriptAddress":"2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ","ownerPubkey":"0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5","timeout":40}'
        
3299b1cb44d66548e53bc974c16b18b070559001f0fa2cca7673e3e3f23f9a7c

Verify that BTC HTLC exists and amount and timeout is correct (taker)

./defi-cli icx_listhtlcs '{"offerTx":"0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6"}'

Recreate BTC HTLC on local node

./defi-cli spv_createhtlc 03d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc843 0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5 40 f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd84448
        
{
"address": "2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ",
"redeemScript": "63a820f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd8444888210348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5670128b2752103d1c45cac0ff3d82d36dea71f96c0bf5db9de8a323f36f57f86fb118ec2cdc84368ac"
}

Check if balance on BTC HTLC address is same as in offer

./defi-cli spv_listreceivedbyaddress 1 "2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ"

Create DFI HTLC for an offer (taker)

Put 10 DFI in DFI HTLC with same hash as BTC HTLC

./defi-cli icx_submitdfchtlc '{"offerTx":"0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6","amount":10,"hash":"f963d8e271fbfbf849fbb63736697de5112849f1edb6c09a87e865548cd84448"}'
        
1f1cceabedd221041735bad877af6eea118ad8800a1ff4212367aa9f1e964bb1

Check and claim DFI HTLC (maker)

Check DFI HTLC

./defi-cli icx_listhtlcs '{"offerTx":"0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6"}'

Claim DFI HTLC

./defi-cli icx_claimdfchtlc '{"dfchtlcTx":"1f1cceabedd221041735bad877af6eea118ad8800a1ff4212367aa9f1e964bb1", "seed":"39ff9fafe5c2f27fd438a7e665de677221a7e515c8b3a3140b80ce53e4b45ee2"}'
        
4aaafac5edac8f903fdab9c3386c6859cba93dc030064e52ec976d1c41243b04

Claim BTC HTLC (taker)

In order to claim BTC HTLC taker must get seed from claimed DFI HTLC

./defi-cli icx_listhtlcs '{"offerTx":"0107a8d8868486d4aa2c2c1ba20d4bea0c5fc8521ea8cf65224e111ce8890ff6"}'

39ff9fafe5c2f27fd438a7e665de677221a7e515c8b3a3140b80ce53e4b45ee2

Claim BTC HTLC

./defi-cli spv_claimhtlc "2NFB3d4bhs6ZruMCDzmZ3ucN9hoxRp4QckQ" "bc1qk65huxcrt54uptsa890rua9fcn34gq09zumzry" "39ff9fafe5c2f27fd438a7e665de677221a7e515c8b3a3140b80ce53e4b45ee2"

{
"txid": "da084b0ac5a1e527bdf570557037bbb757383d72edcf5818ae6afdd474fe91ee",
"sendmessage": "Success"
}

Atomic swap ended - summary

  • Maker balance

    • +10 DFI
    • +takerFee * 0.25 DFI (makerIncentive)
    • -0.001 BTC
  • Taker balance

    • -10 DFI
    • -takerFee DFI
    • +0.001 BTC - txfee
Clone this wiki locally