update schema, tests and insert controllers for borrow fields (#331)

* update schema, tests and insert controllers for borrow fields

* tvl -> usd

* update adapter handler

* round total supply/total borrow
This commit is contained in:
slasher125
2022-09-19 03:27:44 -07:00
committed by GitHub
parent 31d90a205c
commit f9dac34bf4
6 changed files with 55 additions and 5 deletions

View File

@@ -26,6 +26,12 @@ interface Pool {
underlyingTokens?: Array<string>;
poolMeta?: string;
url?: string;
// optional lending protocol specific fields:
apyBaseBorrow?: number;
apyRewardBorrow?: number;
totalSupplyUsd?: number;
totalBorrowUsd?: number;
ltv?: number;
}
```

View File

@@ -0,0 +1,11 @@
exports.up = (pgm) => {
pgm.addColumns('yield', {
apyBaseBorrow: 'numeric',
apyRewardBorrow: 'numeric',
totalSupplyUsd: 'numeric',
totalBorrowUsd: 'numeric',
});
pgm.addColumns('config', {
ltv: 'numeric',
});
};

View File

@@ -22,6 +22,11 @@ describe(`Running ${process.env.npm_config_adapter} Test`, () => {
'rewardTokens',
'poolMeta',
'url',
'apyBaseBorrow',
'apyRewardBorrow',
'totalSupplyUsd',
'totalBorrowUsd',
'ltv',
];
const fields = [...Object.keys(baseFields), ...optionalFields, 'tvlUsd'];
apy.forEach((pool) => {

View File

@@ -100,6 +100,7 @@ const buildInsertConfigQuery = (payload) => {
{ name: 'underlyingTokens', def: null },
{ name: 'rewardTokens', def: null },
'url',
{ name: 'ltv', def: null },
];
const cs = new pgp.helpers.ColumnSet(columns, { table: tableName });
const query =

View File

@@ -250,6 +250,10 @@ const buildInsertYieldQuery = (payload) => {
'apy',
'apyBase',
'apyReward',
{ name: 'apyBaseBorrow', def: null },
{ name: 'apyRewardBorrow', def: null },
{ name: 'totalSupplyUsd', def: null },
{ name: 'totalBorrowUsd', def: null },
];
const cs = new pgp.helpers.ColumnSet(columns, { table: tableName });
return pgp.helpers.insert(payload, cs);

View File

@@ -60,12 +60,14 @@ const main = async (body) => {
// number to string. so in order for the below filters to work proplerly we need to guarantee that the
// datatypes are correct (on db insert, mongoose checks field types against the schema and the bulk insert
// will fail if a pools field types doesnt match)
const strToNum = (val) => (typeof val === 'string' ? Number(val) : val);
data = data.map((p) => ({
...p,
apy: typeof p.apy === 'string' ? Number(p.apy) : p.apy,
apyBase: typeof p.apyBase === 'string' ? Number(p.apyBase) : p.apyBase,
apyReward:
typeof p.apyReward === 'string' ? Number(p.apyReward) : p.apyReward,
apy: strToNum(p.apy),
apyBase: strToNum(p.apyBase),
apyReward: strToNum(p.apyReward),
apyBaseBorrow: strToNum(p.apyBaseBorrow),
apyRewardBorrow: strToNum(p.apyRewardBorrow),
}));
// filter tvl to be btw lb-ub
@@ -81,6 +83,10 @@ const main = async (body) => {
apy: Number.isFinite(p.apy) ? p.apy : null,
apyBase: Number.isFinite(p.apyBase) ? p.apyBase : null,
apyReward: Number.isFinite(p.apyReward) ? p.apyReward : null,
apyBaseBorrow: Number.isFinite(p.apyBaseBorrow) ? p.apyBaseBorrow : null,
apyRewardBorrow: Number.isFinite(p.apyRewardBorrow)
? p.apyRewardBorrow
: null,
}));
// remove pools where all 3 apy related fields are null
@@ -93,8 +99,9 @@ const main = async (body) => {
...p,
apy: p.apy < 0 ? 0 : p.apy,
apyBase: p.apyBase < 0 ? 0 : p.apyBase,
// this shouldn't be lower than 0 lol, but leaving it here anyways in case of bug in adapter
apyReward: p.apyReward < 0 ? 0 : p.apyReward,
apyBaseBorrow: p.apyBaseBorrow < 0 ? 0 : p.apyBaseBorrow,
apyRewardBorrow: p.apyRewardBorrow < 0 ? 0 : p.apyRewardBorrow,
}));
// derive final total apy field
@@ -150,6 +157,22 @@ const main = async (body) => {
p.apyReward !== null ? +p.apyReward.toFixed(precision) : p.apyReward,
url: p.url ?? project.url,
timestamp,
apyBaseBorrow:
p.apyBaseBorrow !== null
? +p.apyBaseBorrow.toFixed(precision)
: p.apyBaseBorrow,
apyRewardBorrow:
p.apyRewardBorrow !== null
? +p.apyRewardBorrow.toFixed(precision)
: p.apyRewardBorrow,
totalSupplyUsd:
p.totalSupplyUsd === undefined || p.totalSupplyUsd === null
? null
: Math.round(p.totalSupplyUsd),
totalBorrowUsd:
p.totalBorrowUsd === undefined || p.totalBorrowUsd === null
? null
: Math.round(p.totalBorrowUsd),
};
});