Skip to content

Commit

Permalink
@defichain/testing new add/remove PoolLiquidity for testing (#235)
Browse files Browse the repository at this point in the history
* @defichain/testing added add/remove PoolLiquidity for testing

* changed to use options for poolpair
  • Loading branch information
fuxingloh authored May 17, 2021
1 parent 61f46e9 commit 459629d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 38 deletions.
9 changes: 8 additions & 1 deletion .idea/dictionaries/fuxing.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 51 additions & 32 deletions packages/testing/__tests__/poolpair.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import {
createToken,
createPoolPair
} from '../src'
import { createToken, createPoolPair, addPoolLiquidity, mintTokens, utxosToAccount, removePoolLiquidity } from '../src'

describe('utils', () => {
const container = new MasterNodeRegTestContainer()
const container = new MasterNodeRegTestContainer()

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

afterAll(async () => {
await container.stop()
})

describe('createPoolPair', () => {
beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(300)
await createToken(container, 'DOG')
})

afterAll(async () => {
await container.stop()
})
it('should createPoolPair', async () => {
const before = await container.call('listpoolpairs')

describe('createPoolPair', () => {
beforeAll(async () => {
await createToken(container, 'DOG')
})
const txid = await createPoolPair(container, 'DFI', 'DOG')
expect(typeof txid).toBe('string')

it('should createPoolPair', async () => {
let assertions = 0
const after: Record<string, any> = await container.call('listpoolpairs')
expect(Object.keys(after).length).toBe(Object.keys(before).length + 1)

const poolpairsBefore = await container.call('listpoolpairs')
const poolpairsLengthBefore = Object.keys(poolpairsBefore).length
for (const poolpair of Object.values(after)) {
expect(poolpair.name).toBe('Default Defi token-DOG')
}

const data = await createPoolPair(container, 'DFI', 'DOG')
expect(typeof data).toBe('string')
expect.assertions(3)
})
})

describe('add/remove pool pair liquidity', () => {
beforeAll(async () => {
await createToken(container, 'LPT')
await mintTokens(container, 'LPT', { mintAmount: 100 })
await utxosToAccount(container, 100)
await createPoolPair(container, 'DFI', 'LPT')
})

const poolpairsAfter = await container.call('listpoolpairs')
expect(Object.keys(poolpairsAfter).length).toBe(poolpairsLengthBefore + 1)
it('should add and remove liquidity', async () => {
const address = await container.getNewAddress()
const amount = await addPoolLiquidity(container, {
tokenA: 'DFI',
amountA: 50,
tokenB: 'LPT',
amountB: 50,
shareAddress: address
})
expect(amount.toFixed(8)).toBe('49.99999000')

for (const k in poolpairsAfter) {
const poolpair = poolpairsAfter[k]
if (poolpair.name === 'Default Defi token-DOG') {
assertions += 1
}
}
expect(assertions).toBe(1)
await removePoolLiquidity(container, {
address: address,
amountLP: amount,
tokenLP: 'DFI-LPT'
})

const shares: Record<string, any> = await container.call('listpoolshares')
expect(Object.keys(shares).length).toBe(0)
})
})
5 changes: 4 additions & 1 deletion packages/testing/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { getNewAddress } from './wallet'

/**
* send utxos to account
* Send utxos to account.
* This method will also ensure there is enough UTXO to be send to the address.
*
* @param {MasterNodeRegTestContainer} container
* @param {number} amount
Expand All @@ -15,6 +16,8 @@ export async function utxosToAccount (
amount: number,
options?: UtxosToAccountOptions
): Promise<void> {
await container.waitForWalletBalanceGTE(amount + 0.1)

const address = options?.address ?? await getNewAddress(container)
const payload: { [key: string]: string } = {}
payload[address] = `${amount.toString()}@0`
Expand Down
68 changes: 65 additions & 3 deletions packages/testing/src/poolpair.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js'
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { CreatePoolPairUTXO } from '@defichain/jellyfish-api-core/dist/category/poolpair'
import { getNewAddress } from './wallet'
Expand Down Expand Up @@ -27,10 +28,9 @@ export async function createPoolPair (
status: options?.status ?? true,
ownerAddress: options?.ownerAddress ?? await getNewAddress(container)
}
const hashed = await container.call('createpoolpair', [metadata, options?.utxos])
const txid = await container.call('createpoolpair', [metadata, options?.utxos])
await container.generate(1)

return hashed
return txid
}

export interface CreatePoolPairOptions {
Expand All @@ -39,3 +39,65 @@ export interface CreatePoolPairOptions {
ownerAddress?: string
utxos?: CreatePoolPairUTXO[]
}

/**
* @param {MasterNodeRegTestContainer} container
* @param {AddPoolLiquidity} options
* @param {string} options.tokenA pair token symbol
* @param {number} options.amountA pair amount
* @param {string} options.tokenB pair token symbol
* @param {number} options.amountB pair amount
* @param {string} options.shareAddress for LP
* @return {Promise<BigNumber>} amount LP token share
*/
export async function addPoolLiquidity (
container: MasterNodeRegTestContainer,
options: AddPoolLiquidity
): Promise<BigNumber> {
const { amountA, amountB, tokenA, tokenB, shareAddress } = options
const from = { '*': [`${amountA}@${tokenA}`, `${amountB}@${tokenB}`] }
await container.call('addpoolliquidity', [from, shareAddress])
await container.generate(1)

const tokens: string[] = await container.call('getaccount', [shareAddress])
const lpToken = tokens.find(value => value.endsWith(`@${tokenA}-${tokenB}`))
if (lpToken === undefined) {
throw new Error('LP token not found in account')
}

const amount = lpToken.replace(`@${tokenA}-${tokenB}`, '')
return new BigNumber(amount)
}

export interface AddPoolLiquidity {
tokenA: string
amountA: number
tokenB: string
amountB: number
shareAddress: string
}

/**
* @param {MasterNodeRegTestContainer} container
* @param {RemovePoolLiquidity} options
* @param {string} options.address which has the LP token
* @param {string} options.tokenLP to remove
* @param {BigNumber} options.amountLP to remove
* @return {Promise<string>} txid
*/
export async function removePoolLiquidity (
container: MasterNodeRegTestContainer,
options: RemovePoolLiquidity
): Promise<string> {
const { address, tokenLP, amountLP } = options
const amount = `${amountLP.toFixed(8)}@${tokenLP}`
const txid = await container.call('removepoolliquidity', [address, amount])
await container.generate(1)
return txid
}

export interface RemovePoolLiquidity {
address: string
tokenLP: string
amountLP: BigNumber
}
3 changes: 2 additions & 1 deletion packages/testing/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { getNewAddress } from './wallet'
import { utxosToAccount } from './account'

/**
* Create a new token and return id of token
* Create a new token and return id of token.
* This method will ensure there is enough DFI to create a token.
*
* @param {MasterNodeRegTestContainer} container
* @param {string} symbol
Expand Down

0 comments on commit 459629d

Please sign in to comment.