UNPKG

caver-js

Version:

caver-js is a JavaScript API library that allows developers to interact with a Klaytn node

905 lines (886 loc) 149 kB
/* Modifications copyright 2018 The caver-js Authors This file is part of web3.js. web3.js is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. web3.js is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see <http://www.gnu.org/licenses/>. This file is derived from web3.js/packages/web3-eth/src/index.js (2019/06/12). Modified and improved for the caver-js development. */ /** * @file index.js * @author Fabian Vogelsteller <fabian@ethereum.org> * @date 2017 */ /* eslint-disable max-classes-per-file */ const _ = require('lodash') const core = require('../../caver-core') const { formatters } = require('../../caver-core-helpers') const Subscriptions = require('../../caver-core-subscriptions').subscriptions const MethodBase = require('../../caver-core-method') const utils = require('../../caver-utils') const AbstractTransaction = require('../../caver-transaction/src/transactionTypes/abstractTransaction') /** * A class that can invoke Klay RPC Calls. * @class * @hideconstructor */ class Klay { constructor(...args) { const _this = this // sets _requestmanager core.packageInit(this, args) // overwrite package setRequestManager const setRequestManager = this.setRequestManager this.setRequestManager = function(manager) { setRequestManager(manager) return true } // overwrite setProvider const setProvider = this.setProvider this.setProvider = function(...arg) { setProvider.apply(_this, arg) _this.setRequestManager(_this._requestManager) } this.clearSubscriptions = _this._requestManager.clearSubscriptions class Method extends MethodBase { constructor(options) { options.outputFormatterDisable = true super(options) } } const _klaytnCall = [ /** * Returns the chain ID of the chain. * * @memberof Klay * @method getChainId * @instance * * @example * const result = await caver.rpc.klay.getChainId() * * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The chain ID of the chain. */ new Method({ name: 'getChainId', call: 'klay_chainID', params: 0, }), /** * Returns the current price per gas in peb. * * @memberof Klay * @method getGasPrice * @instance * * @example * const result = await caver.rpc.klay.getGasPrice() * * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The current gas price in peb. */ new Method({ name: 'getGasPrice', call: 'klay_gasPrice', params: 0, }), /** * Returns the total number of transactions sent from an address. * * @memberof Klay * @method getTransactionCount * @instance * * @example * const result = await caver.rpc.klay.getTransactionCount('0x{address in hex}') * * @param {string} address The address to get the number of transactions from. * @param {number|string} [blocNumber] A block number, the string pending for the pending nonce, or the string `earliest` or `latest` as in the default block parameter. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The number of transactions sent from the given address in hex. */ new Method({ name: 'getTransactionCount', call: 'klay_getTransactionCount', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Returns a block header by block number. * * @memberof Klay * @method getHeaderByNumber * @instance * * @example * const result = await caver.rpc.klay.getHeaderByNumber(0) * const result = await caver.rpc.klay.getHeaderByNumber('latest') * * @param {string|number|BN|BigNumber} blockNumberOrTag The block number or block tag string to query block header. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Header>} An object includes block header. */ new Method({ name: 'getHeaderByNumber', call: 'klay_getHeaderByNumber', params: 1, inputFormatter: [formatters.inputBlockNumberFormatter], }), /** * An object for reward distribution from Klaytn. * * @example * * @typedef {object} Klay.Rewards * @property {number} minted - The amount minted. * @property {number} totalFee - Total tx fee spent. * @property {number} burntFee - The amount burnt. * @property {number} proposer - The amount for the block proposer. * @property {number} stakers - Total amount for stakers. * @property {number} kgf - The amount for KGF. * @property {number} kir - The amount for KIR. * @property {object} rewards - A mapping from reward recipient addresses to reward amounts. */ /** * An object defines the AccountKeyLegacy. * * @example * { keyType: 1, key: {} } * * @typedef {object} Klay.AccountKeyLegacy * @property {number} keyType - The key type number. The AccountKeyLegacy key type is `1`. * @property {object} key - The key information object. For AccountKeyLegacy this field will be empty. */ /** * An object defines the public key. * * @typedef {object} Klay.PublicKeyObject * @property {string} x - The x point of the public key. * @property {string} y - The y point of the public key. */ /** * An object defines the AccountKeyPublic. * * @example * { * keyType: 2, * key: { x:'0xb9a4b...', y:'0x7a285...' } * } * * @typedef {object} Klay.AccountKeyPublic * @property {number} keyType - The key type number. The AccountKeyPublic key type is `2`. * @property {Klay.PublicKeyObject} key - The key information object. */ /** * An object defines the AccountKeyFail. * * @example * { keyType: 3, key:{} } * * @typedef {object} Klay.AccountKeyFail * @property {number} keyType - The key type number. The AccountKeyFail key type is `3`. * @property {object} key - The key information object. For AccountKeyFail this field will be empty. */ /** * An object defines the AccountKeyWeightedMultiSig. * * @typedef {object} Klay.WeightedPublicKey * @property {number} weight - The weight of the key. * @property {Klay.PublicKeyObject} key - The public key. */ /** * An object defines the AccountKeyWeightedMultiSig. * * @typedef {object} Klay.WeightedMultiSigKey * @property {number} threshold - The threshold of the AccountKeyWeightedMultiSig. * @property {Array.<Klay.WeightedPublicKey>} keys - An array that defines weighted public keys. */ /** * An object defines the AccountKeyWeightedMultiSig. * * @example * { * keyType: 4, * key: { * threshold: 2, * keys: [ * { * weight: 1, * key: { x: '0xae6b7...', y: '0x79ddf...' } * }, * { * weight: 1, * key: { x: '0xd4256...', y: '0xfc5e7...' } * }, * { * weight: 1, * key: { x: '0xd653e...', y: '0xe974e...' } * } * ] * } * } * * @typedef {object} Klay.AccountKeyWeightedMultiSig * @property {number} keyType - The key type number. The AccountKeyWeightedMultiSig key type is `4`. * @property {Klay.WeightedMultiSigKey} key - The key information object. For AccountKeyWeightedMultiSig this field will be defined with threshold and weighted public keys to use. */ /** * An object defines the AccountKeyRoleBased. * * @example * { * keyType: 5, * key: [ * { * key: { x: '0x81965...', y: '0x18242...' }, * keyType: 2 * }, * { * key: { x: '0x73363...', y: '0xfc3e3...' }, * keyType: 2 * }, * { * key: { x: '0x95c92...', y: '0xef783...' }, * keyType: 2 * } * ] * } * * @typedef {object} Klay.AccountKeyRoleBased * @property {number} keyType - The key type number. The AccountKeyRoleBased key type is `5`. * @property {Array.<Klay.AccountKeyLegacy|Klay.AccountKeyPublic|Klay.AccountKeyFail|Klay.AccountKeyWeightedMultiSig>} key - The key information object. AccountKeyRoleBased defines account key for each roles. */ /** * An account key type. * * @typedef {Klay.AccountKeyLegacy|Klay.AccountKeyPublic|Klay.AccountKeyFail|Klay.AccountKeyWeightedMultiSig|Klay.AccountKeyRoleBased} Klay.AccountKey */ /** * Returns AccountKey of a given address. * If the account has {@link https://docs.klaytn.com/klaytn/design/accounts#accountkeylegacy|AccountKeyLegacy} or the account of the given address is a {@link https://docs.klaytn.com/klaytn/design/accounts#smart-contract-accounts-scas|Smart Contract Account}, it will return an empty key value. * Please refer to {@link https://docs.klaytn.com/klaytn/design/accounts#account-key|Account Key} for more details. * * @memberof Klay * @method getAccountKey * @instance * * @example * const result = await caver.rpc.klay.getAccountKey('0x{address in hex}') * * @param {string} address The address of Klaytn account from which you want to get an object of AccountKey information. * @param {number|string} [blocNumber] A block number, the string pending for the pending nonce, or the string `earliest` or `latest` as in the default block parameter. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.AccountKey>} An object that contains AccountKey information. Each AccountKey type has different attributes. */ new Method({ name: 'getAccountKey', call: 'klay_getAccountKey', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * An object defines the signature data from the Node. * * @typedef {object} Klay.SignatureData * @property {string} V - V contains ECDSA recovery id. * @property {string} R - R contains ECDSA signature r. * @property {string} S - S contains ECDSA signature s. */ /** * An object defines the Transaction from the Node. * * @typedef {object} Klay.Transaction * @property {string} blockHash - Hash of the block where this transaction was in. * @property {string} blockNumber - Block number where this transaction was in. * @property {string} [codeFormat] - The code format of smart contract code. * @property {string} [feePayer] - Address of the fee payer. * @property {Array.<Klay.SignatureData>} [feePayerSignatures] - An array of fee payer's signature objects. A signature object contains three fields (V, R, and S) * @property {string} [feeRatio] - Fee ratio of the fee payer. If it is 30, 30% of the fee will be paid by the fee payer. 70% will be paid by the sender. * @property {string} from - Address of the sender. * @property {string} gas - Gas provided by the sender. * @property {string} gasPrice - Gas price provided by the sender in peb. * @property {string} hash - Hash of the transaction. * @property {boolean} [humanReadable] - `true` if the address is humanReadable, `false` if the address is not humanReadable. * @property {string} [key] - The RLP-encoded AccountKey used to update AccountKey of an Klaytn account. See {@link https://docs.klaytn.com/klaytn/design/accounts#account-key|AccountKey} for more details. * @property {string} [input] - The data sent along with the transaction. * @property {string} nonce - The number of transactions made by the sender prior to this one. * @property {string} senderTxHash - Hash of the tx without the fee payer's address and signature. This value is always the same as the value of `hash` for non-fee-delegated transactions. * @property {Array.<Klay.SignatureData>} signatures - An array of signature objects. A signature object contains three fields (V, R, and S). * @property {string} to - Address of the receiver. null when it is a contract deploying transaction. * @property {string} transactionIndex - Integer of the transaction index position in the block. * @property {string} type - A string representing the type of the transaction. * @property {number} typeInt - An integer representing the type of the transaction. * @property {string} value - Value transferred in peb. */ /** * Returns the information about a transaction requested by transaction hash. * * @memberof Klay * @method getTransactionByHash * @instance * * @example * const result = await caver.rpc.klay.getTransactionByHash('0x991d2e63b91104264d2886fb2ae2ccdf90551377af4e334b313abe123a5406aa') * * @param {string} transactionHash A transaction hash. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Transaction>} A transaction object, or null when no transaction was found. */ new Method({ name: 'getTransactionByHash', call: 'klay_getTransactionByHash', params: 1, }), /** * Returns a suggestion for a gas tip cap for dynamic fee transactions in peb. * Since Klaytn has a fixed gas price, this `caver.rpc.klay.getMaxPriorityFeePerGas` returns the gas price set by Klaytn. * * @memberof Klay * @method getMaxPriorityFeePerGas * @instance * * @example * const result = await caver.rpc.klay.getMaxPriorityFeePerGas() * * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} As a suggested value for the gas tip cap, the current Klaytn uses a fixed gas price, so the gasPrice value is returned. */ new Method({ name: 'getMaxPriorityFeePerGas', call: 'klay_maxPriorityFeePerGas', params: 0, }), ] AbstractTransaction._klaytnCall = {} this.klaytnCall = {} _.each(_klaytnCall, function(method) { method = new Method(method) method.attachToObject(AbstractTransaction._klaytnCall) method.attachToObject(_this.klaytnCall) method.setRequestManager(_this._requestManager) }) const methods = [ ..._klaytnCall, // Account /** * Returns `true` if the account associated with the address is created in the Klaytn blockchain platform. It returns `false` otherwise. * * @memberof Klay * @method accountCreated * @instance * * @example * const result = await caver.rpc.klay.accountCreated('0x{address in hex}') * * @param {string} address The address of the account you want to query to see if it has been created on the network. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<boolean>} The existence of an input address in the Klaytn. */ new Method({ name: 'accountCreated', call: 'klay_accountCreated', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Returns a list of addresses owned by the Klaytn Node. * * @memberof Klay * @method getAccounts * @instance * * @example * const result = await caver.rpc.klay.getAccounts() * * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Array.<string>>} An array of addresses owned by the Klaytn Node. */ new Method({ name: 'getAccounts', call: 'klay_accounts', params: 0, }), /** * Encodes an object that contains AccountKey information using the Recursive Length Prefix (RLP) encoding scheme. Also you can use {@link Account#getRLPEncodingAccountKey|account.getRLPEncodingAccountKey} to get RLP-encoded AccountKey. * * @memberof Klay * @method encodeAccountKey * @instance * * @example * // Using Klay.AccountKey(AccountKeyLegacy) * const result = await caver.rpc.klay.encodeAccountKey({ keyType: 1, key: {} }) * // Using Klay.AccountKey(AccountKeyPublic) * const result = await caver.rpc.klay.encodeAccountKey({ keyType: 2, key: { x: '0xdbac8...', y: '0x906d7...' } }) * // Using Klay.AccountKey(AccountKeyFail) * const result = await caver.rpc.klay.encodeAccountKey({ keyType: 3, key: {} }) * // Using Klay.AccountKey(AccountKeyWeightedMultiSig) * const result = await caver.rpc.klay.encodeAccountKey({ * keyType: 4, * key: { * threshold: 2, * keys: [ * { weight: 1, key: { x: '0xc734b...', y: '0x61a44...' } } * { weight: 1, key: { x: '0x12d45...', y: '0x8ef35...' } } * ] * } * }) * // Using Klay.AccountKey(AccountKeyLegacy) * const result = await caver.rpc.klay.encodeAccountKey({ * keyType: 5, * key: [ * { keyType: 2, key: { x: '0xe4a01...', y: '0xa5735...' } }, * { * keyType: 4, * key: { * threshold: 2, * keys: [ * { weight: 1, key: { x: '0xe4a01...', y: '0xa5735...' } }, * { weight: 1, key: { x: '0x36f63...', y: '0x6fdf9...' } }, * ], * }, * }, * { keyType: 2, key: { x: '0xc8785...', y: '0x94c27...' } }, * ], * }) * * // Using Account.AccountKey * const accountKey = caver.account.create('0x{address in hex}', '0xf1d2e...').accountKey * const result = await caver.rpc.klay.encodeAccountKey(accountKey) * * @param {Klay.AccountKey|Account.AccountKey} accountKey An object defines `keyType` and `key` inside or an instance of AccountKey. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} An RLP-encoded AccountKey. */ new Method({ name: 'encodeAccountKey', call: 'klay_encodeAccountKey', params: 1, inputFormatter: [formatters.inputAccountKeyFormatter], }), /** * Decodes An RLP-encoded AccountKey. Also you can use {@link AccountKeyDecoder.decode|caver.account.accountKey.decode} to decode An RLP-encoded AccountKey. * * @memberof Klay * @method decodeAccountKey * @instance * * @example * // Decode an accountKeyLegacy * const result = await caver.rpc.klay.decodeAccountKey('0x01c0') * // Decode an accountKeyPublic * const result = await caver.rpc.klay.decodeAccountKey('0x02a102dbac81e8486d68eac4e6ef9db617f7fbd79a04a3b323c982a09cdfc61f0ae0e8') * // Decode an accountKeyFail * const result = await caver.rpc.klay.decodeAccountKey('0x03c0') * // Decode an accountKeyWeightedMultiSig * const result = await caver.rpc.klay.decodeAccountKey('0x04f84b02f848e301a102c734b50ddb229be5e929fc4aa8080ae8240a802d23d3290e5e6156ce029b110ee301a10212d45f1cc56fbd6cd8fc877ab63b5092ac77db907a8a42c41dad3e98d7c64dfb') * // Decode an accountKeyRoleBased * const result = await caver.rpc.klay.decodeAccountKey('0x05f898a302a103e4a01407460c1c03ac0c82fd84f303a699b210c0b054f4aff72ff7dcdf01512db84e04f84b02f848e301a103e4a01407460c1c03ac0c82fd84f303a699b210c0b054f4aff72ff7dcdf01512de301a10336f6355f5b532c3c160') * * @param {string} encodedKey An RLP-encoded AccountKey. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.AccountKey>} An object defines `keyType` and `key` inside. */ new Method({ name: 'decodeAccountKey', call: 'klay_decodeAccountKey', params: 1, }), /** * An object defines the detailed information for EOA. * * @example * { * nonce: 0, * balance: '0x', * humanReadable: false, * key: { keyType: 1, key: {} } * } * * @typedef {object} Klay.EOA * @property {number} nonce - A sequence number used to determine the order of transactions. The transaction to be processed next has the same nonce with this value. * @property {string} balance - The amount of KLAY the account has. * @property {boolean} humanReadable - A boolean value indicating that the account is associated with a human-readable address. Since {@link https://docs.klaytn.com/klaytn/design/accounts#human-readable-address-hra|HRA} is under development, this value is false for all accounts. * @property {Klay.AccountKey} key - The key associated with this account. */ /** * An object defines the detailed information for SCA. * * @example * { * nonce: 1, * balance: '0x', * humanReadable: false, * key: { keyType: 3, key: {} }, * storageRoot: '0xd0ce6b9ba63cf727d48833bcaf69f398bb353e9a5b6235ac5bb3a8e95ff90ecf', * codeHash: '7pemrmP8fcguH/ut/SYHJoUSecfUIcUyeCpMf0sBYVI=', * codeFormat: 0 * } * * @typedef {object} Klay.SCA * @property {number} nonce - A sequence number used to determine the order of transactions. The transaction to be processed next has the same nonce with this value. * @property {string} balance - The amount of KLAY the account has. * @property {boolean} humanReadable - A boolean value indicating that the account is associated with a human-readable address. Since {@link https://docs.klaytn.com/klaytn/design/accounts#human-readable-address-hra|HRA} is under development, this value is false for all accounts. * @property {Klay.AccountKey} key - The key associated with this account. * @property {string} codeHash - The hash of the account's smart contract code. This value is immutable, which means it is set only when the smart contract is created. * @property {string} storageRoot - A 256-bit hash of the root of the Merkle Patricia Trie that contains the values of all the storage variables in the account. * @property {number} codeFormat - A format of the code in this account. Currently, it supports EVM(0x00) only. */ /** * An object defines the Klaytn account. * * @example * // EOA * { * accType: 1, * account: { * nonce: 0, * balance: '0x', * humanReadable: false, * key: { keyType: 1, key: {} } * } * } * // SCA * { * accType: 2, * account: { * nonce: 1, * balance: '0x', * humanReadable: false, * key: { keyType: 3, key: {} }, * storageRoot: '0xd0ce6b9ba63cf727d48833bcaf69f398bb353e9a5b6235ac5bb3a8e95ff90ecf', * codeHash: '7pemrmP8fcguH/ut/SYHJoUSecfUIcUyeCpMf0sBYVI=', * codeFormat: 0 * } * } * * @typedef {object} Klay.Account * @property {number} accType - The account type number. * @property {Klay.EOA|Klay.SCA} account - The key information object. For AccountKeyLegacy this field will be empty. */ /** * Returns the account information of a given address in the Klaytn. * For more details about the types of an account in Klaytn, please refer to {@link https://docs.klaytn.com/klaytn/design/accounts#klaytn-account-types|Klaytn Account Types}. * * @memberof Klay * @method getAccount * @instance * * @example * const result = await caver.rpc.klay.getAccount('0x{address in hex}') * * @param {string} address The address of the account for which you want to get account information. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Account>} An object defines `keyType` and `key` inside. */ new Method({ name: 'getAccount', call: 'klay_getAccount', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Returns the balance of the account of the given address in Klaytn. * * @memberof Klay * @method getBalance * @instance * * @example * const result = await caver.rpc.klay.getBalance('0x{address in hex}') * * @param {string} address The address of the account for which you want to get balance. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The current balance for the given address in peb. */ new Method({ name: 'getBalance', call: 'klay_getBalance', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Returns code at a given address. * * @memberof Klay * @method getCode * @instance * * @example * const result = await caver.rpc.klay.getCode('0x{address in hex}') * * @param {string} address The address to get the code from. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The code from the given address. */ new Method({ name: 'getCode', call: 'klay_getCode', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Returns `true` if an input account has a non-empty codeHash at the time of a specific block number. * It returns `false` if the account is an EOA or a smart contract account which doesn't have codeHash. * Please refer to {@link https://docs.klaytn.com/klaytn/design/accounts#smart-contract-accounts-scas|Smart Contract Account} for more details. * * @memberof Klay * @method isContractAccount * @instance * * @example * const result = await caver.rpc.klay.isContractAccount('0x{address in hex}') * * @param {string} address The address you want to check for isContractAccount. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<boolean>} `true` means the input parameter is an existing smart contract address. */ new Method({ name: 'isContractAccount', call: 'klay_isContractAccount', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], }), /** * Generates signed data specific to the Klaytn. * Refer to {@link https://docs.klaytn.com/dapp/json-rpc/api-references/klay/account#klay_sign|Klaytn Platform API - klay_sign} to know how the signature is generated * * This API provides the function to sign a message using an {@link https://docs.klaytn.com/dapp/json-rpc/api-references/personal#personal_importrawkey|imported account} in your Klaytn node. * The imported account in your node must be {@link https://docs.klaytn.com/dapp/json-rpc/api-references/personal#personal_unlockaccount|unlocked} to sign the message. * To sign a transaction with imported account in your Klaytn node, use {@link signTransaction|caver.rpc.klay.signTransaction}. * * @memberof Klay * @method sign * @instance * * @example * const result = await caver.rpc.klay.sign('0x{address in hex}', '0xdeadbeaf') * * @param {string} address The address of the imported account to sign the message. * @param {string} message Message to sign. * @param {string|number} [blockNumber] A block number, or the string `latest` or `earliest`. If omitted, `latest` will be used. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The signature made from an imported account. */ new Method({ name: 'sign', call: 'klay_sign', params: 2, inputFormatter: [formatters.inputAddressFormatter, formatters.inputSignFormatter], }), // Block /** * Returns the number of the most recent block. * * @memberof Klay * @method getBlockNumber * @instance * * @example * const result = await caver.rpc.klay.getBlockNumber() * * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<string>} The number of the most recent block in hex. */ new Method({ name: 'getBlockNumber', call: 'klay_blockNumber', params: 0, }), /** * An object for block from Klaytn. * * @example * { * blockScore: '0x1', * extraData: '0xd883010602846b6c617988676f312e31352e37856c696e757800000000000000f90164f85494571e53df607be97431a5bbefca1dffe5aef56f4d945cb1a7dccbd0dc446e3640898ede8820368554c89499fb17d324fa0e07f23b49d09028ac0919414db694b74ff9dea397fe9e231df545eb53fe2adf776cb2b841dd5a72e9f6af1f59f18efd9d205314bed1077be5083318274e6284adf82806f3339d0f88d8cb97f297b9f6a239149224a4f26e01a5692f2392ffb0ab73b10d9600f8c9b8417a43d087a58a32299f4d5a647371e31ecd1298c1cdb5921b5e575a93cfd7d65f470f2fbd936b1b80206c73daba3fec2038bc25d521bbc21b428d3067598bd95501b8411678bb3221f448d4f9e2dd3e7bda57b0da954eb5f1dff350751b6fd895b4643f3f14b56742fe091db68c162b3e1a9dd17676a9f4a95445e295f00d1d146f49e801b841df4fef80626bc00f2a0048d7718a499defece1ac3e849aefc5c04f2691ec7951377b7022d8d6b20fecce5e03f5ea891597e0d9aadbba0f2f82c5d7230806a62c01', * gasUsed: '0x3ea49', * governanceData: '0x', * hash: '0x188d4531d668ae3da20d70d4cb4c5d96a0cc5190771f0920c56b461c4d356566', * logsBloom: '0x00000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000008000000000000000000000000000000000000000000000000000200008000000000000000000000000000000000000004010000000020000000000000000000800000000000000002000000010000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000002000000000000000000020000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000', * number: '0x3f79aa7', * parentHash: '0x777d344c8c59c4d8d0041bb4c2ee66e95ec110303fb59d3e329f80e7a9c9c617', * receiptsRoot: '0xffbae3190f858531ff785bcbdc70278d91c3d9becdd8b134b0ab7974b9ef3641', * reward: '0xb2bd3178affccd9f9f5189457f1cad7d17a01c9d', * size: '0x507', * stateRoot: '0xa60d0868bd41b63b4fd67e5a8f801c5949e89a8994a13426747890b77d6bc0c4', * timestamp: '0x610b3164', * timestampFoS: '0xc', * totalBlockScore: '0x3f79aa8', * transactions: [ * { * blockHash: '0x188d4531d668ae3da20d70d4cb4c5d96a0cc5190771f0920c56b461c4d356566', * blockNumber: '0x3f79aa7', * feePayer: '0xfee998d423d5bd2bf5b5c0f0acb4e3aae2bd2286', * feePayerSignatures: [{ * V: '0x7f5', * R: '0xf9aff6f39feb7a18d3e1b8ab9f590f0227e465c72cfe05e8d7c9e390cbf1d349', * S: '0x6e7317d121a3951a8cbca110be8cc86c5314349f8fb1c37f9af4cadf72fe89ec' * }], * from: '0x11eb23f57151a88d4bb53cc9c27355437138c278', * gas: '0x2dc6c0', * gasPrice: '0x5d21dba00', * hash: '0x109d2836d9fde9d8081a27dd6ac545fd7a53530a56bdc40f2a11e5d6dbc2a09f', * input: '0x850ba1b300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000011eb23f57151a88d4bb53cc9c27355437138c278000000000000000000000000000000000000000000000000000000000000002b352f38366264316536392d346263392d343239622d613632622d3039366134353231613964632e6a736f6e000000000000000000000000000000000000000000', * nonce: '0x0', * senderTxHash: '0xeca2d3650403a1e27af0bbe9878dcbb248d764fc88751f35a6e05636d2ad9e78', * signatures: [ { * V: '0x7f6', * R: '0x9ea78985b004afa86acd455c017da374ec1aec885f963ec8134a38f7ede451b0', * S: '0xfac0e417f7f7b15023e3f5ac95f1fb5b3280746a2eff04394ddedbdd259fc1' * }], * to: '0x78ca9a1105c3392b56625f3fcfd149b29322c56f', * transactionIndex: '0x0', * type: 'TxTypeFeeDelegatedSmartContractExecution', * typeInt: 49, * value: '0x0' * } * ], * transactionsRoot: '0x109d2836d9fde9d8081a27dd6ac545fd7a53530a56bdc40f2a11e5d6dbc2a09f', * voteData: '0x' * } * * @typedef {object} Klay.Block * @property {string} blockScore - The difficulty of mining in the blockchain network. The use of `blockScore` differs from the consensus of the network. Always 1 in the BFT consensus engine. * @property {string} extraData - The "extra data" field of this block. * @property {string} gasUsed - The gas in total that was used by all transactions in this block. * @property {string} governanceData - RLP encoded governance configuration * @property {string} hash - Hash of the block. `null` when it is a pending block. * @property {string} logsBloom - The bloom filter for the logs of the block. `null` when it is a pending block. * @property {string} number - The block number. `null` when it is a pending block. * @property {string} parentHash - Hash of the parent block. * @property {string} receiptsRoot - The root of the receipts trie of the block. * @property {string} reward - The address of the beneficiary to whom the block rewards were given * @property {string} size - Integer the size of this block in bytes. * @property {string} stateRoot - The root of the final state trie of the block. * @property {string} timestamp - The unix timestamp for when the block was collated. * @property {string} timestampFoS - The fraction of a second of the timestamp for when the block was collated. * @property {string} totalBlockScore - Integer of the total blockScore of the chain until this block. * @property {Array.<Klay.Transaction>} transactions - Array of transaction objects, or 32-byte transaction hashes depending on the `returnTransactionObjects` parameter. * @property {string} transactionsRoot - The root of the transaction trie of the block. * @property {string} voteData - RLP encoded governance vote of the proposer. * @property {string} [baseFeePerGas] - Base fee per gas. */ /** * Returns information about a block. * If parameter is hex string, this will use {@link Klay#getBlockByHash|caver.rpc.klay.getBlockByHash}, if paramter is number type, this will use {@link Klay#getBlockByNumber|caver.rpc.klay.getBlockByNumber}. * * @memberof Klay * @method getBlock * @instance * * @example * // Use `caver.rpc.klay.getBlockByNumber` * const result = await caver.rpc.klay.getBlock(0) * // Use `caver.rpc.klay.getBlockByHash` * const result = await caver.rpc.klay.getBlock('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') * * @param {string|number} blockHashOrNumber The block hash or block number. * @param {boolean} [returnTransactionObjects] (default `false`) If `true`, the returned block will contain all transactions as objects, and if `false`, it will only contain the transaction hashes. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Block>} An object includes block. */ new Method({ name: 'getBlock', call: 'klay_getBlockByNumber', hexCall: 'klay_getBlockByHash', params: 2, inputFormatter: [formatters.inputBlockNumberFormatter, formatters.toBoolean], }), /** * Returns information about a block by block number. * * @memberof Klay * @method getBlockByNumber * @instance * * @example * const result = await caver.rpc.klay.getBlockByNumber(0) * * @param {number} blockNumber The block number. * @param {boolean} [returnTransactionObjects] (default `false`) If `true`, the returned block will contain all transactions as objects, and if `false`, it will only contain the transaction hashes. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Block>} An object includes block. */ new Method({ name: 'getBlockByNumber', call: 'klay_getBlockByNumber', params: 2, inputFormatter: [formatters.inputBlockNumberFormatter, formatters.toBoolean], }), /** * Returns information about a block by block hash. * * @memberof Klay * @method getBlockByHash * @instance * * @example * const result = await caver.rpc.klay.getBlockByHash('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') * * @param {string} blockHash The block hash. * @param {boolean} [returnTransactionObjects] (default `false`) If `true`, the returned block will contain all transactions as objects, and if `false`, it will only contain the transaction hashes. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Block>} An object includes block. */ new Method({ name: 'getBlockByHash', call: 'klay_getBlockByHash', params: 2, inputFormatter: [formatters.inputBlockNumberFormatter, formatters.toBoolean], }), /** * An object for block header from Klaytn. * * @example * * @typedef {object} Klay.Header * @property {string} parentHash - Hash of the parent block. * @property {string} reward - The address of the beneficiary to whom the block rewards were given. * @property {string} stateRoot - The root of the final state trie of the block. * @property {string} transactionsRoot - The root of the transaction trie of the block. * @property {string} receiptsRoot - The root of the receipts trie of the block. * @property {string} logsBloom - The bloom filter for the logs of the block. `null` when it is pending block. * @property {string} blockScore - Former difficulty. Always 1 in the BFT consensus engine. * @property {string} number - The block number. `null` when it is pending block. * @property {string} gasUsed - The total used gas by all transactions in this block. * @property {string} timestamp - The Unix timestamp for when the block was collated. * @property {string} timestampFoS - The fraction of a second of the timestamp for when the block was collated. * @property {string} extraData - The "extra data" field of this block. * @property {string} governanceData - RLP encoded governance configuration. * @property {string} hash - Hash of the current block. * @property {string} [baseFeePerGas] - Base fee per gas. * @property {string} [voteData] - RLP encoded governance vote of the proposer. */ /** * Returns a block header. * If parameter is hex string, this will use {@link Klay#getHeaderByHash|caver.rpc.klay.getHeaderByHash}, if paramter is number type, this will use {@link Klay#getHeaderByNumber|caver.rpc.klay.getHeaderByNumber}. * * @memberof Klay * @method getHeader * @instance * * @example * // Use `caver.rpc.klay.getHeaderByNumber` * const result = await caver.rpc.klay.getHeader(0) * // Use `caver.rpc.klay.getHeaderByHash` * const result = await caver.rpc.klay.getHeader('0x58482921af951cf42a069436ac9338de50fd963bdbea40e396f416f9ac96a08b') * * @param {string|number|BN|BigNumber} blockHashOrNumber The block hash or block number to query block header. * @param {function} [callback] Optional callback, returns an error object as the first parameter and the result as the second. * @return {Promise<Klay.Header>} An object includes block header. */