mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 04:05:52 +00:00
committed by
Mayukha Vadari
parent
8c5bc22317
commit
e200de3073
@@ -1,11 +1,11 @@
|
||||
import { assert } from "chai";
|
||||
import { assert } from 'chai'
|
||||
|
||||
import { BookOffersRequest } from "../../src";
|
||||
import requests from "../fixtures/requests";
|
||||
import responses from "../fixtures/responses";
|
||||
import rippled from "../fixtures/rippled";
|
||||
import setupClient from "../setupClient";
|
||||
import { addressTests, assertResultMatch, assertRejects } from "../testUtils";
|
||||
import { BookOffersRequest } from '../../src'
|
||||
import requests from '../fixtures/requests'
|
||||
import responses from '../fixtures/responses'
|
||||
import rippled from '../fixtures/rippled'
|
||||
import setupClient from '../setupClient'
|
||||
import { addressTests, assertResultMatch, assertRejects } from '../testUtils'
|
||||
// import BigNumber from 'bignumber.js'
|
||||
|
||||
// function checkSortingOfOrders(orders) {
|
||||
@@ -44,16 +44,16 @@ import { addressTests, assertResultMatch, assertRejects } from "../testUtils";
|
||||
|
||||
function isUSD(currency: string) {
|
||||
return (
|
||||
currency === "USD" ||
|
||||
currency === "0000000000000000000000005553440000000000"
|
||||
);
|
||||
currency === 'USD' ||
|
||||
currency === '0000000000000000000000005553440000000000'
|
||||
)
|
||||
}
|
||||
|
||||
function isBTC(currency: string) {
|
||||
return (
|
||||
currency === "BTC" ||
|
||||
currency === "0000000000000000000000004254430000000000"
|
||||
);
|
||||
currency === 'BTC' ||
|
||||
currency === '0000000000000000000000004254430000000000'
|
||||
)
|
||||
}
|
||||
|
||||
function normalRippledResponse(request: BookOffersRequest): object {
|
||||
@@ -61,69 +61,69 @@ function normalRippledResponse(request: BookOffersRequest): object {
|
||||
isBTC(request.taker_gets.currency) &&
|
||||
isUSD(request.taker_pays.currency)
|
||||
) {
|
||||
return rippled.book_offers.fabric.requestBookOffersBidsResponse(request);
|
||||
return rippled.book_offers.fabric.requestBookOffersBidsResponse(request)
|
||||
}
|
||||
if (
|
||||
isUSD(request.taker_gets.currency) &&
|
||||
isBTC(request.taker_pays.currency)
|
||||
) {
|
||||
return rippled.book_offers.fabric.requestBookOffersAsksResponse(request);
|
||||
return rippled.book_offers.fabric.requestBookOffersAsksResponse(request)
|
||||
}
|
||||
throw new Error("unexpected end");
|
||||
throw new Error('unexpected end')
|
||||
}
|
||||
|
||||
function xrpRippledResponse(request: BookOffersRequest): object {
|
||||
if (request.taker_pays.issuer === "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw") {
|
||||
return rippled.book_offers.xrp_usd;
|
||||
if (request.taker_pays.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
|
||||
return rippled.book_offers.xrp_usd
|
||||
}
|
||||
if (request.taker_gets.issuer === "rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw") {
|
||||
return rippled.book_offers.usd_xrp;
|
||||
if (request.taker_gets.issuer === 'rp8rJYTpodf8qbSCHVTNacf8nSW8mRakFw') {
|
||||
return rippled.book_offers.usd_xrp
|
||||
}
|
||||
throw new Error("unexpected end");
|
||||
throw new Error('unexpected end')
|
||||
}
|
||||
|
||||
describe("client.getOrderbook", function () {
|
||||
beforeEach(setupClient.setup);
|
||||
afterEach(setupClient.teardown);
|
||||
describe('client.getOrderbook', function () {
|
||||
beforeEach(setupClient.setup)
|
||||
afterEach(setupClient.teardown)
|
||||
|
||||
addressTests.forEach(function (test) {
|
||||
describe(test.type, function () {
|
||||
it("normal", async function () {
|
||||
this.mockRippled.addResponse("book_offers", normalRippledResponse);
|
||||
it('normal', async function () {
|
||||
this.mockRippled.addResponse('book_offers', normalRippledResponse)
|
||||
const response = await this.client.getOrderbook(
|
||||
test.address,
|
||||
requests.getOrderbook.normal,
|
||||
{ limit: 20 }
|
||||
);
|
||||
{ limit: 20 },
|
||||
)
|
||||
assertResultMatch(
|
||||
response,
|
||||
responses.getOrderbook.normal,
|
||||
"getOrderbook"
|
||||
);
|
||||
});
|
||||
'getOrderbook',
|
||||
)
|
||||
})
|
||||
|
||||
it("invalid options", async function () {
|
||||
this.mockRippled.addResponse("book_offers", normalRippledResponse);
|
||||
it('invalid options', async function () {
|
||||
this.mockRippled.addResponse('book_offers', normalRippledResponse)
|
||||
assertRejects(
|
||||
this.client.getOrderbook(test.address, requests.getOrderbook.normal, {
|
||||
invalid: "options",
|
||||
invalid: 'options',
|
||||
}),
|
||||
this.client.errors.ValidationError
|
||||
);
|
||||
});
|
||||
this.client.errors.ValidationError,
|
||||
)
|
||||
})
|
||||
|
||||
it("with XRP", async function () {
|
||||
this.mockRippled.addResponse("book_offers", xrpRippledResponse);
|
||||
it('with XRP', async function () {
|
||||
this.mockRippled.addResponse('book_offers', xrpRippledResponse)
|
||||
const response = await this.client.getOrderbook(
|
||||
test.address,
|
||||
requests.getOrderbook.withXRP
|
||||
);
|
||||
requests.getOrderbook.withXRP,
|
||||
)
|
||||
assertResultMatch(
|
||||
response,
|
||||
responses.getOrderbook.withXRP,
|
||||
"getOrderbook"
|
||||
);
|
||||
});
|
||||
'getOrderbook',
|
||||
)
|
||||
})
|
||||
|
||||
// 'sample XRP/JPY book has orders sorted correctly', async function () {
|
||||
// const orderbookInfo = {
|
||||
@@ -157,61 +157,61 @@ describe("client.getOrderbook", function () {
|
||||
// },
|
||||
|
||||
// WARNING: This test fails to catch the sorting bug, issue #766
|
||||
it("sorted so that best deals come first [bad test]", async function () {
|
||||
this.mockRippled.addResponse("book_offers", normalRippledResponse);
|
||||
it('sorted so that best deals come first [bad test]', async function () {
|
||||
this.mockRippled.addResponse('book_offers', normalRippledResponse)
|
||||
const response = await this.client.getOrderbook(
|
||||
test.address,
|
||||
requests.getOrderbook.normal
|
||||
);
|
||||
requests.getOrderbook.normal,
|
||||
)
|
||||
const bidRates = response.bids.map(
|
||||
(bid) => bid.properties.makerExchangeRate
|
||||
);
|
||||
(bid) => bid.properties.makerExchangeRate,
|
||||
)
|
||||
const askRates = response.asks.map(
|
||||
(ask) => ask.properties.makerExchangeRate
|
||||
);
|
||||
(ask) => ask.properties.makerExchangeRate,
|
||||
)
|
||||
// makerExchangeRate = quality = takerPays.value/takerGets.value
|
||||
// so the best deal for the taker is the lowest makerExchangeRate
|
||||
// bids and asks should be sorted so that the best deals come first
|
||||
assert.deepEqual(
|
||||
bidRates.sort((x) => Number(x)),
|
||||
bidRates
|
||||
);
|
||||
bidRates,
|
||||
)
|
||||
assert.deepEqual(
|
||||
askRates.sort((x) => Number(x)),
|
||||
askRates
|
||||
);
|
||||
});
|
||||
askRates,
|
||||
)
|
||||
})
|
||||
|
||||
it("currency & counterparty are correct", async function () {
|
||||
this.mockRippled.addResponse("book_offers", normalRippledResponse);
|
||||
it('currency & counterparty are correct', async function () {
|
||||
this.mockRippled.addResponse('book_offers', normalRippledResponse)
|
||||
const response = await this.client.getOrderbook(
|
||||
test.address,
|
||||
requests.getOrderbook.normal
|
||||
);
|
||||
[...response.bids, ...response.asks].forEach((order) => {
|
||||
const quantity = order.specification.quantity;
|
||||
const totalPrice = order.specification.totalPrice;
|
||||
const { base, counter } = requests.getOrderbook.normal;
|
||||
assert.strictEqual(quantity.currency, base.currency);
|
||||
assert.strictEqual(quantity.counterparty, base.counterparty);
|
||||
assert.strictEqual(totalPrice.currency, counter.currency);
|
||||
assert.strictEqual(totalPrice.counterparty, counter.counterparty);
|
||||
});
|
||||
});
|
||||
requests.getOrderbook.normal,
|
||||
)
|
||||
;[...response.bids, ...response.asks].forEach((order) => {
|
||||
const quantity = order.specification.quantity
|
||||
const totalPrice = order.specification.totalPrice
|
||||
const { base, counter } = requests.getOrderbook.normal
|
||||
assert.strictEqual(quantity.currency, base.currency)
|
||||
assert.strictEqual(quantity.counterparty, base.counterparty)
|
||||
assert.strictEqual(totalPrice.currency, counter.currency)
|
||||
assert.strictEqual(totalPrice.counterparty, counter.counterparty)
|
||||
})
|
||||
})
|
||||
|
||||
it("direction is correct for bids and asks", async function () {
|
||||
this.mockRippled.addResponse("book_offers", normalRippledResponse);
|
||||
it('direction is correct for bids and asks', async function () {
|
||||
this.mockRippled.addResponse('book_offers', normalRippledResponse)
|
||||
const response = await this.client.getOrderbook(
|
||||
test.address,
|
||||
requests.getOrderbook.normal
|
||||
);
|
||||
requests.getOrderbook.normal,
|
||||
)
|
||||
assert(
|
||||
response.bids.every((bid) => bid.specification.direction === "buy")
|
||||
);
|
||||
response.bids.every((bid) => bid.specification.direction === 'buy'),
|
||||
)
|
||||
assert(
|
||||
response.asks.every((ask) => ask.specification.direction === "sell")
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
response.asks.every((ask) => ask.specification.direction === 'sell'),
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user