@celo/connect
Version:
Light Toolkit for connecting with the Celo network
101 lines • 5.09 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TxParamsNormalizer = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
function isEmpty(value) {
return (value === undefined ||
value === null ||
value === '0' ||
value.toLowerCase() === '0x' ||
value.toLowerCase() === '0x0');
}
function isPresent(value) {
return !isEmpty(value);
}
class TxParamsNormalizer {
constructor(connection) {
this.connection = connection;
this.chainId = null;
}
populate(celoTxParams) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
const txParams = Object.assign({}, celoTxParams);
const [chainId, nonce, gas, maxFeePerGas] = yield Promise.all([
() => __awaiter(this, void 0, void 0, function* () {
if (txParams.chainId == null) {
return this.getChainId();
}
return txParams.chainId;
}),
() => __awaiter(this, void 0, void 0, function* () {
if (txParams.nonce == null) {
return this.connection.nonce(txParams.from.toString());
}
return txParams.nonce;
}),
() => __awaiter(this, void 0, void 0, function* () {
if (!txParams.gas || isEmpty(txParams.gas.toString())) {
return this.connection.estimateGas(txParams);
}
return txParams.gas;
}),
() => __awaiter(this, void 0, void 0, function* () {
var _e, _f;
// if gasPrice is not set and maxFeePerGas is not set, set maxFeePerGas
if (isEmpty((_e = txParams.gasPrice) === null || _e === void 0 ? void 0 : _e.toString()) &&
isEmpty((_f = txParams.maxFeePerGas) === null || _f === void 0 ? void 0 : _f.toString())) {
const suggestedPrice = yield this.connection.gasPrice(txParams.feeCurrency);
// add small buffer to suggested price like other libraries do
const priceWithRoom = new bignumber_js_1.default(suggestedPrice)
.times(120)
.dividedBy(100)
.integerValue()
.toString(16);
return `0x${priceWithRoom}`;
}
return txParams.maxFeePerGas;
}),
].map((fn) => __awaiter(this, void 0, void 0, function* () { return fn(); })));
txParams.chainId = chainId;
txParams.nonce = nonce;
txParams.gas = gas;
txParams.maxFeePerGas = maxFeePerGas;
// need to wait until after gas price has been handled
// if maxFeePerGas is set make sure maxPriorityFeePerGas is also set
if (isPresent((_a = txParams.maxFeePerGas) === null || _a === void 0 ? void 0 : _a.toString()) &&
isEmpty((_b = txParams.maxPriorityFeePerGas) === null || _b === void 0 ? void 0 : _b.toString())) {
const clientMaxPriorityFeePerGas = yield this.connection.rpcCaller.call('eth_maxPriorityFeePerGas', []);
txParams.maxPriorityFeePerGas = clientMaxPriorityFeePerGas.result;
}
// remove gasPrice if maxFeePerGas is set
if (isPresent((_c = txParams.gasPrice) === null || _c === void 0 ? void 0 : _c.toString()) && isPresent((_d = txParams.maxFeePerGas) === null || _d === void 0 ? void 0 : _d.toString())) {
txParams.gasPrice = undefined;
delete txParams.gasPrice;
}
return txParams;
});
}
getChainId() {
return __awaiter(this, void 0, void 0, function* () {
if (this.chainId === null) {
this.chainId = yield this.connection.chainId();
}
return this.chainId;
});
}
}
exports.TxParamsNormalizer = TxParamsNormalizer;
//# sourceMappingURL=tx-params-normalizer.js.map