Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
izzycsy committed Jul 1, 2021
1 parent 93b5d4d commit 873d26c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 106 deletions.
14 changes: 12 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"devDependencies": {
"@size-limit/preset-app": "^4.10.1",
"@types/jest": "^26.0.22",
"@types/randombytes": "^2.0.0",
"husky": "^6.0.0",
"jest": "^26.6.3",
"lerna": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,112 +1,122 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { addPoolLiquidity, createPoolPair, createToken, getNewAddress, mintTokens } from '@defichain/testing'

describe('Poolpair', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
let poolLiquidityAddress: string

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

await createToken('DDAI')
await mintTokens('DDAI')
await createPoolPair('DDAI')
await addPoolLiquidity()
})

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

async function createToken (symbol: string): Promise<void> {
const address = await container.call('getnewaddress')
const metadata = {
symbol,
name: symbol,
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}
await container.call('createtoken', [metadata])
await container.generate(1)
}

async function createPoolPair (tokenB: string, metadata?: any): Promise<void> {
const address = await container.call('getnewaddress')
const defaultMetadata = {
tokenA: 'DFI',
tokenB: 'DDAI',
commission: 0,
status: true,
ownerAddress: address
}
await client.poolpair.createPoolPair({ ...defaultMetadata, ...metadata })
await container.generate(1)
}

async function mintTokens (symbol: string): Promise<void> {
const address = await container.call('getnewaddress')
const payload: { [key: string]: string } = {}
payload[address] = '100@0'
await container.call('utxostoaccount', [payload])
await container.call('minttokens', [`2000@${symbol}`])
await container.generate(1)
}

async function addPoolLiquidity (): Promise<void> {
const shareAddress = await container.call('getnewaddress')
const data = await client.poolpair.addPoolLiquidity({
'*': ['10@DFI', '200@DDAI']
}, shareAddress)
await container.generate(1)
it.only('should removeLiquidity', async () => {
const dogAddress = await getNewAddress(container)
const foxAddress = await getNewAddress(container)
// const poolPairAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)

await createToken(container, 'DOG')
await mintTokens(container, 'DOG', { address: dogAddress })

await createToken(container, 'FOX')
await mintTokens(container, 'FOX', { address: foxAddress })
const dogAccount = await container.call('getaccount', [dogAddress])
console.log('dogAccount: ', dogAccount)

await createPoolPair(container, 'DOG', 'FOX')
// const poolpairs = await container.call('listpoolpairs')
// console.log('poolpairs: ', poolpairs)
await addPoolLiquidity(container, {
tokenA: 'DOG',
amountA: 10,
tokenB: 'FOX',
amountB: 200,
shareAddress: poolLiquidityAddress
})
// totalLiquidity = sqrt(10 * 200) =
// const dogAccountAfter = await container.call('getaccount', [dogAddress])
// console.log('dogAccountAfter: ', dogAccountAfter)

expect(typeof data).toStrictEqual('string')
}
const poolLiquidityAccount = await container.call('getaccount', [poolLiquidityAddress])
console.log('poolLiquidityAccountBefore: ', poolLiquidityAccount)

it('should removePoolLiquidity', async () => {
const poolPairBefore = await container.call('listpoolpairs')
console.log('poolpairs: ', poolPairBefore)
console.log('poolPairBefore: ', poolPairBefore)
const totalLiquidityBefore = poolPairBefore['3'].totalLiquidity
const hash = await client.poolpair.removePoolLiquidity(poolLiquidityAddress, '13@DOG-FOX')

const totalLiquidityBefore = poolPairBefore['2'].totalLiquidity

try {
await container.call('removepoolliquidity', [poolLiquidityAddress, '13@DFI-DDAI'])
await container.generate(1)
} catch (err) {
console.log('err: ', err)
}
expect(typeof hash).toStrictEqual('string')
expect(hash.length).toStrictEqual(64)
await container.generate(1)

const poolPairAfter = await container.call('listpoolpairs')
console.log('poolpairs: ', poolPairAfter)
const totalLiquidityAfter = poolPairAfter['2'].totalLiquidity
console.log('poolPairAfter: ', poolPairAfter)
console.log('poolLiquidityAddress: ', poolLiquidityAddress)

// expect(poolPairAfter['2'].totalLiquidity - poolPairBefore['2'].totalLiquidity.toStrictEqual(13))
expect(totalLiquidityBefore - totalLiquidityAfter).toStrictEqual(0) // 13
})
const poolLiquidityAccountAfter = await container.call('getaccount', [poolLiquidityAddress])
console.log('poolLiquidityAccountAfter: ', poolLiquidityAccountAfter)

it('should fail while removePoolLiquidity with utxos which does not include account owner', async () => {
const shareAddress = await container.call('getnewaddress')
const tokenAAddress = await container.call('getnewaddress')
const tokenBAddress = await container.call('getnewaddress')

const utxos = await container.call('listunspent')
const inputs = utxos.map((utxo: any) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
const dogAccountAfter = await container.call('getaccount', [dogAddress])
const foxAccountAfter = await container.call('getaccount', [foxAddress])

console.log('dogAccountAfter: ', dogAccountAfter, foxAccountAfter)

const promise = client.poolpair.removePoolLiquidity({
[tokenAAddress]: '10@DFI',
[tokenBAddress]: '200@DDAI'
}, shareAddress, { utxos: inputs })
const totalLiquidityAfter = poolPairAfter['3'].totalLiquidity
console.log('totalLiquidityAfter: ', totalLiquidityAfter)
console.log('totalLiquidityDiff: ', totalLiquidityBefore - totalLiquidityAfter)

expect(typeof promise).toStrictEqual('object')
expect(totalLiquidityBefore - totalLiquidityAfter).toStrictEqual(13)
})

// it('should removeLiquidity with utxos', async () => {
// const shareAddress = await container.call('getnewaddress')
// const tokenAAddress = await container.call('getnewaddress', '10@DDFI')
// const tokenBAddress = await container.call('getnewaddress', '200@DBTC')
// const utxos = await container.call('listunspent')
// await container.generate(1)
//
// const inputs = utxos.map((utxo: any) => {
// return {
// txid: utxo.txid,
// vout: utxo.vout
// }
// })
//
// const hash = await client.poolpair.removePoolLiquidity({
// [tokenAAddress]: '10@DOG',
// [tokenBAddress]: '200@FOX'
// }, shareAddress, { utxos: inputs })
//
// expect(typeof hash).toStrictEqual('string')
// expect(hash.length).toStrictEqual(64)
// })

// it('fail as utxos does not include an account owner', async () => {
// const shareAddress = await container.call('getnewaddress')
// const tokenAAddress = await container.call('getnewaddress')
// const tokenBAddress = await container.call('getnewaddress')
// const utxos = await container.call('listunspent')
//
// const inputs = utxos.map((utxo: any) => {
// return {
// txid: utxo.txid,
// vout: utxo.vout
// }
// })
//
// const hash = await client.poolpair.removePoolLiquidity({
// [tokenAAddress]: '10@DOG',
// [tokenBAddress]: '200@FOX'
// }, shareAddress, { utxos: inputs })
//
// expect(typeof hash).rejects.toThrow('tx must have at least one input from account owner')
// })
})
5 changes: 2 additions & 3 deletions packages/jellyfish-api-core/src/category/poolpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ export class PoolPair {
/**
* Remove pool liquidity transaction
*
* @param {RemovePoolLiquiditySource} from pool liquidity sources
* @param {string | string[]} from[address] provides at least two types of token with format 'amoun@token'
* @param {string} shareAddress defi address for crediting tokens
* @param {string} address defi address for crediting tokens
* @param {string} poolAccount pool liquidity account of owner
* @param {RemovePoolLiquidityOptions} [options]
* @param {RemovePoolLiquidityUTXO[]} [options.utxos] utxos array of specific UTXOs to spend
* @param {string} [options.utxos.txid]
Expand Down
19 changes: 0 additions & 19 deletions website/docs/jellyfish/api/poolpair.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,6 @@ interface PoolShareOptions {
}
```

## removePoolLiquidity

Remove pool liquidity transaction

```ts title="client.poolpair.addPoolLiquidity()"
interface poolpair {
removePoolLiquidity (from: removePoolLiquiditySource, shareAddress: string, options: removePoolLiquidityOptions = {}): Promise<string>
}

interface RemovePoolLiquidityOptions {
utxos?: RemovePoolLiquidityUTXO[]
}

interface RemovePoolLiquidityUTXO {
txid: string
vout: number
}
```

## testPoolSwap

Create a test pool swap transaction to check pool swap's return result
Expand Down

0 comments on commit 873d26c

Please sign in to comment.