Skip to content

Commit

Permalink
Interface icx_submitdfchtlc RPC (#404)
Browse files Browse the repository at this point in the history
* Added ICXOrderBook.submitDFCHTLC() function, testing and documentation
  • Loading branch information
surangap authored Jul 6, 2021
1 parent 32bb9b0 commit e397609
Show file tree
Hide file tree
Showing 9 changed files with 682 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, UTXO, ICXOrderStatus } from '../../../src/category/icxorderbook'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXOrderStatus } from '../../../src/category/icxorderbook'
import BigNumber from 'bignumber.js'
import { accountDFI, idDFI, accountBTC, ICXSetup, symbolDFI } from './icx_setup'
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 Expand Up @@ -175,15 +175,10 @@ describe('ICXOrderBook.closeOffer', () => {
expect((ordersAfterMakeOffer as Record<string, ICXOfferInfo>)[makeOfferTxId].status).toStrictEqual(ICXOrderStatus.OPEN)

// input utxos
const utxos = await container.call('listunspent', [1, 9999999, [accountBTC], true])
const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
const inputUTXOs = await container.fundAddress(accountBTC, 10)

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

// List the ICX offers for orderTx = createOrderTxId and check no more offers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import {
ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOrder, UTXO, ICXOrderStatus, ICXOrderType
ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOrder, ICXOrderStatus, ICXOrderType
} from '../../../src/category/icxorderbook'
import BigNumber from 'bignumber.js'
import { accountBTC, accountDFI, ICXSetup, idDFI, symbolDFI } from './icx_setup'
Expand All @@ -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 Expand Up @@ -120,14 +120,9 @@ describe('ICXOrderBook.createOrder', () => {
}

// input utxos
const utxos = await container.call('listunspent', [1, 9999999, [accountDFI], true])
const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
const result: ICXGenericResult = await client.icxorderbook.createOrder(order, inputUTXOs)
const inputUTXOs = await container.fundAddress(accountDFI, 10)

const result: ICXGenericResult = await client.icxorderbook.createOrder(order, [inputUTXOs])
const createOrderTxId = result.txid
await container.generate(1)

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, ICXOrderStatus } from '../../../src/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 @@ -91,13 +96,13 @@ export class ICXSetup {
expect(pool[idDFIBTC].idTokenB).toBe(idDFI)
}

async addLiquidityToBTCDFIPool (amountInBTC: number, amoutInDFI: number): Promise<void> {
async addLiquidityToBTCDFIPool (amountInBTC: number, amountInDFI: number): Promise<void> {
const poolLiquidityMetadata: { [key: string]: string [] } = {}
poolLiquidityMetadata[accountDFI] = [`${amountInBTC}@${symbolBTC}`, `${amoutInDFI}@${symbolDFI}`]
poolLiquidityMetadata[accountDFI] = [`${amountInBTC}@${symbolBTC}`, `${amountInDFI}@${symbolDFI}`]

await this.container.call('addpoolliquidity', [poolLiquidityMetadata, accountDFI, []])
await this.container.generate(1)
DEX_DFI_PER_BTC_RATE = amoutInDFI / amountInBTC
DEX_DFI_PER_BTC_RATE = amountInDFI / amountInBTC
}

async setTakerFee (fee: number): Promise<void> {
Expand All @@ -120,4 +125,60 @@ export class ICXSetup {
}
await this.container.generate(1)
}

// creates DFI sell order
async createDFISellOrder (chainTo: string, ownerAddress: string, receivePubkey: string, amountFrom: BigNumber, orderPrice: BigNumber): Promise<{order: ICXOrder, createOrderTxId: string}> {
// create order - maker
const order: ICXOrder = {
tokenFrom: idDFI,
chainTo: chainTo,
ownerAddress: ownerAddress,
receivePubkey: receivePubkey,
amountFrom: amountFrom,
orderPrice: orderPrice
}

const createOrderResult: ICXGenericResult = await this.client.icxorderbook.createOrder(order)
const createOrderTxId = createOrderResult.txid
await this.container.generate(1)

// list ICX orders
const ordersAfterCreateOrder: Record<string, ICXOrderInfo | ICXOfferInfo> = await this.client.icxorderbook.listOrders()
expect((ordersAfterCreateOrder as Record<string, ICXOrderInfo>)[createOrderTxId].status).toStrictEqual(ICXOrderStatus.OPEN)

return {
order: order,
createOrderTxId: createOrderTxId
}
}

// creates DFI buy offer
async createDFIBuyOffer (orderTx: string, amount: BigNumber, ownerAddress: string): Promise<{offer: ICXOffer, makeOfferTxId: string}> {
const accountBTCBeforeOffer: Record<string, BigNumber> = await this.client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// make Offer to partial amount 10 DFI - taker
const offer: ICXOffer = {
orderTx: orderTx,
amount: amount, // 0.10 BTC = 10 DFI
ownerAddress: ownerAddress
}

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

const accountBTCAfterOffer: Record<string, BigNumber> = await this.client.call('getaccount', [accountBTC, {}, true], 'bignumber')
// check fee of 0.01 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.01))

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

return {
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
@@ -1,14 +1,14 @@
import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXOrderStatus, ICXOrderType, UTXO } from '../../../src/category/icxorderbook'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, ICXOrderStatus, ICXOrderType } from '../../../src/category/icxorderbook'
import BigNumber from 'bignumber.js'
import { ICXSetup, symbolDFI, accountDFI, idDFI, accountBTC, ICX_TAKERFEE_PER_BTC, DEX_DFI_PER_BTC_RATE } from './icx_setup'
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 Expand Up @@ -277,16 +277,11 @@ describe('ICXOrderBook.makeOffer', () => {
}

// input utxos
const utxos = await container.call('listunspent', [1, 9999999, [accountBTC], true])
const inputUTXOs: UTXO[] = utxos.map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
const inputUTXOs = await container.fundAddress(accountBTC, 10)

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

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

Expand Down
Loading

0 comments on commit e397609

Please sign in to comment.