Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add removePoolLiquidity RPC #420

Merged
merged 10 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'

describe('Poolpair', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)
let poolLiquidityAddress: string
izzycsy marked this conversation as resolved.
Show resolved Hide resolved

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> {
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
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)

expect(typeof data).toStrictEqual('string')
}

it('should removePoolLiquidity', async () => {
const poolPairBefore = await container.call('listpoolpairs')
console.log('poolpairs: ', poolPairBefore)
izzycsy marked this conversation as resolved.
Show resolved Hide resolved

const totalLiquidityBefore = poolPairBefore['2'].totalLiquidity

try {
await container.call('removepoolliquidity', [poolLiquidityAddress, '13@DFI-DDAI'])
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
await container.generate(1)
} catch (err) {
console.log('err: ', err)
}
canonbrother marked this conversation as resolved.
Show resolved Hide resolved

const poolPairAfter = await container.call('listpoolpairs')
console.log('poolpairs: ', poolPairAfter)
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
const totalLiquidityAfter = poolPairAfter['2'].totalLiquidity

// expect(poolPairAfter['2'].totalLiquidity - poolPairBefore['2'].totalLiquidity.toStrictEqual(13))
expect(totalLiquidityBefore - totalLiquidityAfter).toStrictEqual(0) // 13
canonbrother marked this conversation as resolved.
Show resolved Hide resolved
})

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 promise = client.poolpair.removePoolLiquidity({
[tokenAAddress]: '10@DFI',
[tokenBAddress]: '200@DDAI'
}, shareAddress, { utxos: inputs })

expect(typeof promise).toStrictEqual('object')
canonbrother marked this conversation as resolved.
Show resolved Hide resolved
})
})
30 changes: 30 additions & 0 deletions packages/jellyfish-api-core/src/category/poolpair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ export class PoolPair {
async testPoolSwap (metadata: TestPoolSwapMetadata): Promise<string> {
return await this.client.call('testpoolswap', [metadata], 'bignumber')
}

/**
* 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'
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
* @param {string} shareAddress defi address for crediting tokens
* @param {RemovePoolLiquidityOptions} [options]
* @param {RemovePoolLiquidityUTXO[]} [options.utxos] utxos array of specific UTXOs to spend
* @param {string} [options.utxos.txid]
* @param {number} [options.utxos.vout]
* @return {Promise<string>}
*/
async removePoolLiquidity (from: RemovePoolLiquiditySource, shareAddress: string, options: RemovePoolLiquidityOptions = {}): Promise<string> {
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
const { utxos } = options
return await this.client.call('removepoolliquidity', [from, shareAddress, utxos], 'bignumber')
}
}

export interface CreatePoolPairMetadata {
Expand Down Expand Up @@ -206,3 +223,16 @@ export interface TestPoolSwapMetadata {
tokenTo: string
maxPrice?: number
}

export interface RemovePoolLiquiditySource {
[address: string]: string | string[]
}

export interface RemovePoolLiquidityOptions {
utxos?: RemovePoolLiquidityUTXO[]
}

export interface RemovePoolLiquidityUTXO {
txid: string
vout: number
}
25 changes: 24 additions & 1 deletion website/docs/jellyfish/api/poolpair.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ interface PoolShareOptions {
}
```

## removePoolLiquidity

Remove pool liquidity transaction

```ts title="client.poolpair.addPoolLiquidity()"
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
interface poolpair {
removePoolLiquidity (from: removePoolLiquiditySource, shareAddress: string, options: removePoolLiquidityOptions = {}): Promise<string>
izzycsy marked this conversation as resolved.
Show resolved Hide resolved
}

interface RemovePoolLiquiditySource {
[address: string]: string | 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 All @@ -180,4 +203,4 @@ interface TestPoolSwapMetadata {
tokenTo: string
maxPrice?: number
}
```
```