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