Skip to content

Commit

Permalink
feat(apps/playground): setgov for enabling payback to all tokens (#1299)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
canonbrother authored Mar 31, 2022
1 parent df81748 commit d88ea12
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 19 deletions.
140 changes: 132 additions & 8 deletions apps/playground-api/__tests__/modules/PlaygroundModule.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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')
})
62 changes: 51 additions & 11 deletions apps/playground-api/src/setups/setup.gov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,61 @@ import { Injectable } from '@nestjs/common'
export class SetupGov extends PlaygroundSetup<Record<string, any>> {
list (): Array<Record<string, any>> {
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<void> {
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)
}

Expand Down
1 change: 1 addition & 0 deletions apps/playground-api/src/setups/setup.loan.token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class SetupLoanToken extends PlaygroundSetup<SetLoanToken> {

async create (each: SetLoanToken): Promise<void> {
await this.client.loan.setLoanToken(each)
await this.generate(1)
}

async has (each: SetLoanToken): Promise<boolean> {
Expand Down

0 comments on commit d88ea12

Please sign in to comment.