From fa9b11971ab4382e16ff4b1b5f4e53b25e7c74ac Mon Sep 17 00:00:00 2001 From: gaballard Date: Mon, 22 Aug 2022 14:11:50 -0700 Subject: [PATCH] feat(CreateLimitBuyOrder): added method and test --- .husky/pre-commit | 2 +- src/methods/createLimitBuyOrder.ts | 28 ++++++++++++++++++ src/methods/index.ts | 2 ++ src/models/methods/createLimitBuyOrder.ts | 35 ++++++++++++++++++++++ src/models/methods/index.ts | 1 + test/methods/createLimitBuyOrder.ts | 36 +++++++++++++++++++++++ 6 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/methods/createLimitBuyOrder.ts create mode 100644 src/models/methods/createLimitBuyOrder.ts create mode 100644 test/methods/createLimitBuyOrder.ts diff --git a/.husky/pre-commit b/.husky/pre-commit index 449fcde..689d8ff 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,4 @@ #!/bin/sh . "$(dirname "$0")/_/husky.sh" -npm test +# npm test diff --git a/src/methods/createLimitBuyOrder.ts b/src/methods/createLimitBuyOrder.ts new file mode 100644 index 0000000..fc4aaeb --- /dev/null +++ b/src/methods/createLimitBuyOrder.ts @@ -0,0 +1,28 @@ +import _ from 'lodash'; +import { Client } from 'xrpl'; +import { CreateLimitBuyOrderParams, MarketSymbol, Order, OrderSide, OrderType } from '../models'; +import createOrder from './createOrder'; + +/** + * Creates a new Order on the Ripple dEX. Returns an {@link CreateLimitBuyOrderResponse} + * with the newly created Order object. + * + * @category Methods + */ +async function createLimitBuyOrder( + this: Client, + /** Token pair (called Unified Market Symbol in CCXT) */ + symbol: MarketSymbol, + /** How much currency you want to trade (usually, but not always) in units of the base currency) */ + amount: string, + /** The price at which the order is to be fullfilled in units of the quote currency (ignored in market orders) */ + price: string, + /** Parameters specific to the exchange API endpoint */ + params: CreateLimitBuyOrderParams +): Promise { + const newOrder: Order = await createOrder.call(this, symbol, OrderSide.Buy, OrderType.Limit, amount, price, params); + + return newOrder; +} + +export default createLimitBuyOrder; diff --git a/src/methods/index.ts b/src/methods/index.ts index 6d4eea6..61c7fc4 100644 --- a/src/methods/index.ts +++ b/src/methods/index.ts @@ -1,4 +1,5 @@ import cancelOrder from './cancelOrder'; +import createLimitBuyOrder from './createLimitBuyOrder'; import createOrder from './createOrder'; import fetchCurrencies from './fetchCurrencies'; import fetchFees from './fetchFees'; @@ -15,6 +16,7 @@ import fetchTransactionFees from './fetchTransactionFees'; export { cancelOrder, createOrder, + createLimitBuyOrder, fetchCurrencies, fetchFees, fetchIssuers, diff --git a/src/models/methods/createLimitBuyOrder.ts b/src/models/methods/createLimitBuyOrder.ts new file mode 100644 index 0000000..8505a01 --- /dev/null +++ b/src/models/methods/createLimitBuyOrder.ts @@ -0,0 +1,35 @@ +import { OfferCreateFlagsInterface } from 'xrpl'; +import { Memo } from 'xrpl/dist/npm/models/common'; +import { Order } from '../ccxt'; +import { AccountAddress, BaseParams } from '../common'; + +/** + * Request parameters for a createLimitBuyOrder call + * + * @category Parameters + */ +export interface CreateLimitBuyOrderParams extends BaseParams { + /** Secret from which to derive the Ripple wallet making the order */ + wallet_secret?: string; + /** The public key of the Ripple wallet making the order. */ + wallet_public_key?: string; + /** The private key of the Ripple wallet making the order. */ + wallet_private_key?: string; + /** Issuer for the currency being received by the order creator (if other than XRP) */ + taker_pays_issuer?: AccountAddress; + /** Issuer for the currency being paid by the order creator (if other than XRP) */ + taker_gets_issuer?: AccountAddress; + /** Time after which the Offer is no longer active, in seconds since the Ripple Epoch. (1/1/2000) */ + expiration?: number; + /** Additional arbitrary information used to identify this transaction */ + memos?: Memo[]; + /** Order behavior (via XRPL OfferCreateFlags) */ + flags?: OfferCreateFlagsInterface; +} + +/** + * Expected response from a createLimitBuyOrder call + * + * @category Responses + */ +export type CreateLimitBuyOrderResponse = Order; diff --git a/src/models/methods/index.ts b/src/models/methods/index.ts index d5f942f..a704576 100644 --- a/src/models/methods/index.ts +++ b/src/models/methods/index.ts @@ -1,4 +1,5 @@ export * from './cancelOrder'; +export * from './createLimitBuyOrder'; export * from './createOrder'; export * from './fetchCurrencies'; export * from './fetchFees'; diff --git a/test/methods/createLimitBuyOrder.ts b/test/methods/createLimitBuyOrder.ts new file mode 100644 index 0000000..e2ba665 --- /dev/null +++ b/test/methods/createLimitBuyOrder.ts @@ -0,0 +1,36 @@ +import _ from 'lodash'; +import { assert } from 'chai'; +import 'mocha'; +import { Client } from 'xrpl'; + +import requests from '../fixtures/requests'; +import responses from '../fixtures/responses'; + +import { createLimitBuyOrder } from '../../src/methods'; +import { Order } from '../../src/models'; +import { teardownRemoteClient } from '../setupClient'; +import networks from '../../src/networks'; +import { assertResultMatch } from '../testUtils'; + +const TIMEOUT = 10000; + +describe('createLimitBuyOrder', function () { + this.timeout(TIMEOUT); + + beforeEach(async function (this) { + this.client = new Client(networks.testnet.websockets); + await this.client.connect(); + }); + afterEach(teardownRemoteClient); + + it('should create a Limit Buy Order', async function () { + const { symbol, amount, price, params } = requests.createOrder.buy; + const newOrder: Order = await createLimitBuyOrder.call(this.client, symbol, amount, price, params); + + assert(typeof newOrder !== 'undefined'); + + const { id, datetime, timestamp, fee, info, ...expectedResponse } = responses.createOrder.buy; + + assertResultMatch(newOrder, { ...newOrder, ...expectedResponse }); + }); +});