generic-caver
Version:
64 lines (54 loc) • 2.11 kB
JavaScript
Object.assign(global, require('ffp-js'));
const { findFunctionInABI, fnSpliter, inputGenerator } = require('../utils/convertData')
const CONTRACT = caver => {
const KLAY = require('./klay')(caver);
const UTILS = require('./utils')(caver);
return {
get : (abi, address) => new caver.klay.Contract(abi, address),
write : (signer, contract, funcSig, params) => go(
inputGenerator(contract, funcSig, params),
async inputs => contract.methods[funcSig](...inputs).send(
{
from: signer.address || signer,
gasPrice: '25000000000',
gas: 20000000,
nonce: await KLAY.getNonce(signer.address || signer)
}
)
),
writeFeeDelegate : (signer, feePayer, contract, funcSig, params) => go(
findFunctionInABI(contract, funcSig),
first,
fn => caver.klay.abi.encodeFunctionCall(fn, go(fn.inputs,map(input => params[input.name]))),
async data => go(
{
from: signer.address,
to: contract._address,
type: UTILS.txType('0x31'),
data
},
async option => await KLAY.SEND.klayFeeDelegated(option, signer.privateKey, feePayer)
)
),
read: async (contract, funcSig, params) => await go(
inputGenerator(contract, funcSig, params),
inputs => contract.methods[fnSpliter(funcSig)](...inputs).call()
),
getEventLog: (contract, eventName, filter, fromBlock, toBlock, picks) => go(
contract.getPastEvents(eventName || "allEvents",
{
filter,
fromBlock : fromBlock || 0,
toBlock : toBlock || "latest"
}),
map(({ returnValues }) => go(
returnValues,
a => !picks ? a : pick(picks, a)
))
),
encodeParameters: (types, params) => caver.klay.abi.encodeParameters(types, params),
decodeParameters: (types, hex) => caver.klay.abi.decodeParameters(types, hex),
encodeFunctionCall: (jsonInterface, params) => caver.klay.abi.encodeFunctionCall(jsonInterface, params)
}
}
module.exports = CONTRACT