@wagmi/core 0.5.7 → 0.5.8

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,8 +1,8 @@
1
- import { g as getClient, C as ConnectorAlreadyConnectedError, a as ConnectorNotFoundError, b as ChainMismatchError, U as UserRejectedRequestError, c as ContractMethodDoesNotExistError, d as getProvider, P as ProviderChainsNotFound, e as ChainDoesNotSupportMulticallError, f as ContractMethodRevertedError, h as ContractMethodNoResultError, i as ContractResultDecodeError, n as normalizeChainId, S as SwitchChainNotSupportedError } from './getProvider-f1dfc7e3.esm.js';
2
- export { A as AddChainError, e as ChainDoesNotSupportMulticallError, b as ChainMismatchError, m as ChainNotConfiguredError, k as Client, l as Connector, C as ConnectorAlreadyConnectedError, a as ConnectorNotFoundError, c as ContractMethodDoesNotExistError, h as ContractMethodNoResultError, f as ContractMethodRevertedError, i as ContractResultDecodeError, I as InjectedConnector, P as ProviderChainsNotFound, o as ProviderRpcError, R as ResourceUnavailableError, p as RpcError, q as SwitchChainError, S as SwitchChainNotSupportedError, U as UserRejectedRequestError, j as createClient, r as createStorage, d as getProvider, s as noopStorage, n as normalizeChainId } from './getProvider-f1dfc7e3.esm.js';
1
+ import { g as getClient, C as ConnectorAlreadyConnectedError, a as ConnectorNotFoundError, b as ChainMismatchError, U as UserRejectedRequestError, c as ContractResultDecodeError, d as ContractMethodDoesNotExistError, e as getProvider, P as ProviderChainsNotFound, f as ChainDoesNotSupportMulticallError, h as ContractMethodRevertedError, i as ContractMethodNoResultError, n as normalizeChainId, S as SwitchChainNotSupportedError } from './getProvider-5b4b62c3.esm.js';
2
+ export { A as AddChainError, f as ChainDoesNotSupportMulticallError, b as ChainMismatchError, m as ChainNotConfiguredError, k as Client, l as Connector, C as ConnectorAlreadyConnectedError, a as ConnectorNotFoundError, d as ContractMethodDoesNotExistError, i as ContractMethodNoResultError, h as ContractMethodRevertedError, c as ContractResultDecodeError, I as InjectedConnector, P as ProviderChainsNotFound, o as ProviderRpcError, R as ResourceUnavailableError, p as RpcError, q as SwitchChainError, S as SwitchChainNotSupportedError, U as UserRejectedRequestError, j as createClient, r as createStorage, e as getProvider, s as noopStorage, n as normalizeChainId } from './getProvider-5b4b62c3.esm.js';
3
3
  import { providers, Contract as Contract$1 } from 'ethers';
4
4
  import { Contract, logger } from 'ethers/lib/ethers';
5
- import { FormatTypes, formatUnits, getAddress, isAddress, Logger } from 'ethers/lib/utils';
5
+ import { FormatTypes, parseBytes32String, formatUnits, getAddress, isAddress, Logger } from 'ethers/lib/utils';
6
6
  import { m as mainnet } from './chains-8c76af1b.esm.js';
7
7
  export { a as allChains, c as chain, b as chainId, d as defaultChains, e as defaultL2Chains, f as etherscanBlockExplorers } from './chains-8c76af1b.esm.js';
8
8
  import shallow from 'zustand/shallow';
@@ -214,7 +214,156 @@ function parseContractResult(_ref) {
214
214
  }
215
215
 
216
216
  // https://ethereum.org/en/developers/docs/standards/tokens/erc-20
217
- const erc20ABI = ['event Approval(address indexed _owner, address indexed _spender, uint256 _value)', 'event Transfer(address indexed _from, address indexed _to, uint256 _value)', 'function allowance(address _owner, address _spender) public view returns (uint256 remaining)', 'function approve(address _spender, uint256 _value) public returns (bool success)', 'function balanceOf(address _owner) public view returns (uint256 balance)', 'function decimals() public view returns (uint8)', 'function name() public view returns (string)', 'function symbol() public view returns (string)', 'function totalSupply() public view returns (uint256)', 'function transfer(address _to, uint256 _value) public returns (bool success)', 'function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)']; // https://ethereum.org/en/developers/docs/standards/tokens/erc-721
217
+ const erc20ABI = ['event Approval(address indexed _owner, address indexed _spender, uint256 _value)', 'event Transfer(address indexed _from, address indexed _to, uint256 _value)', 'function allowance(address _owner, address _spender) public view returns (uint256 remaining)', 'function approve(address _spender, uint256 _value) public returns (bool success)', 'function balanceOf(address _owner) public view returns (uint256 balance)', 'function decimals() public view returns (uint8)', 'function name() public view returns (string)', 'function symbol() public view returns (string)', 'function totalSupply() public view returns (uint256)', 'function transfer(address _to, uint256 _value) public returns (bool success)', 'function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)'];
218
+ /**
219
+ * [bytes32-flavored ERC-20](https://docs.makerdao.com/smart-contract-modules/mkr-module#4.-gotchas-potential-source-of-user-error)
220
+ * for tokens (ie. Maker) that use bytes32 instead of string.
221
+ */
222
+
223
+ const erc20ABI_bytes32 = [{
224
+ type: 'event',
225
+ name: 'Approval',
226
+ inputs: [{
227
+ indexed: true,
228
+ name: 'owner',
229
+ type: 'address'
230
+ }, {
231
+ indexed: true,
232
+ name: 'spender',
233
+ type: 'address'
234
+ }, {
235
+ indexed: false,
236
+ name: 'value',
237
+ type: 'uint256'
238
+ }]
239
+ }, {
240
+ type: 'event',
241
+ name: 'Transfer',
242
+ inputs: [{
243
+ indexed: true,
244
+ name: 'from',
245
+ type: 'address'
246
+ }, {
247
+ indexed: true,
248
+ name: 'to',
249
+ type: 'address'
250
+ }, {
251
+ indexed: false,
252
+ name: 'value',
253
+ type: 'uint256'
254
+ }]
255
+ }, {
256
+ type: 'function',
257
+ name: 'allowance',
258
+ stateMutability: 'view',
259
+ inputs: [{
260
+ name: 'owner',
261
+ type: 'address'
262
+ }, {
263
+ name: 'spender',
264
+ type: 'address'
265
+ }],
266
+ outputs: [{
267
+ name: '',
268
+ type: 'uint256'
269
+ }]
270
+ }, {
271
+ type: 'function',
272
+ name: 'approve',
273
+ stateMutability: 'nonpayable',
274
+ inputs: [{
275
+ name: 'spender',
276
+ type: 'address'
277
+ }, {
278
+ name: 'amount',
279
+ type: 'uint256'
280
+ }],
281
+ outputs: [{
282
+ name: '',
283
+ type: 'bool'
284
+ }]
285
+ }, {
286
+ type: 'function',
287
+ name: 'balanceOf',
288
+ stateMutability: 'view',
289
+ inputs: [{
290
+ name: 'account',
291
+ type: 'address'
292
+ }],
293
+ outputs: [{
294
+ name: '',
295
+ type: 'uint256'
296
+ }]
297
+ }, {
298
+ type: 'function',
299
+ name: 'decimals',
300
+ stateMutability: 'view',
301
+ inputs: [],
302
+ outputs: [{
303
+ name: '',
304
+ type: 'uint8'
305
+ }]
306
+ }, {
307
+ type: 'function',
308
+ name: 'name',
309
+ stateMutability: 'view',
310
+ inputs: [],
311
+ outputs: [{
312
+ name: '',
313
+ type: 'bytes32'
314
+ }]
315
+ }, {
316
+ type: 'function',
317
+ name: 'symbol',
318
+ stateMutability: 'view',
319
+ inputs: [],
320
+ outputs: [{
321
+ name: '',
322
+ type: 'bytes32'
323
+ }]
324
+ }, {
325
+ type: 'function',
326
+ name: 'totalSupply',
327
+ stateMutability: 'view',
328
+ inputs: [],
329
+ outputs: [{
330
+ name: '',
331
+ type: 'uint256'
332
+ }]
333
+ }, {
334
+ type: 'function',
335
+ name: 'transfer',
336
+ stateMutability: 'nonpayable',
337
+ inputs: [{
338
+ name: 'recipient',
339
+ type: 'address'
340
+ }, {
341
+ name: 'amount',
342
+ type: 'uint256'
343
+ }],
344
+ outputs: [{
345
+ name: '',
346
+ type: 'bool'
347
+ }]
348
+ }, {
349
+ type: 'function',
350
+ name: 'transferFrom',
351
+ stateMutability: 'nonpayable',
352
+ inputs: [{
353
+ name: 'sender',
354
+ type: 'address'
355
+ }, {
356
+ name: 'recipient',
357
+ type: 'address'
358
+ }, {
359
+ name: 'amount',
360
+ type: 'uint256'
361
+ }],
362
+ outputs: [{
363
+ name: '',
364
+ type: 'bool'
365
+ }]
366
+ }]; // https://ethereum.org/en/developers/docs/standards/tokens/erc-721
218
367
 
219
368
  const erc721ABI = ['event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId)', 'event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved)', 'event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId)', 'function approve(address _approved, uint256 _tokenId) external payable', 'function balanceOf(address _owner) external view returns (uint256)', 'function getApproved(uint256 _tokenId) external view returns (address)', 'function isApprovedForAll(address _owner, address _operator) external view returns (bool)', 'function name() view returns (string memory)', 'function ownerOf(uint256 _tokenId) external view returns (address)', 'function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable', 'function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable', 'function setApprovalForAll(address _operator, bool _approved) external', 'function symbol() view returns (string memory)', 'function tokenByIndex(uint256 _index) view returns (uint256)', 'function tokenOfOwnerByIndex(address _owner, uint256 _index) view returns (uint256 tokenId)', 'function tokenURI(uint256 _tokenId) view returns (string memory)', 'function totalSupply() view returns (uint256)', 'function transferFrom(address _from, address _to, uint256 _tokenId) external payable'];
220
369
 
@@ -371,33 +520,65 @@ async function fetchToken(_ref) {
371
520
  chainId,
372
521
  formatUnits: units = 'ether'
373
522
  } = _ref;
374
- const erc20Config = {
375
- addressOrName: address,
376
- contractInterface: erc20ABI,
377
- chainId
378
- };
379
- const [decimals, name, symbol, totalSupply] = await readContracts({
380
- allowFailure: false,
381
- contracts: [{ ...erc20Config,
382
- functionName: 'decimals'
383
- }, { ...erc20Config,
384
- functionName: 'name'
385
- }, { ...erc20Config,
386
- functionName: 'symbol'
387
- }, { ...erc20Config,
388
- functionName: 'totalSupply'
389
- }]
390
- });
391
- return {
392
- address,
393
- decimals,
394
- name,
395
- symbol,
396
- totalSupply: {
397
- formatted: formatUnits(totalSupply, units),
398
- value: totalSupply
523
+
524
+ async function fetchToken_(_ref2) {
525
+ let {
526
+ contractInterface
527
+ } = _ref2;
528
+ const erc20Config = {
529
+ addressOrName: address,
530
+ contractInterface,
531
+ chainId
532
+ };
533
+ const [decimals, name, symbol, totalSupply] = await readContracts({
534
+ allowFailure: false,
535
+ contracts: [{ ...erc20Config,
536
+ functionName: 'decimals'
537
+ }, { ...erc20Config,
538
+ functionName: 'name'
539
+ }, { ...erc20Config,
540
+ functionName: 'symbol'
541
+ }, { ...erc20Config,
542
+ functionName: 'totalSupply'
543
+ }]
544
+ });
545
+ return {
546
+ address,
547
+ decimals,
548
+ name,
549
+ symbol,
550
+ totalSupply: {
551
+ formatted: formatUnits(totalSupply, units),
552
+ value: totalSupply
553
+ }
554
+ };
555
+ }
556
+
557
+ try {
558
+ return await fetchToken_({
559
+ contractInterface: erc20ABI
560
+ });
561
+ } catch (err) {
562
+ // In the chance that there is an error upon decoding the contract result,
563
+ // it could be likely that the contract data is represented as bytes32 instead
564
+ // of a string.
565
+ if (err instanceof ContractResultDecodeError) {
566
+ const {
567
+ name,
568
+ symbol,
569
+ ...rest
570
+ } = await fetchToken_({
571
+ contractInterface: erc20ABI_bytes32
572
+ });
573
+ return {
574
+ name: parseBytes32String(name),
575
+ symbol: parseBytes32String(symbol),
576
+ ...rest
577
+ };
399
578
  }
400
- };
579
+
580
+ throw err;
581
+ }
401
582
  }
402
583
 
403
584
  /**
@@ -792,18 +973,17 @@ async function fetchBlockNumber() {
792
973
  }
793
974
 
794
975
  function watchBlockNumber(args, callback) {
795
- var _client$webSocketProv;
796
-
976
+ var _getWebSocketProvider;
977
+
978
+ // We need to debounce the listener as we want to opt-out
979
+ // of the behavior where ethers emits a "block" event for
980
+ // every block that was missed in between the `pollingInterval`.
981
+ // We are setting a wait time of 1 as emitting an event in
982
+ // ethers takes ~0.1ms.
983
+ const debouncedCallback = debounce(callback, 1);
797
984
  let previousProvider;
798
985
 
799
986
  const createListener = provider => {
800
- // We need to debounce the listener as we want to opt-out
801
- // of the behavior where ethers emits a "block" event for
802
- // every block that was missed in between the `pollingInterval`.
803
- // We are setting a wait time of 1 as emitting an event in
804
- // ethers takes ~0.1ms.
805
- const debouncedCallback = debounce(callback, 1);
806
-
807
987
  if (previousProvider) {
808
988
  var _previousProvider;
809
989
 
@@ -814,9 +994,14 @@ function watchBlockNumber(args, callback) {
814
994
  previousProvider = provider;
815
995
  };
816
996
 
817
- const client = getClient();
818
- const provider_ = (_client$webSocketProv = client.webSocketProvider) !== null && _client$webSocketProv !== void 0 ? _client$webSocketProv : client.provider;
997
+ const provider_ = (_getWebSocketProvider = getWebSocketProvider({
998
+ chainId: args.chainId
999
+ })) !== null && _getWebSocketProvider !== void 0 ? _getWebSocketProvider : getProvider({
1000
+ chainId: args.chainId
1001
+ });
819
1002
  if (args.listen) createListener(provider_);
1003
+ let active = true;
1004
+ const client = getClient();
820
1005
  const unsubscribe = client.subscribe(_ref => {
821
1006
  let {
822
1007
  provider,
@@ -833,17 +1018,25 @@ function watchBlockNumber(args, callback) {
833
1018
  } = _ref2;
834
1019
  const provider_ = webSocketProvider !== null && webSocketProvider !== void 0 ? webSocketProvider : provider;
835
1020
 
836
- if (args.listen && provider_) {
1021
+ if (args.listen && !args.chainId && provider_) {
837
1022
  createListener(provider_);
838
1023
  }
839
1024
 
840
- callback(await fetchBlockNumber());
1025
+ const blockNumber = await fetchBlockNumber({
1026
+ chainId: args.chainId
1027
+ });
1028
+ if (!active) return;
1029
+ callback(blockNumber);
841
1030
  }, {
842
1031
  equalityFn: shallow
843
1032
  });
844
1033
  return () => {
1034
+ var _previousProvider2;
1035
+
1036
+ active = false;
845
1037
  unsubscribe();
846
- provider_ === null || provider_ === void 0 ? void 0 : provider_.off('block', callback);
1038
+ provider_ === null || provider_ === void 0 ? void 0 : provider_.off('block', debouncedCallback);
1039
+ (_previousProvider2 = previousProvider) === null || _previousProvider2 === void 0 ? void 0 : _previousProvider2.off('block', debouncedCallback);
847
1040
  };
848
1041
  }
849
1042
 
@@ -1245,12 +1438,7 @@ async function fetchBalance(_ref) {
1245
1438
  });
1246
1439
 
1247
1440
  if (token) {
1248
- const erc20Config = {
1249
- addressOrName: token,
1250
- contractInterface: erc20ABI,
1251
- chainId
1252
- }; // Convert ENS name to address if required
1253
-
1441
+ // Convert ENS name to address if required
1254
1442
  let resolvedAddress;
1255
1443
  if (isAddress(addressOrName)) resolvedAddress = addressOrName;else {
1256
1444
  const address = await provider.resolveName(addressOrName); // Same error `provider.getBalance` throws for invalid ENS name
@@ -1260,23 +1448,58 @@ async function fetchBalance(_ref) {
1260
1448
  });
1261
1449
  resolvedAddress = address;
1262
1450
  }
1263
- const [value, decimals, symbol] = await readContracts({
1264
- allowFailure: false,
1265
- contracts: [{ ...erc20Config,
1266
- functionName: 'balanceOf',
1267
- args: resolvedAddress
1268
- }, { ...erc20Config,
1269
- functionName: 'decimals'
1270
- }, { ...erc20Config,
1271
- functionName: 'symbol'
1272
- }]
1273
- });
1274
- return {
1275
- decimals,
1276
- formatted: formatUnits(value !== null && value !== void 0 ? value : '0', unit !== null && unit !== void 0 ? unit : decimals),
1277
- symbol,
1278
- value
1451
+
1452
+ const fetchContractBalance = async _ref2 => {
1453
+ let {
1454
+ contractInterface
1455
+ } = _ref2;
1456
+ const erc20Config = {
1457
+ addressOrName: token,
1458
+ contractInterface,
1459
+ chainId
1460
+ };
1461
+ const [value, decimals, symbol] = await readContracts({
1462
+ allowFailure: false,
1463
+ contracts: [{ ...erc20Config,
1464
+ functionName: 'balanceOf',
1465
+ args: resolvedAddress
1466
+ }, { ...erc20Config,
1467
+ functionName: 'decimals'
1468
+ }, { ...erc20Config,
1469
+ functionName: 'symbol'
1470
+ }]
1471
+ });
1472
+ return {
1473
+ decimals,
1474
+ formatted: formatUnits(value !== null && value !== void 0 ? value : '0', unit !== null && unit !== void 0 ? unit : decimals),
1475
+ symbol,
1476
+ value
1477
+ };
1279
1478
  };
1479
+
1480
+ try {
1481
+ return await fetchContractBalance({
1482
+ contractInterface: erc20ABI
1483
+ });
1484
+ } catch (err) {
1485
+ // In the chance that there is an error upon decoding the contract result,
1486
+ // it could be likely that the contract data is represented as bytes32 instead
1487
+ // of a string.
1488
+ if (err instanceof ContractResultDecodeError) {
1489
+ const {
1490
+ symbol,
1491
+ ...rest
1492
+ } = await fetchContractBalance({
1493
+ contractInterface: erc20ABI_bytes32
1494
+ });
1495
+ return {
1496
+ symbol: parseBytes32String(symbol),
1497
+ ...rest
1498
+ };
1499
+ }
1500
+
1501
+ throw err;
1502
+ }
1280
1503
  }
1281
1504
 
1282
1505
  const chains = [...(client.provider.chains || []), ...((_client$chains = client.chains) !== null && _client$chains !== void 0 ? _client$chains : [])];
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@wagmi/core",
3
3
  "description": "Vanilla JS library for Ethereum",
4
4
  "license": "WAGMIT",
5
- "version": "0.5.7",
5
+ "version": "0.5.8",
6
6
  "repository": {
7
7
  "type": "git",
8
8
  "url": "https://github.com/wagmi-dev/wagmi.git",