-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 92 additions & 82 deletions
174
packages/jellyfish-api-core/__tests__/category/account/removePoolLiquidity.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
// }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters