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.

Selling DFI to 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_listorders 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

Selling Bitcoin to 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}