Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Jun 23, 2021
1 parent 30006d4 commit f712ce8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, InputUTXO } from '../../../src/category/icxorderbook'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, UTXO, ICXOrderStatus, ICXOrderType } from '../../../src/category/icxorderbook'
import BigNumber from 'bignumber.js'
import { setup, accountDFI, idDFI, checkDFISellOrderDetails, accountBTC, checkDFIBuyOfferDetails } from './common.test'
import { accountDFI, idDFI, accountBTC, ICXSetup, symbolDFI, symbolBTC, ICX_TAKERFEE_PER_BTC, DEX_DFI_PER_BTC_RATE } from './icx_setup'
import { RpcApiError } from '../../../src'

describe('Should test ICXOrderBook.closeOffer', () => {
describe('ICXOrderBook.closeOffer', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await setup(container)
await icxSetup.createAccounts()
await icxSetup.createBTCToken()
await icxSetup.initializeTokensIds()
await icxSetup.mintBTCtoken(100)
await icxSetup.fundAccount(accountDFI, symbolDFI, 500)
await icxSetup.fundAccount(accountBTC, symbolDFI, 10) // for fee
await icxSetup.createBTCDFIPool()
await icxSetup.addLiquidityToBTCDFIPool(1, 100)
await icxSetup.setTakerFee(0.001)
})

afterAll(async () => {
Expand All @@ -24,7 +33,7 @@ describe('Should test ICXOrderBook.closeOffer', () => {
// cleanup code here
})

it('Should close an offer', async () => {
it('should close an offer', async () => {
// create order - maker
const order: ICXOrder = {
tokenFrom: idDFI,
Expand All @@ -39,8 +48,23 @@ describe('Should test ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await container.call('icx_listorders', [])
await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record<string, ICXOrderInfo>)
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
expect((orders as Record<string, ICXOrderInfo>)[createOrderTxId]).toStrictEqual(
{
status: ICXOrderStatus.OPEN,
type: ICXOrderType.INTERNAL,
tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
amountFrom: order.amountFrom,
amountToFill: order.amountFrom,
orderPrice: order.orderPrice,
amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice),
height: expect.any(BigNumber),
expireHeight: expect.any(BigNumber)
}
)

// make offer to partial amout 10 DFI - taker
const offer: ICXOffer = {
Expand All @@ -56,12 +80,22 @@ describe('Should test ICXOrderBook.closeOffer', () => {
const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true])
// check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI]
// Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000))
expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01)

// List the ICX offers for orderTx = createOrderTxId and check
orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record<string, ICXOfferInfo>)
expect((orders as Record<string, ICXOfferInfo>)[makeOfferTxId]).toStrictEqual(
{
orderTx: createOrderTxId,
status: ICXOrderStatus.OPEN,
amount: offer.amount,
amountInFromAsset: offer.amount.dividedBy(order.orderPrice),
ownerAddress: offer.ownerAddress,
takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE),
expireHeight: expect.any(BigNumber)
}
)

// close offer makeOfferTxId - taker
await client.icxorderbook.closeOffer(makeOfferTxId)
Expand All @@ -76,7 +110,7 @@ describe('Should test ICXOrderBook.closeOffer', () => {
expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer)
})

it('Should close an offer with input utxos', async () => {
it('should close an offer with input utxos', async () => {
// create order - maker
const order: ICXOrder = {
tokenFrom: idDFI,
Expand All @@ -91,8 +125,23 @@ describe('Should test ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await container.call('icx_listorders', [])
await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record<string, ICXOrderInfo>)
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
expect((orders as Record<string, ICXOrderInfo>)[createOrderTxId]).toStrictEqual(
{
status: ICXOrderStatus.OPEN,
type: ICXOrderType.INTERNAL,
tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
amountFrom: order.amountFrom,
amountToFill: order.amountFrom,
orderPrice: order.orderPrice,
amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice),
height: expect.any(BigNumber),
expireHeight: expect.any(BigNumber)
}
)

// make offer to partial amout 10 DFI - taker
const offer: ICXOffer = {
Expand All @@ -108,16 +157,26 @@ describe('Should test ICXOrderBook.closeOffer', () => {
const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true])
// check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI]
// Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000))
expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01)

// List the ICX offers for orderTx = createOrderTxId and check
orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record<string, ICXOfferInfo>)
expect((orders as Record<string, ICXOfferInfo>)[makeOfferTxId]).toStrictEqual(
{
orderTx: createOrderTxId,
status: ICXOrderStatus.OPEN,
amount: offer.amount,
amountInFromAsset: offer.amount.dividedBy(order.orderPrice),
ownerAddress: offer.ownerAddress,
takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE),
expireHeight: expect.any(BigNumber)
}
)

// input utxos
const utxos = await container.call('listunspent', [1, 9999999, [accountBTC], true])
const inputUTXOs: InputUTXO[] = utxos.map((utxo: InputUTXO) => {
const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
Expand All @@ -128,15 +187,15 @@ describe('Should test ICXOrderBook.closeOffer', () => {
await container.generate(1)

// List the ICX offers for orderTx = createOrderTxId and check no more offers
orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
expect(Object.keys(orders).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 = await container.call('getaccount', [accountBTC, {}, true])
expect(accountBTCAfterOfferClose).toStrictEqual(accountBTCBeforeOffer)
})

it('Should return an error when incorrect offer transaction is passed', async () => {
it('should return an error when incorrect offer transaction is passed', async () => {
// create order - maker
const order: ICXOrder = {
tokenFrom: idDFI,
Expand All @@ -151,8 +210,23 @@ describe('Should test ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await container.call('icx_listorders', [])
await checkDFISellOrderDetails(container, order, createOrderTxId, orders as Record<string, ICXOrderInfo>)
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
expect((orders as Record<string, ICXOrderInfo>)[createOrderTxId]).toStrictEqual(
{
status: ICXOrderStatus.OPEN,
type: ICXOrderType.INTERNAL,
tokenFrom: order.tokenFrom === '0' ? symbolDFI : symbolBTC,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
amountFrom: order.amountFrom,
amountToFill: order.amountFrom,
orderPrice: order.orderPrice,
amountToFillInToAsset: order.amountFrom.multipliedBy(order.orderPrice),
height: expect.any(BigNumber),
expireHeight: expect.any(BigNumber)
}
)

// make offer to partial amout 10 DFI - taker
const offer: ICXOffer = {
Expand All @@ -168,12 +242,22 @@ describe('Should test ICXOrderBook.closeOffer', () => {
const accountBTCAfterOffer = await container.call('getaccount', [accountBTC, {}, true])
// check fee of 0.01 DFI has been reduced from the accountBTCBefore[idDFI]
// Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
expect(Number(accountBTCAfterOffer[idDFI])).toStrictEqual(Number(accountBTCBeforeOffer[idDFI]) - Number(0.01000000))
expect(accountBTCAfterOffer[idDFI]).toStrictEqual(accountBTCBeforeOffer[idDFI] - 0.01)

// List the ICX offers for orderTx = createOrderTxId and check
orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
await checkDFIBuyOfferDetails(container, offer, makeOfferTxId, orders as Record<string, ICXOfferInfo>)
expect((orders as Record<string, ICXOfferInfo>)[makeOfferTxId]).toStrictEqual(
{
orderTx: createOrderTxId,
status: ICXOrderStatus.OPEN,
amount: offer.amount,
amountInFromAsset: offer.amount.dividedBy(order.orderPrice),
ownerAddress: offer.ownerAddress,
takerFee: offer.amount.multipliedBy(ICX_TAKERFEE_PER_BTC).multipliedBy(DEX_DFI_PER_BTC_RATE),
expireHeight: expect.any(BigNumber)
}
)

// close offer "123" - taker
const promise = client.icxorderbook.closeOffer('123')
Expand All @@ -182,7 +266,7 @@ describe('Should test ICXOrderBook.closeOffer', () => {
await expect(promise).rejects.toThrow('RpcApiError: \'OfferTx (0000000000000000000000000000000000000000000000000000000000000123) does not exist\', code: -8, method: icx_closeoffer')

// List the ICX offers for orderTx = createOrderTxId and check
orders = await container.call('icx_listorders', [{ orderTx: createOrderTxId }])
orders = await client.call('icx_listorders', [{ orderTx: createOrderTxId }], 'bignumber')
expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
})
})
4 changes: 2 additions & 2 deletions packages/jellyfish-api-core/src/category/icxorderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ export class ICXOrderBook {
* Closes offer transaction.
*
* @param {string} offerTx Transaction Id of maker offer
* @param {InputUTXO[]} inputUTXOs Specific utxos to spend
* @param {UTXO[]} inputUTXOs Specific utxos to spend
* @param {string} [inputUTXOs.txid] transaction Id
* @param {number} [inputUTXOs.vout] The output number
* @return {Promise<ICXGenericResult>} Object indluding transaction id of the the transaction
*/
async closeOffer (offerTx: string, inputUTXOs: InputUTXO[] = []): Promise<ICXGenericResult> {
async closeOffer (offerTx: string, inputUTXOs: UTXO[] = []): Promise<ICXGenericResult> {
return await this.client.call(
'icx_closeoffer',
[
Expand Down
4 changes: 2 additions & 2 deletions website/docs/jellyfish/api/icxorderbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ Closes offer transaction.

```ts title="client.icxorderbook.closeOffer()"
interface icxorderbook {
closeOffer (offerTx: string, inputUTXOs: InputUTXO[] = []): Promise<ICXGenericResult>
closeOffer (offerTx: string, inputUTXOs: UTXO[] = []): Promise<ICXGenericResult>
}

interface InputUTXO {
interface UTXO {
txid: string
vout: number
}
Expand Down

0 comments on commit f712ce8

Please sign in to comment.