Skip to content

Interchain Exchange Tutorial

Ahmed Hilali edited this page May 18, 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)[https://github.com/DeFiCh/pinkpaper/tree/main/interchain-exchange]

This tutorial will teach you how to use the Interchain Exchange through the command line interface.

Creating an order, Selling DFI for Bitcoin

  1. 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/std@0.74.0/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/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')};
}

Alternatively, you may skip this step, and omit the hash in step 4, the node will generate it for you.

  1. Generate an SPV address to receive your Bitcoin using spv_getnewaddress and get the pub key using spv_getaddresspubkey ADDRESS

  2. Create the order using icx_createorder {"tokenFrom": 0, "chainTo":"BTC","ownerAddress": YOUR_DEFICHAIN_ADDRESS,"amountFrom":30,"orderPrice":0.1,"expiry":1000, "receivePubkey": YOUR_SPV_PUBKEY}

  3. Run icx_listorders and observe if you have an offer. Once you have an offer, create the DeFiChain HTLC using: icx_submitdfchtlc {"offerTx": offerTxId, "amount": 10, "hash": YOUR_HASH, "receiveAddress": newAddress, "receivePubkey": newAddressPubKey, "timeout": 500}

  4. Run icx_listhtlcs again until you see an external HTLC posted for your order. Once it's posted claim it using the seed by doing spv_claimhtlc EXT_HTLC_ADDRESS YOUR_SPV_ADDRESS YOUR_SEED 2000

Creating an order, Selling Bitcoin for DFI

  1. Firstly, the Bitcoin 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/std@0.74.0/hash/mod.ts";
import { cryptoRandomString } from "https://deno.land/x/crypto_random_string@1.0.0/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')};
}

Alternatively, you may skip this step, and omit the hash in step 4, the node will generate it for you.

  1. Create the order using icx_createorder {"tokenTo": 0, "chainFrom":"BTC","ownerAddress": YOUR_DEFICHAIN_ADDRESS,"amountFrom":30,"orderPrice":0.1,"expiry": 1000}

  2. Obtain your SPV pubkey using spv_getaddresspubkey ADDRESS

  3. Run icx_listorders and observe if you have an offer. Once you have an offer, create the Bitcoin HTLC using: spv_createhtlc OTHER_PUBKEY YOUR_PUBKEY 30 YOUR_HASH

  4. Run icx_listorders again until you see a DeFiChain HTLC posted for your order. Once it's posted claim it using the seed by doing icx_claimdfchtlc {"dfchtlcTx": HTLC_TXID, "amount": 0.00000001, "seed": YOUR_SEED}

Making an offer, Selling Bitcoin for DFI

  1. Firstly, create a new SPV address for Bitcoin using spv_getnewaddress, send some Bitcoin there and then list orders using icx_listorders

  2. Make an offer for the order you want using icx_makeoffer {"orderTx": orderTxId, "amount": 1, "receiveAddress": YOUR_DFI_ADDRESS, "ownerAddress": YOUR_DFI_ADDRESS}

  3. Once your order is accepted, the seller will create a DeFiChain HTLC, use icx_listhtlcs to obtain the hash. Submit HTLC on BTC side spv_createhtlc spvReceiverPubKey spvSenderPubKey 15 hash. You will receive some info regarding the HTLC properties.

  4. Send Bitcoin to the HTLC address using spv_sendtoaddress.

  5. Submit the Bitcoin HTLC to DeFiChain using icx_submitexthtlc {"offerTx": OFFER_TX_ID, "amount": 1, "htlcScriptAddress": BITCOIN_HTLC_SCRIPT_ADDRESS, "hash": YOUR_HASH, "ownerPubkey": YOUR_SPV_PUBKEY, "timeout": 15}

  6. Wait for the seller to claim your HTLC, once they do, you can get the seed using spv_gethtlcseed BITCOIN_HTLC_ADDRESS

  7. List HTLCs on DeFiChain using icx_listhtlcs and claim the DeFiChain HTLC using icx_claimdfchtlc {"dfchtlcTx": DFC_HTLC_TX, "amount": 0.1, "seed": HTLC_SEED}

Making an offer, Selling DFI for Bitcoin

  1. Firstly, list orders using icx_listorders

  2. Make an offer for the order you want using icx_makeoffer {"orderTx": orderTxId,"amount": 1, "receivePubkey": YOUR_BITCOIN_SPV_ADDRESS, "ownerAddress": YOUR_DEFICHAIN_ADDRESS}

  3. Once the seller has accepted your order, they will create an external HTLC. List HTLC using icx_listhtlcs, find the external HTLC and create an HTLC on the DeFiChain side using the same hash using icx_submitdfchtlc {"offerTx": offerTxId, "amount": 1, "hash": HTLC_HASH, "receivePubkey": SELLER_PUBKEY, "timeout": 400, "receiveAddress": SELLER_ADDRESS}

  4. Send Bitcoin to the HTLC address using spv_sendtoaddress.

  5. List HTLCs on DeFiChain using icx_listhtlcs and claim the Bitcoin HTLC using spv_claimhtlc HTLC_SCRIPT_ADDRESS YOUR_BITCOIN_SPV_ADDRESS HTLC_SEED 2000

Clone this wiki locally