github.com/diadata-org/diadata@v1.4.593/cmd/interlay/interlayhelper.js (about) 1 2 const { ApiPromise, WsProvider } = require("@polkadot/api"); 3 const ethers = require("ethers"); 4 const bignumber = ethers.BigNumber; 5 const {createResponse,getPrice} = require("./utils") 6 7 8 async function collateralCurrencies(api) { 9 const collateralCurrencies = 10 await api.query.vaultRegistry.systemCollateralCeiling.entries(); 11 const collateralCurrenciesmap = new Map(); 12 collateralCurrencies.map((collateralCurrencie) => { 13 let key = collateralCurrencie[0].toHuman(); 14 let value = collateralCurrencie[1].toHuman(); 15 // TODO filter only ibtc as wrapped {"collateral":{"Token":"DOT"},"wrapped":{"Token":"IBTC"}} 16 console.log("key",JSON.stringify(key[0].collateral)); 17 // console.log("value",JSON.stringify(value)); 18 19 20 collateralCurrenciesmap.set(JSON.stringify(key[0].collateral), value); 21 }); 22 return collateralCurrenciesmap; 23 } 24 25 //https://github.com/DefiLlama/DefiLlama-Adapters/blob/f0bcc4e65ebe43ce00c77951376a2249f2931dc3/projects/interlay-collateral/api.js 26 27 async function totalUserVaultCollateral(api) { 28 const totalUserVaultCollaterals = 29 await api.query.vaultRegistry.totalUserVaultCollateral.entries(); 30 const totalUserVaultCollateralsmap = new Map(); 31 32 totalUserVaultCollaterals.map((totalUserVaultCollateral) => { 33 let key = totalUserVaultCollateral[0].toHuman(); 34 let value = totalUserVaultCollateral[1].toHuman(); 35 totalUserVaultCollateralsmap.set(JSON.stringify(key[0].collateral), value); 36 }); 37 return totalUserVaultCollateralsmap; 38 } 39 40 async function oracleaggregrate(api) { 41 const oracleaggregrators = await api.query.oracle.aggregate.entries(); 42 43 const oracleaggregratemap = new Map(); 44 45 oracleaggregrators.map((oracleaggregrate) => { 46 let key = oracleaggregrate[0].toHuman(); 47 let value = oracleaggregrate[1].toHuman(); 48 49 if (key[0].ExchangeRate) { 50 oracleaggregratemap.set(JSON.stringify(key[0].ExchangeRate), value); 51 } 52 }); 53 return oracleaggregratemap; 54 } 55 56 async function totalIssuance(api) { 57 const totalIssuances = await api.query.tokens.totalIssuance.entries(); 58 const totalIssuancesemap = new Map(); 59 60 totalIssuances.map((totalIssuance) => { 61 let key = totalIssuance[0].toHuman(); 62 63 let value = totalIssuance[1].toHuman(); 64 totalIssuancesemap.set(JSON.stringify(key[0]), value); 65 }); 66 return totalIssuancesemap; 67 } 68 69 async function getInterlayValues(token) { 70 let providerurl = ""; 71 72 switch (token) { 73 case "IBTC": 74 providerurl = process.env.INTERLAY_NODE_URL || "wss://interlay-rpc.dwellir.com"; 75 break; 76 case "KBTC": 77 providerurl = process.env.KITSUNGI_NODE_URL || "wss://kintsugi-rpc.dwellir.com"; 78 break; 79 } 80 const wsProvider = new WsProvider( 81 providerurl 82 ); 83 84 console.log("getinterlay api") 85 let api; 86 try{ 87 api = await ApiPromise.create({ 88 provider: wsProvider, 89 throwOnConnect: true, 90 throwOnUnknown:true 91 }); 92 }catch(e){ 93 console.log("throw getinterlay api") 94 95 throw e 96 } 97 98 99 100 let collateralCurrenciesmap = new Map(); 101 let totalUserVaultCollateralmap = new Map(); 102 let oracleaggregatormap = new Map(); 103 let totalIssuancesemap = new Map(); 104 105 collateralCurrenciesmap = await collateralCurrencies(api); 106 console.log("collateralCurrencies", collateralCurrenciesmap); 107 108 totalUserVaultCollateralmap = await totalUserVaultCollateral(api); 109 console.log("totalUserVaultCollateral", totalUserVaultCollateralmap); 110 111 oracleaggregatormap = await oracleaggregrate(api); 112 console.log("oracleaggregrate", oracleaggregatormap); 113 114 totalIssuancesemap = await totalIssuance(api); 115 console.log("totalIssuance", totalIssuancesemap); 116 117 /* 118 119 total_backable = collateral_currencies.map(|currency| vaultRegistry.totalUserVaultCollateral[currency] / oracle.aggregate[currency]).sum(); 120 total_issued = tokens.totalIssuance[IBTC]; 121 assert!(total_issued < total_backable); 122 123 */ 124 125 let total_backable = bignumber.from(0); 126 127 for (let [collateralCurrency,value] of collateralCurrenciesmap) { 128 console.log("collateralCurrency", collateralCurrency); 129 130 let collateralCurrencyString = totalUserVaultCollateralmap.get(collateralCurrency) 131 let totalUserVaultCollateralcurrency; 132 if(collateralCurrencyString){ 133 totalUserVaultCollateralcurrency = bignumber.from( 134 totalUserVaultCollateralmap.get(collateralCurrency).replaceAll(",", "") 135 ); 136 } 137 138 139 140 let oac = oracleaggregatormap.get(collateralCurrency) 141 let oracleaggregatecurrency; 142 143 if(oac){ 144 console.log("----------",collateralCurrency,oac) 145 146 oracleaggregatecurrency = bignumber.from( 147 oracleaggregatormap.get(collateralCurrency).replaceAll(",", "") 148 ); 149 }else{ 150 continue 151 } 152 153 154 155 // console.log( 156 // "totalUserVaultCollateralcurrenct", 157 // totalUserVaultCollateralcurrency.toString() 158 // ); 159 160 oracleaggregatecurrency = oracleaggregatecurrency.div(1e12); 161 oracleaggregatecurrency = oracleaggregatecurrency.div(1e6); //TODO while doing 1e18 its crashing somehow. 162 163 let currentcurrencydecimal = 10; 164 let btcdecimal = 8; 165 // TODO oracleaggregatecurrency/currentcurrencydecimal ie (10^10 / 10^8) 166 oracleaggregatecurrency = oracleaggregatecurrency.div(1e2); 167 168 // totalUserVaultCollateralcurrency = 169 // totalUserVaultCollateralcurrency.div(1e10); 170 171 172 if(totalUserVaultCollateralcurrency){ 173 console.log("adding ---------this ---",totalUserVaultCollateralcurrency.div(oracleaggregatecurrency).toString()) 174 console.log(" ---------oracleaggregatecurrency ---",oracleaggregatecurrency.toString()) 175 176 console.log(" ---------totalUserVaultCollateralcurrency ---",totalUserVaultCollateralcurrency.toString()) 177 178 179 total_backable = total_backable.add( 180 totalUserVaultCollateralcurrency.div(oracleaggregatecurrency) 181 ); 182 } 183 184 console.log("total_backable",total_backable.toString()) 185 186 187 } 188 189 console.log("totalIssuancesemap", totalIssuancesemap); 190 191 let t = {} 192 t.Token = token 193 let total_issued = bignumber.from( 194 totalIssuancesemap.get(JSON.stringify(t)).replaceAll(",", "") 195 ); 196 197 // total_issued = total_issued.div(1e8); 198 199 await api.disconnect(); 200 total_backable = total_backable.div(1e2); 201 202 203 return { 204 total_backable: total_backable.toString(), 205 total_issued: total_issued.toString(), 206 decimal: 8, 207 token: token, 208 }; 209 210 // return total_backable,total_issued; 211 212 // console.log("total_backable", total_backable.toString()); 213 // console.log("total_issued", total_issued.toString()); 214 } 215 216 217 module.exports = { 218 totalIssuance:totalIssuance, 219 oracleaggregrate:oracleaggregrate, 220 totalUserVaultCollateral:totalUserVaultCollateral, 221 collateralCurrencies:collateralCurrencies, 222 getInterlayValues:getInterlayValues, 223 }