-
Notifications
You must be signed in to change notification settings - Fork 120
Interchain Exchange Tutorial
Ahmed Hilali edited this page May 17, 2021
·
11 revisions
const seedHashPair = createSeedHashPair();
const seed = seedHashPair.seed;
const hash = seedHashPair.hash;
console.log(`Seed/Hash generated: ${seed}/${hash}`);
const spvAddressSender = "SPV_ADDRESS";
const dfcHtlcTimeout = 500;
const extHtlcTimeout = 15;
// Convert some UTXO DFI to Account DFI
await waitConfirmation(await rpcMethod('utxostoaccount', [{ [ownerAddress]: '100@0' }]));
const newAddressSpv = (await rpcMethod('spv_getnewaddress')).result;
console.log(`New SPV address generated: ${newAddressSpv}`);
const spvReceiverPubKey = (await rpcMethod('spv_getaddresspubkey', [newAddressSpv])).result;
// Create order
const orderObj = await rpcMethod('icx_createorder', [{"tokenFrom": 0, "chainTo":"BTC","ownerAddress": ownerAddress,"amountFrom":30,"orderPrice":0.1,"expiry":1000, "receivePubkey": spvReceiverPubKey}]);
const orderTxId = await waitConfirmation(orderObj.result);
const newAddress = (await rpcMethod('getnewaddress')).result;
console.log(`New address generated: ${newAddress}`);
// Convert some UTXO DFI to Account DFI
await waitConfirmation(await rpcMethod('utxostoaccount', [{ [newAddress]: '100@0' }]));
// Get pub key of address
const newAddressPubKey = (await rpcMethod('getaddressinfo', [newAddress])).result.pubkey;
// Make offer
const offerTxId = await waitConfirmation((await rpcMethod('icx_makeoffer', [{"orderTx": orderTxId,"amount": 1,
"receiveAddress": newAddress, "ownerAddress": newAddress}])).result);
// Submit HTLC on DFC side
const htlcTxId = await waitConfirmation((await rpcMethod('icx_submitdfchtlc', [{"offerTx": offerTxId, "amount": 10,
"hash": hash, "receiveAddress": newAddress, "receivePubkey": newAddressPubKey, "timeout": dfcHtlcTimeout}])).result);
const spvSenderPubKey = (await rpcMethod('spv_getaddresspubkey', [spvAddressSender])).result;
console.log(`spvReceiverPubKey: ${spvReceiverPubKey}`);
console.log(`spvSenderPubKey: ${spvSenderPubKey}`);
// Submit HTLC on BTC side
const spvHtlc = await waitSPVConnected(async () => {
return await rpcMethod('spv_createhtlc', [spvReceiverPubKey, spvSenderPubKey, `${extHtlcTimeout}`, hash], false, true);
});
const spvHtlcInfo = spvHtlc.result;
console.log(`SPV HTLC info: ${JSON.stringify(spvHtlcInfo)}`);
// Send money to HTLC script address
await waitSPVConnected(async () => {
return await rpcMethod('spv_sendtoaddress', [spvHtlcInfo.address, 0.00002], true, true);
});
// List HTLC outputs
const spvHtlcOutputsResult = await rpcMethod('spv_listhtlcoutputs');
console.log(`SPV HTLC outputs: ${JSON.stringify(spvHtlcOutputsResult, null, 2)}`);
// Submit external HTLC
await waitConfirmation((await rpcMethod('icx_submitexthtlc', [{"offerTx": offerTxId, "amount": 1,
"htlcScriptAddress": spvHtlcInfo.address, "hash": hash, "ownerPubkey": spvReceiverPubKey,
"timeout": extHtlcTimeout}])).result);
// List HTLCs
const listHTLC = await rpcMethod('icx_listhtlcs', [{"offerTx": offerTxId}]);
console.log(`HTLC list: ${JSON.stringify(listHTLC.result, null, 2)}`);
// Claim ext HTLC
await waitSPVConnected(async () => {
return await rpcMethod('spv_claimhtlc', [spvHtlcInfo.address, newAddressSpv, seed, 2000], false, false);
});
// Get HTLC seed
const retrievedSeed = (await rpcMethod('spv_gethtlcseed', [spvHtlcInfo.address])).result;
console.log(`Seed retrieved: ${JSON.stringify(retrievedSeed)}`);
// Claim DFC HTLC
await waitConfirmation((await rpcMethod('icx_claimdfchtlc',
[{"dfchtlcTx": htlcTxId, "amount": 0.1, "seed": retrievedSeed}])).result);