@latticexyz/common 2.2.18-f0433092876e2ac9b5b12cd0ecae9c120a2d0368 → 2.2.18
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/dist/actions.js +39 -1
- package/dist/actions.js.map +1 -1
- package/dist/chains.d.ts +10 -6
- package/dist/chains.js +79 -1
- package/dist/chains.js.map +1 -1
- package/dist/chunk-324BIDBP.js +70 -0
- package/dist/{chunk-DPUUE7NM.js.map → chunk-324BIDBP.js.map} +1 -1
- package/dist/chunk-5Q2OTK63.js +208 -0
- package/dist/{chunk-6FIKI2CG.js.map → chunk-5Q2OTK63.js.map} +1 -1
- package/dist/chunk-6Y3PYSE7.js +64 -0
- package/dist/{chunk-ZIUX7JCQ.js.map → chunk-6Y3PYSE7.js.map} +1 -1
- package/dist/chunk-73UWXFXB.js +11 -0
- package/dist/{chunk-TCWGPC6G.js.map → chunk-73UWXFXB.js.map} +1 -1
- package/dist/chunk-CHXZROA7.js +16 -0
- package/dist/{chunk-ZV2KGJCD.js.map → chunk-CHXZROA7.js.map} +1 -1
- package/dist/chunk-GRGLAPN2.js +12 -0
- package/dist/{chunk-QQCZY3XJ.js.map → chunk-GRGLAPN2.js.map} +1 -1
- package/dist/codegen.js +706 -49
- package/dist/codegen.js.map +1 -1
- package/dist/errors.js +6 -1
- package/dist/foundry.js +71 -2
- package/dist/foundry.js.map +1 -1
- package/dist/index.js +233 -1
- package/dist/index.js.map +1 -1
- package/dist/internal.js +205 -9
- package/dist/internal.js.map +1 -1
- package/dist/kms.js +168 -1
- package/dist/kms.js.map +1 -1
- package/dist/utils.js +122 -1
- package/dist/utils.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-6FIKI2CG.js +0 -2
- package/dist/chunk-DPUUE7NM.js +0 -2
- package/dist/chunk-QQCZY3XJ.js +0 -2
- package/dist/chunk-TCWGPC6G.js +0 -2
- package/dist/chunk-ZIUX7JCQ.js +0 -2
- package/dist/chunk-ZV2KGJCD.js +0 -2
package/dist/actions.js
CHANGED
|
@@ -1,2 +1,40 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
writeContract
|
|
3
|
+
} from "./chunk-324BIDBP.js";
|
|
4
|
+
import {
|
|
5
|
+
sendTransaction
|
|
6
|
+
} from "./chunk-5Q2OTK63.js";
|
|
7
|
+
import "./chunk-73UWXFXB.js";
|
|
8
|
+
|
|
9
|
+
// src/actions/transactionQueue.ts
|
|
10
|
+
function transactionQueue(opts = {}) {
|
|
11
|
+
return (client) => ({
|
|
12
|
+
// Applies to: `client.writeContract`, `getContract(client, ...).write`
|
|
13
|
+
writeContract: (args) => writeContract(client, args, opts),
|
|
14
|
+
// Applies to: `client.sendTransaction`
|
|
15
|
+
sendTransaction: (args) => sendTransaction(client, args, opts)
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// src/actions/writeObserver.ts
|
|
20
|
+
import { getAction } from "viem/utils";
|
|
21
|
+
import { writeContract as writeContract2 } from "viem/actions";
|
|
22
|
+
function writeObserver({
|
|
23
|
+
onWrite
|
|
24
|
+
}) {
|
|
25
|
+
let nextWriteId = 0;
|
|
26
|
+
return (client) => ({
|
|
27
|
+
// Applies to: `client.writeContract`, `getContract(client, ...).write`
|
|
28
|
+
writeContract: (args) => {
|
|
29
|
+
const result = getAction(client, writeContract2, "writeContract")(args);
|
|
30
|
+
const id = `${client.chain.id}:${client.account.address}:${nextWriteId++}`;
|
|
31
|
+
onWrite({ id, request: args, result });
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
transactionQueue,
|
|
38
|
+
writeObserver
|
|
39
|
+
};
|
|
2
40
|
//# sourceMappingURL=actions.js.map
|
package/dist/actions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/actions/transactionQueue.ts","../src/actions/writeObserver.ts"],"sourcesContent":["import type { Transport, Chain, Account, WalletActions, Client } from \"viem\";\nimport { writeContract as mud_writeContract } from \"../writeContract\";\nimport { sendTransaction as mud_sendTransaction } from \"../sendTransaction\";\n\nexport type TransactionQueueOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `simulateContract`, `call`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\nexport function transactionQueue<chain extends Chain | undefined>(\n opts: TransactionQueueOptions<chain> = {},\n): <transport extends Transport, account extends Account | undefined = Account | undefined>(\n client: Client<transport, chain, account>,\n) => Pick<WalletActions<chain, account>, \"writeContract\" | \"sendTransaction\"> {\n return (client) => ({\n // Applies to: `client.writeContract`, `getContract(client, ...).write`\n writeContract: (args) => mud_writeContract(client, args, opts),\n // Applies to: `client.sendTransaction`\n sendTransaction: (args) => mud_sendTransaction(client, args, opts),\n });\n}\n","import type {\n WriteContractParameters,\n Transport,\n Chain,\n Account,\n WalletActions,\n WriteContractReturnType,\n Client,\n} from \"viem\";\nimport { getAction } from \"viem/utils\";\nimport { writeContract } from \"viem/actions\";\nimport { type ContractWrite } from \"../getContract\";\n\ntype WriteObserverParameters = { onWrite: (write: ContractWrite) => void };\n\nexport function writeObserver<TChain extends Chain, TAccount extends Account>({\n onWrite,\n}: WriteObserverParameters): (\n client: Client<Transport, TChain, TAccount>,\n) => Pick<WalletActions<TChain, TAccount>, \"writeContract\"> {\n let nextWriteId = 0;\n\n return (client) => ({\n // Applies to: `client.writeContract`, `getContract(client, ...).write`\n writeContract: (args): Promise<WriteContractReturnType> => {\n const result = getAction(client, writeContract, \"writeContract\")(args);\n\n const id = `${client.chain.id}:${client.account.address}:${nextWriteId++}`;\n onWrite({ id, request: args as WriteContractParameters, result });\n\n return result;\n },\n });\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/actions/transactionQueue.ts","../src/actions/writeObserver.ts"],"sourcesContent":["import type { Transport, Chain, Account, WalletActions, Client } from \"viem\";\nimport { writeContract as mud_writeContract } from \"../writeContract\";\nimport { sendTransaction as mud_sendTransaction } from \"../sendTransaction\";\n\nexport type TransactionQueueOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `simulateContract`, `call`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\nexport function transactionQueue<chain extends Chain | undefined>(\n opts: TransactionQueueOptions<chain> = {},\n): <transport extends Transport, account extends Account | undefined = Account | undefined>(\n client: Client<transport, chain, account>,\n) => Pick<WalletActions<chain, account>, \"writeContract\" | \"sendTransaction\"> {\n return (client) => ({\n // Applies to: `client.writeContract`, `getContract(client, ...).write`\n writeContract: (args) => mud_writeContract(client, args, opts),\n // Applies to: `client.sendTransaction`\n sendTransaction: (args) => mud_sendTransaction(client, args, opts),\n });\n}\n","import type {\n WriteContractParameters,\n Transport,\n Chain,\n Account,\n WalletActions,\n WriteContractReturnType,\n Client,\n} from \"viem\";\nimport { getAction } from \"viem/utils\";\nimport { writeContract } from \"viem/actions\";\nimport { type ContractWrite } from \"../getContract\";\n\ntype WriteObserverParameters = { onWrite: (write: ContractWrite) => void };\n\nexport function writeObserver<TChain extends Chain, TAccount extends Account>({\n onWrite,\n}: WriteObserverParameters): (\n client: Client<Transport, TChain, TAccount>,\n) => Pick<WalletActions<TChain, TAccount>, \"writeContract\"> {\n let nextWriteId = 0;\n\n return (client) => ({\n // Applies to: `client.writeContract`, `getContract(client, ...).write`\n writeContract: (args): Promise<WriteContractReturnType> => {\n const result = getAction(client, writeContract, \"writeContract\")(args);\n\n const id = `${client.chain.id}:${client.account.address}:${nextWriteId++}`;\n onWrite({ id, request: args as WriteContractParameters, result });\n\n return result;\n },\n });\n}\n"],"mappings":";;;;;;;;;AAqBO,SAAS,iBACd,OAAuC,CAAC,GAGoC;AAC5E,SAAO,CAAC,YAAY;AAAA;AAAA,IAElB,eAAe,CAAC,SAAS,cAAkB,QAAQ,MAAM,IAAI;AAAA;AAAA,IAE7D,iBAAiB,CAAC,SAAS,gBAAoB,QAAQ,MAAM,IAAI;AAAA,EACnE;AACF;;;ACvBA,SAAS,iBAAiB;AAC1B,SAAS,iBAAAA,sBAAqB;AAKvB,SAAS,cAA8D;AAAA,EAC5E;AACF,GAE4D;AAC1D,MAAI,cAAc;AAElB,SAAO,CAAC,YAAY;AAAA;AAAA,IAElB,eAAe,CAAC,SAA2C;AACzD,YAAM,SAAS,UAAU,QAAQA,gBAAe,eAAe,EAAE,IAAI;AAErE,YAAM,KAAK,GAAG,OAAO,MAAM,EAAE,IAAI,OAAO,QAAQ,OAAO,IAAI,aAAa;AACxE,cAAQ,EAAE,IAAI,SAAS,MAAiC,OAAO,CAAC;AAEhE,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":["writeContract"]}
|
package/dist/chains.d.ts
CHANGED
|
@@ -359,6 +359,16 @@ declare const redstone: {
|
|
|
359
359
|
};
|
|
360
360
|
|
|
361
361
|
declare const garnet: {
|
|
362
|
+
readonly rpcUrls: {
|
|
363
|
+
readonly bundler: {
|
|
364
|
+
readonly http: readonly ["https://rpc.garnetchain.com"];
|
|
365
|
+
readonly webSocket: readonly ["wss://rpc.garnetchain.com"];
|
|
366
|
+
};
|
|
367
|
+
readonly default: {
|
|
368
|
+
readonly http: readonly ["https://rpc.garnetchain.com"];
|
|
369
|
+
readonly webSocket: readonly ["wss://rpc.garnetchain.com"];
|
|
370
|
+
};
|
|
371
|
+
};
|
|
362
372
|
readonly iconUrls: readonly ["https://redstone.xyz/chain-icons/garnet.png"];
|
|
363
373
|
readonly indexerUrl: "https://indexer.mud.garnetchain.com";
|
|
364
374
|
readonly blockExplorers: {
|
|
@@ -415,12 +425,6 @@ declare const garnet: {
|
|
|
415
425
|
readonly symbol: "ETH";
|
|
416
426
|
readonly decimals: 18;
|
|
417
427
|
};
|
|
418
|
-
readonly rpcUrls: {
|
|
419
|
-
readonly default: {
|
|
420
|
-
readonly http: readonly ["https://rpc.garnetchain.com"];
|
|
421
|
-
readonly webSocket: readonly ["wss://rpc.garnetchain.com"];
|
|
422
|
-
};
|
|
423
|
-
};
|
|
424
428
|
readonly sourceId: 17000;
|
|
425
429
|
readonly testnet: true;
|
|
426
430
|
readonly custom?: Record<string, unknown> | undefined;
|
package/dist/chains.js
CHANGED
|
@@ -1,2 +1,80 @@
|
|
|
1
|
-
|
|
1
|
+
// src/chains/mudFoundry.ts
|
|
2
|
+
import { foundry } from "viem/chains";
|
|
3
|
+
var mudFoundry = {
|
|
4
|
+
...foundry,
|
|
5
|
+
fees: {
|
|
6
|
+
defaultPriorityFee: 0n
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// src/chains/redstone.ts
|
|
11
|
+
import { redstone as redstoneConfig } from "viem/chains";
|
|
12
|
+
var redstone = {
|
|
13
|
+
...redstoneConfig,
|
|
14
|
+
iconUrls: ["https://redstone.xyz/chain-icons/redstone.png"],
|
|
15
|
+
indexerUrl: "https://indexer.mud.redstonechain.com"
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/chains/garnet.ts
|
|
19
|
+
import { garnet as garnetConfig } from "viem/chains";
|
|
20
|
+
var garnet = {
|
|
21
|
+
...garnetConfig,
|
|
22
|
+
rpcUrls: {
|
|
23
|
+
...garnetConfig.rpcUrls,
|
|
24
|
+
bundler: garnetConfig.rpcUrls.default
|
|
25
|
+
},
|
|
26
|
+
iconUrls: ["https://redstone.xyz/chain-icons/garnet.png"],
|
|
27
|
+
indexerUrl: "https://indexer.mud.garnetchain.com"
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/chains/rhodolite.ts
|
|
31
|
+
import { chainConfig } from "viem/op-stack";
|
|
32
|
+
var sourceId = 17e3;
|
|
33
|
+
var defaultRpcUrls = {
|
|
34
|
+
http: ["https://rpc.rhodolitechain.com"],
|
|
35
|
+
webSocket: ["wss://rpc.rhodolitechain.com"]
|
|
36
|
+
};
|
|
37
|
+
var rhodolite = {
|
|
38
|
+
...chainConfig,
|
|
39
|
+
name: "Rhodolite Devnet",
|
|
40
|
+
testnet: true,
|
|
41
|
+
id: 17420,
|
|
42
|
+
sourceId,
|
|
43
|
+
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
44
|
+
rpcUrls: {
|
|
45
|
+
default: defaultRpcUrls,
|
|
46
|
+
bundler: defaultRpcUrls,
|
|
47
|
+
quarryPassIssuer: defaultRpcUrls,
|
|
48
|
+
wiresaw: defaultRpcUrls
|
|
49
|
+
},
|
|
50
|
+
contracts: {
|
|
51
|
+
...chainConfig.contracts,
|
|
52
|
+
l1StandardBridge: {
|
|
53
|
+
[sourceId]: {
|
|
54
|
+
address: "0x6487446e0B9FAEa90F6a9772A6448cFa780E30F9"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
quarryPaymaster: {
|
|
58
|
+
address: "0x7ca1b85aca23fccf2fbac14c02b5e8a6432639b9"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
blockExplorers: {
|
|
62
|
+
default: {
|
|
63
|
+
name: "Blockscout",
|
|
64
|
+
url: "https://explorer.rhodolitechain.com"
|
|
65
|
+
},
|
|
66
|
+
worldsExplorer: {
|
|
67
|
+
name: "MUD Worlds Explorer",
|
|
68
|
+
url: "https://explorer.mud.dev/rhodolite/worlds"
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
iconUrls: ["https://redstone.xyz/chain-icons/rhodolite.png"],
|
|
72
|
+
indexerUrl: "https://indexer.mud.rhodolitechain.com"
|
|
73
|
+
};
|
|
74
|
+
export {
|
|
75
|
+
garnet,
|
|
76
|
+
mudFoundry,
|
|
77
|
+
redstone,
|
|
78
|
+
rhodolite
|
|
79
|
+
};
|
|
2
80
|
//# sourceMappingURL=chains.js.map
|
package/dist/chains.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/chains/mudFoundry.ts","../src/chains/redstone.ts","../src/chains/garnet.ts","../src/chains/rhodolite.ts"],"sourcesContent":["import { foundry } from \"viem/chains\";\nimport { MUDChain } from \"./types\";\n\nexport const mudFoundry = {\n ...foundry,\n fees: {\n defaultPriorityFee: 0n,\n },\n} as const satisfies MUDChain;\n","import { redstone as redstoneConfig } from \"viem/chains\";\nimport type { MUDChain } from \"./types\";\n\nexport const redstone = {\n ...redstoneConfig,\n iconUrls: [\"https://redstone.xyz/chain-icons/redstone.png\"],\n indexerUrl: \"https://indexer.mud.redstonechain.com\",\n} as const satisfies MUDChain;\n","import { garnet as garnetConfig } from \"viem/chains\";\nimport type { MUDChain } from \"./types\";\n\nexport const garnet = {\n ...garnetConfig,\n iconUrls: [\"https://redstone.xyz/chain-icons/garnet.png\"],\n indexerUrl: \"https://indexer.mud.garnetchain.com\",\n} as const satisfies MUDChain;\n","import { chainConfig } from \"viem/op-stack\";\nimport { MUDChain } from \"./types\";\nimport { Chain } from \"viem\";\n\nconst sourceId = 17000;\n\nconst defaultRpcUrls = {\n http: [\"https://rpc.rhodolitechain.com\"],\n webSocket: [\"wss://rpc.rhodolitechain.com\"],\n} as const satisfies Chain[\"rpcUrls\"][\"default\"];\n\nexport const rhodolite = {\n ...chainConfig,\n name: \"Rhodolite Devnet\",\n testnet: true,\n id: 17420,\n sourceId,\n nativeCurrency: { name: \"Ether\", symbol: \"ETH\", decimals: 18 },\n rpcUrls: {\n default: defaultRpcUrls,\n bundler: defaultRpcUrls,\n quarryPassIssuer: defaultRpcUrls,\n wiresaw: defaultRpcUrls,\n },\n contracts: {\n ...chainConfig.contracts,\n l1StandardBridge: {\n [sourceId]: {\n address: \"0x6487446e0B9FAEa90F6a9772A6448cFa780E30F9\",\n },\n },\n quarryPaymaster: {\n address: \"0x7ca1b85aca23fccf2fbac14c02b5e8a6432639b9\",\n },\n },\n blockExplorers: {\n default: {\n name: \"Blockscout\",\n url: \"https://explorer.rhodolitechain.com\",\n },\n worldsExplorer: {\n name: \"MUD Worlds Explorer\",\n url: \"https://explorer.mud.dev/rhodolite/worlds\",\n },\n },\n iconUrls: [\"https://redstone.xyz/chain-icons/rhodolite.png\"],\n indexerUrl: \"https://indexer.mud.rhodolitechain.com\",\n} as const satisfies MUDChain;\n"],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"sources":["../src/chains/mudFoundry.ts","../src/chains/redstone.ts","../src/chains/garnet.ts","../src/chains/rhodolite.ts"],"sourcesContent":["import { foundry } from \"viem/chains\";\nimport { MUDChain } from \"./types\";\n\nexport const mudFoundry = {\n ...foundry,\n fees: {\n defaultPriorityFee: 0n,\n },\n} as const satisfies MUDChain;\n","import { redstone as redstoneConfig } from \"viem/chains\";\nimport type { MUDChain } from \"./types\";\n\nexport const redstone = {\n ...redstoneConfig,\n iconUrls: [\"https://redstone.xyz/chain-icons/redstone.png\"],\n indexerUrl: \"https://indexer.mud.redstonechain.com\",\n} as const satisfies MUDChain;\n","import { garnet as garnetConfig } from \"viem/chains\";\nimport type { MUDChain } from \"./types\";\n\nexport const garnet = {\n ...garnetConfig,\n rpcUrls: {\n ...garnetConfig.rpcUrls,\n bundler: garnetConfig.rpcUrls.default,\n },\n iconUrls: [\"https://redstone.xyz/chain-icons/garnet.png\"],\n indexerUrl: \"https://indexer.mud.garnetchain.com\",\n} as const satisfies MUDChain;\n","import { chainConfig } from \"viem/op-stack\";\nimport { MUDChain } from \"./types\";\nimport { Chain } from \"viem\";\n\nconst sourceId = 17000;\n\nconst defaultRpcUrls = {\n http: [\"https://rpc.rhodolitechain.com\"],\n webSocket: [\"wss://rpc.rhodolitechain.com\"],\n} as const satisfies Chain[\"rpcUrls\"][\"default\"];\n\nexport const rhodolite = {\n ...chainConfig,\n name: \"Rhodolite Devnet\",\n testnet: true,\n id: 17420,\n sourceId,\n nativeCurrency: { name: \"Ether\", symbol: \"ETH\", decimals: 18 },\n rpcUrls: {\n default: defaultRpcUrls,\n bundler: defaultRpcUrls,\n quarryPassIssuer: defaultRpcUrls,\n wiresaw: defaultRpcUrls,\n },\n contracts: {\n ...chainConfig.contracts,\n l1StandardBridge: {\n [sourceId]: {\n address: \"0x6487446e0B9FAEa90F6a9772A6448cFa780E30F9\",\n },\n },\n quarryPaymaster: {\n address: \"0x7ca1b85aca23fccf2fbac14c02b5e8a6432639b9\",\n },\n },\n blockExplorers: {\n default: {\n name: \"Blockscout\",\n url: \"https://explorer.rhodolitechain.com\",\n },\n worldsExplorer: {\n name: \"MUD Worlds Explorer\",\n url: \"https://explorer.mud.dev/rhodolite/worlds\",\n },\n },\n iconUrls: [\"https://redstone.xyz/chain-icons/rhodolite.png\"],\n indexerUrl: \"https://indexer.mud.rhodolitechain.com\",\n} as const satisfies MUDChain;\n"],"mappings":";AAAA,SAAS,eAAe;AAGjB,IAAM,aAAa;AAAA,EACxB,GAAG;AAAA,EACH,MAAM;AAAA,IACJ,oBAAoB;AAAA,EACtB;AACF;;;ACRA,SAAS,YAAY,sBAAsB;AAGpC,IAAM,WAAW;AAAA,EACtB,GAAG;AAAA,EACH,UAAU,CAAC,+CAA+C;AAAA,EAC1D,YAAY;AACd;;;ACPA,SAAS,UAAU,oBAAoB;AAGhC,IAAM,SAAS;AAAA,EACpB,GAAG;AAAA,EACH,SAAS;AAAA,IACP,GAAG,aAAa;AAAA,IAChB,SAAS,aAAa,QAAQ;AAAA,EAChC;AAAA,EACA,UAAU,CAAC,6CAA6C;AAAA,EACxD,YAAY;AACd;;;ACXA,SAAS,mBAAmB;AAI5B,IAAM,WAAW;AAEjB,IAAM,iBAAiB;AAAA,EACrB,MAAM,CAAC,gCAAgC;AAAA,EACvC,WAAW,CAAC,8BAA8B;AAC5C;AAEO,IAAM,YAAY;AAAA,EACvB,GAAG;AAAA,EACH,MAAM;AAAA,EACN,SAAS;AAAA,EACT,IAAI;AAAA,EACJ;AAAA,EACA,gBAAgB,EAAE,MAAM,SAAS,QAAQ,OAAO,UAAU,GAAG;AAAA,EAC7D,SAAS;AAAA,IACP,SAAS;AAAA,IACT,SAAS;AAAA,IACT,kBAAkB;AAAA,IAClB,SAAS;AAAA,EACX;AAAA,EACA,WAAW;AAAA,IACT,GAAG,YAAY;AAAA,IACf,kBAAkB;AAAA,MAChB,CAAC,QAAQ,GAAG;AAAA,QACV,SAAS;AAAA,MACX;AAAA,IACF;AAAA,IACA,iBAAiB;AAAA,MACf,SAAS;AAAA,IACX;AAAA,EACF;AAAA,EACA,gBAAgB;AAAA,IACd,SAAS;AAAA,MACP,MAAM;AAAA,MACN,KAAK;AAAA,IACP;AAAA,IACA,gBAAgB;AAAA,MACd,MAAM;AAAA,MACN,KAAK;AAAA,IACP;AAAA,EACF;AAAA,EACA,UAAU,CAAC,gDAAgD;AAAA,EAC3D,YAAY;AACd;","names":[]}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getFeeRef,
|
|
3
|
+
getNonceManager
|
|
4
|
+
} from "./chunk-5Q2OTK63.js";
|
|
5
|
+
import {
|
|
6
|
+
debug
|
|
7
|
+
} from "./chunk-73UWXFXB.js";
|
|
8
|
+
|
|
9
|
+
// src/writeContract.ts
|
|
10
|
+
import { writeContract as viem_writeContract } from "viem/actions";
|
|
11
|
+
import pRetry from "p-retry";
|
|
12
|
+
import { parseAccount } from "viem/accounts";
|
|
13
|
+
import { getAction } from "viem/utils";
|
|
14
|
+
var debug2 = debug.extend("writeContract");
|
|
15
|
+
async function writeContract(client, request, opts = {}) {
|
|
16
|
+
const rawAccount = request.account ?? client.account;
|
|
17
|
+
if (!rawAccount) {
|
|
18
|
+
throw new Error("No account provided");
|
|
19
|
+
}
|
|
20
|
+
const account = parseAccount(rawAccount);
|
|
21
|
+
const chain = client.chain;
|
|
22
|
+
const nonceManager = await getNonceManager({
|
|
23
|
+
client: opts.publicClient ?? client,
|
|
24
|
+
address: account.address,
|
|
25
|
+
queueConcurrency: opts.queueConcurrency
|
|
26
|
+
});
|
|
27
|
+
const feeRef = await getFeeRef({
|
|
28
|
+
client: opts.publicClient ?? client,
|
|
29
|
+
refreshInterval: 1e4,
|
|
30
|
+
args: { chain }
|
|
31
|
+
});
|
|
32
|
+
return nonceManager.mempoolQueue.add(
|
|
33
|
+
() => pRetry(
|
|
34
|
+
async () => {
|
|
35
|
+
const nonce = nonceManager.nextNonce();
|
|
36
|
+
const params = {
|
|
37
|
+
// viem_writeContract internally estimates gas, which we want to happen on the pending block
|
|
38
|
+
blockTag: "pending",
|
|
39
|
+
...feeRef.fees,
|
|
40
|
+
...request,
|
|
41
|
+
nonce
|
|
42
|
+
};
|
|
43
|
+
debug2("calling", params.functionName, "at", params.address, "with nonce", nonce);
|
|
44
|
+
return await getAction(client, viem_writeContract, "writeContract")(params);
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
retries: 3,
|
|
48
|
+
onFailedAttempt: async (error) => {
|
|
49
|
+
debug2("failed, resetting nonce");
|
|
50
|
+
await nonceManager.resetNonce();
|
|
51
|
+
if (nonceManager.shouldResetNonce(error)) {
|
|
52
|
+
debug2("got nonce error, retrying", error.message);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (String(error).includes("transaction underpriced")) {
|
|
56
|
+
debug2("got transaction underpriced error, retrying", error.message);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
),
|
|
63
|
+
{ throwOnTimeout: true }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
writeContract
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=chunk-324BIDBP.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/writeContract.ts"],"sourcesContent":["import {\n Abi,\n Account,\n Chain,\n Client,\n Transport,\n WriteContractParameters,\n WriteContractReturnType,\n ContractFunctionName,\n ContractFunctionArgs,\n} from \"viem\";\nimport { writeContract as viem_writeContract } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\nimport { getFeeRef } from \"./getFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst debug = parentDebug.extend(\"writeContract\");\n\nexport type WriteContractExtraOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `simulateContract`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\n/** @deprecated Use `walletClient.extend(transactionQueue())` instead. */\nexport async function writeContract<\n chain extends Chain | undefined,\n account extends Account | undefined,\n const abi extends Abi | readonly unknown[],\n functionName extends ContractFunctionName<abi, \"nonpayable\" | \"payable\">,\n args extends ContractFunctionArgs<abi, \"nonpayable\" | \"payable\", functionName>,\n chainOverride extends Chain | undefined,\n>(\n client: Client<Transport, chain, account>,\n request: WriteContractParameters<abi, functionName, args, chain, account, chainOverride>,\n opts: WriteContractExtraOptions<chain> = {},\n): Promise<WriteContractReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n const chain = client.chain;\n\n const nonceManager = await getNonceManager({\n client: opts.publicClient ?? client,\n address: account.address,\n queueConcurrency: opts.queueConcurrency,\n });\n\n const feeRef = await getFeeRef({\n client: opts.publicClient ?? client,\n refreshInterval: 10000,\n args: { chain },\n });\n\n return nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n const nonce = nonceManager.nextNonce();\n const params = {\n // viem_writeContract internally estimates gas, which we want to happen on the pending block\n blockTag: \"pending\",\n ...feeRef.fees,\n ...request,\n nonce,\n } as const satisfies WriteContractParameters<abi, functionName, args, chain, account, chainOverride>;\n debug(\"calling\", params.functionName, \"at\", params.address, \"with nonce\", nonce);\n return await getAction(client, viem_writeContract, \"writeContract\")(params as never);\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // in case this tx failed before hitting the mempool (i.e. gas estimation error), reset nonce so we don't skip past the unused nonce\n debug(\"failed, resetting nonce\");\n await nonceManager.resetNonce();\n // retry nonce errors\n // TODO: upgrade p-retry and move this to shouldRetry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n return;\n }\n\n if (String(error).includes(\"transaction underpriced\")) {\n debug(\"got transaction underpriced error, retrying\", error.message);\n return;\n }\n\n throw error;\n },\n },\n ),\n { throwOnTimeout: true },\n );\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/writeContract.ts"],"sourcesContent":["import {\n Abi,\n Account,\n Chain,\n Client,\n Transport,\n WriteContractParameters,\n WriteContractReturnType,\n ContractFunctionName,\n ContractFunctionArgs,\n} from \"viem\";\nimport { writeContract as viem_writeContract } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\nimport { getFeeRef } from \"./getFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst debug = parentDebug.extend(\"writeContract\");\n\nexport type WriteContractExtraOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `simulateContract`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\n/** @deprecated Use `walletClient.extend(transactionQueue())` instead. */\nexport async function writeContract<\n chain extends Chain | undefined,\n account extends Account | undefined,\n const abi extends Abi | readonly unknown[],\n functionName extends ContractFunctionName<abi, \"nonpayable\" | \"payable\">,\n args extends ContractFunctionArgs<abi, \"nonpayable\" | \"payable\", functionName>,\n chainOverride extends Chain | undefined,\n>(\n client: Client<Transport, chain, account>,\n request: WriteContractParameters<abi, functionName, args, chain, account, chainOverride>,\n opts: WriteContractExtraOptions<chain> = {},\n): Promise<WriteContractReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n const chain = client.chain;\n\n const nonceManager = await getNonceManager({\n client: opts.publicClient ?? client,\n address: account.address,\n queueConcurrency: opts.queueConcurrency,\n });\n\n const feeRef = await getFeeRef({\n client: opts.publicClient ?? client,\n refreshInterval: 10000,\n args: { chain },\n });\n\n return nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n const nonce = nonceManager.nextNonce();\n const params = {\n // viem_writeContract internally estimates gas, which we want to happen on the pending block\n blockTag: \"pending\",\n ...feeRef.fees,\n ...request,\n nonce,\n } as const satisfies WriteContractParameters<abi, functionName, args, chain, account, chainOverride>;\n debug(\"calling\", params.functionName, \"at\", params.address, \"with nonce\", nonce);\n return await getAction(client, viem_writeContract, \"writeContract\")(params as never);\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // in case this tx failed before hitting the mempool (i.e. gas estimation error), reset nonce so we don't skip past the unused nonce\n debug(\"failed, resetting nonce\");\n await nonceManager.resetNonce();\n // retry nonce errors\n // TODO: upgrade p-retry and move this to shouldRetry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n return;\n }\n\n if (String(error).includes(\"transaction underpriced\")) {\n debug(\"got transaction underpriced error, retrying\", error.message);\n return;\n }\n\n throw error;\n },\n },\n ),\n { throwOnTimeout: true },\n );\n}\n"],"mappings":";;;;;;;;;AAWA,SAAS,iBAAiB,0BAA0B;AACpD,OAAO,YAAY;AAGnB,SAAS,oBAAoB;AAE7B,SAAS,iBAAiB;AAE1B,IAAMA,SAAQ,MAAY,OAAO,eAAe;AAoBhD,eAAsB,cAQpB,QACA,SACA,OAAyC,CAAC,GACR;AAClC,QAAM,aAAa,QAAQ,WAAW,OAAO;AAC7C,MAAI,CAAC,YAAY;AAEf,UAAM,IAAI,MAAM,qBAAqB;AAAA,EACvC;AACA,QAAM,UAAU,aAAa,UAAU;AACvC,QAAM,QAAQ,OAAO;AAErB,QAAM,eAAe,MAAM,gBAAgB;AAAA,IACzC,QAAQ,KAAK,gBAAgB;AAAA,IAC7B,SAAS,QAAQ;AAAA,IACjB,kBAAkB,KAAK;AAAA,EACzB,CAAC;AAED,QAAM,SAAS,MAAM,UAAU;AAAA,IAC7B,QAAQ,KAAK,gBAAgB;AAAA,IAC7B,iBAAiB;AAAA,IACjB,MAAM,EAAE,MAAM;AAAA,EAChB,CAAC;AAED,SAAO,aAAa,aAAa;AAAA,IAC/B,MACE;AAAA,MACE,YAAY;AACV,cAAM,QAAQ,aAAa,UAAU;AACrC,cAAM,SAAS;AAAA;AAAA,UAEb,UAAU;AAAA,UACV,GAAG,OAAO;AAAA,UACV,GAAG;AAAA,UACH;AAAA,QACF;AACA,QAAAA,OAAM,WAAW,OAAO,cAAc,MAAM,OAAO,SAAS,cAAc,KAAK;AAC/E,eAAO,MAAM,UAAU,QAAQ,oBAAoB,eAAe,EAAE,MAAe;AAAA,MACrF;AAAA,MACA;AAAA,QACE,SAAS;AAAA,QACT,iBAAiB,OAAO,UAAU;AAEhC,UAAAA,OAAM,yBAAyB;AAC/B,gBAAM,aAAa,WAAW;AAG9B,cAAI,aAAa,iBAAiB,KAAK,GAAG;AACxC,YAAAA,OAAM,6BAA6B,MAAM,OAAO;AAChD;AAAA,UACF;AAEA,cAAI,OAAO,KAAK,EAAE,SAAS,yBAAyB,GAAG;AACrD,YAAAA,OAAM,+CAA+C,MAAM,OAAO;AAClE;AAAA,UACF;AAEA,gBAAM;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACF,EAAE,gBAAgB,KAAK;AAAA,EACzB;AACF;","names":["debug"]}
|
|
@@ -0,0 +1,208 @@
|
|
|
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 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/getNonceManagerId.ts","../src/findCause.ts","../src/createNonceManager.ts","../src/getNonceManager.ts","../src/sendTransaction.ts","../src/getFeeRef.ts","../src/createFeeRef.ts"],"sourcesContent":["import { BlockTag, Client, Hex, getAddress } from \"viem\";\nimport { getChainId } from \"viem/actions\";\nimport { getAction } from \"viem/utils\";\n\nexport async function getNonceManagerId({\n client,\n address,\n blockTag,\n}: {\n client: Client;\n address: Hex;\n blockTag: BlockTag;\n}): Promise<string> {\n // TODO: improve this so we don't have to call getChainId every time\n const chainId = client.chain?.id ?? (await getAction(client, getChainId, \"getChainId\")({}));\n return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`;\n}\n","export function findCause(error: Error, fn?: (error: Error) => boolean): Error | null {\n if (fn?.(error)) return error;\n if (error.cause instanceof Error) return findCause(error.cause, fn);\n return fn ? null : error;\n}\n","import { BlockTag, Client, Hex } from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\nimport { getTransactionCount } from \"viem/actions\";\nimport PQueue from \"p-queue\";\nimport { getAction } from \"viem/utils\";\nimport { findCause } from \"./findCause\";\n\nconst debug = parentDebug.extend(\"createNonceManager\");\n\nexport type CreateNonceManagerOptions = {\n client: Client;\n address: Hex;\n blockTag?: BlockTag;\n broadcastChannelName?: string;\n queueConcurrency?: number;\n};\n\nexport type CreateNonceManagerResult = {\n hasNonce: () => boolean;\n getNonce: () => number;\n nextNonce: () => number;\n resetNonce: () => Promise<void>;\n shouldResetNonce: (error: Error) => boolean;\n mempoolQueue: PQueue;\n};\n\nexport function createNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"latest\",\n broadcastChannelName,\n queueConcurrency = 1,\n}: CreateNonceManagerOptions): CreateNonceManagerResult {\n const ref = { nonce: -1, noncePromise: null as Promise<void> | null };\n let channel: BroadcastChannel | null = null;\n\n if (typeof BroadcastChannel !== \"undefined\") {\n const channelName = broadcastChannelName\n ? Promise.resolve(broadcastChannelName)\n : getNonceManagerId({ client, address, blockTag });\n channelName.then((name) => {\n channel = new BroadcastChannel(name);\n // TODO: emit some sort of \"connected\" event so other channels can broadcast current nonce\n channel.addEventListener(\"message\", (event) => {\n const nonce = JSON.parse(event.data);\n debug(\"got nonce from broadcast channel\", nonce);\n ref.nonce = nonce;\n });\n });\n }\n\n function hasNonce(): boolean {\n return ref.nonce >= 0;\n }\n\n function getNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using getNonce\");\n return ref.nonce;\n }\n\n function nextNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using nextNonce\");\n const nonce = ref.nonce++;\n channel?.postMessage(JSON.stringify(ref.nonce));\n return nonce;\n }\n\n async function resetNonce(): Promise<void> {\n ref.noncePromise ??= (async (): Promise<void> => {\n ref.nonce = await getAction(client, getTransactionCount, \"getTransactionCount\")({ address, blockTag });\n ref.noncePromise = null;\n channel?.postMessage(JSON.stringify(ref.nonce));\n debug(\"reset nonce to\", ref.nonce);\n })();\n await ref.noncePromise;\n }\n\n function shouldResetNonce(error: Error): boolean {\n const nonceError = findCause(error, ({ name }) => name === \"NonceTooLowError\" || name === \"NonceTooHighError\");\n return nonceError != null;\n }\n\n const mempoolQueue = new PQueue({ concurrency: queueConcurrency });\n\n return {\n hasNonce,\n getNonce,\n nextNonce,\n resetNonce,\n shouldResetNonce,\n mempoolQueue,\n };\n}\n","import { CreateNonceManagerOptions, CreateNonceManagerResult, createNonceManager } from \"./createNonceManager\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\n\nconst nonceManagers = new Map<string, CreateNonceManagerResult>();\n\nexport async function getNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"latest\",\n ...opts\n}: CreateNonceManagerOptions): Promise<CreateNonceManagerResult> {\n const id = await getNonceManagerId({ client, address, blockTag });\n\n const nonceManager = nonceManagers.get(id) ?? createNonceManager({ client, address, blockTag, ...opts });\n if (!nonceManagers.has(id)) {\n nonceManagers.set(id, nonceManager);\n }\n\n if (!nonceManager.hasNonce()) {\n await nonceManager.resetNonce();\n }\n\n return nonceManager;\n}\n","import {\n Account,\n Chain,\n Client,\n SendTransactionParameters,\n Transport,\n SendTransactionReturnType,\n SendTransactionRequest,\n} from \"viem\";\nimport { sendTransaction as viem_sendTransaction } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\nimport { getFeeRef } from \"./getFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst debug = parentDebug.extend(\"sendTransaction\");\n\nexport type SendTransactionExtraOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `call`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\n/** @deprecated Use `walletClient.extend(transactionQueue())` instead. */\nexport async function sendTransaction<\n chain extends Chain | undefined,\n account extends Account | undefined,\n const request extends SendTransactionRequest<chain, chainOverride>,\n chainOverride extends Chain | undefined = undefined,\n>(\n client: Client<Transport, chain, account>,\n request: SendTransactionParameters<chain, account, chainOverride, request>,\n opts: SendTransactionExtraOptions<chain> = {},\n): Promise<SendTransactionReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n const chain = client.chain;\n\n const nonceManager = await getNonceManager({\n client: opts.publicClient ?? client,\n address: account.address,\n queueConcurrency: opts.queueConcurrency,\n });\n\n const feeRef = await getFeeRef({\n client: opts.publicClient ?? client,\n refreshInterval: 10000,\n args: { chain },\n });\n\n return await nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n const nonce = nonceManager.nextNonce();\n const params = {\n // viem_sendTransaction internally estimates gas, which we want to happen on the pending block\n blockTag: \"pending\",\n ...feeRef.fees,\n ...request,\n nonce,\n } as const satisfies SendTransactionParameters<chain, account, chainOverride, request>;\n debug(\"sending tx to\", request.to, \"with nonce\", nonce);\n return await getAction(client, viem_sendTransaction, \"sendTransaction\")(params as never);\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // in case this tx failed before hitting the mempool (i.e. gas estimation error), reset nonce so we don't skip past the unused nonce\n debug(\"failed, resetting nonce\");\n await nonceManager.resetNonce();\n // retry nonce errors\n // TODO: upgrade p-retry and move this to shouldRetry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n return;\n }\n\n if (String(error).includes(\"transaction underpriced\")) {\n debug(\"got transaction underpriced error, retrying\", error.message);\n return;\n }\n\n throw error;\n },\n },\n ),\n { throwOnTimeout: true },\n );\n}\n","import { getChainId } from \"viem/actions\";\nimport { CreateFeeRefOptions, FeeRef, createFeeRef } from \"./createFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst feeRefs = new Map<number, FeeRef>();\n\nexport async function getFeeRef(opts: CreateFeeRefOptions): Promise<FeeRef> {\n const chainId =\n opts.args?.chain?.id ?? opts.client.chain?.id ?? (await getAction(opts.client, getChainId, \"getChainId\")({}));\n\n const existingFeeRef = feeRefs.get(chainId);\n if (existingFeeRef) {\n return existingFeeRef;\n }\n\n const feeRef = await createFeeRef(opts);\n feeRefs.set(chainId, feeRef);\n return feeRef;\n}\n","import { EstimateFeesPerGasParameters, Client, EstimateFeesPerGasReturnType } from \"viem\";\nimport { estimateFeesPerGas } from \"viem/actions\";\nimport { getAction } from \"viem/utils\";\n\nexport type CreateFeeRefOptions = {\n client: Client;\n refreshInterval: number;\n args?: EstimateFeesPerGasParameters;\n};\n\nexport type FeeRef = {\n fees: EstimateFeesPerGasReturnType | {};\n lastUpdatedTimestamp: number;\n};\n\n/** Update fee values once every `refreshInterval` instead of right before every request */\nexport async function createFeeRef({ client, args, refreshInterval }: CreateFeeRefOptions): Promise<FeeRef> {\n const feeRef: FeeRef = { fees: {}, lastUpdatedTimestamp: 0 };\n\n async function updateFees(): Promise<void> {\n const fees = await getAction(client, estimateFeesPerGas, \"estimateFeesPerGas\")(args);\n feeRef.fees = fees;\n feeRef.lastUpdatedTimestamp = Date.now();\n }\n\n setInterval(updateFees, refreshInterval);\n await updateFees();\n\n return feeRef;\n}\n"],"mappings":"wCAAA,OAAgC,cAAAA,MAAkB,OAClD,OAAS,cAAAC,MAAkB,eAC3B,OAAS,aAAAC,MAAiB,aAE1B,eAAsBC,EAAkB,CACtC,OAAAC,EACA,QAAAC,EACA,SAAAC,CACF,EAIoB,CAGlB,MAAO,0BADSF,EAAO,OAAO,IAAO,MAAMF,EAAUE,EAAQH,EAAY,YAAY,EAAE,CAAC,CAAC,CACjD,IAAID,EAAWK,CAAO,CAAC,IAAIC,CAAQ,EAC7E,CChBO,SAASC,EAAUC,EAAcC,EAA8C,CACpF,OAAIA,IAAKD,CAAK,EAAUA,EACpBA,EAAM,iBAAiB,MAAcD,EAAUC,EAAM,MAAOC,CAAE,EAC3DA,EAAK,KAAOD,CACrB,CCDA,OAAS,uBAAAE,MAA2B,eACpC,OAAOC,MAAY,UACnB,OAAS,aAAAC,MAAiB,aAG1B,IAAMC,EAAQA,EAAY,OAAO,oBAAoB,EAmB9C,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AAOA,CAAK,EAAE,SAAS,yBAAyB,EAAG,CACrDjB,EAAM,8CAA+CiB,EAAM,OAAO,EAClE,MACF,CAEA,MAAMA,CACR,CACF,CACF,EACF,CAAE,eAAgB,EAAK,CACzB,CACF","names":["getAddress","getChainId","getAction","getNonceManagerId","client","address","blockTag","findCause","error","fn","getTransactionCount","PQueue","getAction","debug","createNonceManager","client","address","blockTag","broadcastChannelName","queueConcurrency","ref","channel","getNonceManagerId","name","event","nonce","hasNonce","getNonce","nextNonce","resetNonce","getAction","getTransactionCount","shouldResetNonce","error","findCause","mempoolQueue","PQueue","nonceManagers","getNonceManager","client","address","blockTag","opts","id","getNonceManagerId","nonceManager","createNonceManager","viem_sendTransaction","pRetry","parseAccount","getChainId","estimateFeesPerGas","getAction","createFeeRef","client","args","refreshInterval","feeRef","updateFees","fees","getAction","feeRefs","getFeeRef","opts","chainId","getChainId","existingFeeRef","feeRef","createFeeRef","getAction","debug","sendTransaction","client","request","opts","rawAccount","account","parseAccount","chain","nonceManager","getNonceManager","feeRef","getFeeRef","pRetry","nonce","params","viem_sendTransaction","error"]}
|
|
1
|
+
{"version":3,"sources":["../src/getNonceManagerId.ts","../src/findCause.ts","../src/createNonceManager.ts","../src/getNonceManager.ts","../src/sendTransaction.ts","../src/getFeeRef.ts","../src/createFeeRef.ts"],"sourcesContent":["import { BlockTag, Client, Hex, getAddress } from \"viem\";\nimport { getChainId } from \"viem/actions\";\nimport { getAction } from \"viem/utils\";\n\nexport async function getNonceManagerId({\n client,\n address,\n blockTag,\n}: {\n client: Client;\n address: Hex;\n blockTag: BlockTag;\n}): Promise<string> {\n // TODO: improve this so we don't have to call getChainId every time\n const chainId = client.chain?.id ?? (await getAction(client, getChainId, \"getChainId\")({}));\n return `mud:createNonceManager:${chainId}:${getAddress(address)}:${blockTag}`;\n}\n","export function findCause(error: Error, fn?: (error: Error) => boolean): Error | null {\n if (fn?.(error)) return error;\n if (error.cause instanceof Error) return findCause(error.cause, fn);\n return fn ? null : error;\n}\n","import { BlockTag, Client, Hex } from \"viem\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\nimport { getTransactionCount } from \"viem/actions\";\nimport PQueue from \"p-queue\";\nimport { getAction } from \"viem/utils\";\nimport { findCause } from \"./findCause\";\n\nconst debug = parentDebug.extend(\"createNonceManager\");\n\nexport type CreateNonceManagerOptions = {\n client: Client;\n address: Hex;\n blockTag?: BlockTag;\n broadcastChannelName?: string;\n queueConcurrency?: number;\n};\n\nexport type CreateNonceManagerResult = {\n hasNonce: () => boolean;\n getNonce: () => number;\n nextNonce: () => number;\n resetNonce: () => Promise<void>;\n shouldResetNonce: (error: Error) => boolean;\n mempoolQueue: PQueue;\n};\n\nexport function createNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"latest\",\n broadcastChannelName,\n queueConcurrency = 1,\n}: CreateNonceManagerOptions): CreateNonceManagerResult {\n const ref = { nonce: -1, noncePromise: null as Promise<void> | null };\n let channel: BroadcastChannel | null = null;\n\n if (typeof BroadcastChannel !== \"undefined\") {\n const channelName = broadcastChannelName\n ? Promise.resolve(broadcastChannelName)\n : getNonceManagerId({ client, address, blockTag });\n channelName.then((name) => {\n channel = new BroadcastChannel(name);\n // TODO: emit some sort of \"connected\" event so other channels can broadcast current nonce\n channel.addEventListener(\"message\", (event) => {\n const nonce = JSON.parse(event.data);\n debug(\"got nonce from broadcast channel\", nonce);\n ref.nonce = nonce;\n });\n });\n }\n\n function hasNonce(): boolean {\n return ref.nonce >= 0;\n }\n\n function getNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using getNonce\");\n return ref.nonce;\n }\n\n function nextNonce(): number {\n if (!hasNonce()) throw new Error(\"call resetNonce before using nextNonce\");\n const nonce = ref.nonce++;\n channel?.postMessage(JSON.stringify(ref.nonce));\n return nonce;\n }\n\n async function resetNonce(): Promise<void> {\n ref.noncePromise ??= (async (): Promise<void> => {\n ref.nonce = await getAction(client, getTransactionCount, \"getTransactionCount\")({ address, blockTag });\n ref.noncePromise = null;\n channel?.postMessage(JSON.stringify(ref.nonce));\n debug(\"reset nonce to\", ref.nonce);\n })();\n await ref.noncePromise;\n }\n\n function shouldResetNonce(error: Error): boolean {\n const nonceError = findCause(error, ({ name }) => name === \"NonceTooLowError\" || name === \"NonceTooHighError\");\n return nonceError != null;\n }\n\n const mempoolQueue = new PQueue({ concurrency: queueConcurrency });\n\n return {\n hasNonce,\n getNonce,\n nextNonce,\n resetNonce,\n shouldResetNonce,\n mempoolQueue,\n };\n}\n","import { CreateNonceManagerOptions, CreateNonceManagerResult, createNonceManager } from \"./createNonceManager\";\nimport { getNonceManagerId } from \"./getNonceManagerId\";\n\nconst nonceManagers = new Map<string, CreateNonceManagerResult>();\n\nexport async function getNonceManager({\n client,\n address, // TODO: rename to account?\n blockTag = \"latest\",\n ...opts\n}: CreateNonceManagerOptions): Promise<CreateNonceManagerResult> {\n const id = await getNonceManagerId({ client, address, blockTag });\n\n const nonceManager = nonceManagers.get(id) ?? createNonceManager({ client, address, blockTag, ...opts });\n if (!nonceManagers.has(id)) {\n nonceManagers.set(id, nonceManager);\n }\n\n if (!nonceManager.hasNonce()) {\n await nonceManager.resetNonce();\n }\n\n return nonceManager;\n}\n","import {\n Account,\n Chain,\n Client,\n SendTransactionParameters,\n Transport,\n SendTransactionReturnType,\n SendTransactionRequest,\n} from \"viem\";\nimport { sendTransaction as viem_sendTransaction } from \"viem/actions\";\nimport pRetry from \"p-retry\";\nimport { debug as parentDebug } from \"./debug\";\nimport { getNonceManager } from \"./getNonceManager\";\nimport { parseAccount } from \"viem/accounts\";\nimport { getFeeRef } from \"./getFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst debug = parentDebug.extend(\"sendTransaction\");\n\nexport type SendTransactionExtraOptions<chain extends Chain | undefined> = {\n /**\n * `publicClient` can be provided to be used in place of the extended viem client for making public action calls\n * (`getChainId`, `getTransactionCount`, `call`). This helps in cases where the extended\n * viem client is a smart account client, like in [permissionless.js](https://github.com/pimlicolabs/permissionless.js),\n * where the transport is the bundler, not an RPC.\n */\n publicClient?: Client<Transport, chain>;\n /**\n * Adjust the number of concurrent calls to the mempool. This defaults to `1` to ensure transactions are ordered\n * and nonces are handled properly. Any number greater than that is likely to see nonce errors and/or transactions\n * arriving out of order, but this may be an acceptable trade-off for some applications that can safely retry.\n * @default 1\n */\n queueConcurrency?: number;\n};\n\n/** @deprecated Use `walletClient.extend(transactionQueue())` instead. */\nexport async function sendTransaction<\n chain extends Chain | undefined,\n account extends Account | undefined,\n const request extends SendTransactionRequest<chain, chainOverride>,\n chainOverride extends Chain | undefined = undefined,\n>(\n client: Client<Transport, chain, account>,\n request: SendTransactionParameters<chain, account, chainOverride, request>,\n opts: SendTransactionExtraOptions<chain> = {},\n): Promise<SendTransactionReturnType> {\n const rawAccount = request.account ?? client.account;\n if (!rawAccount) {\n // TODO: replace with viem AccountNotFoundError once its exported\n throw new Error(\"No account provided\");\n }\n const account = parseAccount(rawAccount);\n const chain = client.chain;\n\n const nonceManager = await getNonceManager({\n client: opts.publicClient ?? client,\n address: account.address,\n queueConcurrency: opts.queueConcurrency,\n });\n\n const feeRef = await getFeeRef({\n client: opts.publicClient ?? client,\n refreshInterval: 10000,\n args: { chain },\n });\n\n return await nonceManager.mempoolQueue.add(\n () =>\n pRetry(\n async () => {\n const nonce = nonceManager.nextNonce();\n const params = {\n // viem_sendTransaction internally estimates gas, which we want to happen on the pending block\n blockTag: \"pending\",\n ...feeRef.fees,\n ...request,\n nonce,\n } as const satisfies SendTransactionParameters<chain, account, chainOverride, request>;\n debug(\"sending tx to\", request.to, \"with nonce\", nonce);\n return await getAction(client, viem_sendTransaction, \"sendTransaction\")(params as never);\n },\n {\n retries: 3,\n onFailedAttempt: async (error) => {\n // in case this tx failed before hitting the mempool (i.e. gas estimation error), reset nonce so we don't skip past the unused nonce\n debug(\"failed, resetting nonce\");\n await nonceManager.resetNonce();\n // retry nonce errors\n // TODO: upgrade p-retry and move this to shouldRetry\n if (nonceManager.shouldResetNonce(error)) {\n debug(\"got nonce error, retrying\", error.message);\n return;\n }\n\n if (String(error).includes(\"transaction underpriced\")) {\n debug(\"got transaction underpriced error, retrying\", error.message);\n return;\n }\n\n throw error;\n },\n },\n ),\n { throwOnTimeout: true },\n );\n}\n","import { getChainId } from \"viem/actions\";\nimport { CreateFeeRefOptions, FeeRef, createFeeRef } from \"./createFeeRef\";\nimport { getAction } from \"viem/utils\";\n\nconst feeRefs = new Map<number, FeeRef>();\n\nexport async function getFeeRef(opts: CreateFeeRefOptions): Promise<FeeRef> {\n const chainId =\n opts.args?.chain?.id ?? opts.client.chain?.id ?? (await getAction(opts.client, getChainId, \"getChainId\")({}));\n\n const existingFeeRef = feeRefs.get(chainId);\n if (existingFeeRef) {\n return existingFeeRef;\n }\n\n const feeRef = await createFeeRef(opts);\n feeRefs.set(chainId, feeRef);\n return feeRef;\n}\n","import { EstimateFeesPerGasParameters, Client, EstimateFeesPerGasReturnType } from \"viem\";\nimport { estimateFeesPerGas } from \"viem/actions\";\nimport { getAction } from \"viem/utils\";\n\nexport type CreateFeeRefOptions = {\n client: Client;\n refreshInterval: number;\n args?: EstimateFeesPerGasParameters;\n};\n\nexport type FeeRef = {\n fees: EstimateFeesPerGasReturnType | {};\n lastUpdatedTimestamp: number;\n};\n\n/** Update fee values once every `refreshInterval` instead of right before every request */\nexport async function createFeeRef({ client, args, refreshInterval }: CreateFeeRefOptions): Promise<FeeRef> {\n const feeRef: FeeRef = { fees: {}, lastUpdatedTimestamp: 0 };\n\n async function updateFees(): Promise<void> {\n const fees = await getAction(client, estimateFeesPerGas, \"estimateFeesPerGas\")(args);\n feeRef.fees = fees;\n feeRef.lastUpdatedTimestamp = Date.now();\n }\n\n setInterval(updateFees, refreshInterval);\n await updateFees();\n\n return feeRef;\n}\n"],"mappings":";;;;;AAAA,SAAgC,kBAAkB;AAClD,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;AAE1B,eAAsB,kBAAkB;AAAA,EACtC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,CAAC;AAED,QAAM,SAAS,MAAM,UAAU;AAAA,IAC7B,QAAQ,KAAK,gBAAgB;AAAA,IAC7B,iBAAiB;AAAA,IACjB,MAAM,EAAE,MAAM;AAAA,EAChB,CAAC;AAED,SAAO,MAAM,aAAa,aAAa;AAAA,IACrC,MACE;AAAA,MACE,YAAY;AACV,cAAM,QAAQ,aAAa,UAAU;AACrC,cAAM,SAAS;AAAA;AAAA,UAEb,UAAU;AAAA,UACV,GAAG,OAAO;AAAA,UACV,GAAG;AAAA,UACH;AAAA,QACF;AACA,QAAAA,OAAM,iBAAiB,QAAQ,IAAI,cAAc,KAAK;AACtD,eAAO,MAAMD,WAAU,QAAQ,sBAAsB,iBAAiB,EAAE,MAAe;AAAA,MACzF;AAAA,MACA;AAAA,QACE,SAAS;AAAA,QACT,iBAAiB,OAAO,UAAU;AAEhC,UAAAC,OAAM,yBAAyB;AAC/B,gBAAM,aAAa,WAAW;AAG9B,cAAI,aAAa,iBAAiB,KAAK,GAAG;AACxC,YAAAA,OAAM,6BAA6B,MAAM,OAAO;AAChD;AAAA,UACF;AAEA,cAAI,OAAO,KAAK,EAAE,SAAS,yBAAyB,GAAG;AACrD,YAAAA,OAAM,+CAA+C,MAAM,OAAO;AAClE;AAAA,UACF;AAEA,gBAAM;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACF,EAAE,gBAAgB,KAAK;AAAA,EACzB;AACF;","names":["getAction","debug","getAction","getChainId","getAction","getAction","getChainId","getAction","debug"]}
|