Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
add vault bot (#289)
Browse files Browse the repository at this point in the history
* fix broken tests

* fix broken tests
speed up playground setup with reduced container.generate() calls

* add vault bot

* fixed wallet.test.ts
  • Loading branch information
fuxingloh authored Nov 22, 2021
1 parent 3c3faad commit a82f6b1
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 19 deletions.
20 changes: 1 addition & 19 deletions packages/playground-api-client/__tests__/api/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,7 @@ afterAll(async () => {
it('should get wallet', async () => {
const wallet = await client.wallet.balances()

expect(wallet).toStrictEqual({
balance: expect.any(Number),
tokens: [
{ id: '1', balance: expect.any(Number) },
{ id: '2', balance: expect.any(Number) },
{ id: '3', balance: expect.any(Number) },
{ id: '4', balance: expect.any(Number) },
{ id: '5', balance: expect.any(Number) },
{ id: '6', balance: expect.any(Number) },
{ id: '7', balance: expect.any(Number) },
{ id: '8', balance: expect.any(Number) },
{ id: '9', balance: expect.any(Number) },
{ id: '15', balance: expect.any(Number) },
{ id: '16', balance: expect.any(Number) },
{ id: '17', balance: expect.any(Number) },
{ id: '18', balance: expect.any(Number) },
{ id: '19', balance: expect.any(Number) }
]
})
expect(wallet.tokens.length).toBeGreaterThan(10)
})

describe('tokens', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/module.playground/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { OracleBot } from './bot/oracle.bot'
import { SetupLoanScheme } from '@src/module.playground/setup/setup.loan.scheme'
import { SetupLoanToken } from '@src/module.playground/setup/setup.loan.token'
import { SetupLoanCollateral } from '@src/module.playground/setup/setup.loan.collateral'
import { VaultBot } from '@src/module.playground/bot/vault.bot'

@Global()
@Module({
Expand All @@ -26,6 +27,7 @@ import { SetupLoanCollateral } from '@src/module.playground/setup/setup.loan.col
SetupLoanToken,
SetupLoanCollateral,
OracleBot,
VaultBot,
PlaygroundBlock,
PlaygroundProbeIndicator
],
Expand Down
42 changes: 42 additions & 0 deletions src/module.playground/bot/vault.bot.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PlaygroundTesting } from '@src/e2e.module'
import waitForExpect from 'wait-for-expect'
import { PlaygroundProbeIndicator } from '@src/module.playground/playground.indicator'

const testing = new PlaygroundTesting()

/* eslint-disable @typescript-eslint/no-non-null-assertion */

beforeAll(async () => {
await testing.start()

await waitForExpect(() => {
expect(testing.app.get(PlaygroundProbeIndicator).ready).toBeTruthy()
})
})

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

it('should have a vault', async () => {
await waitForExpect(async () => {
const vaults = await testing.jsonRpcClient!.loan.listVaults()
expect(vaults.length).toBeGreaterThan(0)
}, 40000)
})

it('should have liquidity in DUSD-DFI pool', async () => {
await waitForExpect(async () => {
const pairsResult = await testing.jsonRpcClient!.poolpair.getPoolPair('DUSD-DFI')
expect(Object.values(pairsResult)[0].reserveA.gt(0)).toBeTruthy()
expect(Object.values(pairsResult)[0].reserveB.gt(0)).toBeTruthy()
}, 40000)
})

it('should have liquidity in TS25-DUSD pool', async () => {
await waitForExpect(async () => {
const pairsResult = await testing.jsonRpcClient!.poolpair.getPoolPair('TS25-DUSD')
expect(Object.values(pairsResult)[0].reserveA.gt(0)).toBeTruthy()
expect(Object.values(pairsResult)[0].reserveB.gt(0)).toBeTruthy()
}, 40000)
})
49 changes: 49 additions & 0 deletions src/module.playground/bot/vault.bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Injectable } from '@nestjs/common'
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import { Interval } from '@nestjs/schedule'
import { PlaygroundSetup } from '@src/module.playground/setup/setup'

@Injectable()
export class VaultBot {
private vaultId?: string

constructor (protected readonly client: JsonRpcClient) {
}

@Interval(6000)
async run (): Promise<void> {
if (this.vaultId === undefined) {
this.vaultId = await this.client.loan.createVault({
loanSchemeId: 'MIN200',
ownerAddress: PlaygroundSetup.address
})
return
}

await this.client.account.utxosToAccount({
[PlaygroundSetup.address]: '20@0'
})

await this.client.loan.depositToVault({
amount: '10@DFI',
from: PlaygroundSetup.address,
vaultId: this.vaultId
})

await this.client.loan.takeLoan({
amounts: [
'20@DUSD',
'0.1@TS25'
],
to: PlaygroundSetup.address,
vaultId: this.vaultId
})

await this.client.poolpair.addPoolLiquidity({
'*': ['1@DFI', '10@DUSD']
}, PlaygroundSetup.address)
await this.client.poolpair.addPoolLiquidity({
'*': ['2.5@DUSD', '0.1@TS25']
}, PlaygroundSetup.address)
}
}

0 comments on commit a82f6b1

Please sign in to comment.