import { Conflux } from "js-conflux-sdk" // TransactionMeta import { EventEmitter } from "events"; import { JsonRpcSigner } from "../signers"; export class BaseProvider extends EventEmitter { public cfxClient: Conflux; /** * Call a json rpc method with params * * @param {object} data * @param {string} data.method - Json rpc method name. * @param {array} [data.params] - Json rpc method params. * @return {Promise<*>} Json rpc method return value. * */ public request({ method, params = [] }): Promise { let result = this.cfxClient.request({ method, params}); return result; } /** * Call a json rpc method with params * * @param {string} method - Json rpc method name. * @param {any[]} params - Json rpc method params. * @return {Promise<*>} Json rpc method return value. * */ public call(method: string, ...params: any[]): Promise { return this.cfxClient.provider.call(method, ...params); } /** * Send a json rpc method request * * @param {string} method - Json rpc method name. * @param {array} [params] - Json rpc method params. * @return {Promise<*>} Json rpc method return value. * * @example * > await provider.send("cfx_epochNumber"); * > await provider.send("cfx_getBlockByHash", [blockHash]); */ async send(method: string, params: any[]): Promise { return this.cfxClient.provider.send({ method, params }); } // -------------------------------- epoch ----------------------------------- /** * Returns the epoch number of given parameter. * * @param {string|number} [epochNumber="latest_state"] - See [format.epochNumber](utils.md#util/format.js/format/(static)epochNumber) * @return {Promise} integer of the current epoch number of given parameter. * * @example * > await provider.getEpochNumber(); 443 */ public getBlockNumber(): Promise { return this.cfxClient.getEpochNumber(); } /** * Returns the balance of the account of given address. * * @param {string} address - The address to get the balance of. * @param {string|number} [epochNumber="latest_state"] - See [format.epochNumber](utils.md#util/format.js/format/(static)epochNumber) * @return {Promise} The balance in Drip. * */ public getBalance(walletAddress: string, epochNumber:string|number = "latest_state"): Promise { let balance = this.cfxClient.getBalance(walletAddress, epochNumber); return balance; } /** * Returns information about a block by hash. * * @param {string} blockHash - hash of a block. * @param {boolean} [detail=false] - If `true` it returns the full transaction objects, if `false` only the hashes of the transactions. * @return {Promise} A block object, or null when no block was found: * */ public getBlockByHash(blockHash: string, detail: boolean): Promise { return this.cfxClient.getBlockByHash(blockHash, detail); } /** * Returns information about a block by epoch number. * * @param {string|number} epochNumber - See [format.epochNumber](utils.md#util/format.js/format/(static)epochNumber) * @param {boolean} [detail=false] - If `true` it returns the full transaction objects, if `false` only the hashes of the transactions. * @return {Promise} See `getBlockByHash` * */ public getBlockByNumber(blockNumber: string|number, detail: boolean): Promise { return this.cfxClient.getBlockByEpochNumber(blockNumber, detail); } /** * Returns information about a block by epoch number. * * @param {string|number} epochNumber - See [format.epochNumber](utils.md#util/format.js/format/(static)epochNumber) * @param {boolean} [detail=false] - If `true` it returns the full transaction objects, if `false` only the hashes of the transactions. * @return {Promise} See `getBlockByHash` * */ public getBlock(blockNumberOrHash: number | string, detail: boolean): Promise { if (blockNumberOrHash.toString().startsWith("0x")) { return this.getBlockByHash(blockNumberOrHash.toString(), detail); } else { return this.getBlockByNumber(blockNumberOrHash, detail); } } // -------------------------------- transaction ----------------------------------- /** * Returns the information about a transaction requested by transaction hash. * * @param {string} transactionHash - hash of a transaction * @return {Promise} transaction object, or `null` when no transaction was found: * */ public getTransactionByHash(blockHash: string): Promise { return this.cfxClient.getTransactionByHash(blockHash); } // -------------------------------- special for sponsor ----------------------------------- /** * Returns the sponsor info of given contract. * * @param {string} address - Address to contract. * @param {string|number} [epochNumber="latest_state"] - See [format.epochNumber](utils.md#util/format.js/format/(static)epochNumber) * @return {Promise} A sponsor info object * */ public getSponsorInfo(address: string, epochNumber: string|number): Promise { return this.cfxClient.getSponsorInfo(address, epochNumber); } public getAccounts(): Promise { return this.cfxClient.request({ method: "cfx_accounts" }); } public getNetwork(ChainIdOrName?: string): Promise { return this.cfxClient.getStatus(); } // -------------------------------- signer ----------------------------------- public async getSigner(addressOrIndex: string | number = 0) { var address: string; if (typeof (addressOrIndex) === "string") { address = addressOrIndex; var index = 0; } else if (typeof (addressOrIndex) === "number") { var index = addressOrIndex; const addressArray = await this.cfxClient.request({ method: "cfx_accounts" }); address = addressArray[addressOrIndex]; } // var accountArray = await this.cfxClient.request({ method: "cfx_requestAccounts" }); var account = await this.cfxClient.getAccount(address) return new JsonRpcSigner(this, address, account); } // -------------------------------- unimplemented ----------------------------------- public getChainId(): Promise { let result = this.cfxClient.request({ method: "cfx_chainId"}); return result; } }