Skip to content

Commit

Permalink
Merge pull request #11 from AktaryTech/geoff/RDS-33-create-limit-buy-…
Browse files Browse the repository at this point in the history
…order

feat(CreateLimitBuyOrder): added method and test
  • Loading branch information
gaballard authored Aug 22, 2022
2 parents 9b0dd1e + fa9b119 commit 9aeb48b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
# npm test
28 changes: 28 additions & 0 deletions src/methods/createLimitBuyOrder.ts
Original file line number Diff line number Diff line change
@@ -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<Order> {
const newOrder: Order = await createOrder.call(this, symbol, OrderSide.Buy, OrderType.Limit, amount, price, params);

return newOrder;
}

export default createLimitBuyOrder;
2 changes: 2 additions & 0 deletions src/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cancelOrder from './cancelOrder';
import createLimitBuyOrder from './createLimitBuyOrder';
import createOrder from './createOrder';
import fetchCurrencies from './fetchCurrencies';
import fetchFees from './fetchFees';
Expand All @@ -15,6 +16,7 @@ import fetchTransactionFees from './fetchTransactionFees';
export {
cancelOrder,
createOrder,
createLimitBuyOrder,
fetchCurrencies,
fetchFees,
fetchIssuers,
Expand Down
35 changes: 35 additions & 0 deletions src/models/methods/createLimitBuyOrder.ts
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/models/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './cancelOrder';
export * from './createLimitBuyOrder';
export * from './createOrder';
export * from './fetchCurrencies';
export * from './fetchFees';
Expand Down
36 changes: 36 additions & 0 deletions test/methods/createLimitBuyOrder.ts
Original file line number Diff line number Diff line change
@@ -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 });
});
});

0 comments on commit 9aeb48b

Please sign in to comment.