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 e549108 commit 2f6acbd
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { RpcApiError } from '../../../src'
describe('ICXOrderBook.closeOffer', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)
const icxSetup = new ICXSetup(container, client)

beforeAll(async () => {
await container.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RpcApiError } from '../../../src'
describe('ICXOrderBook.createOrder', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)
const icxSetup = new ICXSetup(container, client)

beforeAll(async () => {
await container.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { RpcApiError } from '../../../src'
describe('ICXOrderBook.getOrder', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)
const icxSetup = new ICXSetup(container, client)

beforeAll(async () => {
await container.start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BigNumber } from '@defichain/jellyfish-api-core'
import { ICXOrder, ICXGenericResult, ICXOrderInfo, ICXOfferInfo, ICXOffer } from '@defichain/jellyfish-api-core/category/icxorderbook'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { createToken, mintTokens, accountToAccount } from '@defichain/testing'
import { ContainerAdapterClient } from 'jellyfish-api-core/__tests__/container_adapter_client'

// globals
export let symbolDFI: string
Expand All @@ -14,8 +17,10 @@ export let DEX_DFI_PER_BTC_RATE: number

export class ICXSetup {
private readonly container: MasterNodeRegTestContainer
constructor (container: MasterNodeRegTestContainer) {
private readonly client: ContainerAdapterClient
constructor (container: MasterNodeRegTestContainer, client: ContainerAdapterClient) {
this.container = container
this.client = client
// reset global variables
symbolDFI = ''
symbolBTC = ''
Expand Down Expand Up @@ -107,4 +112,82 @@ export class ICXSetup {
expect(result.ICX_TAKERFEE_PER_BTC as number).toStrictEqual(fee)
ICX_TAKERFEE_PER_BTC = result.ICX_TAKERFEE_PER_BTC as number
}

// setup the flow until ICX offer for DFI sell order
async setupUntilDFIBuyOffer (): Promise<{order: ICXOrder, createOrderTxId: string, offer: ICXOffer, makeOfferTxId: string}> {
// create order - maker
const order: ICXOrder = {
tokenFrom: idDFI,
chainTo: 'BTC',
ownerAddress: accountDFI,
receivePubkey: '037f9563f30c609b19fd435a19b8bde7d6db703012ba1aba72e9f42a87366d1941',
amountFrom: new BigNumber(15),
orderPrice: new BigNumber(0.01)
}

let result: ICXGenericResult = await this.client.icxorderbook.createOrder(order)
const createOrderTxId = result.txid

await this.container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await this.client.icxorderbook.listOrders()
expect((orders as Record<string, ICXOrderInfo>)[createOrderTxId]).toStrictEqual(
{
status: 'OPEN',
type: '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 = {
orderTx: createOrderTxId,
amount: new BigNumber(0.10), // 0.10 BTC = 10 DFI
ownerAddress: accountBTC
}
const accountBTCBeforeOffer = await this.container.call('getaccount', [accountBTC, {}, true])

result = await this.client.icxorderbook.makeOffer(offer, [])
const makeOfferTxId = result.txid
await this.container.generate(1)

const accountBTCAfterOffer = await this.container.call('getaccount', [accountBTC, {}, true])

// check fee of 0.01 DFI has been reduced from the accountBTCBeforeOffer[idDFI]
// Fee = takerFeePerBTC(inBTC) * amount(inBTC) * DEX DFI per BTC rate
// NOTE(surangap): why sometimes garbage values are in expected, floting point representation problems?
expect(accountBTCAfterOffer[idDFI].toPrecision(8)).toStrictEqual((accountBTCBeforeOffer[idDFI] - 0.01).toPrecision(8))

// List the ICX offers for orderTx = createOrderTxId and check
orders = await this.client.icxorderbook.listOrders({ orderTx: createOrderTxId })
expect(Object.keys(orders).length).toBe(2) // extra entry for the warning text returned by the RPC atm.
expect((orders as Record<string, ICXOfferInfo>)[makeOfferTxId]).toStrictEqual(
{
orderTx: createOrderTxId,
status: '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)
}
)

return {
order: order,
createOrderTxId: createOrderTxId,
offer: offer,
makeOfferTxId: makeOfferTxId
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { accountBTC, accountDFI, DEX_DFI_PER_BTC_RATE, ICXSetup, ICX_TAKERFEE_PE
describe('ICXOrderBook.listOrders', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)
const icxSetup = new ICXSetup(container, client)

beforeAll(async () => {
await container.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { RpcApiError } from '@defichain/jellyfish-api-core'
describe('ICXOrderBook.makeOffer', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
const icxSetup = new ICXSetup(container)
const icxSetup = new ICXSetup(container, client)

beforeAll(async () => {
await container.start()
Expand Down
Loading

0 comments on commit 2f6acbd

Please sign in to comment.