github.com/diadata-org/diadata@v1.4.593/cmd/interlay/bifrosthelper.js (about)

     1  const { ApiPromise, WsProvider } = require("@polkadot/api");
     2  const ethers = require("ethers");
     3  const bignumber = ethers.BigNumber;
     4  
     5  
     6  async function tokenPool(api, token) {
     7      const tokenPoolMap = new Map();
     8      const tokenPoolEntries = await api.query.vtokenMinting.tokenPool.entries();
     9  
    10    
    11      tokenPoolEntries.forEach((tokenPool) => {
    12        let key = tokenPool[0].toHuman();
    13        console.log("tokenPool",key)
    14  
    15        let value = tokenPool[1].toHuman();
    16        if (key[0].Token) {
    17          tokenPoolMap.set(key[0].Token, value);
    18        } else if (key[0].Native) {
    19          tokenPoolMap.set(key[0].Native, value);
    20        }
    21      });
    22  
    23      console.log("tokenPoolMap",tokenPoolMap);
    24  
    25    
    26      return tokenPoolMap.get(token);
    27    }
    28  
    29    async function token2Pool(api, token) {
    30      const tokenPoolMap = new Map();
    31      const tokenPoolEntries = await api.query.vtokenMinting.tokenPool.entries();
    32  
    33    
    34      tokenPoolEntries.forEach((tokenPool) => {
    35        let key = tokenPool[0].toHuman();
    36   
    37        let value = tokenPool[1].toHuman();
    38        if (key[0].Token2) {
    39          tokenPoolMap.set(key[0].Token2, value);
    40        }
    41      });
    42  
    43   
    44    
    45      return tokenPoolMap.get(token);
    46    }
    47  
    48  async function tokenIssuance(api, token) {
    49      const tokenIssuanceMap = new Map();
    50      const totalIssuance = await api.query.tokens.totalIssuance.entries();
    51    
    52      totalIssuance.forEach((totalIssuance) => {
    53        let key = totalIssuance[0].toHuman();
    54        let value = totalIssuance[1].toHuman();
    55    
    56        if (key[0].Token) {
    57          tokenIssuanceMap.set(key[0].Token, value);
    58        } else if (key[0].Native) {
    59          tokenIssuanceMap.set(key[0].Native, value);
    60        }
    61      });
    62    
    63      return tokenIssuanceMap.get(token);
    64    }
    65  
    66    async function vTokenIssuance(api, token) {
    67      const totalIssuance = await api.query.tokens.totalIssuance({vToken:token});
    68    
    69      return totalIssuance;
    70    }
    71    async function vToken2Issuance(api, token) {
    72      const totalIssuance = await api.query.tokens.totalIssuance({vToken2:token});
    73    
    74      return totalIssuance;
    75    }
    76  
    77    async function getBiFrostValues(token) {
    78      let providerurl = "";
    79    
    80      switch (token.toLowerCase()) {
    81        case "KSM".toLowerCase():
    82        case "MOVR".toLowerCase():
    83        case "BNC".toLowerCase():
    84          providerurl = process.env.BIFROST_PARACHAIN_NODE_URL || "wss://bifrost-parachain.api.onfinality.io/public-ws";
    85          break;
    86        case "DOT".toLowerCase():
    87        case "GLMR".toLowerCase():
    88        case "ASTR".toLowerCase():
    89        case "FIL".toLowerCase():
    90          providerurl = process.env.BIFROST_POLKADOT_NODE_URL ||"wss://bifrost-polkadot.api.onfinality.io/public-ws"
    91      }
    92    
    93      const wsProvider = new WsProvider(
    94        providerurl
    95      );
    96      let api;
    97      try{
    98        api = await ApiPromise.create({
    99        provider: wsProvider,
   100        throwOnConnect: true,
   101        throwOnUnknown:true
   102      });
   103    }catch(e){
   104      console.log("throw bifrost api token",token.toLowerCase())
   105  
   106      throw e
   107  
   108    }
   109      let tokeninpool;
   110      let vtokenIssuance;
   111      let decimal;
   112    
   113      if (token.toLowerCase() == "DOT".toLowerCase()){
   114       // 0 represents dot token in bifrost polkadot
   115        tokeninpool = await token2Pool(api, "0");
   116        vtokenIssuance = await vToken2Issuance(api, "0");
   117        decimal =  10
   118  
   119      } else if (token.toLowerCase() == "GLMR".toLowerCase()){
   120        // 1 represents glmr token in bifrost polkadot
   121         tokeninpool = await token2Pool(api, "1");
   122         vtokenIssuance = await vToken2Issuance(api, "1");
   123         decimal =  18
   124  
   125      } else if (token.toLowerCase() == "ASTR".toLowerCase()){
   126        // 3 represents astr token in bifrost polkadot
   127         tokeninpool = await token2Pool(api, "3");
   128         vtokenIssuance = await vToken2Issuance(api, "3");
   129         decimal =  18
   130  
   131      } else if (token.toLowerCase() == "FIL".toLowerCase()){
   132        // 4 represents fil token in bifrost polkadot
   133         tokeninpool = await token2Pool(api, "4");
   134         vtokenIssuance = await vToken2Issuance(api, "4");
   135         decimal =  18
   136  
   137      } else if (token.toLowerCase() == "MOVR".toLowerCase()){
   138         tokeninpool = await tokenPool(api, token);
   139  
   140         vtokenIssuance = await vTokenIssuance(api, token);
   141         decimal =  18
   142  
   143      }else{
   144        tokeninpool = await tokenPool(api, token);
   145        // let tokenIssuance = await bifrosttokenIssuance(api, token);
   146          vtokenIssuance = await vTokenIssuance(api, token);
   147          decimal = 12
   148  
   149      }
   150       
   151    
   152   
   153    
   154      return {
   155        total_backable: bignumber.from(tokeninpool.replaceAll(",", "")).toString(),
   156        total_issued: vtokenIssuance.toString(),
   157        decimal: decimal,
   158        token: token,
   159        time: Date.now(),
   160      };
   161    }
   162    
   163    
   164  
   165  module.exports = {
   166    tokenPool: tokenPool,
   167    bifrosttokenIssuance:tokenIssuance,
   168    vTokenIssuance:vTokenIssuance,
   169    getBiFrostValues:getBiFrostValues,
   170  };
   171  
   172  // {
   173  //   "total_backable": "250465499708802373",
   174  //   "total_issued": "223466021908860328",
   175  //   "decimal": 12,
   176  //   "token": "KSM"
   177  //   }