@ardier16/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
62 lines • 2.3 kB
JavaScript
;
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;
/**
* @field default to estimate - 1.3 would mean 30% above estimate
*/
this.DEFAULT_GASBUFFER = 1;
this.adapter = new web3_adapter_1.Web3Adapter(web3);
this.instance = new web3.eth.Contract(abi, address);
this.instance.handleRevert = true;
}
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);
const receipt = await txObject.send(txOptions);
return receipt;
}
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 || this.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;
//# sourceMappingURL=BaseContractInstance.js.map