UNPKG

@kaiachain/web3js-ext

Version:
127 lines (126 loc) 5.61 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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.KlaytnWeb3 = void 0; 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 web3_1 = require("web3"); const web3_core_1 = require("web3-core"); const web3_errors_1 = require("web3-errors"); const index_js_1 = require("./accounts/index.js"); const index_js_2 = require("./eth/index.js"); // Follow the Web3 class from the web3/src/web3.ts // with slight difference in the web3.eth property. class KlaytnWeb3 extends web3_core_1.Web3Context { constructor(providerOrContext) { // Call super() like original Web3.constructor() does const contextInitOptions = getContextInitOptions(providerOrContext); super(contextInitOptions); // Create inner Web3 object this._web3 = new web3_1.Web3(providerOrContext); // Expose required properties from inner Web3 object this.utils = this._web3.utils; // Override web3.eth.accounts methods const accounts = (0, index_js_1.context_accounts)(this); this.eth = { ...this._web3.eth, accounts: accounts }; this._accountProvider = accounts; // inevitable conflict in signTransaction types this._wallet = accounts.wallet; // Override web3.eth RPC method wrappers. See web3-eth/src/web3_eth.ts:Web3Eth // Note that other web3.eth methods should just calling eth_ RPCs to Klaytn node. this.eth.getProtocolVersion = (0, index_js_2.context_getProtocolVersion)(this._web3); this.eth.sendTransaction = (0, index_js_2.context_sendTransaction)(this._web3); this.eth.sendSignedTransaction = (0, index_js_2.context_sendSignedTransaction)(this._web3); this.eth.signTransaction = (0, index_js_2.context_signTransaction)(this._web3); // Attach additional RPC namespaces. const send = this.makeSendFunction(); 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.kaia = (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); } makeSendFunction() { if (!this.provider) { return async (_method, _params) => { throw new web3_errors_1.ProviderError("no provider set"); }; } return async (method, params) => { const response = await this.provider?.request({ jsonrpc: "2.0", id: "1", method: method, params: params, }); if (response?.error) { throw new web3_errors_1.ResponseError(response); } else { return response?.result; } }; } } exports.KlaytnWeb3 = KlaytnWeb3; // Static properties analogous to Web3 class KlaytnWeb3.version = web3_1.Web3.version; KlaytnWeb3.utils = web3_1.Web3.utils; KlaytnWeb3.modules = web3_1.Web3.modules; // Parse providerOrContext into Web3ContextInitOptions. // See web3/src/web3.ts:Web3 function getContextInitOptions(providerOrContext) { // Because console.warn will be printed in `this._web3 = new Web3(..)`, we omit here. // if (...) { console.warn('NOTE: web3.js is running without provider...'); } let contextInitOptions = {}; if (typeof providerOrContext === "string" || (0, web3_core_1.isSupportedProvider)(providerOrContext)) { contextInitOptions.provider = providerOrContext; } else if (providerOrContext) { contextInitOptions = providerOrContext; } else { contextInitOptions = {}; } return contextInitOptions; }