From d88ea12405701c44fffff302a45eef0bba355587 Mon Sep 17 00:00:00 2001 From: canonbrother Date: Thu, 31 Mar 2022 11:23:43 +0800 Subject: [PATCH] feat(apps/playground): setgov for enabling payback to all tokens (#1299) * add setgovbot, enable payback all tokens * store tokenIds from colToken and loanToken, start paybackbot on loantoken setup * rm setgov setup * rm govset test * add govbot test * rm unuse changes * rm interval, gen(1) * update desc * add interval * enhance test * setgov via setup flow, fix loan token id random order --- .../modules/PlaygroundModule.test.ts | 140 +++++++++++++++++- apps/playground-api/src/setups/setup.gov.ts | 62 ++++++-- .../src/setups/setup.loan.token.ts | 1 + 3 files changed, 184 insertions(+), 19 deletions(-) diff --git a/apps/playground-api/__tests__/modules/PlaygroundModule.test.ts b/apps/playground-api/__tests__/modules/PlaygroundModule.test.ts index 93919703ec..59dfd64a61 100644 --- a/apps/playground-api/__tests__/modules/PlaygroundModule.test.ts +++ b/apps/playground-api/__tests__/modules/PlaygroundModule.test.ts @@ -1,13 +1,14 @@ import { PlaygroundApiTesting } from '../../testing/PlaygroundApiTesting' -const testing = PlaygroundApiTesting.create() +const pg = PlaygroundApiTesting.create() +const testing = pg.testing beforeAll(async () => { - await testing.start() + await pg.start() }) afterAll(async () => { - await testing.stop() + await pg.stop() }) it('should have pool pairs setup', async () => { @@ -57,12 +58,135 @@ it('should have loan collateral tokens', async () => { }) it('should have gov set', async () => { - const dusdInfo = await testing.container.call('gettoken', ['DUSD']) - const dusdId = Object.keys(dusdInfo)[0] const gov = await testing.container.call('getgov', ['ATTRIBUTES']) expect(gov).toStrictEqual({ - ATTRIBUTES: { - [`v0/token/${dusdId}/payback_dfi`]: 'true' - } + ATTRIBUTES: expect.objectContaining({ + 'v0/token/12/payback_dfi': 'true', + 'v0/token/12/payback_dfi_fee_pct': '0.01', + 'v0/token/12/loan_payback/1': 'true', + 'v0/token/12/loan_payback/14': 'true', + 'v0/token/12/loan_payback_fee_pct/1': '0.01', + 'v0/token/12/loan_payback_fee_pct/14': '0.01', + 'v0/token/13/loan_payback/6': 'true', + 'v0/token/13/loan_payback/12': 'true', + 'v0/token/13/loan_payback_fee_pct/6': '0.01', + 'v0/token/13/loan_payback_fee_pct/12': '0.01', + 'v0/token/14/loan_payback/1': 'true', + 'v0/token/14/loan_payback/12': 'true', + 'v0/token/14/loan_payback_fee_pct/1': '0.01', + 'v0/token/14/loan_payback_fee_pct/12': '0.01', + 'v0/token/15/loan_payback/13': 'true', + 'v0/token/15/loan_payback_fee_pct/13': '0.01', + 'v0/token/16/payback_dfi': 'true', + 'v0/token/16/payback_dfi_fee_pct': '0.01', + 'v0/token/16/loan_payback/12': 'true', + 'v0/token/16/loan_payback/14': 'true', + 'v0/token/16/loan_payback_fee_pct/12': '0.01', + 'v0/token/16/loan_payback_fee_pct/14': '0.01' + }) + }) + + // sanity test + const dusdInfo = await testing.rpc.token.getToken('DUSD') + const dusdId = Object.keys(dusdInfo)[0] + const td10Info = await testing.rpc.token.getToken('TD10') + const td10Id = Object.keys(td10Info)[0] + const tr50Info = await testing.rpc.token.getToken('TR50') + const tr50Id = Object.keys(tr50Info)[0] + const tu10Info = await testing.rpc.token.getToken('TU10') + const tu10Id = Object.keys(tu10Info)[0] + const ts25Info = await testing.rpc.token.getToken('TS25') + const ts25Id = Object.keys(ts25Info)[0] + + const colAddr = await testing.container.getNewAddress() + await testing.token.dfi({ address: colAddr, amount: 15000 }) + await testing.container.call('sendtokenstoaddress', [{}, { [colAddr]: ['1@BTC', '1@ETH', '1@USDT', '1@CU10', '1@CD10'] }]) + await testing.generate(1) + + const vaultId = await testing.rpc.loan.createVault({ + ownerAddress: await testing.container.getNewAddress(), + loanSchemeId: 'MIN150' + }) + await testing.generate(1) + + await testing.rpc.loan.depositToVault({ + vaultId: vaultId, + from: colAddr, + amount: '10000@DFI' + }) + await testing.generate(1) + + await testing.container.waitForPriceValid('TD10/USD') + await testing.container.waitForPriceValid('TU10/USD') + await testing.container.waitForPriceValid('TR50/USD') + await testing.container.waitForPriceValid('TS25/USD') + + await testing.rpc.loan.takeLoan({ + vaultId: vaultId, + to: colAddr, + amounts: ['100@DUSD', '0.00000095@TD10', '0.06@TR50', '3@TU10', '5@TS25'] + }) + await testing.generate(1) + + // DFI pay dToken + await testing.rpc.loan.paybackLoan({ + vaultId: vaultId, + from: colAddr, + loans: [ + // { dToken: td10Id, amounts: '0.00000001@DFI' }, // x pay + // { dToken: tu10Id, amounts: '0.00000001@DFI' }, // x pay + // { dToken: ts25Id, amounts: '0.00000001@DFI' }, // x pay + { dToken: dusdId, amounts: '0.00000001@DFI' }, + { dToken: tr50Id, amounts: '0.00000001@DFI' } + ] + }) + await testing.generate(1) + + // dToken (DUSD) pay dToken #1 + await testing.rpc.loan.paybackLoan({ + vaultId: vaultId, + from: colAddr, + loans: [ + // { dToken: ts25Id, amounts: '0.00000001@DUSD' }, // x pay + { dToken: td10Id, amounts: '0.00000001@DUSD' }, + { dToken: tu10Id, amounts: '0.00000001@DUSD' }, + { dToken: tr50Id, amounts: '0.00000001@DUSD' } + ] + }) + await testing.generate(1) + + // dToken pay dToken #2 + await testing.rpc.loan.paybackLoan({ + vaultId: vaultId, + from: colAddr, + loans: [ + { dToken: dusdId, amounts: '0.00000001@TD10' }, + { dToken: tr50Id, amounts: '0.00000001@TD10' }, + { dToken: ts25Id, amounts: '0.00000001@TU10' } + ] + }) + await testing.generate(1) + + // colToken pay dToken + await testing.rpc.loan.paybackLoan({ + vaultId: vaultId, + from: colAddr, + loans: [ + { dToken: td10Id, amounts: '0.00000001@BTC' }, + { dToken: dusdId, amounts: '0.00000001@BTC' }, + { dToken: tu10Id, amounts: '0.00000001@CU10' } + ] + }) + await testing.generate(1) + + // test fail payback + // DUSD pay TS25 should be failed + const promise = testing.rpc.loan.paybackLoan({ + vaultId: vaultId, + from: colAddr, + loans: [ + { dToken: ts25Id, amounts: '0.00000001@DUSD' } + ] }) + await expect(promise).rejects.toThrow('Payback of loan via DUSD token is not currently active') }) diff --git a/apps/playground-api/src/setups/setup.gov.ts b/apps/playground-api/src/setups/setup.gov.ts index 3d7bf570c3..ece8f37ee1 100644 --- a/apps/playground-api/src/setups/setup.gov.ts +++ b/apps/playground-api/src/setups/setup.gov.ts @@ -5,21 +5,61 @@ import { Injectable } from '@nestjs/common' export class SetupGov extends PlaygroundSetup> { list (): Array> { return [ - { ATTRIBUTES: { 'v0/token/:dusdId/payback_dfi': 'true' } } + { + ATTRIBUTES: { + // dfi pay dtoken + // DFI pay DUSD + 'v0/token/12/payback_dfi': 'true', + 'v0/token/12/payback_dfi_fee_pct': '0.01', + + // DFI pay TR50 + 'v0/token/16/payback_dfi': 'true', + 'v0/token/16/payback_dfi_fee_pct': '0.01', + + // dtoken (DUSD) pay dtoken #1 + // DUSD pay TD10 + 'v0/token/14/loan_payback/12': 'true', + 'v0/token/14/loan_payback_fee_pct/12': '0.01', + + // DUSD pay TU10 + 'v0/token/13/loan_payback/12': 'true', + 'v0/token/13/loan_payback_fee_pct/12': '0.01', + + // DUSD pay TR50 + 'v0/token/16/loan_payback/12': 'true', + 'v0/token/16/loan_payback_fee_pct/12': '0.01', + + // dtoken pay dtoken #2 + // TD10 pay DUSD + 'v0/token/12/loan_payback/14': 'true', + 'v0/token/12/loan_payback_fee_pct/14': '0.01', + + // TD10 pay TR50 + 'v0/token/16/loan_payback/14': 'true', + 'v0/token/16/loan_payback_fee_pct/14': '0.01', + + // TU10 pay TS25 + 'v0/token/15/loan_payback/13': 'true', + 'v0/token/15/loan_payback_fee_pct/13': '0.01', + + // cToken pay dToken + // BTC pay TD10 + 'v0/token/14/loan_payback/1': 'true', + 'v0/token/14/loan_payback_fee_pct/1': '0.01', + + // BTC pay DUSD + 'v0/token/12/loan_payback/1': 'true', + 'v0/token/12/loan_payback_fee_pct/1': '0.01', + + // CU10 pay TU10 + 'v0/token/13/loan_payback/6': 'true', + 'v0/token/13/loan_payback_fee_pct/6': '0.01' + } + } ] } async create (each: any): Promise { - const key = Object.keys(each)[0] - if (key === 'ATTRIBUTES') { - const dusdInfo = await this.client.token.getToken('DUSD') - const dusdId = Object.keys(dusdInfo)[0] - /* eslint-disable-next-line */ - var re = new RegExp(':dusdId', 'g'); - const k = Object.keys(each[key])[0] - each[key] = { [k.replace(re, dusdId)]: 'true' } - } - await this.client.masternode.setGov(each) } diff --git a/apps/playground-api/src/setups/setup.loan.token.ts b/apps/playground-api/src/setups/setup.loan.token.ts index b9dba61646..000d7c0854 100644 --- a/apps/playground-api/src/setups/setup.loan.token.ts +++ b/apps/playground-api/src/setups/setup.loan.token.ts @@ -42,6 +42,7 @@ export class SetupLoanToken extends PlaygroundSetup { async create (each: SetLoanToken): Promise { await this.client.loan.setLoanToken(each) + await this.generate(1) } async has (each: SetLoanToken): Promise {