Skip to content

Commit

Permalink
feat(jellyfish-api-core): defid v3 poolpair/poolswap internal changes (
Browse files Browse the repository at this point in the history
…#1854)

<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:

Add tests for new fixes on defid v3.
  • Loading branch information
shohamc1 authored Nov 22, 2022
1 parent a78d128 commit af7534a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Poolpair', () => {
const defaultMetadata = {
tokenA: 'DFI',
tokenB,
commission: 0,
commission: 0.02,
status: true,
ownerAddress: address
}
Expand All @@ -60,6 +60,33 @@ describe('Poolpair', () => {
await container.generate(1)
}

it('should give rewards after activation', async () => {
const shareAddress = await container.call('getnewaddress')

await client.poolpair.addPoolLiquidity({
'*': ['5@DFI', '100@DDAI']
}, shareAddress)
await container.generate(1)

const address = await container.getNewAddress()
await container.call('sendtokenstoaddress', [{}, { [address]: ['20@DFI'] }])
await container.generate(1)

await client.poolpair.poolSwap({
from: address,
tokenFrom: 'DFI',
amountFrom: 10,
to: address,
tokenTo: 'DDAI'
})
await container.generate(2)

const accountHistory = (await client.account.listAccountHistory('all')).filter((item) => {
return item.type === 'Commission'
})
expect(accountHistory).toHaveLength(1)
})

it('should addPoolLiquidity', async () => {
const shareAddress = await container.call('getnewaddress')
const data = await client.poolpair.addPoolLiquidity({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import BigNumber from 'bignumber.js'
import { RpcApiError } from '../../../src'

describe('Poolpair', () => {
const container = new MasterNodeRegTestContainer()
Expand Down Expand Up @@ -78,4 +79,19 @@ describe('Poolpair', () => {
}
expect(assertions).toStrictEqual(1)
})

it('should throw error if name is too long', async () => {
const address = await container.call('getnewaddress')
const metadata = {
tokenA: 'DBTC',
tokenB: 'DFI',
commission: 1,
status: true,
ownerAddress: address,
pairSymbol: 'abcdefghijklmnopqrt'
}
const promise = client.poolpair.createPoolPair(metadata)
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('pairSymbol is larger than')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,20 @@ describe('poolSwap asymmetric pool swap fee', () => {

await checkCATtoDFI(poolPairBefore, receiverAddress, feeInPct, feeOutPct)
})

it('should swap to sender if no recipient is specified', async () => {
await testing.fixture.createPoolPair({
a: { amount: 1000, symbol: 'CAT' },
b: { amount: 1000, symbol: 'DFI' }
})
const swapper = await testing.generateAddress()
await utxosToAccount(container, 1, { address: swapper })

await container.call('compositeswap', [{ from: swapper, tokenFrom: 'DFI', amountFrom: 1, tokenTo: 'CAT' }])
await container.generate(1)

const swapperAccount = await client.account.getAccount(swapper)
expect(swapperAccount[0]).toContain('CAT')
expect(swapperAccount[0]).not.toContain('DFI')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,42 @@ describe('Poolpair', () => {
}
})
})

it('should throw error when no tokenFrom', async () => {
const promise = client.poolpair.testPoolSwap({
from: await getNewAddress(container),
tokenFrom: '',
amountFrom: 13,
to: await getNewAddress(container),
tokenTo: 'DFI'
})

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -8,
message: 'tokenFrom is empty',
method: 'testpoolswap'
}
})
})

it('should throw error when no tokenTo', async () => {
const promise = client.poolpair.testPoolSwap({
from: await getNewAddress(container),
tokenFrom: 'DFI',
amountFrom: 13,
to: await getNewAddress(container),
tokenTo: ''
})

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toMatchObject({
payload: {
code: -8,
message: 'tokenTo is empty',
method: 'testpoolswap'
}
})
})
})

0 comments on commit af7534a

Please sign in to comment.