UNPKG

@q-dev/q-ts-gdk-sdk

Version:

Typescript Library to interact with GDK Contracts

67 lines 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseContractInstance = void 0; const web3_adapter_1 = require("../utils/web3-adapter"); /** * Base contract instance for all instances */ class BaseContractInstance { /** * Constructor * @param web3 web3 instance * @param abi abi object * @param address contract address */ constructor(web3, abi, address) { this.address = address; this.adapter = new web3_adapter_1.Web3Adapter(web3); this.instance = new web3.eth.Contract(abi, address); this.instance.handleRevert = BaseContractInstance.HANDLE_REVERT; } async getBalance(convertToQ) { return await this.adapter.getBalance(this.address, convertToQ); } async submitTransaction(txObject, txOptions) { await this.refreshTxDefaultOptions(); if (!txOptions) txOptions = {}; await this.processTxOptions(txObject, txOptions); return { promiEvent: txObject.send(txOptions) }; } async processTxOptions(txObject, txOptions) { // Note: this must be done before the estimateGas call, below await this.processPayableTxOptions(txOptions); if (!txOptions.gas) { // no explicit gas Limit const gasBuffer = Number(txOptions.gasBuffer || BaseContractInstance.DEFAULT_GASBUFFER); const estimate = await txObject.estimateGas(txOptions); txOptions.gas = Math.floor(estimate * gasBuffer); } } async processPayableTxOptions(txOptions) { if (!txOptions) return; if (txOptions.qAmount) { if (txOptions.value) throw new Error("QPayableTx must specify either value (in wei) OR qAmount, but not both"); txOptions.value = this.adapter.web3.utils.toWei(txOptions.qAmount.toString()); } } async refreshTxDefaultOptions() { if (!this.instance.options.from) { this.instance.options.from = await this.adapter.getDefaultAccount(); } } } exports.BaseContractInstance = BaseContractInstance; /** * @field default to estimate - 1.3 would mean 30% above estimate * @example BaseContractInstance.DEFAULT_GASBUFFER = 1.3 */ BaseContractInstance.DEFAULT_GASBUFFER = 1; /** * @field https://web3js.readthedocs.io/en/v1.8.1/web3-eth.html?highlight=handleRevert#handlerevert * @example BaseContractInstance.HANDLE_REVERT = true */ BaseContractInstance.HANDLE_REVERT = false; //# sourceMappingURL=BaseContractInstance.js.map