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

Commit c8357ca

Browse files
bors[bot]Franck Royer
andauthored
Merge #220
220: Follow-up on negotiation r=D4nte a=D4nte Implement TODOs/comments from comit-network/comit-js-sdk#60 & #128. Co-authored-by: Franck Royer <franck@coblox.tech>
2 parents 7cf11c3 + d603118 commit c8357ca

File tree

13 files changed

+71
-1312
lines changed

13 files changed

+71
-1312
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
## Changed
1010
- `separate_apps` example now uses the negotiation protocol provided by comit-sdk.
1111
- Upgrade to `comit-rs:0.4.0`
12+
- Upgrade all examples and new project to `comit-sdk@0.7.0`.
1213

1314
## [0.4.0] - 2019-11-20
1415

new_project/examples/btc_eth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typescript": "^3.7.2"
2222
},
2323
"dependencies": {
24-
"comit-sdk": "^0.6.0",
24+
"comit-sdk": "^0.7.0",
2525
"dotenv": "^8.2.0",
2626
"moment": "^2.24.0",
2727
"satoshi-bitcoin-ts": "^0.2.4"

new_project/examples/btc_eth/src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import { toBitcoin, toSatoshi } from "satoshi-bitcoin-ts";
3737
.getNewSwaps()
3838
.then(swaps => swaps[0]);
3939

40-
const actionConfig = { timeout: 10000, tryInterval: 1000 };
41-
await makerSwapHandle.accept(actionConfig);
40+
const tryParams = { maxTimeoutSecs: 10, tryIntervalSecs: 1 };
41+
await makerSwapHandle.accept(tryParams);
4242

4343
console.log(
4444
"Swap started! Swapping %d %s for %d %s",
@@ -50,22 +50,22 @@ import { toBitcoin, toSatoshi } from "satoshi-bitcoin-ts";
5050

5151
console.log(
5252
"Ethereum HTLC funded! TXID: ",
53-
await takerSwapHandle.fund(actionConfig)
53+
await takerSwapHandle.fund(tryParams)
5454
);
5555

5656
console.log(
5757
"Bitcoin HTLC funded! TXID: ",
58-
await makerSwapHandle.fund(actionConfig)
58+
await makerSwapHandle.fund(tryParams)
5959
);
6060

6161
console.log(
6262
"Bitcoin HTLC redeemed! TXID: ",
63-
await takerSwapHandle.redeem(actionConfig)
63+
await takerSwapHandle.redeem(tryParams)
6464
);
6565

6666
console.log(
6767
"Ethereum HTLC redeemed! TXID: ",
68-
await makerSwapHandle.redeem(actionConfig)
68+
await makerSwapHandle.redeem(tryParams)
6969
);
7070

7171
console.log("Swapped!");
@@ -156,14 +156,13 @@ function checkEnvFile(path: string) {
156156
}
157157

158158
async function printBalances(actor: Actor) {
159-
let tokenWei = await actor.ethereumWallet.getBalance();
159+
const tokenWei = await actor.ethereumWallet.getBalance();
160160
console.log(
161161
"%s Bitcoin balance: %d. Ether balance: %d",
162162
actor.name,
163163
parseFloat(await actor.bitcoinWallet.getBalance()).toFixed(2),
164164
toNominal(tokenWei.toString(), 18)
165165
);
166-
167166
}
168167

169168
function toNominal(tokenWei: string, decimals: number) {

new_project/examples/btc_eth/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,10 +1105,10 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
11051105
dependencies:
11061106
delayed-stream "~1.0.0"
11071107

1108-
comit-sdk@^0.6.0:
1109-
version "0.6.0"
1110-
resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.6.0.tgz#d5baefef1a7b1f85f0ccad2089be1f3bfe55cfaa"
1111-
integrity sha512-gOL5mOXEp0SBOx0bH2Rx+UHDvsoyahUN9JfOgUQnxHxrEvnPe2I3DHkwDDBQ4mbr1LGo/lnMTajS27RFuQ6Row==
1108+
comit-sdk@^0.7.0:
1109+
version "0.7.0"
1110+
resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.7.0.tgz#08cef516247e130a4eb310698a372635f584e08e"
1111+
integrity sha512-KTf6H4xJ2cpRh3UwgJFSSE8AGad1cwV6MR8rrYix9Xhr9w600+ShkHMpESk0diIRXdWttRSZkbaBlNXe5fODpg==
11121112
dependencies:
11131113
axios "^0.19.0"
11141114
bcoin "https://github.com/bcoin-org/bcoin#2496acc7a98a43f00a7b5728eb256877c1bbf001"

new_project/examples/erc20_btc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"typescript": "^3.7.2"
2121
},
2222
"dependencies": {
23-
"comit-sdk": "^0.6.0",
23+
"comit-sdk": "^0.7.0",
2424
"dotenv": "^8.1.0",
2525
"moment": "^2.24.0",
2626
"readline-sync": "^1.4.10",

new_project/examples/erc20_btc/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ import { toBitcoin, toSatoshi } from "satoshi-bitcoin-ts";
3838
.getNewSwaps()
3939
.then(swaps => swaps[0]);
4040

41-
const actionConfig = { timeout: 10000, tryInterval: 1000 };
42-
await makerSwapHandle.accept(actionConfig);
41+
const tryParams = { maxTimeoutSecs: 10, tryIntervalSecs: 1 };
42+
await makerSwapHandle.accept(tryParams);
4343

4444
console.log(
4545
"Swap started! Swapping %d tokens @ contract address %s for %d %s",
@@ -53,35 +53,35 @@ import { toBitcoin, toSatoshi } from "satoshi-bitcoin-ts";
5353

5454
console.log(
5555
"Ethereum HTLC deployed! TXID: ",
56-
await takerSwapHandle.deploy(actionConfig)
56+
await takerSwapHandle.deploy(tryParams)
5757
);
5858

5959
readLineSync.question("Continue?");
6060

6161
console.log(
6262
"Ethereum HTLC funded! TXID: ",
63-
await takerSwapHandle.fund(actionConfig)
63+
await takerSwapHandle.fund(tryParams)
6464
);
6565

6666
readLineSync.question("Continue?");
6767

6868
console.log(
6969
"Bitcoin HTLC funded! TXID: ",
70-
await makerSwapHandle.fund(actionConfig)
70+
await makerSwapHandle.fund(tryParams)
7171
);
7272

7373
readLineSync.question("Continue?");
7474

7575
console.log(
7676
"Bitcoin HTLC redeemed! TXID: ",
77-
await takerSwapHandle.redeem(actionConfig)
77+
await takerSwapHandle.redeem(tryParams)
7878
);
7979

8080
readLineSync.question("Continue?");
8181

8282
console.log(
8383
"Ethereum HTLC redeemed! TXID: ",
84-
await makerSwapHandle.redeem(actionConfig)
84+
await makerSwapHandle.redeem(tryParams)
8585
);
8686

8787
console.log("Swapped!");

new_project/examples/erc20_btc/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,10 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
483483
dependencies:
484484
delayed-stream "~1.0.0"
485485

486-
comit-sdk@^0.6.0:
487-
version "0.6.0"
488-
resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.6.0.tgz#d5baefef1a7b1f85f0ccad2089be1f3bfe55cfaa"
489-
integrity sha512-gOL5mOXEp0SBOx0bH2Rx+UHDvsoyahUN9JfOgUQnxHxrEvnPe2I3DHkwDDBQ4mbr1LGo/lnMTajS27RFuQ6Row==
486+
comit-sdk@^0.7.0:
487+
version "0.7.0"
488+
resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.7.0.tgz#08cef516247e130a4eb310698a372635f584e08e"
489+
integrity sha512-KTf6H4xJ2cpRh3UwgJFSSE8AGad1cwV6MR8rrYix9Xhr9w600+ShkHMpESk0diIRXdWttRSZkbaBlNXe5fODpg==
490490
dependencies:
491491
axios "^0.19.0"
492492
bcoin "https://github.com/bcoin-org/bcoin#2496acc7a98a43f00a7b5728eb256877c1bbf001"

new_project/examples/separate_apps/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"dependencies": {
2424
"@types/express": "^4.17.2",
2525
"axios": "^0.19.0",
26-
"comit-sdk": "^0.6.0",
26+
"comit-sdk": "^0.7.0",
2727
"dotenv": "^8.1.0",
2828
"express": "^4.17.1",
2929
"moment": "^2.24.0",

new_project/examples/separate_apps/src/maker.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
MakerNegotiator,
44
} from "comit-sdk/dist/src/negotiation/maker_negotiator";
55
import { Order } from "comit-sdk/dist/src/negotiation/order";
6+
import { TryParams } from "comit-sdk/dist/src/timeout_promise";
67
import { formatEther } from "ethers/utils";
78
import moment from "moment";
89
import readLineSync from "readline-sync";
@@ -13,14 +14,14 @@ function createOrder(): Order {
1314
return {
1415
id: "123",
1516
tradingPair: "ETH-BTC",
16-
valid_until: moment().unix() + 300,
17+
validUntil: moment().unix() + 300,
1718
ask: {
18-
amount: "9000000000000000000",
19+
nominalAmount: "9",
1920
asset: "ether",
2021
ledger: "ethereum",
2122
},
2223
bid: {
23-
amount: "100000000",
24+
nominalAmount: "1",
2425
asset: "bitcoin",
2526
ledger: "bitcoin",
2627
},
@@ -48,6 +49,8 @@ function createOrder(): Order {
4849
console.log("[Maker] peer id:", peerId);
4950
console.log("[Maker] address hint:", addressHint);
5051

52+
const tryParams: TryParams = { maxTimeoutSecs: 100, tryIntervalSecs: 1 };
53+
5154
// start negotiation protocol handler so that a taker can take the order and receives the latest rate
5255

5356
const makerNegotiator = new MakerNegotiator(
@@ -65,7 +68,7 @@ function createOrder(): Order {
6568
ethereum: { network: "regtest" },
6669
},
6770
},
68-
{ timeout: 100000, tryInterval: 1000 }
71+
tryParams
6972
);
7073

7174
const makerHttpApi = new MakerHttpApi(makerNegotiator);
@@ -89,19 +92,17 @@ function createOrder(): Order {
8992
});
9093
}
9194

92-
const actionConfig = { timeout: 100000, tryInterval: 1000 };
93-
94-
const swap = await swapHandle.getEntity();
95+
const swap = await swapHandle.fetchDetails();
9596
const swapParams = swap.properties!.parameters;
9697

9798
// only accept a request if it fits to the created order above
9899
if (isValid(swapParams, order)) {
99100
console.log("Requested order is invalid");
100-
await swapHandle.decline(actionConfig);
101+
await swapHandle.decline(tryParams);
101102
process.exit();
102103
}
103104
console.log("Requested order is still valid");
104-
await swapHandle.accept(actionConfig);
105+
await swapHandle.accept(tryParams);
105106

106107
console.log(
107108
"Swap started! Swapping %d Ether for %d %s",
@@ -114,15 +115,12 @@ function createOrder(): Order {
114115

115116
console.log(
116117
"Bitcoin HTLC funded! TXID: ",
117-
await swapHandle.fund(actionConfig)
118+
await swapHandle.fund(tryParams)
118119
);
119120

120121
readLineSync.question("4. Continue?");
121122

122-
console.log(
123-
"Ether redeemed! TXID: ",
124-
await swapHandle.redeem(actionConfig)
125-
);
123+
console.log("Ether redeemed! TXID: ", await swapHandle.redeem(tryParams));
126124

127125
console.log("Swapped!");
128126
console.log(
@@ -139,6 +137,6 @@ function isValid(swapParams: any, order: Order) {
139137
return (
140138
swapParams.alpha_asset.name !== order.ask.asset ||
141139
swapParams.beta_asset.name !== order.bid.asset ||
142-
order.valid_until < moment().unix()
140+
order.validUntil < moment().unix()
143141
);
144142
}

new_project/examples/separate_apps/src/taker.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import { Order } from "comit-sdk/dist/src/negotiation/order";
12
import {
23
MakerClient,
34
TakerNegotiator,
45
} from "comit-sdk/dist/src/negotiation/taker_negotiator";
6+
import { TryParams } from "comit-sdk/dist/src/timeout_promise";
57
import { formatEther } from "ethers/utils";
68
import readLineSync from "readline-sync";
79
import { toBitcoin } from "satoshi-bitcoin-ts";
@@ -22,14 +24,31 @@ import { checkEnvFile, startClient } from "./lib";
2224

2325
readLineSync.question("0. Ready?");
2426

25-
const takerNegotiator = new TakerNegotiator();
27+
const takerNegotiator = new TakerNegotiator(taker.comitClient);
2628
const makerClient = new MakerClient("http://localhost:2318/");
2729

2830
// take an order from a maker
2931
// Accept any order
30-
const isOrderAcceptable = () => true;
31-
const { order, swap } = await takerNegotiator.negotiateAndSendRequest(
32-
taker.comitClient,
32+
const isOrderAcceptable = (order: Order) => {
33+
if (order.ask.asset !== "ether" || order.bid.asset !== "bitcoin") {
34+
// These are'nt the droids you're looking for
35+
return false;
36+
}
37+
38+
const ether = parseFloat(order.ask.nominalAmount);
39+
const bitcoin = parseFloat(order.bid.nominalAmount);
40+
41+
if (ether === 0 || bitcoin === 0) {
42+
// Let's do safe maths
43+
return false;
44+
}
45+
// I want at least 1 bitcoin for 10 Ether
46+
const minRate = 0.1;
47+
const orderRate = bitcoin / ether;
48+
console.log("Rate offered: ", orderRate);
49+
return orderRate > minRate;
50+
};
51+
const { order, swap } = await takerNegotiator.negotiateAndInitiateSwap(
3352
makerClient,
3453
"ETH-BTC",
3554
isOrderAcceptable
@@ -39,10 +58,10 @@ import { checkEnvFile, startClient } from "./lib";
3958
throw new Error("Could not find an order or something else went wrong");
4059
}
4160

42-
const swapMessage = await swap.getEntity();
61+
const swapMessage = await swap.fetchDetails();
4362
const swapParameters = swapMessage.properties!.parameters;
44-
const ether = formatEther(order.ask.amount);
45-
const bitcoin = toBitcoin(order.bid.amount);
63+
const ether = order.ask.nominalAmount;
64+
const bitcoin = order.bid.nominalAmount;
4665
console.log(
4766
`Received latest order details: %s:%s for a rate of %d:%d`,
4867
order.ask.asset,
@@ -51,7 +70,7 @@ import { checkEnvFile, startClient } from "./lib";
5170
bitcoin
5271
);
5372

54-
const actionConfig = { timeout: 100000, tryInterval: 1000 };
73+
const actionConfig: TryParams = { maxTimeoutSecs: 100, tryIntervalSecs: 1 };
5574

5675
console.log(
5776
"Swap started! Swapping %d %s for %d %s",

0 commit comments

Comments
 (0)