@keplr-wallet/background 0.13.13 → 0.13.15-rc.0

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.
Files changed (92) hide show
  1. package/build/chains/convert.d.ts +7 -0
  2. package/build/chains/convert.js +109 -0
  3. package/build/chains/convert.js.map +1 -0
  4. package/build/chains/handler.js +1 -1
  5. package/build/chains/handler.js.map +1 -1
  6. package/build/chains/index.d.ts +1 -0
  7. package/build/chains/index.js +1 -0
  8. package/build/chains/index.js.map +1 -1
  9. package/build/chains/messages.d.ts +1 -1
  10. package/build/chains/service.d.ts +3 -9
  11. package/build/chains/service.js +45 -79
  12. package/build/chains/service.js.map +1 -1
  13. package/build/chains-ui/service.d.ts +2 -2
  14. package/build/chains-ui/service.js +5 -6
  15. package/build/chains-ui/service.js.map +1 -1
  16. package/build/chains-update/service.js +2 -2
  17. package/build/chains-update/service.js.map +1 -1
  18. package/build/keyring/handler.js +21 -15
  19. package/build/keyring/handler.js.map +1 -1
  20. package/build/keyring/service.d.ts +0 -1
  21. package/build/keyring/service.js +103 -78
  22. package/build/keyring/service.js.map +1 -1
  23. package/build/keyring-bitcoin/service.js +2 -2
  24. package/build/keyring-bitcoin/service.js.map +1 -1
  25. package/build/keyring-cosmos/service.js +31 -18
  26. package/build/keyring-cosmos/service.js.map +1 -1
  27. package/build/keyring-ethereum/service.js +19 -19
  28. package/build/keyring-ethereum/service.js.map +1 -1
  29. package/build/keyring-ledger/service.js +8 -7
  30. package/build/keyring-ledger/service.js.map +1 -1
  31. package/build/keyring-starknet/service.js +7 -7
  32. package/build/keyring-starknet/service.js.map +1 -1
  33. package/build/manage-view-asset-token/service.d.ts +1 -2
  34. package/build/manage-view-asset-token/service.js +2 -2
  35. package/build/manage-view-asset-token/service.js.map +1 -1
  36. package/build/permission/service.d.ts +1 -2
  37. package/build/permission/service.js +2 -2
  38. package/build/permission/service.js.map +1 -1
  39. package/build/recent-send-history/service.d.ts +4 -4
  40. package/build/recent-send-history/service.js +101 -73
  41. package/build/recent-send-history/service.js.map +1 -1
  42. package/build/secret-wasm/service.d.ts +3 -3
  43. package/build/secret-wasm/service.js +29 -29
  44. package/build/secret-wasm/service.js.map +1 -1
  45. package/build/token-cw20/service.d.ts +6 -6
  46. package/build/token-cw20/service.js +28 -21
  47. package/build/token-cw20/service.js.map +1 -1
  48. package/build/token-erc20/service.d.ts +2 -3
  49. package/build/token-erc20/service.js +10 -15
  50. package/build/token-erc20/service.js.map +1 -1
  51. package/build/token-scan/service.js +27 -23
  52. package/build/token-scan/service.js.map +1 -1
  53. package/build/tx/service.d.ts +1 -0
  54. package/build/tx/service.js +41 -10
  55. package/build/tx/service.js.map +1 -1
  56. package/build/tx-ethereum/service.js +5 -9
  57. package/build/tx-ethereum/service.js.map +1 -1
  58. package/build/tx-executor/service.js +2 -7
  59. package/build/tx-executor/service.js.map +1 -1
  60. package/build/tx-executor/types.d.ts +7 -1
  61. package/build/tx-executor/types.js.map +1 -1
  62. package/build/tx-executor/utils/evm.d.ts +2 -2
  63. package/build/tx-executor/utils/evm.js +75 -26
  64. package/build/tx-executor/utils/evm.js.map +1 -1
  65. package/package.json +13 -13
  66. package/src/chains/convert.ts +126 -0
  67. package/src/chains/handler.ts +1 -1
  68. package/src/chains/index.ts +1 -0
  69. package/src/chains/messages.ts +1 -1
  70. package/src/chains/service.ts +64 -97
  71. package/src/chains-ui/service.ts +8 -7
  72. package/src/chains-update/service.ts +2 -2
  73. package/src/keyring/handler.ts +34 -19
  74. package/src/keyring/service.ts +137 -96
  75. package/src/keyring-bitcoin/service.ts +2 -2
  76. package/src/keyring-cosmos/service.ts +46 -18
  77. package/src/keyring-ethereum/service.ts +24 -17
  78. package/src/keyring-ledger/service.ts +10 -10
  79. package/src/keyring-starknet/service.ts +7 -7
  80. package/src/manage-view-asset-token/service.ts +2 -3
  81. package/src/permission/service.ts +3 -7
  82. package/src/recent-send-history/service.ts +141 -95
  83. package/src/secret-wasm/service.ts +39 -45
  84. package/src/token-cw20/service.ts +36 -30
  85. package/src/token-erc20/service.ts +15 -18
  86. package/src/token-scan/service.ts +34 -21
  87. package/src/tx/service.ts +60 -12
  88. package/src/tx-ethereum/service.ts +8 -8
  89. package/src/tx-executor/service.ts +4 -6
  90. package/src/tx-executor/types.ts +9 -1
  91. package/src/tx-executor/utils/evm.ts +92 -32
  92. package/tsconfig.check.json +2 -0
@@ -2,10 +2,10 @@ import { EVMInfo } from "@keplr-wallet/types";
2
2
  import { simpleFetch } from "@keplr-wallet/simple-fetch";
3
3
  import { UnsignedTransaction } from "@ethersproject/transactions";
4
4
  import { Dec } from "@keplr-wallet/unit";
5
- import { BackgroundTxFeeType } from "../types";
5
+ import { BackgroundTxFeeType, EVMBackgroundTxFeeType } from "../types";
6
6
  import { JsonRpcResponse } from "@keplr-wallet/types";
7
7
 
8
- const ETH_FEE_HISTORY_REWARD_PERCENTILES = [25, 50, 75];
8
+ const ETH_FEE_HISTORY_REWARD_PERCENTILES = [20, 40, 60];
9
9
  const ETH_FEE_SETTINGS_BY_FEE_TYPE: Record<
10
10
  BackgroundTxFeeType,
11
11
  {
@@ -36,6 +36,7 @@ const LATEST_BLOCK_ID = 2;
36
36
  const FEE_HISTORY_ID = 3;
37
37
  const ESTIMATE_GAS_ID = 4;
38
38
  const MAX_PRIORITY_FEE_ID = 5;
39
+ const GAS_PRICE_ID = 6;
39
40
 
40
41
  type BigNumberishLike = string | number | bigint | { toString(): string };
41
42
 
@@ -60,10 +61,14 @@ export async function fillUnsignedEVMTx(
60
61
  evmInfo: EVMInfo,
61
62
  signer: string,
62
63
  tx: UnsignedTransaction,
63
- feeType: BackgroundTxFeeType = "average"
64
+ feeType: EVMBackgroundTxFeeType = "average",
65
+ customPriorityFee?: string,
66
+ customGasPrice?: string
64
67
  ): Promise<UnsignedTransaction> {
65
68
  const hasProvidedPriorityFee = tx.maxPriorityFeePerGas != null;
66
69
  const hasProvidedGasLimit = tx.gasLimit != null;
70
+ const innerFeeType: BackgroundTxFeeType =
71
+ feeType === "custom" ? "average" : feeType;
67
72
 
68
73
  const getTransactionCountRequest = {
69
74
  jsonrpc: "2.0",
@@ -113,6 +118,13 @@ export async function fillUnsignedEVMTx(
113
118
  id: MAX_PRIORITY_FEE_ID,
114
119
  };
115
120
 
121
+ const getGasPriceRequest = {
122
+ jsonrpc: "2.0",
123
+ method: "eth_gasPrice",
124
+ params: [],
125
+ id: GAS_PRICE_ID,
126
+ };
127
+
116
128
  // rpc request in batch (as 2.0 jsonrpc supports batch requests)
117
129
  const batchRequest = [
118
130
  getTransactionCountRequest,
@@ -120,6 +132,7 @@ export async function fillUnsignedEVMTx(
120
132
  ...(getFeeHistoryRequest ? [getFeeHistoryRequest] : []),
121
133
  ...(estimateGasRequest ? [estimateGasRequest] : []),
122
134
  ...(getMaxPriorityFeePerGasRequest ? [getMaxPriorityFeePerGasRequest] : []),
135
+ getGasPriceRequest,
123
136
  ];
124
137
 
125
138
  const { data: rpcResponses } = await simpleFetch<
@@ -149,6 +162,9 @@ export async function fillUnsignedEVMTx(
149
162
  throw new Error(`No response for id=${id}`);
150
163
  }
151
164
  if (res.error) {
165
+ if (optional) {
166
+ return undefined;
167
+ }
152
168
  throw new Error(
153
169
  `RPC error (id=${id}): ${res.error.code} ${res.error.message}`
154
170
  );
@@ -166,6 +182,72 @@ export async function fillUnsignedEVMTx(
166
182
  if (!latestBlock) {
167
183
  throw new Error("Failed to get latest block to fill unsigned transaction");
168
184
  }
185
+
186
+ // Compute nonce and gas limit first (shared by legacy and EIP-1559 paths)
187
+ const finalNonce =
188
+ tx.nonce != null
189
+ ? Math.max(Number(tx.nonce), parseInt(nonceHex, 16))
190
+ : parseInt(nonceHex, 16);
191
+
192
+ const gasLimitHex = hasProvidedGasLimit
193
+ ? undefined
194
+ : getResult<string>(ESTIMATE_GAS_ID, true);
195
+
196
+ let finalGasLimit: UnsignedTransaction["gasLimit"];
197
+ if (tx.gasLimit != null) {
198
+ finalGasLimit = tx.gasLimit;
199
+ } else if (gasLimitHex) {
200
+ const estimatedGas = toBigIntFromTxField(gasLimitHex);
201
+ const adjustedGas =
202
+ (estimatedGas * GAS_ADJUSTMENT_NUM + (GAS_ADJUSTMENT_DEN - BigInt(1))) /
203
+ GAS_ADJUSTMENT_DEN;
204
+ finalGasLimit = `0x${adjustedGas.toString(16)}`;
205
+ } else {
206
+ throw new Error("Failed to estimate gas to fill unsigned transaction");
207
+ }
208
+
209
+ // Legacy chain detection: baseFeePerGas missing or zero
210
+ const isLegacy =
211
+ !latestBlock.baseFeePerGas || parseInt(latestBlock.baseFeePerGas, 16) === 0;
212
+
213
+ if (isLegacy) {
214
+ let gasPriceDec: Dec;
215
+
216
+ if (feeType === "custom" && customGasPrice != null) {
217
+ gasPriceDec = new Dec(customGasPrice);
218
+ } else {
219
+ const gasPriceHex = getResult<string>(GAS_PRICE_ID, true);
220
+ if (gasPriceHex) {
221
+ const multiplier = new Dec(FEE_MULTIPLIERS[innerFeeType]);
222
+ gasPriceDec = new Dec(BigInt(gasPriceHex)).mul(multiplier);
223
+ } else {
224
+ throw new Error(
225
+ "Failed to get gas price for legacy chain to fill unsigned transaction"
226
+ );
227
+ }
228
+ }
229
+
230
+ const gasPriceHexResult = `0x${gasPriceDec
231
+ .truncate()
232
+ .toBigNumber()
233
+ .toString(16)}`;
234
+
235
+ const {
236
+ maxFeePerGas: _maxFeePerGas,
237
+ maxPriorityFeePerGas: _maxPriorityFeePerGas,
238
+ ...legacyTx
239
+ } = tx;
240
+
241
+ return {
242
+ ...legacyTx,
243
+ nonce: finalNonce,
244
+ gasPrice: gasPriceHexResult,
245
+ gasLimit: finalGasLimit,
246
+ type: 0,
247
+ };
248
+ }
249
+
250
+ // EIP-1559 path
169
251
  const feeHistory = hasProvidedPriorityFee
170
252
  ? undefined
171
253
  : getResult<{
@@ -173,10 +255,8 @@ export async function fillUnsignedEVMTx(
173
255
  gasUsedRatio: number[];
174
256
  oldestBlock: string;
175
257
  reward?: string[][];
176
- }>(FEE_HISTORY_ID);
177
- const gasLimitHex = hasProvidedGasLimit
178
- ? undefined
179
- : getResult<string>(ESTIMATE_GAS_ID, true);
258
+ }>(FEE_HISTORY_ID, true);
259
+
180
260
  const networkMaxPriorityFeePerGasHex = hasProvidedPriorityFee
181
261
  ? undefined
182
262
  : getResult<string>(MAX_PRIORITY_FEE_ID, true);
@@ -190,9 +270,11 @@ export async function fillUnsignedEVMTx(
190
270
  maxPriorityFeePerGasDec = new Dec(
191
271
  toBigIntFromTxField(tx.maxPriorityFeePerGas)
192
272
  );
273
+ } else if (feeType === "custom" && customPriorityFee != null) {
274
+ maxPriorityFeePerGasDec = new Dec(customPriorityFee);
193
275
  } else if (feeHistory?.reward && feeHistory.reward.length > 0) {
194
276
  const percentile =
195
- ETH_FEE_SETTINGS_BY_FEE_TYPE[feeType].percentile ??
277
+ ETH_FEE_SETTINGS_BY_FEE_TYPE[innerFeeType].percentile ??
196
278
  ETH_FEE_HISTORY_REWARD_PERCENTILES[1];
197
279
  const percentileIndex =
198
280
  ETH_FEE_HISTORY_REWARD_PERCENTILES.indexOf(percentile);
@@ -247,14 +329,10 @@ export async function fillUnsignedEVMTx(
247
329
  );
248
330
  }
249
331
 
250
- if (!latestBlock.baseFeePerGas) {
251
- throw new Error("Failed to get baseFeePerGas to fill unsigned transaction");
252
- }
253
-
254
- const multiplier = new Dec(FEE_MULTIPLIERS[feeType]);
332
+ const multiplier = new Dec(FEE_MULTIPLIERS[innerFeeType]);
255
333
 
256
334
  // Calculate maxFeePerGas = baseFeePerGas + maxPriorityFeePerGas
257
- const baseFeePerGasDec = new Dec(BigInt(latestBlock.baseFeePerGas));
335
+ const baseFeePerGasDec = new Dec(BigInt(latestBlock.baseFeePerGas!));
258
336
  const suggestedFeeFromBase = baseFeePerGasDec.mul(multiplier);
259
337
 
260
338
  const providedMaxFeePerGasDec = tx.maxFeePerGas
@@ -280,24 +358,6 @@ export async function fillUnsignedEVMTx(
280
358
  .toBigNumber()
281
359
  .toString(16)}`;
282
360
 
283
- const finalNonce =
284
- tx.nonce != null
285
- ? Math.max(Number(tx.nonce), parseInt(nonceHex, 16))
286
- : parseInt(nonceHex, 16);
287
-
288
- let finalGasLimit: UnsignedTransaction["gasLimit"];
289
- if (tx.gasLimit != null) {
290
- finalGasLimit = tx.gasLimit;
291
- } else if (gasLimitHex) {
292
- const estimatedGas = toBigIntFromTxField(gasLimitHex);
293
- const adjustedGas =
294
- (estimatedGas * GAS_ADJUSTMENT_NUM + (GAS_ADJUSTMENT_DEN - BigInt(1))) /
295
- GAS_ADJUSTMENT_DEN;
296
- finalGasLimit = `0x${adjustedGas.toString(16)}`;
297
- } else {
298
- throw new Error("Failed to estimate gas to fill unsigned transaction");
299
- }
300
-
301
361
  const newUnsignedTx: UnsignedTransaction = {
302
362
  ...tx,
303
363
  nonce: finalNonce,
@@ -22,6 +22,8 @@
22
22
  "@keplr-wallet/hooks/build/*": ["../hooks/src/*"],
23
23
  "@keplr-wallet/hooks-bitcoin": ["../hooks-bitcoin/src"],
24
24
  "@keplr-wallet/hooks-bitcoin/build/*": ["../hooks-bitcoin/src/*"],
25
+ "@keplr-wallet/hooks-evm": ["../hooks-evm/src"],
26
+ "@keplr-wallet/hooks-evm/build/*": ["../hooks-evm/src/*"],
25
27
  "@keplr-wallet/hooks-starknet": ["../hooks-starknet/src"],
26
28
  "@keplr-wallet/hooks-starknet/build/*": ["../hooks-starknet/src/*"],
27
29
  "@keplr-wallet/ledger-cosmos": ["../ledger-cosmos/src"],