Skip to content

Commit

Permalink
Refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Jun 28, 2021
1 parent eb2dd6e commit 0c224ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ICXGenericResult, ICXOfferInfo, ICXOrderInfo, ICXOffer, ICXOrder, UTXO, ICXOrderStatus, ICXOrderType } from '../../../src/category/icxorderbook'
import BigNumber from 'bignumber.js'
import { accountDFI, idDFI, accountBTC, ICXSetup, symbolDFI, symbolBTC, ICX_TAKERFEE_PER_BTC, DEX_DFI_PER_BTC_RATE } from './icx_setup'
import { accountDFI, idDFI, accountBTC, ICXSetup, symbolDFI, ICX_TAKERFEE_PER_BTC, DEX_DFI_PER_BTC_RATE } from './icx_setup'
import { RpcApiError } from '../../../src'

describe('ICXOrderBook.closeOffer', () => {
Expand All @@ -29,10 +29,6 @@ describe('ICXOrderBook.closeOffer', () => {
await container.stop()
})

afterEach(async () => {
// cleanup code here
})

it('should close an offer', async () => {
// create order - maker
const order: ICXOrder = {
Expand All @@ -48,12 +44,12 @@ describe('ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
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,
tokenFrom: symbolDFI,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
Expand All @@ -66,13 +62,13 @@ describe('ICXOrderBook.closeOffer', () => {
}
)

// make offer to partial amout 10 DFI - taker
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])
// make offer to partial amount 10 DFI - taker
const offer: ICXOffer = {
orderTx: createOrderTxId,
amount: new BigNumber(0.1), // 10 DFI = 0.1 BTC
ownerAddress: accountBTC
}
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])
result = await client.icxorderbook.makeOffer(offer, [])
const makeOfferTxId = result.txid
await container.generate(1)
Expand Down Expand Up @@ -125,12 +121,12 @@ describe('ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
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,
tokenFrom: symbolDFI,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
Expand All @@ -143,13 +139,14 @@ describe('ICXOrderBook.closeOffer', () => {
}
)

// make offer to partial amout 10 DFI - taker
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])
// make offer to partial amount 10 DFI - taker
const offer: ICXOffer = {
orderTx: createOrderTxId,
amount: new BigNumber(0.1), // 10 DFI = 0.1 BTC
ownerAddress: accountBTC
}
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])

result = await client.icxorderbook.makeOffer(offer, [])
const makeOfferTxId = result.txid
await container.generate(1)
Expand Down Expand Up @@ -210,12 +207,12 @@ describe('ICXOrderBook.closeOffer', () => {
await container.generate(1)

// list ICX orders
let orders: Record<string, ICXOrderInfo| ICXOfferInfo> = await client.call('icx_listorders', [], 'bignumber')
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,
tokenFrom: symbolDFI,
chainTo: order.chainTo,
receivePubkey: order.receivePubkey,
ownerAddress: order.ownerAddress,
Expand All @@ -228,13 +225,14 @@ describe('ICXOrderBook.closeOffer', () => {
}
)

// make offer to partial amout 10 DFI - taker
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])
// make offer to partial amount 10 DFI - taker
const offer: ICXOffer = {
orderTx: createOrderTxId,
amount: new BigNumber(0.1), // 10 DFI = 0.1 BTC
ownerAddress: accountBTC
}
const accountBTCBeforeOffer = await container.call('getaccount', [accountBTC, {}, true])

result = await client.icxorderbook.makeOffer(offer, [])
const makeOfferTxId = result.txid
await container.generate(1)
Expand All @@ -259,11 +257,11 @@ describe('ICXOrderBook.closeOffer', () => {
}
)

// close offer "123" - taker
const promise = client.icxorderbook.closeOffer('123')
// close offer "INVALID_OFFER_TX_ID" - taker
const promise = client.icxorderbook.closeOffer('INVALID_OFFER_TX_ID')

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('RpcApiError: \'OfferTx (0000000000000000000000000000000000000000000000000000000000000123) does not exist\', code: -8, method: icx_closeoffer')
await expect(promise).rejects.toThrow('RpcApiError: \'OfferTx (0000000000000000000000000000000000000000000000000000000000000000) does not exist\', code: -8, method: icx_closeoffer')

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

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

interface UTXO {
Expand Down

0 comments on commit 0c224ff

Please sign in to comment.