1
- import { SwapRequest } from "comit-sdk" ;
1
+ import {
2
+ MakerClient ,
3
+ TakerNegotiator ,
4
+ } from "comit-sdk/dist/src/negotiation/taker_negotiator" ;
2
5
import { formatEther } from "ethers/utils" ;
3
6
import readLineSync from "readline-sync" ;
4
7
import { toBitcoin } from "satoshi-bitcoin-ts" ;
5
- import { Actor , checkEnvFile , startClient } from "./lib" ;
6
- import {
7
- ExecutionParams ,
8
- NegotiationProtocolClient ,
9
- Order ,
10
- } from "./negotiation" ;
8
+ import { checkEnvFile , startClient } from "./lib" ;
11
9
12
10
( async function main ( ) {
13
11
checkEnvFile ( process . env . DOTENV_CONFIG_PATH ! ) ;
@@ -24,16 +22,25 @@ import {
24
22
25
23
readLineSync . question ( "0. Ready?" ) ;
26
24
25
+ const takerNegotiator = new TakerNegotiator ( ) ;
26
+ const makerClient = new MakerClient ( "http://localhost:2318/" ) ;
27
+
27
28
// take an order from a maker
28
- const negotiationProtocolClient = new NegotiationProtocolClient ( ) ;
29
- const {
30
- order,
31
- execution_params ,
32
- } = await negotiationProtocolClient . startNegotiation (
33
- "http://localhost:2318/orders " ,
34
- "ETH-BTC"
29
+ // Accept any order
30
+ const isOrderAcceptable = ( ) => true ;
31
+ const { order, swap } = await takerNegotiator . negotiateAndSendRequest (
32
+ taker . comitClient ,
33
+ makerClient ,
34
+ "ETH-BTC " ,
35
+ isOrderAcceptable
35
36
) ;
36
37
38
+ if ( ! swap ) {
39
+ throw new Error ( "Could not find an order or something else went wrong" ) ;
40
+ }
41
+
42
+ const swapMessage = await swap . getEntity ( ) ;
43
+ const swapParameters = swapMessage . properties ! . parameters ;
37
44
const ether = formatEther ( order . ask . amount ) ;
38
45
const bitcoin = toBitcoin ( order . bid . amount ) ;
39
46
console . log (
@@ -44,33 +51,23 @@ import {
44
51
bitcoin
45
52
) ;
46
53
47
- const swapMessage = createSwap ( taker , order , execution_params ) ;
48
-
49
- const swapHandle = await taker . comitClient . sendSwap ( swapMessage ) ;
50
-
51
54
const actionConfig = { timeout : 100000 , tryInterval : 1000 } ;
52
55
53
56
console . log (
54
57
"Swap started! Swapping %d %s for %d %s" ,
55
- formatEther ( swapMessage . alpha_asset . quantity ) ,
56
- swapMessage . alpha_asset . name ,
57
- toBitcoin ( swapMessage . beta_asset . quantity ) ,
58
- swapMessage . beta_asset . name
58
+ formatEther ( swapParameters . alpha_asset . quantity ) ,
59
+ swapParameters . alpha_asset . name ,
60
+ toBitcoin ( swapParameters . beta_asset . quantity ) ,
61
+ swapParameters . beta_asset . name
59
62
) ;
60
63
61
64
readLineSync . question ( "1. Continue?" ) ;
62
65
63
- console . log (
64
- "Ethereum HTLC funded! TXID: " ,
65
- await swapHandle . fund ( actionConfig )
66
- ) ;
66
+ console . log ( "Ethereum HTLC funded! TXID: " , await swap . fund ( actionConfig ) ) ;
67
67
68
68
readLineSync . question ( "3. Continue?" ) ;
69
69
70
- console . log (
71
- "Bitcoin redeemed! TXID: " ,
72
- await swapHandle . redeem ( actionConfig )
73
- ) ;
70
+ console . log ( "Bitcoin redeemed! TXID: " , await swap . redeem ( actionConfig ) ) ;
74
71
75
72
console . log ( "Swapped!" ) ;
76
73
console . log (
@@ -82,37 +79,3 @@ import {
82
79
) ;
83
80
process . exit ( ) ;
84
81
} ) ( ) ;
85
-
86
- function createSwap (
87
- actor : Actor ,
88
- order : Order ,
89
- executionParams : ExecutionParams
90
- ) : SwapRequest {
91
- const refundAddress = actor . ethereumWallet . getAccount ( ) ;
92
-
93
- return {
94
- alpha_ledger : {
95
- name : order . ask . ledger ,
96
- network : order . ask . network ,
97
- } ,
98
- beta_ledger : {
99
- name : order . bid . ledger ,
100
- network : order . bid . network ,
101
- } ,
102
- alpha_asset : {
103
- name : order . ask . asset ,
104
- quantity : order . ask . amount ,
105
- } ,
106
- beta_asset : {
107
- name : order . bid . asset ,
108
- quantity : order . bid . amount ,
109
- } ,
110
- alpha_ledger_refund_identity : refundAddress ,
111
- alpha_expiry : executionParams . expiries . ask_expiry ,
112
- beta_expiry : executionParams . expiries . bid_expiry ,
113
- peer : {
114
- peer_id : executionParams . connection_info . peer_id ,
115
- address_hint : executionParams . connection_info . address_hint ,
116
- } ,
117
- } ;
118
- }
0 commit comments