Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Forked from RomanFro/eth_call.js
Created October 14, 2021 13:04
Show Gist options
  • Save fxfactorial/5d10a33d05e3a2f94ee41de46308425e to your computer and use it in GitHub Desktop.
Save fxfactorial/5d10a33d05e3a2f94ee41de46308425e to your computer and use it in GitHub Desktop.
eth_call
'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