-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathloan-terms-LTB.js
69 lines (61 loc) · 1.96 KB
/
loan-terms-LTB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const plaid = require('../plaid');
const { decrypt } = require('../encrypt-decrypt');
const fetchTransactions = require('../utils/fetch-transactions');
const findUserByWallet = require('../utils/query-user');
import loanToBalance from "../Risk-Parameters/loan-to-balance.js"
/**
* @api {post} / getLoanTerms
* @apiVersion 1.0.0
* @apiName getLoanTerms
* @apiGroup Base
*
* @apiSuccess {json} Collateral percent, IR, and max loan.
*
* @apiParam {Object} params Parameters
*
* @apiParamExample {json} Request-Example:
* {"jsonrpc": "2.0", "method": "getLoanTerms", "id":1, "params":{"loanSize":
* 500,"account":"account ID","wallet":"wallet ID"}}
*
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "jsonrpc": "2.0",
* "id": 1,
* "result": {
* "collateralPercent": 65,
* "ir": 35,
* "maxLoan": 300
* }
* }
* @apiExample {curl} Example usage:
* curl --location --request POST 'http://localhost:3003/' --header 'Content-Type: application/json' --data-raw '{"jsonrpc": "2.0", "method": "getLoanTerms", "id":1, "params":{"collateralPercent": 65,"ir": 35,"maxLoan": 300}
}'
*
* @apiUse InvalidMethodError
*
* @apiUse InvalidRequestError
*/
async function getLoanTerms({ loanSize, account, wallet }) {
try {
const MAX_LOAN = 100; // DAI
const MIN_IR = 0.08;
const MAX_IR = 0.24;
const collateralProvided = await loanToBalance(wallet, account);
// 135% - (collateral provided - loan size)
const collateralPercent = 1.35 - (collateralProvided / loanSize);
// Inverse of collateralPercent
let ir = 1.0 - collateralPercent;
ir = ir < MIN_IR ? MIN_IR : (ir > MAX_IR ? MAX_IR : ir);
return {
collateralPercent: collateralPercent.toFixed(2),
ir: ir.toFixed(2),
maxLoan: MAX_LOAN
};
} catch (err) {
console.error(err);
throw plaid.onError(err);
}
}
module.exports = { getLoanTerms };