Skip to content

Commit

Permalink
Add getLoanScheme rpc (#638)
Browse files Browse the repository at this point in the history
* Add new getloanscheme rpc

* Improve comment

* Improve code

* Minor

* Add correct error message
  • Loading branch information
jingyi2811 authored Sep 8, 2021
1 parent a610cfb commit 4fed144
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { LoanMasterNodeRegTestContainer } from './loan_container'
import BigNumber from 'bignumber.js'
import { Testing } from '@defichain/jellyfish-testing'

describe('Loan getLoanScheme', () => {
const container = new LoanMasterNodeRegTestContainer()
const testing = Testing.create(container)

beforeAll(async () => {
await testing.container.start()
await testing.container.waitForWalletCoinbaseMaturity()
})

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

it('should getLoanScheme', async () => {
await testing.container.call('createloanscheme', [100, new BigNumber(1.5), 'scheme1'])
await testing.container.generate(1)

const data = await testing.rpc.loan.getLoanScheme('scheme1')
expect(data).toStrictEqual(
{ id: 'scheme1', interestrate: new BigNumber(1.5), mincolratio: new BigNumber(100) }
)
})

it('should not getLoanScheme if id does not exist', async () => {
const promise = testing.rpc.loan.getLoanScheme('scheme2')
await expect(promise).rejects.toThrow('RpcApiError: \'Cannot find existing loan scheme with id scheme2\', code: -8, method: getloanscheme')
})

it('should not getLoanScheme if id is an empty string', async () => {
const promise = testing.rpc.loan.getLoanScheme('')
await expect(promise).rejects.toThrow('RpcApiError: \'id cannot be empty or more than 8 chars long\', code: -8, method: getloanscheme')
})

it('should not getLoanScheme if id is more than 8 characters', async () => {
const promise = testing.rpc.loan.getLoanScheme('x'.repeat(9))
await expect(promise).rejects.toThrow('RpcApiError: \'id cannot be empty or more than 8 chars long\', code: -8, method: getloanscheme')
})
})
16 changes: 16 additions & 0 deletions packages/jellyfish-api-core/src/category/loan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ export class Loan {
return await this.client.call('listloanschemes', [], 'bignumber')
}

/**
* Get loan scheme.
*
* @param {string} id Unique identifier of the loan scheme, max 8 chars.
* @return {Promise<GetLoanSchemeResult>}
*/
async getLoanScheme (id: string): Promise<GetLoanSchemeResult> {
return await this.client.call('getloanscheme', [id], 'bignumber')
}

/**
* Sets the default loan scheme.
*
Expand Down Expand Up @@ -151,6 +161,12 @@ export interface CollateralTokenDetail {
activateAfterBlock: BigNumber
}

export interface GetLoanSchemeResult {
id: string
interestrate: BigNumber
mincolratio: BigNumber
}

export interface UTXO {
txid: string
vout: number
Expand Down
16 changes: 16 additions & 0 deletions website/docs/jellyfish/api/loan.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ interface LoanSchemeResult {
}
```

## getLoanScheme

Get loan scheme.

```ts title="client.loan.getLoanScheme()"
interface loan {
getLoanScheme (id: string): Promise<GetLoanSchemeResult>
}

interface GetLoanSchemeResult {
id: string
interestrate: BigNumber
mincolratio: BigNumber
}
```

## setDefaultLoanScheme

Sets the default loan scheme.
Expand Down

0 comments on commit 4fed144

Please sign in to comment.