Skip to content

Commit

Permalink
Interface icx_listorders RPC (#402)
Browse files Browse the repository at this point in the history
* Added ICXOrderBook.listOrders() function, testing and documentation

* Refactored code.

* Refactored code.

* Refactored code.
  • Loading branch information
surangap authored Jul 2, 2021
1 parent aea49e0 commit b546333
Show file tree
Hide file tree
Showing 5 changed files with 730 additions and 0 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.
// 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 closeAllOpenOffers (): 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)
}
}
Loading

0 comments on commit b546333

Please sign in to comment.