@hairy/ether-lib 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019-PRESENT Hairyf<https://github.com/hairyf>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+
package/dist/index.cjs ADDED
@@ -0,0 +1,131 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ Bignumber: () => import_utils.Bignumber,
34
+ Errors: () => Errors,
35
+ formatEther: () => formatEther,
36
+ idprefix: () => idprefix,
37
+ subscribeForTransaction: () => subscribeForTransaction,
38
+ wait: () => wait
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // src/constants.ts
43
+ var Errors = /* @__PURE__ */ ((Errors2) => {
44
+ Errors2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
45
+ Errors2["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
46
+ Errors2["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
47
+ Errors2["NETWORK_ERROR"] = "NETWORK_ERROR";
48
+ Errors2["SERVER_ERROR"] = "SERVER_ERROR";
49
+ Errors2["TIMEOUT"] = "TIMEOUT";
50
+ Errors2["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
51
+ Errors2["NUMERIC_FAULT"] = "NUMERIC_FAULT";
52
+ Errors2["MISSING_NEW"] = "MISSING_NEW";
53
+ Errors2["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
54
+ Errors2["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
55
+ Errors2["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
56
+ Errors2["CALL_EXCEPTION"] = "CALL_EXCEPTION";
57
+ Errors2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
58
+ Errors2["NONCE_EXPIRED"] = "NONCE_EXPIRED";
59
+ Errors2["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
60
+ Errors2["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
61
+ Errors2["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
62
+ Errors2["ACTION_REJECTED"] = "ACTION_REJECTED";
63
+ return Errors2;
64
+ })(Errors || {});
65
+
66
+ // src/hash.ts
67
+ var import_ethers = require("ethers");
68
+ function idprefix(errorCallString) {
69
+ return (0, import_ethers.id)(errorCallString).slice(0, 10);
70
+ }
71
+
72
+ // src/number.ts
73
+ var import_ethers2 = require("ethers");
74
+ var import_utils = require("@hairy/utils");
75
+ function formatEther(value = "0", options = {}) {
76
+ const { separator = false, decimals, delimiters = false, rounding, zeromove = true } = options;
77
+ const number = (0, import_ethers2.formatEther)((0, import_utils.unum)((0, import_utils.numerfix)(value)).toFixed(0));
78
+ const groupSeparator = separator === false ? "" : ",";
79
+ return (0, import_utils.formatNumeric)(number, {
80
+ format: { groupSeparator, ...options.format },
81
+ delimiters: typeof delimiters === "boolean" ? delimiters && ["m", "b", "t"] : delimiters,
82
+ decimals,
83
+ rounding,
84
+ zeromove
85
+ });
86
+ }
87
+
88
+ // src/wait.ts
89
+ var import_ethers3 = require("ethers");
90
+ var import_mitt = __toESM(require("mitt"));
91
+ var emitter = (0, import_mitt.default)();
92
+ for (const Response of [import_ethers3.TransactionResponse, import_ethers3.ContractTransactionResponse]) {
93
+ const waitForTransaction = Response.prototype.wait;
94
+ Response.prototype._wait = waitForTransaction;
95
+ Response.prototype.wait = async function(...args) {
96
+ try {
97
+ emitter.emit("before");
98
+ const receipt = await waitForTransaction.call(this, ...args);
99
+ emitter.emit("after", receipt);
100
+ return receipt;
101
+ } catch (error) {
102
+ emitter.emit("error", error);
103
+ throw error;
104
+ }
105
+ };
106
+ }
107
+ async function wait(transaction) {
108
+ try {
109
+ emitter.emit("before");
110
+ if (typeof transaction === "function")
111
+ transaction = await transaction();
112
+ if (transaction instanceof Promise)
113
+ transaction = await transaction;
114
+ const receipt = await transaction.getTransaction().then((transaction2) => transaction2 == null ? void 0 : transaction2._wait());
115
+ emitter.emit("after", receipt);
116
+ return receipt;
117
+ } catch (error) {
118
+ emitter.emit("error", error);
119
+ throw error;
120
+ }
121
+ }
122
+ var subscribeForTransaction = emitter.on.bind(emitter);
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ Bignumber,
126
+ Errors,
127
+ formatEther,
128
+ idprefix,
129
+ subscribeForTransaction,
130
+ wait
131
+ });
@@ -0,0 +1,55 @@
1
+ import { Delimiter, Bignumber, Numberish } from '@hairy/utils';
2
+ export { Bignumber } from '@hairy/utils';
3
+ import * as mitt from 'mitt';
4
+ import { TransactionResponse, ContractTransactionResponse, TransactionReceipt } from 'ethers';
5
+
6
+ declare enum Errors {
7
+ UNKNOWN_ERROR = "UNKNOWN_ERROR",
8
+ NOT_IMPLEMENTED = "NOT_IMPLEMENTED",
9
+ UNSUPPORTED_OPERATION = "UNSUPPORTED_OPERATION",
10
+ NETWORK_ERROR = "NETWORK_ERROR",
11
+ SERVER_ERROR = "SERVER_ERROR",
12
+ TIMEOUT = "TIMEOUT",
13
+ BUFFER_OVERRUN = "BUFFER_OVERRUN",
14
+ NUMERIC_FAULT = "NUMERIC_FAULT",
15
+ MISSING_NEW = "MISSING_NEW",
16
+ INVALID_ARGUMENT = "INVALID_ARGUMENT",
17
+ MISSING_ARGUMENT = "MISSING_ARGUMENT",
18
+ UNEXPECTED_ARGUMENT = "UNEXPECTED_ARGUMENT",
19
+ CALL_EXCEPTION = "CALL_EXCEPTION",
20
+ INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS",
21
+ NONCE_EXPIRED = "NONCE_EXPIRED",
22
+ REPLACEMENT_UNDERPRICED = "REPLACEMENT_UNDERPRICED",
23
+ UNPREDICTABLE_GAS_LIMIT = "UNPREDICTABLE_GAS_LIMIT",
24
+ TRANSACTION_REPLACED = "TRANSACTION_REPLACED",
25
+ ACTION_REJECTED = "ACTION_REJECTED"
26
+ }
27
+
28
+ declare function idprefix(errorCallString: string): string;
29
+
30
+ interface FormatEtherOptions {
31
+ delimiters?: Delimiter[] | boolean;
32
+ separator?: boolean;
33
+ rounding?: Bignumber.RoundingMode;
34
+ zeromove?: boolean;
35
+ decimals?: number;
36
+ format?: Bignumber.Format;
37
+ }
38
+ declare function formatEther(value?: Numberish, options?: FormatEtherOptions): string;
39
+
40
+ type GenericTransactionResponse = TransactionResponse | ContractTransactionResponse | Promise<TransactionResponse | ContractTransactionResponse> | (() => TransactionResponse | ContractTransactionResponse) | (() => Promise<TransactionResponse | ContractTransactionResponse>);
41
+ declare function wait(transaction: GenericTransactionResponse): Promise<any>;
42
+ declare const subscribeForTransaction: {
43
+ <Key extends "before" | "after" | "error">(type: Key, handler: mitt.Handler<{
44
+ before: void;
45
+ after: TransactionReceipt | null;
46
+ error: Error;
47
+ }[Key]>): void;
48
+ (type: "*", handler: mitt.WildcardHandler<{
49
+ before: void;
50
+ after: TransactionReceipt | null;
51
+ error: Error;
52
+ }>): void;
53
+ };
54
+
55
+ export { Errors, FormatEtherOptions, GenericTransactionResponse, formatEther, idprefix, subscribeForTransaction, wait };
package/dist/index.mjs ADDED
@@ -0,0 +1,89 @@
1
+ // src/constants.ts
2
+ var Errors = /* @__PURE__ */ ((Errors2) => {
3
+ Errors2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
4
+ Errors2["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
5
+ Errors2["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
6
+ Errors2["NETWORK_ERROR"] = "NETWORK_ERROR";
7
+ Errors2["SERVER_ERROR"] = "SERVER_ERROR";
8
+ Errors2["TIMEOUT"] = "TIMEOUT";
9
+ Errors2["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
10
+ Errors2["NUMERIC_FAULT"] = "NUMERIC_FAULT";
11
+ Errors2["MISSING_NEW"] = "MISSING_NEW";
12
+ Errors2["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
13
+ Errors2["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
14
+ Errors2["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
15
+ Errors2["CALL_EXCEPTION"] = "CALL_EXCEPTION";
16
+ Errors2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
17
+ Errors2["NONCE_EXPIRED"] = "NONCE_EXPIRED";
18
+ Errors2["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
19
+ Errors2["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
20
+ Errors2["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
21
+ Errors2["ACTION_REJECTED"] = "ACTION_REJECTED";
22
+ return Errors2;
23
+ })(Errors || {});
24
+
25
+ // src/hash.ts
26
+ import { id } from "ethers";
27
+ function idprefix(errorCallString) {
28
+ return id(errorCallString).slice(0, 10);
29
+ }
30
+
31
+ // src/number.ts
32
+ import { formatEther as _formatEther } from "ethers";
33
+ import { Bignumber, formatNumeric, numerfix, unum } from "@hairy/utils";
34
+ function formatEther(value = "0", options = {}) {
35
+ const { separator = false, decimals, delimiters = false, rounding, zeromove = true } = options;
36
+ const number = _formatEther(unum(numerfix(value)).toFixed(0));
37
+ const groupSeparator = separator === false ? "" : ",";
38
+ return formatNumeric(number, {
39
+ format: { groupSeparator, ...options.format },
40
+ delimiters: typeof delimiters === "boolean" ? delimiters && ["m", "b", "t"] : delimiters,
41
+ decimals,
42
+ rounding,
43
+ zeromove
44
+ });
45
+ }
46
+
47
+ // src/wait.ts
48
+ import { ContractTransactionResponse, TransactionResponse } from "ethers";
49
+ import mitt from "mitt";
50
+ var emitter = mitt();
51
+ for (const Response of [TransactionResponse, ContractTransactionResponse]) {
52
+ const waitForTransaction = Response.prototype.wait;
53
+ Response.prototype._wait = waitForTransaction;
54
+ Response.prototype.wait = async function(...args) {
55
+ try {
56
+ emitter.emit("before");
57
+ const receipt = await waitForTransaction.call(this, ...args);
58
+ emitter.emit("after", receipt);
59
+ return receipt;
60
+ } catch (error) {
61
+ emitter.emit("error", error);
62
+ throw error;
63
+ }
64
+ };
65
+ }
66
+ async function wait(transaction) {
67
+ try {
68
+ emitter.emit("before");
69
+ if (typeof transaction === "function")
70
+ transaction = await transaction();
71
+ if (transaction instanceof Promise)
72
+ transaction = await transaction;
73
+ const receipt = await transaction.getTransaction().then((transaction2) => transaction2 == null ? void 0 : transaction2._wait());
74
+ emitter.emit("after", receipt);
75
+ return receipt;
76
+ } catch (error) {
77
+ emitter.emit("error", error);
78
+ throw error;
79
+ }
80
+ }
81
+ var subscribeForTransaction = emitter.on.bind(emitter);
82
+ export {
83
+ Bignumber,
84
+ Errors,
85
+ formatEther,
86
+ idprefix,
87
+ subscribeForTransaction,
88
+ wait
89
+ };
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@hairy/ether-lib",
3
+ "version": "1.1.1",
4
+ "license": "MIT",
5
+ "main": "./dist/index.cjs",
6
+ "publishConfig": {
7
+ "jsdelivr": "./dist/index.iife.min.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "dependencies": {
13
+ "mitt": "^3.0.1",
14
+ "@hairy/utils": "0.7.3"
15
+ },
16
+ "devDependencies": {
17
+ "ethers": "^6"
18
+ },
19
+ "peerDependencies": {
20
+ "ethers": ">=6"
21
+ },
22
+ "scripts": {
23
+ "build": "ptsup src/index.ts --dts"
24
+ },
25
+ "module": "./dist/index.mjs",
26
+ "types": "./dist/index.d.ts",
27
+ "unpkg": "./dist/index.iife.min.js",
28
+ "exports": {
29
+ ".": {
30
+ "import": "./dist/index.mjs",
31
+ "require": "./dist/index.cjs",
32
+ "types": "./dist/index.d.ts"
33
+ },
34
+ "./*": "./*"
35
+ }
36
+ }