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

     1  const Web3 = require("web3");
     2  
     3  const {createResponse,getPrice} = require("./utils")
     4  
     5   
     6  const web3 = new Web3(
     7    new Web3.providers.HttpProvider(
     8      process.env.ETHEREUM_NODE_URL || "https://mainnet.infura.io/v3/2883d1b22e0e4d62b535592dd8075fee"
     9    )
    10  );
    11  let abi = [
    12    {
    13      constant: true,
    14      inputs: [],
    15      name: "getTotalPooledEther",
    16      outputs: [
    17        {
    18          name: "",
    19          type: "uint256",
    20        },
    21      ],
    22      payable: false,
    23      stateMutability: "view",
    24      type: "function",
    25    },
    26    {
    27      constant: true,
    28      inputs: [],
    29      name: "totalSupply",
    30      outputs: [
    31        {
    32          name: "",
    33          type: "uint256",
    34        },
    35      ],
    36      payable: false,
    37      stateMutability: "view",
    38      type: "function",
    39    },
    40  ];
    41  
    42  const contractAddress = "0xae7ab96520de3a18e5e111b5eaab095312d7fe84";
    43  
    44  const contract = new web3.eth.Contract(abi, contractAddress);
    45  
    46  function getTotalPooledETH() {
    47    return new Promise(function (resolve, reject) {
    48      contract.methods.getTotalPooledEther().call((error, result) => {
    49        if (error) {
    50          reject(error);
    51        } else {
    52          resolve(result);
    53        }
    54      });
    55    });
    56  }
    57  
    58  function totalSupply() {
    59    return new Promise(function (resolve, reject) {
    60      contract.methods.totalSupply().call((error, result) => {
    61        if (error) {
    62          reject(error);
    63        } else {
    64          resolve(result);
    65        }
    66      });
    67    });
    68  }
    69  
    70  async function getValues() {
    71    let ethPrice = await getPrice("ETH")
    72    let totalIssued = await totalSupply();
    73    let totalBacked = await getTotalPooledETH();
    74  
    75   
    76    let ratio = totalBacked/totalIssued;
    77  
    78    totalIssued = totalIssued/1e18
    79    totalBacked = totalBacked/1e18
    80  
    81    let stETHprice =  ratio > 1
    82    ? ethPrice
    83    : ethPrice *  totalBacked/totalIssued
    84  
    85    return createResponse(totalIssued,totalBacked,stETHprice, ratio)
    86  }
    87  
    88  
    89  
    90  
    91  module.exports = {
    92    totalSupply: totalSupply,
    93    getTotalPooledETH: getTotalPooledETH,
    94    getValues: getValues,
    95  };