From ebca65cb7491348c29d3d8652740a50ab892a55c Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Tue, 18 Feb 2020 10:19:28 +1100 Subject: [PATCH] Upgrade to comit-sdk@0.11.1 --- create/CHANGELOG.md | 3 ++ .../new_project/examples/btc_eth/package.json | 2 +- .../new_project/examples/btc_eth/src/maker.ts | 5 +- .../new_project/examples/btc_eth/src/taker.ts | 52 +++++++------------ create/new_project/examples/btc_eth/yarn.lock | 8 +-- 5 files changed, 30 insertions(+), 40 deletions(-) diff --git a/create/CHANGELOG.md b/create/CHANGELOG.md index fd821a4cb..a310421fc 100644 --- a/create/CHANGELOG.md +++ b/create/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## Changed +- Upgrade to `comit-sdk@0.11.1` to use the new and improved `TakerNegotiator` interface. + ## [0.8.3] - 2020-02-02 ## Changed diff --git a/create/new_project/examples/btc_eth/package.json b/create/new_project/examples/btc_eth/package.json index 4dc928cc5..1f45778a4 100644 --- a/create/new_project/examples/btc_eth/package.json +++ b/create/new_project/examples/btc_eth/package.json @@ -25,7 +25,7 @@ "dependencies": { "@types/express": "^4.17.2", "axios": "^0.19.2", - "comit-sdk": "^0.10.1", + "comit-sdk": "^0.11.1", "dotenv": "^8.1.0", "express": "^4.17.1", "moment": "^2.24.0", diff --git a/create/new_project/examples/btc_eth/src/maker.ts b/create/new_project/examples/btc_eth/src/maker.ts index 79ea6f1c5..2b4062d59 100644 --- a/create/new_project/examples/btc_eth/src/maker.ts +++ b/create/new_project/examples/btc_eth/src/maker.ts @@ -1,4 +1,4 @@ -import { MakerNegotiator, Order, TryParams } from "comit-sdk"; +import { MakerNegotiator, TryParams } from "comit-sdk"; import { formatEther } from "ethers/utils"; import moment from "moment"; import readLineSync from "readline-sync"; @@ -62,9 +62,8 @@ import { createActor, sleep } from "./lib"; // The maker's HTTP service will be served at http://localhost:2318/ await makerNegotiator.listen(2318, "localhost"); // Create an order to be published. - const order: Order = { + const order = { id: "123", - tradingPair: "ETH-BTC", validUntil: moment().unix() + 300, ask: { nominalAmount: "50", diff --git a/create/new_project/examples/btc_eth/src/taker.ts b/create/new_project/examples/btc_eth/src/taker.ts index e2c70a01d..9e929b147 100644 --- a/create/new_project/examples/btc_eth/src/taker.ts +++ b/create/new_project/examples/btc_eth/src/taker.ts @@ -48,36 +48,24 @@ import { createActor, sleep } from "./lib"; makerNegotiatorUrl ); - const order = await takerNegotiator.getOrderByTradingPair( - // Define the trading pair to request and order for. - "ETH-BTC" - ); - - // Check if the returned order matches the requested asset-pair - if (order.ask.asset !== "ether" || order.bid.asset !== "bitcoin") { - // These aren't the droids you're looking for - throw new Error("Maker returned an order with incorrect assets."); - } - - const ether = parseFloat(order.ask.nominalAmount); - const bitcoin = parseFloat(order.bid.nominalAmount); + const criteria = { + buy: { + ledger: "bitcoin", + asset: "bitcoin", + minNominalAmount: "1", + }, + sell: { + ledger: "ethereum", + asset: "ether", + }, + minRate: 0.001, + }; - if (ether === 0 || bitcoin === 0) { - // Let's do safe maths - throw new Error("Maker returned an order with a null assets."); - } + const order = await takerNegotiator.getOrder(criteria); - // Only accept orders that are at least 1 bitcoin for 10 Ether - const minRate = 0.001; - const orderRate = bitcoin / ether; - console.log("Rate offered: ", orderRate); - if (orderRate < minRate) { - throw new Error( - "Maker returned an order which is not good enough, aborting." - ); - } + console.log("Rate offered: ", order.getOfferedRate().toString()); - const swap = await takerNegotiator.takeOrder(order); + const swap = await order.take(); if (!swap) { throw new Error( @@ -86,11 +74,11 @@ import { createActor, sleep } from "./lib"; } console.log( - `Received latest order details: %s:%s for a rate of %d:%d`, - order.ask.asset, - order.bid.asset, - order.ask.nominalAmount, - order.bid.nominalAmount + `Took the following order: %s:%s for a rate of %d:%d`, + order.rawOrder.ask.asset, + order.rawOrder.bid.asset, + order.rawOrder.ask.nominalAmount, + order.rawOrder.bid.nominalAmount ); // Retrieve the details (properties) of the swap diff --git a/create/new_project/examples/btc_eth/yarn.lock b/create/new_project/examples/btc_eth/yarn.lock index c64acc6bf..c978755b3 100644 --- a/create/new_project/examples/btc_eth/yarn.lock +++ b/create/new_project/examples/btc_eth/yarn.lock @@ -576,10 +576,10 @@ comit-scripts@^0.8.3: axios "^0.19.0" targz "^1.0.1" -comit-sdk@^0.10.1: - version "0.10.1" - resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.10.1.tgz#22a88a697e943342d5b6e6e3e72d4d3974cabaad" - integrity sha512-xhJu68EO+lu1KL4AmK+Chc+f6UKcLtryk5zYg6/GhE7NPRTwclYQ4BWrH2/cNcbau2zbUsnQP/+FkCwTW+j4rQ== +comit-sdk@^0.11.1: + version "0.11.1" + resolved "https://registry.yarnpkg.com/comit-sdk/-/comit-sdk-0.11.1.tgz#ceb73dd6c47a388a1294738d34432f00cce44c40" + integrity sha512-bYcMdfdkHjgYmuhV67nXQlyhgRhRxrw6mGRdEdn3OFrPMD7gVMAmLBFmcKJJdnxv+ld8aIbMZwmrpDGhl98tBQ== dependencies: axios "^0.19.0" bcoin "https://github.com/bcoin-org/bcoin#2496acc7a98a43f00a7b5728eb256877c1bbf001"