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

     1  const Web3 = require("web3");
     2  
     3  const { createResponse, getPrice } = require("./utils");
     4  const ethers = require("ethers");
     5  const bignumber = ethers.BigNumber;
     6  
     7    
     8  const web3 = new Web3(
     9    new Web3.providers.HttpProvider(
    10      process.env.ETHEREUM_NODE_URL || "https://mainnet.infura.io/v3/2883d1b22e0e4d62b535592dd8075fee"
    11    )
    12  );
    13  let abi = [
    14    {
    15      constant: true,
    16      inputs: [],
    17      name: "getTotalPooledEther",
    18      outputs: [
    19        {
    20          name: "",
    21          type: "uint256",
    22        },
    23      ],
    24      payable: false,
    25      stateMutability: "view",
    26      type: "function",
    27    },
    28    {
    29      constant: true,
    30      inputs: [],
    31      name: "totalSupply",
    32      outputs: [
    33        {
    34          name: "",
    35          type: "uint256",
    36        },
    37      ],
    38      payable: false,
    39      stateMutability: "view",
    40      type: "function",
    41    },
    42  ];
    43  
    44  let poolabi = [
    45    {
    46      inputs: [],
    47      name: "getActiveMinipoolCount",
    48      outputs: [
    49        {
    50          internalType: "uint256",
    51          name: "",
    52          type: "uint256",
    53        },
    54      ],
    55      stateMutability: "view",
    56      type: "function",
    57    },
    58    {
    59      inputs: [
    60        {
    61          internalType: "uint256",
    62          name: "offset",
    63          type: "uint256",
    64        },
    65        {
    66          internalType: "uint256",
    67          name: "limit",
    68          type: "uint256",
    69        },
    70      ],
    71      name: "getMinipoolCountPerStatus",
    72      outputs: [
    73        {
    74          internalType: "uint256",
    75          name: "initialisedCount",
    76          type: "uint256",
    77        },
    78        {
    79          internalType: "uint256",
    80          name: "prelaunchCount",
    81          type: "uint256",
    82        },
    83        {
    84          internalType: "uint256",
    85          name: "stakingCount",
    86          type: "uint256",
    87        },
    88        {
    89          internalType: "uint256",
    90          name: "withdrawableCount",
    91          type: "uint256",
    92        },
    93        {
    94          internalType: "uint256",
    95          name: "dissolvedCount",
    96          type: "uint256",
    97        },
    98      ],
    99      stateMutability: "view",
   100      type: "function",
   101    },
   102  ];
   103  
   104  let rocketnetwork = [
   105    {
   106      "inputs": [],
   107      "name": "getTotalRETHSupply",
   108      "outputs": [{
   109        "internalType": "uint256",
   110        "name": "",
   111        "type": "uint256"
   112      }],
   113      "stateMutability": "view",
   114      "type": "function"
   115    },
   116    {
   117      "inputs": [],
   118      "name": "getTotalETHBalance",
   119      "outputs": [{
   120        "internalType": "uint256",
   121        "name": "",
   122        "type": "uint256"
   123      }],
   124      "stateMutability": "view",
   125      "type": "function"
   126    },
   127  
   128  
   129  ]
   130  
   131  
   132  const contractAddress = "0xae78736cd615f374d3085123a210448e74fc6393";
   133  
   134  const contract = new web3.eth.Contract(abi, contractAddress);
   135  
   136  const poolcontractAddress = "0x6293B8abC1F36aFB22406Be5f96D893072A8cF3a";
   137  
   138  const rocketNetworkBalancesaddress = "0x138313f102ce9a0662f826fca977e3ab4d6e5539";
   139  
   140  
   141  const rocketNetworkBalancesContract = new web3.eth.Contract(rocketnetwork, rocketNetworkBalancesaddress);
   142  
   143  function getTotalRETHSupply() {
   144    return new Promise(function (resolve, reject) {
   145      rocketNetworkBalancesContract.methods.getTotalRETHSupply().call((error, result) => {
   146        if (error) {
   147          reject(error);
   148        } else {
   149          resolve(result);
   150        }
   151      });
   152    });
   153  }
   154  function getTotalETHBalance() {
   155    return new Promise(function (resolve, reject) {
   156      rocketNetworkBalancesContract.methods.getTotalETHBalance().call((error, result) => {
   157        if (error) {
   158          reject(error);
   159        } else {
   160          resolve(result);
   161        }
   162      });
   163    });
   164  }
   165  
   166  function totalSupply() {
   167    return new Promise(function (resolve, reject) {
   168      contract.methods.totalSupply().call((error, result) => {
   169        if (error) {
   170          reject(error);
   171        } else {
   172          resolve(result);
   173        }
   174      });
   175    });
   176  }
   177  
   178  function getActiveMinipoolCount() {
   179    return new Promise(function (resolve, reject) {
   180      poolcontract.methods.getActiveMinipoolCount().call((error, result) => {
   181        if (error) {
   182          reject(error);
   183        } else {
   184          resolve(result);
   185        }
   186      });
   187    });
   188  }
   189  
   190  function getMinipoolCountPerStatus(offset, limit) {
   191    return new Promise(function (resolve, reject) {
   192      poolcontract.methods
   193        .getMinipoolCountPerStatus(bignumber.from(offset), bignumber.from(limit))
   194        .call((error, result) => {
   195          if (error) {
   196            reject(error);
   197          } else {
   198            resolve(result);
   199          }
   200        });
   201    });
   202  }
   203  
   204  async function getValues() {
   205    let ethPrice = await getPrice("ETH")
   206    let totalIssued = await getTotalRETHSupply();
   207    let totalBacked = await getTotalETHBalance();
   208  
   209   
   210    let ratio = totalBacked/totalIssued;
   211  
   212    totalIssued = totalIssued/1e18
   213    totalBacked = totalBacked/1e18
   214   
   215    let rethPrice =  ratio  *ethPrice
   216  console.log("------------reth--------------",rethPrice)
   217    return createResponse(totalIssued,totalBacked,rethPrice, ratio)
   218  }
   219  
   220  async function getActivePool() {
   221    let ethPrice = await getPrice("ETH");
   222  
   223    let totalIssued = await totalSupply();
   224    let activeMinipoolCount =   (await getActiveMinipoolCount());
   225  
   226    let offset = 0;
   227    const limit = 400;
   228    const status = new Map();
   229    status.set("initialisedCount", 0);
   230    status.set("prelaunchCount", 0);
   231    status.set("stakingCount", 0);
   232    status.set("withdrawableCount", 0);
   233    status.set("dissolvedCount", 0);
   234    while (true) {
   235      let ans = await getMinipoolCountPerStatus(offset, limit);
   236      // console.log("====================getActiveMinipoolCount", activeMinipoolCount);
   237      // console.log("====================ans", ans);
   238      status.set(
   239        "initialisedCount",
   240        status.get("initialisedCount") + parseInt(ans["initialisedCount"])
   241      );
   242      status.set(
   243        "prelaunchCount",
   244        status.get("prelaunchCount") + parseInt(ans["prelaunchCount"])
   245      );
   246      status.set(
   247        "stakingCount",
   248        status.get("stakingCount") + parseInt(ans["stakingCount"])
   249      );
   250      status.set(
   251        "withdrawableCount",
   252        status.get("withdrawableCount") + parseInt(ans["withdrawableCount"])
   253      );
   254      status.set(
   255        "dissolvedCount",
   256        status.get("dissolvedCount") + parseInt(ans["dissolvedCount"])
   257      );
   258      offset += limit;
   259      if (offset >= activeMinipoolCount) break;
   260    }
   261  
   262    console.log("------",status)
   263    let totalBacked = 0
   264  
   265    for (let [key, value] of status) {
   266      switch(key){
   267          case "initialisedCount": totalBacked = totalBacked + (value * 16) 
   268          break;
   269          case "prelaunchCount": totalBacked = totalBacked + (value * 32) 
   270          break;
   271          case "stakingCount": totalBacked = totalBacked + (value * 32) 
   272          break;
   273          case "withdrawableCount": totalBacked = totalBacked + (value * 32) 
   274          break;
   275          // case "dissolvedCount": totalBacked = totalBacked + (value * 16) 
   276          // break;
   277      }
   278   
   279      }
   280      totalIssued = bignumber.from(totalIssued+"").div(1e12)
   281      totalIssued = totalIssued.div(1e6)
   282  
   283      let ratio = totalBacked/totalIssued;
   284  
   285      let rethPrice =   ratio  *ethPrice;
   286  
   287   
   288  
   289   
   290    return createResponse(totalIssued.toNumber(), totalBacked, rethPrice, ratio);
   291  }
   292  
   293  module.exports = {
   294    totalSupply: totalSupply,
   295    getValues: getValues,
   296  };