Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit f33478a

Browse files
bors[bot]Franck Royer
andauthored
Merge #208
208: Replace local negotiation protocol with the comit-sdk one r=D4nte a=D4nte See comit-network/comit-js-sdk#60 for the SDK changes. To be finalised once sdk changes are released. Resolves #128 Co-authored-by: Franck Royer <franck@coblox.tech>
2 parents efa3c25 + ea034d3 commit f33478a

File tree

4 files changed

+51
-174
lines changed

4 files changed

+51
-174
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## Changed
10+
- `separate_apps` example now uses the negotiation protocol provided by comit-sdk.
11+
912
## [0.4.0] - 2019-11-20
1013

1114
### Fixed

new_project/examples/separate_apps/src/maker.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1+
import {
2+
MakerHttpApi,
3+
MakerNegotiator,
4+
} from "comit-sdk/dist/src/negotiation/maker_negotiator";
5+
import { Order } from "comit-sdk/dist/src/negotiation/order";
16
import { formatEther } from "ethers/utils";
27
import moment from "moment";
38
import readLineSync from "readline-sync";
49
import { toBitcoin } from "satoshi-bitcoin-ts";
510
import { checkEnvFile, startClient } from "./lib";
6-
import { NegotiationProtocolHandler, Order } from "./negotiation";
711

812
function createOrder(): Order {
913
return {
1014
id: "123",
11-
key: "ETH-BTC",
15+
tradingPair: "ETH-BTC",
1216
valid_until: moment().unix() + 300,
1317
ask: {
1418
amount: "9000000000000000000",
1519
asset: "ether",
1620
ledger: "ethereum",
17-
network: "regtest",
1821
},
1922
bid: {
2023
amount: "100000000",
2124
asset: "bitcoin",
2225
ledger: "bitcoin",
23-
network: "regtest",
2426
},
2527
};
2628
}
@@ -48,25 +50,29 @@ function createOrder(): Order {
4850

4951
// start negotiation protocol handler so that a taker can take the order and receives the latest rate
5052

51-
const negotiationProtocolHandler = new NegotiationProtocolHandler(
53+
const makerNegotiator = new MakerNegotiator(
54+
maker.comitClient,
5255
{
53-
connection_info: {
56+
peer: {
5457
peer_id: peerId,
5558
address_hint: addressHint,
5659
},
57-
expiries: {
58-
ask_expiry: moment().unix() + 7200,
59-
bid_expiry: moment().unix() + 3600,
60+
alpha_expiry: moment().unix() + 7200,
61+
beta_expiry: moment().unix() + 3600,
62+
ledgers: {
63+
bitcoin: { network: "regtest" },
64+
// TODO: It should be possible to use the chain_id
65+
ethereum: { network: "regtest" },
6066
},
61-
role: "alice",
62-
swap_id: "SOME_RANDOM_ID",
6367
},
64-
2318
65-
); // CoBloX Founding Date 🚀
68+
{ timeout: 100000, tryInterval: 1000 }
69+
);
70+
71+
const makerHttpApi = new MakerHttpApi(makerNegotiator);
6672

67-
negotiationProtocolHandler.start();
73+
makerHttpApi.listen(2318);
6874
const order = createOrder();
69-
negotiationProtocolHandler.addOrder(order);
75+
makerNegotiator.addOrder(order);
7076

7177
const invitationDetails = `http://localhost:2318/orders/ETH-BTC`;
7278
console.log(`Waiting for someone taking my order at: ${invitationDetails}`);

new_project/examples/separate_apps/src/negotiation.ts

Lines changed: 0 additions & 95 deletions
This file was deleted.
Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { SwapRequest } from "comit-sdk";
1+
import {
2+
MakerClient,
3+
TakerNegotiator,
4+
} from "comit-sdk/dist/src/negotiation/taker_negotiator";
25
import { formatEther } from "ethers/utils";
36
import readLineSync from "readline-sync";
47
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";
119

1210
(async function main() {
1311
checkEnvFile(process.env.DOTENV_CONFIG_PATH!);
@@ -24,16 +22,25 @@ import {
2422

2523
readLineSync.question("0. Ready?");
2624

25+
const takerNegotiator = new TakerNegotiator();
26+
const makerClient = new MakerClient("http://localhost:2318/");
27+
2728
// 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
3536
);
3637

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;
3744
const ether = formatEther(order.ask.amount);
3845
const bitcoin = toBitcoin(order.bid.amount);
3946
console.log(
@@ -44,33 +51,23 @@ import {
4451
bitcoin
4552
);
4653

47-
const swapMessage = createSwap(taker, order, execution_params);
48-
49-
const swapHandle = await taker.comitClient.sendSwap(swapMessage);
50-
5154
const actionConfig = { timeout: 100000, tryInterval: 1000 };
5255

5356
console.log(
5457
"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
5962
);
6063

6164
readLineSync.question("1. Continue?");
6265

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));
6767

6868
readLineSync.question("3. Continue?");
6969

70-
console.log(
71-
"Bitcoin redeemed! TXID: ",
72-
await swapHandle.redeem(actionConfig)
73-
);
70+
console.log("Bitcoin redeemed! TXID: ", await swap.redeem(actionConfig));
7471

7572
console.log("Swapped!");
7673
console.log(
@@ -82,37 +79,3 @@ import {
8279
);
8380
process.exit();
8481
})();
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

Comments
 (0)