evm-blockchain-tools
Version:
This is a collection of resuseable tools to support development for EVM-powered blockchains
69 lines • 3.23 kB
JavaScript
;
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 });
exports.ContractModel = void 0;
const ethers_1 = require("ethers");
class ContractModel {
constructor(address, abi, signer) {
this.address = address;
this.signer = signer;
this._signerList = [];
this.abi = new ethers_1.ethers.utils.Interface(abi);
this.contract = new ethers_1.Contract(address, abi, signer);
}
get provider() {
return this.signer.provider;
}
generateTransaction(data, options) {
return __awaiter(this, void 0, void 0, function* () {
const signer = this.signer;
const params = data.data;
const gasPrice = (options === null || options === void 0 ? void 0 : options.gasPrice) || (yield signer.provider.getGasPrice());
const signerAddress = yield signer.getAddress();
const unsignedPopulatedTx = yield this.populateTransaction(data.functionName, params, {
gasPrice: gasPrice.toString(),
});
const signerPopulatedTx = yield signer.populateTransaction(unsignedPopulatedTx);
const signerNextNonce = signerPopulatedTx.nonce.toString();
if (data.nonce && data.nonce !== signerNextNonce) {
throw new Error(`signer ${signerAddress} next nonce ${signerNextNonce} is less than to passed nonce of ${data.nonce}`);
}
const signedTransaction = yield signer.signTransaction(signerPopulatedTx);
const preCalculatedHash = ethers_1.ethers.utils.keccak256(signedTransaction);
return {
preCalculatedHash,
signedTransaction,
};
});
}
populateTransaction(functionName, data, options) {
let contract = this.contract;
return contract.populateTransaction[functionName](...data, {
gasPrice: options.gasPrice,
});
}
queryForEvents(eventFilter, startingBlock, endingBlock) {
return __awaiter(this, void 0, void 0, function* () {
const events = yield this.contract.queryFilter(eventFilter, startingBlock, endingBlock);
return events;
});
}
subscribeForEvent(eventFilter, callback) {
this.contract.on(eventFilter, callback);
return {
unsubscribe: () => {
this.contract.removeListener(eventFilter, callback);
},
};
}
}
exports.ContractModel = ContractModel;
//# sourceMappingURL=contract-model.js.map