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

Commit

Permalink
Merge #440
Browse files Browse the repository at this point in the history
440: Use new `TakerNegotiator` interface r=D4nte a=D4nte

Incorporates SDK changes from comit-network/comit-js-sdk#116

Related to comit-network/comit-js-sdk#89

Co-authored-by: Franck Royer <[email protected]>
  • Loading branch information
bors[bot] and Franck Royer authored Feb 20, 2020
2 parents 5abec63 + ebca65c commit c35ae89
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
3 changes: 3 additions & 0 deletions create/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## Changed
- Upgrade to `[email protected]` to use the new and improved `TakerNegotiator` interface.

## [0.8.3] - 2020-02-02

## Changed
Expand Down
2 changes: 1 addition & 1 deletion create/new_project/examples/btc_eth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 2 additions & 3 deletions create/new_project/examples/btc_eth/src/maker.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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",
Expand Down
52 changes: 20 additions & 32 deletions create/new_project/examples/btc_eth/src/taker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions create/new_project/examples/btc_eth/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit c35ae89

Please sign in to comment.