Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Jul 2, 2021
1 parent a698813 commit f43bb2d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,59 @@ describe('ICXOrderBook.closeOffer', () => {
expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer)
})

// NOTE(surangap): This test is failing. looks like offers for BTC sell order can not be closed.
// it('should close an offer for sell BTC order from chain:BTC to chain:DFI', async () => {
// // create order - maker
// const order: ICXOrder = {
// chainFrom: 'BTC',
// tokenTo: idDFI,
// ownerAddress: accountDFI,
// amountFrom: new BigNumber(2),
// orderPrice: new BigNumber(100)
// }
// const createOrderResult: ICXGenericResult = await client.icxorderbook.createOrder(order, [])
// const createOrderTxId = createOrderResult.txid
// await container.generate(1)

// // list ICX orders and check status
// const ordersAfterCreateOrder: Record<string, ICXOrderInfo | ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
// expect((ordersAfterCreateOrder as Record<string, ICXOrderInfo>)[createOrderTxId].status).toStrictEqual(ICXOrderStatus.OPEN)

// const accountBTCBeforeOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// // make offer to partial amount 10 DFI - taker
// const offer: ICXOffer = {
// orderTx: createOrderTxId,
// amount: new BigNumber(10), //
// ownerAddress: accountBTC,
// receivePubkey: '0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5'
// }
// const makeOfferResult = await client.icxorderbook.makeOffer(offer, [])
// const makeOfferTxId = makeOfferResult.txid
// await container.generate(1)

// const accountBTCAfterOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// // check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI]
// // Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
// expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.01))

// // List the ICX offers for orderTx = createOrderTxId and check
// const ordersAfterMakeOffer: Record<string, ICXOrderInfo | ICXOfferInfo> = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
// expect(Object.keys(ordersAfterMakeOffer).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
// expect((ordersAfterMakeOffer as Record<string, ICXOfferInfo>)[makeOfferTxId].status).toStrictEqual(ICXOrderStatus.OPEN)

// // close offer makeOfferTxId - taker
// await client.icxorderbook.closeOffer(makeOfferTxId)
// await container.generate(1)

// // List the ICX offers for orderTx = createOrderTxId and check no more offers
// const ordersAfterCloseOffer: Record<string, ICXOrderInfo | ICXOfferInfo> = await await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
// expect(Object.keys(ordersAfterCloseOffer).length).toBe(1) // extra entry for the warning text returned by the RPC atm.

// // check accountBTC balance, should be the same as accountBTCBeforeOffer
// const accountBTCAfterOfferClose: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer)
// })

it('should close an offer with input utxos', async () => {
// create order - maker
const order: ICXOrder = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,17 @@ export class ICXSetup {
expect(result.ICX_TAKERFEE_PER_BTC as number).toStrictEqual(fee)
ICX_TAKERFEE_PER_BTC = result.ICX_TAKERFEE_PER_BTC as number
}

async clossAllOpenOffers (): Promise<void> {
const orders = await this.container.call('icx_listorders', [])
for (const orderTx of Object.keys(orders).splice(1)) {
const offers = await this.container.call('icx_listorders', [{ orderTx: orderTx }])
for (const offerTx of Object.keys(offers).splice(1)) {
if (offers[offerTx].status === 'OPEN') {
await this.container.call('icx_closeoffer', [offerTx])
await this.container.generate(1)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe('ICXOrderBook.listOrders', () => {
await container.stop()
})

afterEach(async () => {
await icxSetup.clossAllOpenOffers()
})

it('should list all the orders', async () => {
// create first order - maker
const order: ICXOrder = {
Expand Down Expand Up @@ -128,11 +132,12 @@ describe('ICXOrderBook.listOrders', () => {

// create second order - maker
const order2: ICXOrder = {
chainFrom: 'BTC',
tokenTo: idDFI,
tokenFrom: idDFI,
chainTo: 'BTC',
ownerAddress: accountDFI,
amountFrom: new BigNumber(2),
orderPrice: new BigNumber(100)
receivePubkey: '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941',
amountFrom: new BigNumber(20),
orderPrice: new BigNumber(0.01)
}
const createOrder2Result = await client.icxorderbook.createOrder(order2, [])
const createOrder2TxId = createOrder2Result.txid
Expand All @@ -148,12 +153,11 @@ describe('ICXOrderBook.listOrders', () => {
const makeOfferTxId = makeOfferResult.txid
await container.generate(1)

// create offer to order createOrderTxId
// create offer to order createOrder2TxId
const offer2: ICXOffer = {
orderTx: createOrder2TxId,
amount: new BigNumber(1), //
ownerAddress: accountBTC,
receivePubkey: '0348790cb93b203a8ea5ce07279cb209d807b535b2ca8b0988a6f7a6578e41f7a5'
amount: new BigNumber(0.2), // 20 DFI = 0.2 BTC
ownerAddress: accountBTC
}
const makeOffer2Result = await client.icxorderbook.makeOffer(offer2, [])
const makeOffer2TxId = makeOffer2Result.txid
Expand All @@ -168,9 +172,8 @@ describe('ICXOrderBook.listOrders', () => {
amount: offer2.amount,
amountInFromAsset: offer2.amount.dividedBy(order2.orderPrice),
ownerAddress: offer2.ownerAddress,
takerFee: offer2.amount.multipliedBy(ICX_TAKERFEE_PER_BTC),
expireHeight: expect.any(BigNumber),
receivePubkey: offer2.receivePubkey
takerFee: offer2.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE),
expireHeight: expect.any(BigNumber)
}
)

Expand Down Expand Up @@ -420,7 +423,7 @@ describe('ICXOrderBook.listOrders', () => {
}
)

// const accountBTCBeforeOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
const accountBTCBeforeOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// make offer to partial amount 10 DFI - taker
const offer: ICXOffer = {
orderTx: createOrderTxId,
Expand All @@ -441,12 +444,10 @@ describe('ICXOrderBook.listOrders', () => {
const makeOffer2TxId = makeOffer2Result.txid
await container.generate(1)

// const accountBTCAfterOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')

// NOTE(surangap): why below check is failing?
const accountBTCAfterOffer: Record<string, BigNumber> = await client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// check fee of 0.02 DFI has been reduced from the accountBTCBeforeOffer[idDFI]
// Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
// expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.02))
expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI].minus(0.02))

// List the ICX offers for orderTx = createOrderTxId and check
const offersForOrder1: Record<string, ICXOrderInfo | ICXOfferInfo> = await client.icxorderbook.listOrders({ orderTx: createOrderTxId })
Expand Down
22 changes: 22 additions & 0 deletions packages/jellyfish-api-core/src/category/icxorderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ export class ICXOrderBook {
)
}

/**
* Returns information about offers for passed order
*
* @param {} options
* @param {string} [options.orderTx] Order txid to list all offers for this order
* @return {Promise<Record<string, ICXOfferInfo>>} Object indluding details of offers.
*/
async listOrders (options: { orderTx: string }): Promise<Record<string, ICXOfferInfo>>

/**
* Returns information about orders or fillorders based on ICXListOrderOptions passed
*
* @param {ICXListOrderOptions} options
* @param {string} [options.token] Token asset
* @param {string} [options.chain] Chain asset
* @param {string} [options.orderTx] Order txid to list all offers for this order
* @param {number} [options.limit = 50] Maximum number of orders to return (default: 50)
* @param {boolean} [options.closed] Display closed orders (default: false)
* @return {Promise<Record<string, ICXOrderInfo | ICXOfferInfo>>} Object indluding details of orders and offers.
*/
async listOrders (options?: ICXListOrderOptions): Promise<Record<string, ICXOrderInfo | ICXOfferInfo>>

/**
* Returns information about orders or fillorders based on ICXListOrderOptions passed
*
Expand Down
70 changes: 70 additions & 0 deletions website/docs/jellyfish/api/icxorderbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,76 @@ interface ICXOfferInfo {

## listOrders

Returns information about offers for passed order

```ts title="client.icxorderbook.listOrders()"
interface icxorderbook {
listOrders (options: { orderTx: string }): Promise<Record<string, ICXOfferInfo>>
}

interface ICXOfferInfo {
orderTx: string
status: ICXOrderStatus
amount: BigNumber
amountInFromAsset: BigNumber
ownerAddress: string
receivePubkey?: string
takerFee: BigNumber
expireHeight: BigNumber
}
```

## listOrders

Returns information about orders or fillorders based on ICXListOrderOptions passed

```ts title="client.icxorderbook.listOrders()"
interface icxorderbook {
listOrders (options?: ICXListOrderOptions): Promise<Record<string, ICXOrderInfo | ICXOfferInfo>>
}

interface ICXListOrderOptions {
token?: string
chain?: string
orderTx?: string
limit?: number
closed?: boolean
}

interface ICXOrderInfo {
status: ICXOrderStatus
type: ICXOrderType
tokenFrom: string
chainTo?: string
receivePubkey?: string
chainFrom?: string
tokenTo?: string
ownerAddress: string
amountFrom: BigNumber
amountToFill: BigNumber
orderPrice: BigNumber
amountToFillInToAsset: BigNumber
height: BigNumber
expireHeight: BigNumber
closeHeight?: BigNumber
closeTx?: string
expired?: boolean
}

interface ICXOfferInfo {
orderTx: string
status: ICXOrderStatus
amount: BigNumber
amountInFromAsset: BigNumber
ownerAddress: string
receivePubkey?: string
takerFee: BigNumber
expireHeight: BigNumber
}
```

## listOrders

Returns information about orders or fillorders based on ICXListOrderOptions passed

```ts title="client.icxorderbook.listOrders()"
Expand Down

0 comments on commit f43bb2d

Please sign in to comment.