@latticexyz/common 2.2.18-318924725246340b208d3fbee8793314686f1759 → 2.2.18-4565714f5e9421cc7b2de56fe51db4434c55f5d1

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.
@@ -1,208 +0,0 @@
1
- import {
2
- debug
3
- } from "./chunk-73UWXFXB.js";
4
-
5
- // src/getNonceManagerId.ts
6
- import { getAddress } from "viem";
7
- import { getChainId } from "viem/actions";
8
- import { getAction } from "viem/utils";
9
- async function getNonceManagerId({
10
- client,
11
- address,
12
- blockTag
13
- }) {
14
- const chainId = client.chain?.id ?? await getAction(client, getChainId, "getChainId")({});
15
- return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`;
16
- }
17
-
18
- // src/findCause.ts
19
- function findCause(error, fn) {
20
- if (fn?.(error)) return error;
21
- if (error.cause instanceof Error) return findCause(error.cause, fn);
22
- return fn ? null : error;
23
- }
24
-
25
- // src/createNonceManager.ts
26
- import { getTransactionCount } from "viem/actions";
27
- import PQueue from "p-queue";
28
- import { getAction as getAction2 } from "viem/utils";
29
- var debug2 = debug.extend("createNonceManager");
30
- function createNonceManager({
31
- client,
32
- address,
33
- // TODO: rename to account?
34
- blockTag = "latest",
35
- broadcastChannelName,
36
- queueConcurrency = 1
37
- }) {
38
- const ref = { nonce: -1, noncePromise: null };
39
- let channel = null;
40
- if (typeof BroadcastChannel !== "undefined") {
41
- const channelName = broadcastChannelName ? Promise.resolve(broadcastChannelName) : getNonceManagerId({ client, address, blockTag });
42
- channelName.then((name) => {
43
- channel = new BroadcastChannel(name);
44
- channel.addEventListener("message", (event) => {
45
- const nonce = JSON.parse(event.data);
46
- debug2("got nonce from broadcast channel", nonce);
47
- ref.nonce = nonce;
48
- });
49
- });
50
- }
51
- function hasNonce() {
52
- return ref.nonce >= 0;
53
- }
54
- function getNonce() {
55
- if (!hasNonce()) throw new Error("call resetNonce before using getNonce");
56
- return ref.nonce;
57
- }
58
- function nextNonce() {
59
- if (!hasNonce()) throw new Error("call resetNonce before using nextNonce");
60
- const nonce = ref.nonce++;
61
- channel?.postMessage(JSON.stringify(ref.nonce));
62
- return nonce;
63
- }
64
- async function resetNonce() {
65
- ref.noncePromise ??= (async () => {
66
- ref.nonce = await getAction2(client, getTransactionCount, "getTransactionCount")({ address, blockTag });
67
- ref.noncePromise = null;
68
- channel?.postMessage(JSON.stringify(ref.nonce));
69
- debug2("reset nonce to", ref.nonce);
70
- })();
71
- await ref.noncePromise;
72
- }
73
- function shouldResetNonce(error) {
74
- const nonceError = findCause(error, ({ name }) => name === "NonceTooLowError" || name === "NonceTooHighError");
75
- return nonceError != null;
76
- }
77
- const mempoolQueue = new PQueue({ concurrency: queueConcurrency });
78
- return {
79
- hasNonce,
80
- getNonce,
81
- nextNonce,
82
- resetNonce,
83
- shouldResetNonce,
84
- mempoolQueue
85
- };
86
- }
87
-
88
- // src/getNonceManager.ts
89
- var nonceManagers = /* @__PURE__ */ new Map();
90
- async function getNonceManager({
91
- client,
92
- address,
93
- // TODO: rename to account?
94
- blockTag = "latest",
95
- ...opts
96
- }) {
97
- const id = await getNonceManagerId({ client, address, blockTag });
98
- const nonceManager = nonceManagers.get(id) ?? createNonceManager({ client, address, blockTag, ...opts });
99
- if (!nonceManagers.has(id)) {
100
- nonceManagers.set(id, nonceManager);
101
- }
102
- if (!nonceManager.hasNonce()) {
103
- await nonceManager.resetNonce();
104
- }
105
- return nonceManager;
106
- }
107
-
108
- // src/sendTransaction.ts
109
- import { sendTransaction as viem_sendTransaction } from "viem/actions";
110
- import pRetry from "p-retry";
111
- import { parseAccount } from "viem/accounts";
112
-
113
- // src/getFeeRef.ts
114
- import { getChainId as getChainId2 } from "viem/actions";
115
-
116
- // src/createFeeRef.ts
117
- import { estimateFeesPerGas } from "viem/actions";
118
- import { getAction as getAction3 } from "viem/utils";
119
- async function createFeeRef({ client, args, refreshInterval }) {
120
- const feeRef = { fees: {}, lastUpdatedTimestamp: 0 };
121
- async function updateFees() {
122
- const fees = await getAction3(client, estimateFeesPerGas, "estimateFeesPerGas")(args);
123
- feeRef.fees = fees;
124
- feeRef.lastUpdatedTimestamp = Date.now();
125
- }
126
- setInterval(updateFees, refreshInterval);
127
- await updateFees();
128
- return feeRef;
129
- }
130
-
131
- // src/getFeeRef.ts
132
- import { getAction as getAction4 } from "viem/utils";
133
- var feeRefs = /* @__PURE__ */ new Map();
134
- async function getFeeRef(opts) {
135
- const chainId = opts.args?.chain?.id ?? opts.client.chain?.id ?? await getAction4(opts.client, getChainId2, "getChainId")({});
136
- const existingFeeRef = feeRefs.get(chainId);
137
- if (existingFeeRef) {
138
- return existingFeeRef;
139
- }
140
- const feeRef = await createFeeRef(opts);
141
- feeRefs.set(chainId, feeRef);
142
- return feeRef;
143
- }
144
-
145
- // src/sendTransaction.ts
146
- import { getAction as getAction5 } from "viem/utils";
147
- var debug3 = debug.extend("sendTransaction");
148
- async function sendTransaction(client, request, opts = {}) {
149
- const rawAccount = request.account ?? client.account;
150
- if (!rawAccount) {
151
- throw new Error("No account provided");
152
- }
153
- const account = parseAccount(rawAccount);
154
- const chain = client.chain;
155
- const nonceManager = await getNonceManager({
156
- client: opts.publicClient ?? client,
157
- address: account.address,
158
- queueConcurrency: opts.queueConcurrency
159
- });
160
- const feeRef = await getFeeRef({
161
- client: opts.publicClient ?? client,
162
- refreshInterval: 1e4,
163
- args: { chain }
164
- });
165
- return await nonceManager.mempoolQueue.add(
166
- () => pRetry(
167
- async () => {
168
- const nonce = nonceManager.nextNonce();
169
- const params = {
170
- // viem_sendTransaction internally estimates gas, which we want to happen on the pending block
171
- blockTag: "pending",
172
- ...feeRef.fees,
173
- ...request,
174
- nonce
175
- };
176
- debug3("sending tx to", request.to, "with nonce", nonce);
177
- return await getAction5(client, viem_sendTransaction, "sendTransaction")(params);
178
- },
179
- {
180
- retries: 3,
181
- onFailedAttempt: async (error) => {
182
- debug3("failed, resetting nonce");
183
- await nonceManager.resetNonce();
184
- if (nonceManager.shouldResetNonce(error)) {
185
- debug3("got nonce error, retrying", error.message);
186
- return;
187
- }
188
- if (String(error).includes("transaction underpriced")) {
189
- debug3("got transaction underpriced error, retrying", error.message);
190
- return;
191
- }
192
- throw error;
193
- }
194
- }
195
- ),
196
- { throwOnTimeout: true }
197
- );
198
- }
199
-
200
- export {
201
- getNonceManagerId,
202
- findCause,
203
- createNonceManager,
204
- getNonceManager,
205
- getFeeRef,
206
- sendTransaction
207
- };
208
- //# sourceMappingURL=chunk-5Q2OTK63.js.map
@@ -1,64 +0,0 @@
1
- // src/resourceTypes.ts
2
- var resourceTypes = ["table", "offchainTable", "namespace", "system"];
3
-
4
- // src/resourceToHex.ts
5
- import { stringToHex, concatHex } from "viem";
6
- var resourceTypeIds = {
7
- // keep these in sync with storeResourceTypes.sol
8
- table: "tb",
9
- offchainTable: "ot",
10
- // keep these in sync with worldResourceTypes.sol
11
- namespace: "ns",
12
- system: "sy"
13
- };
14
- function resourceToHex(resource) {
15
- const typeId = resourceTypeIds[resource.type];
16
- if (resource.namespace.length > 14) {
17
- throw new Error(`Namespaces must fit into \`bytes14\`, but "${resource.namespace}" is too long.`);
18
- }
19
- return concatHex([
20
- stringToHex(typeId, { size: 2 }),
21
- stringToHex(resource.namespace, { size: 14 }),
22
- stringToHex(resource.name.slice(0, 16), { size: 16 })
23
- ]);
24
- }
25
-
26
- // src/resourceToLabel.ts
27
- var rootNamespace = "";
28
- function resourceToLabel({
29
- namespace,
30
- name
31
- }) {
32
- return namespace === rootNamespace ? name : `${namespace}__${name}`;
33
- }
34
-
35
- // src/hexToResource.ts
36
- import { hexToString, sliceHex } from "viem";
37
- var resourceTypeIdToType = Object.fromEntries(
38
- Object.entries(resourceTypeIds).map(([key, value]) => [value, key])
39
- );
40
- function getResourceType(resourceTypeId) {
41
- const type = resourceTypeIdToType[resourceTypeId];
42
- if (resourceTypes.includes(type)) {
43
- return type;
44
- }
45
- }
46
- function hexToResource(hex) {
47
- const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\0+$/, "");
48
- const type = getResourceType(resourceTypeId);
49
- const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\0+$/, "");
50
- const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, "");
51
- if (!type) {
52
- throw new Error(`Unknown type (${resourceTypeId}) for resource (${resourceToLabel({ namespace, name })})`);
53
- }
54
- return { resourceId: hex, type, namespace, name };
55
- }
56
-
57
- export {
58
- resourceTypes,
59
- resourceTypeIds,
60
- resourceToHex,
61
- resourceToLabel,
62
- hexToResource
63
- };
64
- //# sourceMappingURL=chunk-6Y3PYSE7.js.map
@@ -1,11 +0,0 @@
1
- // src/debug.ts
2
- import createDebug from "debug";
3
- var debug = createDebug("mud:common");
4
- var error = createDebug("mud:common");
5
- debug.log = console.debug.bind(console);
6
- error.log = console.error.bind(console);
7
-
8
- export {
9
- debug
10
- };
11
- //# sourceMappingURL=chunk-73UWXFXB.js.map
@@ -1,16 +0,0 @@
1
- // src/utils/uniqueBy.ts
2
- function uniqueBy(values, getKey) {
3
- const map = /* @__PURE__ */ new Map();
4
- for (const value of values) {
5
- const key = getKey(value);
6
- if (!map.has(key)) {
7
- map.set(key, value);
8
- }
9
- }
10
- return Array.from(map.values());
11
- }
12
-
13
- export {
14
- uniqueBy
15
- };
16
- //# sourceMappingURL=chunk-CHXZROA7.js.map
@@ -1,12 +0,0 @@
1
- // src/errors/MUDError.ts
2
- var MUDError = class extends Error {
3
- constructor() {
4
- super(...arguments);
5
- this.name = "MUDError";
6
- }
7
- };
8
-
9
- export {
10
- MUDError
11
- };
12
- //# sourceMappingURL=chunk-GRGLAPN2.js.map