UNPKG

descent-js

Version:

A Typescript library for interacting with the Descent Protocol

95 lines (94 loc) 3.72 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var internal_exports = {}; __export(internal_exports, { Internal: () => Internal }); module.exports = __toCommonJS(internal_exports); var import_ethers = require("ethers"); class Internal { constructor(descent) { this.descent = descent; /** * Returns TransactionObject for any calls. * * Parameter `gasPrice` must be defined in mode `node` and is optional in mode `browser`. * * Parameter `nonce` only takes effect in mode `node`. * * @param params.from * @param params.to * @param params.callData * @param params.value (optional) * @param params.gasLimit (optional) * @param params.gasPrice (optional only for "browser" mode) * @param params.nonce (optional) mostly for "node" mode * @param params.maxFeePerGas (optional only for "browser" mode) * @param params.maxPriorityFeePerGas (optional only for "browser" mode) */ this.getTransactionConfig = async (params) => { if (!params.from) throw new Error("Parameter 'from' is not defined."); if (!params.to) throw new Error("Parameter 'to' is not defined."); if (!params.data) throw new Error("Parameter 'data' is not defined."); const from = params.from; const to = params.to; const data = params.data !== "0x" ? params.data : "0x"; const value = params.value ?? 0; const gasLimit = params.gasLimit ?? await this.getGas({ from, to, data, value }); const transactionConfig = { from, to, data, value, gasLimit }; if (params.gasPrice) { transactionConfig.gasPrice = params.gasPrice; } if (params.maxFeePerGas) { transactionConfig.maxFeePerGas = params.maxFeePerGas; } if (params.maxPriorityFeePerGas) { transactionConfig.maxPriorityFeePerGas = params.maxPriorityFeePerGas; } if (this.descent.configMode === "node") { if (!(params.maxFeePerGas && params.maxPriorityFeePerGas) && !params.gasPrice) { throw new Error( "Parameter 'gasPrice' or `maxFeePerGas` and `maxPriorityFeePerGas` must be defined when using mode 'node'." ); } transactionConfig.nonce = params.nonce ?? await this.getNonce(String(from)); } else if (!!params.nonce) { transactionConfig.nonce = params.nonce; } return transactionConfig; }; this.getNonce = async (from) => { var _a; return await ((_a = this.descent.signer.provider) == null ? void 0 : _a.getTransactionCount(String(from))); }; this.getGas = async (transactionConfig) => { return await this.descent.signer.estimateGas(transactionConfig) * BigInt(2); }; /** * Returns the ABI interface for any Descent Protocol contract. */ this.getInterface = (abiItems) => { const abiItem = new import_ethers.ethers.Interface(abiItems); return abiItem; }; } }