@awarizon/web3 1.0.2 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { createPublicClient, http } from 'viem';
1
+ import { erc20Abi, createPublicClient, http } from 'viem';
2
2
  import { WalletEngine } from '@awarizon/wallet-engine';
3
- export { ChainSwitchError, InvalidMnemonicError, InvalidPrivateKeyError, WalletEngine, WalletNotConnectedError } from '@awarizon/wallet-engine';
3
+ export { ChainMismatchError, ChainSwitchError, InvalidMnemonicError, InvalidPrivateKeyError, WalletEngine, WalletNotConnectedError } from '@awarizon/wallet-engine';
4
4
  import { TransactionEngine } from '@awarizon/tx-engine';
5
5
  export { ContractExecutionError, GasEstimationError, SimulationError, TransactionEngine, TransactionTimeoutError } from '@awarizon/tx-engine';
6
6
  import { moonbeam, fantom, gnosis, celo, mantle, scroll, linea, zora, sepolia, avalanche, bsc, optimismSepolia, optimism, arbitrumSepolia, arbitrum, polygonAmoy, polygon, baseSepolia, base, mainnet } from 'viem/chains';
@@ -362,6 +362,36 @@ var TelemetryClient = class {
362
362
  );
363
363
  }
364
364
  };
365
+ var ERC20_ABI = erc20Abi;
366
+ var ERC721_ABI = [
367
+ { type: "function", name: "name", stateMutability: "view", inputs: [], outputs: [{ type: "string" }] },
368
+ { type: "function", name: "symbol", stateMutability: "view", inputs: [], outputs: [{ type: "string" }] },
369
+ { type: "function", name: "tokenURI", stateMutability: "view", inputs: [{ name: "tokenId", type: "uint256" }], outputs: [{ type: "string" }] },
370
+ { type: "function", name: "totalSupply", stateMutability: "view", inputs: [], outputs: [{ type: "uint256" }] },
371
+ { type: "function", name: "ownerOf", stateMutability: "view", inputs: [{ name: "tokenId", type: "uint256" }], outputs: [{ type: "address" }] },
372
+ { type: "function", name: "balanceOf", stateMutability: "view", inputs: [{ name: "owner", type: "address" }], outputs: [{ type: "uint256" }] },
373
+ { type: "function", name: "getApproved", stateMutability: "view", inputs: [{ name: "tokenId", type: "uint256" }], outputs: [{ type: "address" }] },
374
+ { type: "function", name: "isApprovedForAll", stateMutability: "view", inputs: [{ name: "owner", type: "address" }, { name: "operator", type: "address" }], outputs: [{ type: "bool" }] },
375
+ { type: "function", name: "transferFrom", stateMutability: "nonpayable", inputs: [{ name: "from", type: "address" }, { name: "to", type: "address" }, { name: "tokenId", type: "uint256" }], outputs: [] },
376
+ { type: "function", name: "safeTransferFrom", stateMutability: "nonpayable", inputs: [{ name: "from", type: "address" }, { name: "to", type: "address" }, { name: "tokenId", type: "uint256" }], outputs: [] },
377
+ { type: "function", name: "approve", stateMutability: "nonpayable", inputs: [{ name: "to", type: "address" }, { name: "tokenId", type: "uint256" }], outputs: [] },
378
+ { type: "function", name: "setApprovalForAll", stateMutability: "nonpayable", inputs: [{ name: "operator", type: "address" }, { name: "approved", type: "bool" }], outputs: [] },
379
+ { type: "event", name: "Transfer", inputs: [{ indexed: true, name: "from", type: "address" }, { indexed: true, name: "to", type: "address" }, { indexed: true, name: "tokenId", type: "uint256" }] },
380
+ { type: "event", name: "Approval", inputs: [{ indexed: true, name: "owner", type: "address" }, { indexed: true, name: "approved", type: "address" }, { indexed: true, name: "tokenId", type: "uint256" }] },
381
+ { type: "event", name: "ApprovalForAll", inputs: [{ indexed: true, name: "owner", type: "address" }, { indexed: true, name: "operator", type: "address" }, { name: "approved", type: "bool" }] }
382
+ ];
383
+ var ERC1155_ABI = [
384
+ { type: "function", name: "uri", stateMutability: "view", inputs: [{ name: "id", type: "uint256" }], outputs: [{ type: "string" }] },
385
+ { type: "function", name: "balanceOf", stateMutability: "view", inputs: [{ name: "account", type: "address" }, { name: "id", type: "uint256" }], outputs: [{ type: "uint256" }] },
386
+ { type: "function", name: "balanceOfBatch", stateMutability: "view", inputs: [{ name: "accounts", type: "address[]" }, { name: "ids", type: "uint256[]" }], outputs: [{ type: "uint256[]" }] },
387
+ { type: "function", name: "isApprovedForAll", stateMutability: "view", inputs: [{ name: "account", type: "address" }, { name: "operator", type: "address" }], outputs: [{ type: "bool" }] },
388
+ { type: "function", name: "setApprovalForAll", stateMutability: "nonpayable", inputs: [{ name: "operator", type: "address" }, { name: "approved", type: "bool" }], outputs: [] },
389
+ { type: "function", name: "safeTransferFrom", stateMutability: "nonpayable", inputs: [{ name: "from", type: "address" }, { name: "to", type: "address" }, { name: "id", type: "uint256" }, { name: "amount", type: "uint256" }, { name: "data", type: "bytes" }], outputs: [] },
390
+ { type: "function", name: "safeBatchTransferFrom", stateMutability: "nonpayable", inputs: [{ name: "from", type: "address" }, { name: "to", type: "address" }, { name: "ids", type: "uint256[]" }, { name: "amounts", type: "uint256[]" }, { name: "data", type: "bytes" }], outputs: [] },
391
+ { type: "event", name: "TransferSingle", inputs: [{ indexed: true, name: "operator", type: "address" }, { indexed: true, name: "from", type: "address" }, { indexed: true, name: "to", type: "address" }, { name: "id", type: "uint256" }, { name: "value", type: "uint256" }] },
392
+ { type: "event", name: "TransferBatch", inputs: [{ indexed: true, name: "operator", type: "address" }, { indexed: true, name: "from", type: "address" }, { indexed: true, name: "to", type: "address" }, { name: "ids", type: "uint256[]" }, { name: "values", type: "uint256[]" }] },
393
+ { type: "event", name: "ApprovalForAll", inputs: [{ indexed: true, name: "account", type: "address" }, { indexed: true, name: "operator", type: "address" }, { name: "approved", type: "bool" }] }
394
+ ];
365
395
  var WalletProxy = class {
366
396
  constructor(engine, ensureReady) {
367
397
  this.engine = engine;
@@ -399,8 +429,11 @@ var WalletProxy = class {
399
429
  hasInternalWallet() {
400
430
  return this.engine.hasInternalWallet();
401
431
  }
402
- connectExternal(c) {
403
- this.engine.connectExternal(c);
432
+ getConnectorInfo() {
433
+ return this.engine.getConnectorInfo();
434
+ }
435
+ connectExternal(c, info) {
436
+ this.engine.connectExternal(c, info);
404
437
  }
405
438
  disconnectExternal() {
406
439
  this.engine.disconnectExternal();
@@ -411,12 +444,40 @@ var WalletProxy = class {
411
444
  async switchChain(chain) {
412
445
  return this.engine.switchChain(chain);
413
446
  }
447
+ // ── Gated async — EIP-1193, signing, chain awareness ───────────────────────
448
+ async connectEIP1193(provider, info) {
449
+ await this.ensureReady();
450
+ return this.engine.connectEIP1193(provider, info);
451
+ }
452
+ async signMessage(message) {
453
+ await this.ensureReady();
454
+ return this.engine.signMessage(message);
455
+ }
456
+ async signTypedData(params) {
457
+ await this.ensureReady();
458
+ return this.engine.signTypedData(params);
459
+ }
460
+ async getChainId() {
461
+ return this.engine.getChainId();
462
+ }
463
+ async isChainMismatch() {
464
+ return this.engine.isChainMismatch();
465
+ }
466
+ // ── Event subscriptions ────────────────────────────────────────────────────
467
+ onWalletChange(cb) {
468
+ return this.engine.onWalletChange(cb);
469
+ }
470
+ onChainChange(cb) {
471
+ return this.engine.onChainChange(cb);
472
+ }
414
473
  };
415
474
  var AwarizonWeb3 = class {
416
475
  constructor(config) {
417
476
  // Contract instances are cached by address — same address returns same instance.
418
477
  // Cache is cleared on switchChain() since contracts are chain-specific.
419
478
  this._contractCache = /* @__PURE__ */ new Map();
479
+ // Named contract registry — register once, reference by name anywhere.
480
+ this._registry = /* @__PURE__ */ new Map();
420
481
  if (!config.apiKey) throw new ApiKeyRequiredError();
421
482
  this.chain = resolveChain(config.chain);
422
483
  this.publicClient = createPublicClient({
@@ -442,8 +503,27 @@ var AwarizonWeb3 = class {
442
503
  }
443
504
  }
444
505
  // ─── Wallet API surface ─────────────────────────────────────────────────────
445
- connectWallet(walletClient) {
446
- this._engine.connectExternal(walletClient);
506
+ /**
507
+ * Connect any viem WalletClient (wagmi, RainbowKit, custom).
508
+ * Optionally pass connector metadata to display in the UI.
509
+ *
510
+ * @example
511
+ * awarizon.connectWallet(walletClient, { name: "MetaMask" })
512
+ */
513
+ connectWallet(walletClient, connectorInfo) {
514
+ this._engine.connectExternal(walletClient, connectorInfo);
515
+ return this;
516
+ }
517
+ /**
518
+ * Connect directly from a raw EIP-1193 provider without needing wagmi.
519
+ * Works with window.ethereum, WalletConnect v2, and any EIP-1193 object.
520
+ *
521
+ * @example
522
+ * await awarizon.connectEIP1193Provider(window.ethereum, { name: "MetaMask" })
523
+ */
524
+ async connectEIP1193Provider(provider, connectorInfo) {
525
+ await this._telemetry.ensureValidated();
526
+ await this._engine.connectEIP1193(provider, connectorInfo);
447
527
  return this;
448
528
  }
449
529
  disconnectWallet() {
@@ -458,12 +538,86 @@ var AwarizonWeb3 = class {
458
538
  return this;
459
539
  }
460
540
  getWalletInfo() {
541
+ const info = this._engine.getConnectorInfo();
461
542
  return {
462
543
  address: this._engine.address(),
463
544
  chain: this.chain,
464
- isExternal: this._engine.hasExternalWallet()
545
+ isExternal: this._engine.hasExternalWallet(),
546
+ connectorName: info.name
465
547
  };
466
548
  }
549
+ /**
550
+ * Sign a plain text or raw-bytes message.
551
+ * Use for Sign-In with Ethereum (SIWE) and off-chain authorization.
552
+ *
553
+ * @example
554
+ * const sig = await awarizon.signMessage("Hello, Awarizon!")
555
+ */
556
+ async signMessage(message) {
557
+ await this._telemetry.ensureValidated();
558
+ return this._engine.signMessage(message);
559
+ }
560
+ /**
561
+ * Sign EIP-712 typed structured data (permit2, SIWE, Seaport orders, etc.).
562
+ *
563
+ * @example
564
+ * const sig = await awarizon.signTypedData({
565
+ * domain: { name: "MyApp", version: "1", chainId: 8453 },
566
+ * types: { Transfer: [{ name: "to", type: "address" }, { name: "amount", type: "uint256" }] },
567
+ * primaryType: "Transfer",
568
+ * message: { to: "0x...", amount: "1000000" },
569
+ * })
570
+ */
571
+ async signTypedData(params) {
572
+ await this._telemetry.ensureValidated();
573
+ return this._engine.signTypedData(params);
574
+ }
575
+ /**
576
+ * Returns the chain ID the active wallet is currently on.
577
+ * For external wallets this may differ from the SDK's configured chain.
578
+ */
579
+ async getChainId() {
580
+ return this._engine.getChainId();
581
+ }
582
+ /**
583
+ * Returns true when the external wallet is on a different chain than the SDK.
584
+ * Use this to show a "wrong network" banner in your UI.
585
+ *
586
+ * @example
587
+ * if (await awarizon.isChainMismatch()) {
588
+ * alert("Please switch to Base in your wallet")
589
+ * }
590
+ */
591
+ async isChainMismatch() {
592
+ return this._engine.isChainMismatch();
593
+ }
594
+ /**
595
+ * Returns metadata about the currently active connector.
596
+ * Useful for displaying "Connected via MetaMask" in the UI.
597
+ */
598
+ getConnectorInfo() {
599
+ return this._engine.getConnectorInfo();
600
+ }
601
+ /**
602
+ * Subscribe to wallet changes (connect, disconnect, account switch).
603
+ * Returns an unsubscribe function — call it on cleanup.
604
+ *
605
+ * @example
606
+ * const unsub = awarizon.onWalletChange(({ address, type, name }) => {
607
+ * console.log("Wallet changed:", address, type, name)
608
+ * })
609
+ * unsub() // stop listening
610
+ */
611
+ onWalletChange(cb) {
612
+ return this._engine.onWalletChange(cb);
613
+ }
614
+ /**
615
+ * Subscribe to chain changes (switchChain calls).
616
+ * Returns an unsubscribe function.
617
+ */
618
+ onChainChange(cb) {
619
+ return this._engine.onChainChange(cb);
620
+ }
467
621
  // ─── Contract API ───────────────────────────────────────────────────────────
468
622
  /**
469
623
  * Load a deployed contract and return a fully typed, ABI-powered instance.
@@ -639,6 +793,105 @@ var AwarizonWeb3 = class {
639
793
  );
640
794
  }
641
795
  }
796
+ // ─── ERC Standard Clients ───────────────────────────────────────────────────
797
+ /**
798
+ * Load an ERC-20 token with fully typed methods — no ABI import needed.
799
+ *
800
+ * ```ts
801
+ * const token = await awarizon.erc20("0x...")
802
+ * const balance = await token.balanceOf(userAddress)
803
+ * const symbol = await token.symbol()
804
+ * await token.transfer(recipient, 100n)
805
+ * ```
806
+ */
807
+ async erc20(address) {
808
+ const instance = await this.contract({ address, abi: ERC20_ABI });
809
+ return instance;
810
+ }
811
+ /**
812
+ * Load an ERC-721 NFT contract with fully typed methods — no ABI import needed.
813
+ *
814
+ * ```ts
815
+ * const nft = await awarizon.erc721("0x...")
816
+ * const owner = await nft.ownerOf(1n)
817
+ * const uri = await nft.tokenURI(1n)
818
+ * await nft.transferFrom(from, to, 1n)
819
+ * ```
820
+ */
821
+ async erc721(address) {
822
+ const instance = await this.contract({ address, abi: ERC721_ABI });
823
+ return instance;
824
+ }
825
+ /**
826
+ * Load an ERC-1155 multi-token contract with fully typed methods — no ABI import needed.
827
+ *
828
+ * ```ts
829
+ * const items = await awarizon.erc1155("0x...")
830
+ * const balance = await items.balanceOf(userAddress, 42n)
831
+ * const uri = await items.uri(42n)
832
+ * await items.safeTransferFrom(from, to, 42n, 1n, "0x")
833
+ * ```
834
+ */
835
+ async erc1155(address) {
836
+ const instance = await this.contract({ address, abi: ERC1155_ABI });
837
+ return instance;
838
+ }
839
+ // ─── Named Contract Registry ────────────────────────────────────────────────
840
+ /**
841
+ * Register a contract under a human-readable name.
842
+ * After registering, use `awarizon.use(name)` anywhere instead of repeating
843
+ * address + ABI every time.
844
+ *
845
+ * ```ts
846
+ * awarizon.register("USDC", { address: "0x...", abi: erc20Abi })
847
+ * awarizon.register("Vault", { address: "0x...", abi: vaultAbi })
848
+ *
849
+ * const usdc = await awarizon.use("USDC")
850
+ * const vault = await awarizon.use("Vault")
851
+ * ```
852
+ */
853
+ register(name, config) {
854
+ this._registry.set(name, config);
855
+ return this;
856
+ }
857
+ /**
858
+ * Retrieve a previously registered contract by name.
859
+ * Throws a descriptive error if the name was never registered.
860
+ *
861
+ * ```ts
862
+ * const usdc = await awarizon.use("USDC")
863
+ * await usdc.transfer(recipient, 100n)
864
+ * ```
865
+ */
866
+ async use(name) {
867
+ const config = this._registry.get(name);
868
+ if (!config) {
869
+ const registered = [...this._registry.keys()].join(", ") || "none";
870
+ throw new Error(
871
+ `[awarizon/web3] Contract "${name}" is not registered.
872
+ Call awarizon.register("${name}", { address, abi }) first.
873
+ Currently registered: ${registered}`
874
+ );
875
+ }
876
+ return this.contract(config);
877
+ }
878
+ /**
879
+ * Remove a registered contract from the registry.
880
+ *
881
+ * ```ts
882
+ * awarizon.unregister("USDC")
883
+ * ```
884
+ */
885
+ unregister(name) {
886
+ this._registry.delete(name);
887
+ return this;
888
+ }
889
+ /**
890
+ * Return all registered contract names.
891
+ */
892
+ registeredContracts() {
893
+ return [...this._registry.keys()];
894
+ }
642
895
  // ─── Accessors ──────────────────────────────────────────────────────────────
643
896
  get chainId() {
644
897
  return this.chain.id;
@@ -655,6 +908,6 @@ var AwarizonWeb3 = class {
655
908
  }
656
909
  };
657
910
 
658
- export { ApiKeyRequiredError, AwarizonWeb3, CHAINS, ContractNotLoadedError, InvalidApiKeyError, NetworkMismatchError, ProviderError, TelemetryClient, UnsupportedChainError, getSupportedChainIds, resolveChain };
911
+ export { ApiKeyRequiredError, AwarizonWeb3, CHAINS, ContractNotLoadedError, ERC1155_ABI, ERC20_ABI, ERC721_ABI, InvalidApiKeyError, NetworkMismatchError, ProviderError, TelemetryClient, UnsupportedChainError, getSupportedChainIds, resolveChain };
659
912
  //# sourceMappingURL=index.mjs.map
660
913
  //# sourceMappingURL=index.mjs.map