github.com/diadata-org/diadata@v1.4.593/cmd/interlay/stdothelper.js (about) 1 const Web3 = require("web3"); 2 const web3 = new Web3( 3 new Web3.providers.HttpProvider("https://rpc.api.moonbeam.network/") 4 ); 5 6 let abi = [ 7 { 8 "inputs": [], 9 "name": "fundRaisedBalance", 10 "outputs": [{ 11 "internalType": "uint256", 12 "name": "", 13 "type": "uint256" 14 }], 15 "stateMutability": "view", 16 "type": "function" 17 },{ 18 "inputs": [], 19 "name": "totalSupply", 20 "outputs": [{ 21 "internalType": "uint256", 22 "name": "", 23 "type": "uint256" 24 }], 25 "stateMutability": "view", 26 "type": "function" 27 }, 28 ]; 29 30 const { createResponse, getPrice } = require("./utils"); 31 32 const contractAddress = "0xfa36fe1da08c89ec72ea1f0143a35bfd5daea108"; 33 34 const contract = new web3.eth.Contract(abi, contractAddress); 35 36 function fundRaisedBalance() { 37 return new Promise(function (resolve, reject) { 38 contract.methods.fundRaisedBalance().call((error, result) => { 39 if (error) { 40 reject(error); 41 } else { 42 resolve(result); 43 } 44 }); 45 }); 46 } 47 48 function totalSupply() { 49 return new Promise(function (resolve, reject) { 50 contract.methods.totalSupply().call((error, result) => { 51 if (error) { 52 reject(error); 53 } else { 54 resolve(result); 55 } 56 }); 57 }); 58 } 59 60 async function getValues() { 61 let dotPrice = await getPrice("DOT"); 62 let totalIssued = await totalSupply(); 63 let totalBacked = await fundRaisedBalance(); 64 65 let ratio = totalBacked / totalIssued; 66 67 let stdotPrice = 68 ratio > 1 ? dotPrice : (dotPrice * totalBacked) / totalIssued; 69 70 totalIssued = totalIssued / 1e10; 71 totalBacked = totalBacked / 1e10; 72 73 return createResponse(totalIssued, totalBacked, stdotPrice, ratio); 74 } 75 76 function getStdot() { 77 return new Promise(function (resolve, reject) { 78 contract.methods.stDOTPrice().call((error, result) => { 79 if (error) { 80 reject(error); 81 } else { 82 resolve(result); 83 } 84 }); 85 }); 86 } 87 88 module.exports = { 89 getStdot: getStdot, 90 getValues: getValues, 91 };