Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 105 additions & 13 deletions cmd/interlay/interlayhelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const ethers = require("ethers");
const bignumber = ethers.BigNumber;
const {createResponse,getPrice} = require("./utils")

// for exchange rate decoding
const FIXEDI128_SCALING_FACTOR = 18;

async function collateralCurrencies(api) {
const collateralCurrencies =
Expand All @@ -30,8 +32,13 @@ async function collateralCurrencies(api) {
const totalUserVaultCollateralsmap = new Map();

totalUserVaultCollaterals.map((totalUserVaultCollateral) => {
let key = totalUserVaultCollateral[0].toHuman();

let key = totalUserVaultCollateral[0].toHuman();
console.log("totalUserVaultCollaterals----key-----",key)

let value = totalUserVaultCollateral[1].toHuman();
console.log("totalUserVaultCollaterals-----value----",value)

totalUserVaultCollateralsmap.set(JSON.stringify(key[0].collateral), value);
});
return totalUserVaultCollateralsmap;
Expand All @@ -53,6 +60,74 @@ async function collateralCurrencies(api) {
return oracleaggregratemap;
}

async function lending(api,oracleaggregatormap){
const data = (await api.query.loans.markets.entries());

for (let i = 0; i < data.length; i++) {
const [underlyingId, marketData] = data[i];

const isActive = marketData?.toJSON().state == "Active";
console.log("---------")

console.log("lending",isActive)
console.log("underlyingId",i)
console.log("---------")


if (!isActive) {
continue;
}

const lendTokenId = marketData.toJSON().lendTokenId;

const [issuanceExchangeRate, totalIssuance, totalBorrows] = await Promise.all([
api.query.loans.exchangeRate(underlyingId.toHuman()[0]).then((rawExchangeRate) =>
parseInt(rawExchangeRate) / (10 ** FIXEDI128_SCALING_FACTOR)
),
api.query.tokens.totalIssuance(lendTokenId),
api.query.loans.totalBorrows(underlyingId.toHuman()[0]),
]);

const totalTvl = (totalIssuance * issuanceExchangeRate) - totalBorrows;

console.log("--------")

let oac = oracleaggregatormap.get(JSON.stringify(underlyingId.toHuman()[0]))
try{


if(oac){

const data = lendTokenId;
const matchData = {};

matchData.LendToken = data.lendToken.toString(); // converting 4 to string

oracleaggregatormap.set(JSON.stringify(matchData),oac)

}
} catch(e){
console.log("err",e)
console.log(lendTokenId)
}



console.log("underlyingId",underlyingId.toHuman()[0])
console.log("underlyingId o",oac)


console.log("lendTokenId",lendTokenId)
console.log("totalIssuance",totalIssuance.toString())
console.log("issuanceExchangeRate",issuanceExchangeRate)



console.log("totalTvl",totalTvl)
}

return oracleaggregatormap
}
async function totalIssuance(api) {
const totalIssuances = await api.query.tokens.totalIssuance.entries();
const totalIssuancesemap = new Map();
Expand All @@ -67,14 +142,14 @@ async function collateralCurrencies(api) {
}

async function getInterlayValues(token) {
let providerurl = "";
let providerurl = "";

switch (token) {
case "IBTC":
providerurl = process.env.INTERLAY_NODE_URL || "wss://interlay-rpc.dwellir.com";
break;
case "KBTC":
providerurl = process.env.KITSUNGI_NODE_URL || "wss://kintsugi-rpc.dwellir.com";
providerurl = process.env.KITSUNGI_NODE_URL || "wss://kintsugi.api.onfinality.io/public-ws";
break;
}
const wsProvider = new WsProvider(
Expand All @@ -95,7 +170,6 @@ async function collateralCurrencies(api) {
throw e
}



let collateralCurrenciesmap = new Map();
let totalUserVaultCollateralmap = new Map();
Expand All @@ -109,6 +183,9 @@ throw e
console.log("totalUserVaultCollateral", totalUserVaultCollateralmap);

oracleaggregatormap = await oracleaggregrate(api);

oracleaggregatormap = await lending(api,oracleaggregatormap)

console.log("oracleaggregrate", oracleaggregatormap);

totalIssuancesemap = await totalIssuance(api);
Expand All @@ -123,16 +200,25 @@ throw e
*/

let total_backable = bignumber.from(0);

let consideredTokens = [];

for (let [collateralCurrency,value] of collateralCurrenciesmap) {
console.log("collateralCurrency", collateralCurrency);

let collateralCurrencyString = totalUserVaultCollateralmap.get(collateralCurrency)

console.log("collateralCurrencycollateralCurrencyString", collateralCurrency);


let totalUserVaultCollateralcurrency;
if(collateralCurrencyString){
totalUserVaultCollateralcurrency = bignumber.from(
totalUserVaultCollateralmap.get(collateralCurrency).replaceAll(",", "")
);
}else{
console.log("collateralCurrency not found in totalUserVaultCollateralmap", collateralCurrency);

}


Expand All @@ -141,13 +227,16 @@ throw e
let oracleaggregatecurrency;

if(oac){
console.log("----------",collateralCurrency,oac)

console.log("oac of",collateralCurrency)

oracleaggregatecurrency = bignumber.from(
oracleaggregatormap.get(collateralCurrency).replaceAll(",", "")
);
}else{
continue
console.log("oac not found for", collateralCurrency);

continue
}


Expand All @@ -170,24 +259,26 @@ throw e


if(totalUserVaultCollateralcurrency){
console.log("adding ---------this ---",totalUserVaultCollateralcurrency.div(oracleaggregatecurrency).toString())
console.log(" ---------oracleaggregatecurrency ---",oracleaggregatecurrency.toString())

console.log(" ---------totalUserVaultCollateralcurrency ---",totalUserVaultCollateralcurrency.toString())


let by = totalUserVaultCollateralcurrency.div(oracleaggregatecurrency).toString()
consideredTokens.push({collateralCurrency,by})


total_backable = total_backable.add(
totalUserVaultCollateralcurrency.div(oracleaggregatecurrency)
total_backable = total_backable.add(
by
);


}

console.log("total_backable",total_backable.toString())


}

console.log("totalIssuancesemap", totalIssuancesemap);


let t = {}
t.Token = token
let total_issued = bignumber.from(
Expand All @@ -205,6 +296,7 @@ throw e
total_issued: total_issued.toString(),
decimal: 8,
token: token,
consideredTokens,
};

// return total_backable,total_issued;
Expand Down
1 change: 0 additions & 1 deletion cmd/interlay/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"license": "ISC",
"dependencies": {
"@polkadot/api": "10.10.1",
"@rocketpool/api": "^0.0.15",
"body-parser": "^1.20.1",
"ethers": "^5.7.2",
"express": "^4.18.2",
Expand Down
27 changes: 14 additions & 13 deletions cmd/interlay/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,18 @@ const allowedTokens = [
source: "stDOT",
issuer: "Lido",
},
{
vtoken: "rETH",
token: "ETH",
source: "rETH",
issuer: "RocketPool",
},
{
vtoken: "stETH",
token: "ETH",
source: "stETH",
issuer: "Lido",
},
// {
// vtoken: "rETH",
// token: "ETH",
// source: "rETH",
// issuer: "RocketPool",
// },
// {
// vtoken: "stETH",
// token: "ETH",
// source: "stETH",
// issuer: "Lido",
// },
// {
// vtoken: "cbETH",
// token: "ETH",
Expand All @@ -144,9 +144,10 @@ const allowedTokens = [
];

async function getPrice(asset) {
var providerurl = process.env.DIADATA_API || "https://api.diadata.org/v1/quotation/";
var providerurl = process.env.DIADATA_API || "https://api.diadata.org/v1";

let response = await fetch(providerurl + "/quotation/" + asset);
console.log(providerurl + "/quotation/" + asset);
let ethprice = await response.json();
return ethprice.Price;
}
Expand Down
Loading