UNPKG

generic-caver

Version:

89 lines (72 loc) 3.38 kB
Object.assign(global, require('ffp-js')); const { findFunctionInABI, inputGenerator } = require('../utils/convertData') const tokenABI = require(`../abi/KCT20.json`); module.exports = caver => { const UTILS = require('./utils')(caver); const optionDefaultSettinng = async (option, type) => { option.type = UTILS.txType(type || '0x08') option.nonce = await KLAY.getNonce(option.from) option.gas = option.gas || '20000000' option.gasPrice = await KLAY.getGasPrice() option.value = option.value || '0x0' return option }; const KLAY = { //pure function getBlockNumber : _ => caver.klay.getBlockNumber(), getBalance : address => caver.klay.getBalance(address), getTransactionReceipt : transactionHash => caver.klay.getTransactionReceipt(transactionHash), getPastLogs : options => caver.klay.getPastLogs(options), getGasPrice : _ => caver.klay.getGasPrice(), getNonce : address => caver.klay.getTransactionCount(address, 'pending'), //custom function getBalanceKCT20 : (tokenAddress, balanceAddress) => go( new caver.klay.Contract(tokenABI, tokenAddress), contract => contract.methods.balanceOf(balanceAddress).call() ) }; KLAY.SIGN = { tx : (option, privateKey) => go( optionDefaultSettinng(option), opt => caver.klay.accounts.signTransaction(opt, privateKey) ), klayFeeDelegated : (option, privateKey) => go( optionDefaultSettinng(option, '0x09'), opt => caver.klay.accounts.signTransaction(opt, privateKey), ), contractFeeDelegated : async (option, privateKey, contract, funcSig, params) => go( option, _ => match(option) .case(opt => opt.data == undefined && contract !== null) (opt => (opt.data = go( findFunctionInABI(contract, funcSig), first, fn => ( caver.klay.abi.encodeFunctionCall( fn, inputGenerator(contract, funcSig, params) ) ) ), opt)) .else(_ => _), opt => optionDefaultSettinng(opt, '0x31'), opt => caver.klay.accounts.signTransaction(opt, privateKey), ) }; KLAY.SEND = { tx : (option, rawTx, feePayer) => match(option) .case(opt => opt === null)(_ => caver.klay.sendTransaction({ senderRawTransaction : rawTx, feePayer: feePayer.address || feePayer })) .else(_ => go( optionDefaultSettinng(option), opt => caver.klay.sendTransaction(opt) )), klayFeeDelegated : async (option, signerPrivateKey, feePayerAccount, contract, funcSig, params) => go( KLAY.SIGN.contractFeeDelegated(option, signerPrivateKey, contract, funcSig, params), ({ rawTransaction }) => KLAY.SEND.tx(null, rawTransaction, feePayerAccount) ) }; return KLAY; }