moltspay 0.2.7 → 0.2.9

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.d.mts CHANGED
@@ -133,6 +133,47 @@ declare class AgentWallet {
133
133
  * @param permit - Optional, uses stored permit if not provided
134
134
  */
135
135
  spend(to: string, amount: number, permit?: OwnerPermit): Promise<SpendResult>;
136
+ /**
137
+ * Get USDC balance
138
+ */
139
+ getBalance(): Promise<{
140
+ usdc: string;
141
+ eth: string;
142
+ }>;
143
+ /**
144
+ * Transfer USDC to a recipient (direct payment)
145
+ *
146
+ * This is the simplest payment method - Agent pays directly from its wallet.
147
+ * Requires Agent wallet to have USDC (funded by Owner).
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const wallet = new AgentWallet({ chain: 'base' });
152
+ *
153
+ * // Check balance
154
+ * const balance = await wallet.getBalance();
155
+ * console.log('USDC:', balance.usdc);
156
+ *
157
+ * // Pay for service
158
+ * const result = await wallet.transfer({
159
+ * to: '0xServiceProvider...',
160
+ * amount: 0.99
161
+ * });
162
+ * console.log('Tx:', result.txHash);
163
+ * ```
164
+ */
165
+ transfer(params: {
166
+ to: string;
167
+ amount: number;
168
+ }): Promise<{
169
+ success: boolean;
170
+ txHash?: string;
171
+ error?: string;
172
+ from?: string;
173
+ to?: string;
174
+ amount?: number;
175
+ explorerUrl?: string;
176
+ }>;
136
177
  /**
137
178
  * Get gas balance (ETH needed for transactions)
138
179
  */
package/dist/index.d.ts CHANGED
@@ -133,6 +133,47 @@ declare class AgentWallet {
133
133
  * @param permit - Optional, uses stored permit if not provided
134
134
  */
135
135
  spend(to: string, amount: number, permit?: OwnerPermit): Promise<SpendResult>;
136
+ /**
137
+ * Get USDC balance
138
+ */
139
+ getBalance(): Promise<{
140
+ usdc: string;
141
+ eth: string;
142
+ }>;
143
+ /**
144
+ * Transfer USDC to a recipient (direct payment)
145
+ *
146
+ * This is the simplest payment method - Agent pays directly from its wallet.
147
+ * Requires Agent wallet to have USDC (funded by Owner).
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * const wallet = new AgentWallet({ chain: 'base' });
152
+ *
153
+ * // Check balance
154
+ * const balance = await wallet.getBalance();
155
+ * console.log('USDC:', balance.usdc);
156
+ *
157
+ * // Pay for service
158
+ * const result = await wallet.transfer({
159
+ * to: '0xServiceProvider...',
160
+ * amount: 0.99
161
+ * });
162
+ * console.log('Tx:', result.txHash);
163
+ * ```
164
+ */
165
+ transfer(params: {
166
+ to: string;
167
+ amount: number;
168
+ }): Promise<{
169
+ success: boolean;
170
+ txHash?: string;
171
+ error?: string;
172
+ from?: string;
173
+ to?: string;
174
+ amount?: number;
175
+ explorerUrl?: string;
176
+ }>;
136
177
  /**
137
178
  * Get gas balance (ETH needed for transactions)
138
179
  */
package/dist/index.js CHANGED
@@ -569,6 +569,94 @@ var AgentWallet = class {
569
569
  };
570
570
  }
571
571
  }
572
+ /**
573
+ * Get USDC balance
574
+ */
575
+ async getBalance() {
576
+ const usdcContract = new import_ethers2.ethers.Contract(
577
+ this.chainConfig.usdc,
578
+ PERMIT_ABI,
579
+ this.provider
580
+ );
581
+ const [usdcBalance, ethBalance] = await Promise.all([
582
+ usdcContract.balanceOf(this.address),
583
+ this.provider.getBalance(this.address)
584
+ ]);
585
+ return {
586
+ usdc: (Number(usdcBalance) / 1e6).toFixed(2),
587
+ eth: import_ethers2.ethers.formatEther(ethBalance)
588
+ };
589
+ }
590
+ /**
591
+ * Transfer USDC to a recipient (direct payment)
592
+ *
593
+ * This is the simplest payment method - Agent pays directly from its wallet.
594
+ * Requires Agent wallet to have USDC (funded by Owner).
595
+ *
596
+ * @example
597
+ * ```typescript
598
+ * const wallet = new AgentWallet({ chain: 'base' });
599
+ *
600
+ * // Check balance
601
+ * const balance = await wallet.getBalance();
602
+ * console.log('USDC:', balance.usdc);
603
+ *
604
+ * // Pay for service
605
+ * const result = await wallet.transfer({
606
+ * to: '0xServiceProvider...',
607
+ * amount: 0.99
608
+ * });
609
+ * console.log('Tx:', result.txHash);
610
+ * ```
611
+ */
612
+ async transfer(params) {
613
+ const { to, amount } = params;
614
+ try {
615
+ const toAddress = import_ethers2.ethers.getAddress(to);
616
+ const amountWei = BigInt(Math.floor(amount * 1e6));
617
+ const usdcContract = new import_ethers2.ethers.Contract(
618
+ this.chainConfig.usdc,
619
+ PERMIT_ABI,
620
+ this.wallet
621
+ );
622
+ const balance = await usdcContract.balanceOf(this.address);
623
+ if (BigInt(balance) < amountWei) {
624
+ return {
625
+ success: false,
626
+ error: `Insufficient USDC: have ${(Number(balance) / 1e6).toFixed(2)}, need ${amount}`
627
+ };
628
+ }
629
+ if (!await this.hasGas()) {
630
+ return {
631
+ success: false,
632
+ error: "Insufficient ETH for gas. Need at least 0.0005 ETH."
633
+ };
634
+ }
635
+ const tx = await usdcContract.transfer(toAddress, amountWei);
636
+ const receipt = await tx.wait();
637
+ if (receipt.status === 1) {
638
+ return {
639
+ success: true,
640
+ txHash: tx.hash,
641
+ from: this.address,
642
+ to: toAddress,
643
+ amount,
644
+ explorerUrl: `${this.chainConfig.explorerTx}${tx.hash}`
645
+ };
646
+ } else {
647
+ return {
648
+ success: false,
649
+ txHash: tx.hash,
650
+ error: "Transaction reverted"
651
+ };
652
+ }
653
+ } catch (error) {
654
+ return {
655
+ success: false,
656
+ error: error.message
657
+ };
658
+ }
659
+ }
572
660
  /**
573
661
  * Get gas balance (ETH needed for transactions)
574
662
  */