UNPKG

jcc-ethereum-utils

Version:

Toolkit of crossing chain from Ethereum to SWTC chain

102 lines 3.31 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const abi_1 = require("@ethersproject/abi"); /** * toolkit of smart contract * * @class SmartContract */ class SmartContract { /** * Creates an instance of SmartContract * @memberof SmartContract */ constructor() { this._contract = null; this._address = null; this._ethereum = null; } /** * return ethereum instance * * @readonly * @type {Ethereum} * @memberof SmartContract */ get ethereum() { return this._ethereum; } /** * return contract instance * * @readonly * @type {Contract} * @memberof SmartContract */ get contract() { return this._contract; } get contractAddress() { return this._contract.options.address; } /** * init instance of smart contract * * @param {string} tokenContractAddress contract address of smart contract * @param {Ethereum} ethereum instance * @param {any} abi * @memberof SmartContract */ init(tokenContractAddress, ethereum, abi) { try { if (!ethereum.contractInitialied(this._contract, tokenContractAddress)) { this._address = tokenContractAddress; this._ethereum = ethereum; this._abi = abi; this._contract = this._ethereum.contract(this._abi, this._address); this.interface = new abi_1.Interface(this._abi); } } catch (e) { throw e; } } /** * destroy instance of smart contract * * @memberof SmartContract */ destroy() { this._contract = null; } /** * call defined function in the abi * */ callABI(name, ...args) { return __awaiter(this, void 0, void 0, function* () { // Validate that `name` exists in the ABI before dynamic property access, // preventing injection of arbitrary method names. const fragment = this.interface.getFunction(name); if (!fragment) { throw new Error(`${name} is not defined in the contract ABI.`); } const { stateMutability } = fragment; if (stateMutability === "view" || stateMutability === "pure") { return yield this._contract.methods[name](...args).call(); } return yield this.interface.encodeFunctionData(name, args); }); } } exports.default = SmartContract; //# sourceMappingURL=smartContract.js.map