Skip to content

Instantly share code, notes, and snippets.

@shingonu
Created August 3, 2018 07:51
Show Gist options
  • Select an option

  • Save shingonu/277858eeeacc713e54cc207d6f2dc4e5 to your computer and use it in GitHub Desktop.

Select an option

Save shingonu/277858eeeacc713e54cc207d6f2dc4e5 to your computer and use it in GitHub Desktop.
api.post('/:token_id/call', async (req, res, next) => {
const query = await Token.findOne({
token_id: req.params.token_id,
}).exec();
if(_.isNull(query))
return res.status(500).json({
code: 1,
message: 'The token id does not exist.'
});
const abiPath = path.join(__dirname, '..', '..', 'contracts', 'HanwhaToken.json');
const abi = JSON.parse(fs.readFileSync(abiPath).toString()).abi;
const contract = new web3.eth.Contract(abi, query.token_address);
let params = {};
if (!_.isUndefined(req.body.params)) params = Object.values(req.body.params);
const result = await contract.methods[req.body.method](...params).call();
return res.status(200).json({
code: 0,
message: 'success',
response: result,
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment