Last active
October 14, 2021 13:04
-
-
Save RomanFro/20683c4d3b9e27511835c9be05f3fb90 to your computer and use it in GitHub Desktop.
eth_call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const WEB3_URL = process.env.WEB3_URL; | |
const prom = require('util').promisify; | |
const ethABI = require('ethereumjs-abi'); | |
const Web3 = require('web3'); | |
const provider = new Web3.providers.HttpProvider(WEB3_URL); | |
const web3 = module.exports = new Web3(provider); | |
const sendAsync = prom(web3.currentProvider.sendAsync).bind(web3.currentProvider); | |
/** | |
* Raw json rpc request to method eth_call. | |
* | |
* @param {Object} txObject Raw transaction object. | |
* @param {Number|String} blockNumber Block number or tag (e.g. 'latest', 'earliest' or 'pending'). | |
* @return {Object?} Response or null. | |
*/ | |
async function call(params) { | |
let response; | |
let options = { | |
jsonrpc: '2.0', | |
method: 'eth_call', | |
params: params, | |
id: new Date().getTime() | |
}; | |
try { | |
console.log('before sendAsync'); | |
response = await sendAsync(options); | |
console.log('after sendAsync'); | |
} catch (e) { | |
console.error(e); | |
return null; | |
} | |
return response; | |
} | |
/** | |
* Get forecaster FR from User Storage. | |
* | |
* @param {String} to Callee (User Storage contract). | |
* @param {String} forecasterAddress | |
* @param {String} [blockNumber='latest'] Block number or tag (e.g. 'latest', 'earliest' or 'pending'). | |
* @return {Number?} User FR at the time of particular block. | |
*/ | |
web3.getFR = async function getFR(to, forecasterAddress, blockNumber='latest') { | |
const data = '0x' + ethABI.simpleEncode('getFR(address)', forecasterAddress).toString('hex'); | |
const rawTx = { | |
to: to, | |
data: data | |
}; | |
const response = await call([rawTx, blockNumber]); | |
console.log(response); | |
if (response) { | |
return parseInt(response.result, 16); | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment