@q-dev/q-js-sdk
Version:
Typescript Library to interact with Q System Contracts
62 lines • 2.61 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseContractInstance = void 0;
const ethers_1 = require("ethers");
const web3_adapter_1 = require("../utils/web3-adapter");
const omit_1 = __importDefault(require("lodash/omit"));
/**
* Base contract instance for all instances
*/
class BaseContractInstance {
/**
* Constructor
* @param signerOrProvider signer or provider
* @param abi abi object
* @param address contract address
*/
constructor(signerOrProvider, abi, address) {
this.address = address;
this.instance = new ethers_1.Contract(address, abi, signerOrProvider);
this.adapter = new web3_adapter_1.Web3Adapter(signerOrProvider);
}
async getBalance(convertToQ) {
return this.adapter.getBalance(this.address, convertToQ);
}
async submitTransaction(method,
// @ts-ignore: TODO: types
args, txOptions = {}) {
const populatedTxOptions = await this.populateTxOptions(txOptions);
if (!populatedTxOptions.gasLimit) {
const gasBuffer = Number(txOptions.gasBuffer || BaseContractInstance.DEFAULT_GASBUFFER);
// @ts-ignore - estimateGas properties duplicate contract method properties
const estimate = await this.instance.estimateGas[method](...args, populatedTxOptions);
populatedTxOptions.gasLimit = ethers_1.BigNumber.from(estimate)
.mul(gasBuffer * 10)
.div(10);
}
// @ts-ignore: TODO: types
return this.instance[method](...args, populatedTxOptions);
}
async populateTxOptions(txOptions) {
const populatedTxOptions = Object.assign({}, txOptions);
if (txOptions.qAmount) {
if (txOptions.value)
throw new Error('QPayableTx must specify either value (in wei) OR qAmount, but not both');
populatedTxOptions.value = this.adapter.toWei(txOptions.qAmount.toString());
}
if (!txOptions.from) {
populatedTxOptions.from = await this.adapter.getDefaultAccount();
}
return (0, omit_1.default)(populatedTxOptions, ['qAmount', 'gasBuffer']);
}
}
exports.BaseContractInstance = BaseContractInstance;
/**
* @field default to estimate - 1.3 would mean 30% above estimate
* @example BaseContractInstance.DEFAULT_GASBUFFER = 1.3
*/
BaseContractInstance.DEFAULT_GASBUFFER = 1;
//# sourceMappingURL=BaseContractInstance.js.map