UNPKG

@kaiachain/ethers-ext

Version:
158 lines (157 loc) 6.93 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Web3Provider = exports.JsonRpcProvider = void 0; const ethers_1 = require("ethers"); const js_ext_core_1 = require("@kaiachain/js-ext-core"); // @ts-ignore: package @kaiachain/web3rpc has no .d.ts file. const web3rpc = __importStar(require("@kaiachain/web3rpc")); const signer_js_1 = require("./signer.js"); /* eslint-disable no-multi-spaces */ class JsonRpcProvider extends ethers_1.JsonRpcProvider { constructor(url, network) { super(url, network); const send = (method, params) => { return this.send(method, params); }; const { AdminApi, DebugApi, GovernanceApi, KlayApi, NetApi, PersonalApi, TxpoolApi } = web3rpc; this.admin = (0, js_ext_core_1.asyncOpenApi)(send, AdminApi); this.debug = (0, js_ext_core_1.asyncOpenApi)(send, DebugApi); this.governance = (0, js_ext_core_1.asyncOpenApi)(send, GovernanceApi); this.klay = (0, js_ext_core_1.asyncOpenApi)(send, KlayApi); this.net = (0, js_ext_core_1.asyncOpenApi)(send, NetApi); this.personal = (0, js_ext_core_1.asyncOpenApi)(send, PersonalApi); this.txpool = (0, js_ext_core_1.asyncOpenApi)(send, TxpoolApi); } } exports.JsonRpcProvider = JsonRpcProvider; class Web3Provider extends ethers_1.BrowserProvider { constructor(provider, network) { // Making window.klaytn to EIP1193 compatible request,chainId if (!provider.request) { provider.request = async (_method, _params) => { }; } if (!provider.chainId) { provider.chainId = undefined; } super(provider, network); // temporary solution because this.provider is not receive isKaikas from provider this.provider.isKaikas = provider.isKaikas; this.provider.isMobile = provider?.sendAsync && provider?.isMobile; const send = async (method, params = []) => { if (provider.isKaikas) { method = method.replace("eth_", "klay_"); } if (provider?.send) { return super.send(method, params); } else if (this.isMobile) { if (method === "wallet_switchEthereumChain") { method = "wallet_switchKlaytnChain"; } if (method === "wallet_addEthereumChain") { method = "wallet_addKlaytnChain"; } if (method.endsWith("_requestAccounts") || method.endsWith("_accounts")) { return provider?.enable(); } else { return new Promise((resolve, reject) => { provider.sendAsync({ method, params }, (err, result) => { if (err) { reject(err); } else { resolve(result?.result || ""); } }); }); } } else { throw new Error("Provider does not support sendAsync or send methods"); } }; // Store the send function for use in the override this._sendFunction = send; const { AdminApi, DebugApi, GovernanceApi, KlayApi, NetApi, PersonalApi, TxpoolApi } = web3rpc; this.admin = (0, js_ext_core_1.asyncOpenApi)(send, AdminApi); this.debug = (0, js_ext_core_1.asyncOpenApi)(send, DebugApi); this.governance = (0, js_ext_core_1.asyncOpenApi)(send, GovernanceApi); this.klay = (0, js_ext_core_1.asyncOpenApi)(send, KlayApi); this.net = (0, js_ext_core_1.asyncOpenApi)(send, NetApi); this.personal = (0, js_ext_core_1.asyncOpenApi)(send, PersonalApi); this.txpool = (0, js_ext_core_1.asyncOpenApi)(send, TxpoolApi); } async getSigner(addressOrIndex) { if (!addressOrIndex) { addressOrIndex = 0; } if (typeof addressOrIndex === "number") { const accounts = await this.provider.send("eth_accounts", []); (0, ethers_1.assert)(accounts.length > addressOrIndex, "unknown account #" + addressOrIndex, "UNSUPPORTED_OPERATION", { operation: "getAddress" }); addressOrIndex = await this.provider._getAddress(accounts[addressOrIndex]); } return Promise.resolve(new signer_js_1.JsonRpcSigner(this, addressOrIndex)); } async send(method, params) { return this._sendFunction(method, params); } async listAccounts() { const accounts = await this.send("eth_accounts", []); return accounts.map((a) => new signer_js_1.JsonRpcSigner(this, a)); } // async getTransaction(txhash: string): Promise<any> { // if (this.isMobile) { // return await this._sendFunction("eth_getTransactionByHash", [txhash]); // } // return super.getTransaction(txhash); // } async getTransactionCount(address) { if (this.isMobile) { return await this._sendFunction("eth_getTransactionCount", [address]); } return super.getTransactionCount(address); } async estimateGas(tx) { if (!(0, ethers_1.isHexString)(tx.value)) { tx.value = "0x" + BigInt(tx.value || 0).toString(16); } return await this._sendFunction("eth_estimateGas", [tx]); } async signMessage(message) { return await this._sendFunction("eth_sign", [message]); } async getGasPrice() { return await this._sendFunction("eth_gasPrice", []); } async getFeeData() { if (this.isMobile) { return { gasPrice: (await this.getGasPrice()) }; } return super.getFeeData(); } } exports.Web3Provider = Web3Provider;