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

     1  const Web3 = require("web3");
     2  const web3 = new Web3(
     3    new Web3.providers.HttpProvider("https://evm.astar.network/")
     4  );
     5  const {createResponse,getPrice} = require("./utils")
     6  
     7  
     8  let abi = [
     9    {
    10      type: "function",
    11      stateMutability: "view",
    12      outputs: [
    13        {
    14          type: "uint256",
    15          name: "",
    16          internalType: "uint256",
    17        },
    18      ],
    19      name: "getAstrInStakingContract",
    20      inputs: [],
    21    },
    22    {
    23      type: "function",
    24      stateMutability: "view",
    25      outputs: [
    26        {
    27          type: "uint256",
    28          name: "",
    29          internalType: "uint256",
    30        },
    31        {
    32          type: "uint128",
    33          name: "",
    34          internalType: "uint128",
    35        },
    36      ],
    37      name: "getNAstrPriceFromCollateralRatio",
    38      inputs: [],
    39    },
    40    {
    41      type: "function",
    42      stateMutability: "view",
    43      outputs: [
    44        {
    45          type: "uint256",
    46          name: "",
    47          internalType: "uint256",
    48        },
    49      ],
    50      name: "getNAstrTotalSupply",
    51      inputs: [],
    52    },
    53    {
    54      type: "function",
    55      stateMutability: "view",
    56      outputs: [
    57        {
    58          type: "uint256",
    59          name: "",
    60          internalType: "uint256",
    61        },
    62      ],
    63      name: "values",
    64      inputs: [
    65        {
    66          type: "string",
    67          name: "",
    68          internalType: "string",
    69        },
    70      ],
    71    },
    72  ];
    73  
    74  const contractAddress = "0xCb2158f97367596A68dd1670bC848dc6B2245111";
    75  
    76  const contract = new web3.eth.Contract(abi, contractAddress);
    77  
    78  function getAstrInStakingContract() {
    79    return new Promise(function (resolve, reject) {
    80      contract.methods.getAstrInStakingContract().call((error, result) => {
    81        if (error) {
    82          reject(error);
    83        } else {
    84          resolve(result);
    85        }
    86      });
    87    });
    88  }
    89  
    90  function getNAstrPriceFromCollateralRatio() {
    91    return new Promise(function (resolve, reject) {
    92      contract.methods
    93        .getNAstrPriceFromCollateralRatio()
    94        .call((error, result) => {
    95          if (error) {
    96            reject(error);
    97          } else {
    98            resolve(result);
    99          }
   100        });
   101    });
   102  }
   103  
   104  function getNAstrTotalSupply() {
   105    return new Promise(function (resolve, reject) {
   106      contract.methods.getNAstrTotalSupply().call((error, result) => {
   107        if (error) {
   108          reject(error);
   109        } else {
   110          resolve(result);
   111        }
   112      });
   113    });
   114  }
   115  
   116  async function getValues(){
   117    let astrPrice = await getPrice("ASTR")
   118  
   119     let totalIssued = await getNAstrTotalSupply();
   120    let totalBacked = await getAstrInStakingContract();
   121  
   122   
   123    let ratio = totalBacked/totalIssued;
   124  
   125    totalIssued = totalIssued/1e18
   126    totalBacked = totalBacked/1e18
   127  
   128    let nastrPrice =  ratio > 1
   129    ? astrPrice
   130    : astrPrice *  totalBacked/totalIssued
   131  
   132  
   133  
   134    return createResponse(totalIssued,totalBacked,nastrPrice, ratio)
   135  
   136  
   137  }
   138  
   139  module.exports = {
   140    getNAstrTotalSupply: getNAstrTotalSupply,
   141    getNAstrPriceFromCollateralRatio: getNAstrPriceFromCollateralRatio,
   142    getAstrInStakingContract: getAstrInStakingContract,
   143    getValues:getValues,
   144  };