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/cli.mjs CHANGED
@@ -327,6 +327,94 @@ var init_AgentWallet = __esm({
327
327
  };
328
328
  }
329
329
  }
330
+ /**
331
+ * Get USDC balance
332
+ */
333
+ async getBalance() {
334
+ const usdcContract = new ethers2.Contract(
335
+ this.chainConfig.usdc,
336
+ PERMIT_ABI,
337
+ this.provider
338
+ );
339
+ const [usdcBalance, ethBalance] = await Promise.all([
340
+ usdcContract.balanceOf(this.address),
341
+ this.provider.getBalance(this.address)
342
+ ]);
343
+ return {
344
+ usdc: (Number(usdcBalance) / 1e6).toFixed(2),
345
+ eth: ethers2.formatEther(ethBalance)
346
+ };
347
+ }
348
+ /**
349
+ * Transfer USDC to a recipient (direct payment)
350
+ *
351
+ * This is the simplest payment method - Agent pays directly from its wallet.
352
+ * Requires Agent wallet to have USDC (funded by Owner).
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const wallet = new AgentWallet({ chain: 'base' });
357
+ *
358
+ * // Check balance
359
+ * const balance = await wallet.getBalance();
360
+ * console.log('USDC:', balance.usdc);
361
+ *
362
+ * // Pay for service
363
+ * const result = await wallet.transfer({
364
+ * to: '0xServiceProvider...',
365
+ * amount: 0.99
366
+ * });
367
+ * console.log('Tx:', result.txHash);
368
+ * ```
369
+ */
370
+ async transfer(params) {
371
+ const { to, amount } = params;
372
+ try {
373
+ const toAddress = ethers2.getAddress(to);
374
+ const amountWei = BigInt(Math.floor(amount * 1e6));
375
+ const usdcContract = new ethers2.Contract(
376
+ this.chainConfig.usdc,
377
+ PERMIT_ABI,
378
+ this.wallet
379
+ );
380
+ const balance = await usdcContract.balanceOf(this.address);
381
+ if (BigInt(balance) < amountWei) {
382
+ return {
383
+ success: false,
384
+ error: `Insufficient USDC: have ${(Number(balance) / 1e6).toFixed(2)}, need ${amount}`
385
+ };
386
+ }
387
+ if (!await this.hasGas()) {
388
+ return {
389
+ success: false,
390
+ error: "Insufficient ETH for gas. Need at least 0.0005 ETH."
391
+ };
392
+ }
393
+ const tx = await usdcContract.transfer(toAddress, amountWei);
394
+ const receipt = await tx.wait();
395
+ if (receipt.status === 1) {
396
+ return {
397
+ success: true,
398
+ txHash: tx.hash,
399
+ from: this.address,
400
+ to: toAddress,
401
+ amount,
402
+ explorerUrl: `${this.chainConfig.explorerTx}${tx.hash}`
403
+ };
404
+ } else {
405
+ return {
406
+ success: false,
407
+ txHash: tx.hash,
408
+ error: "Transaction reverted"
409
+ };
410
+ }
411
+ } catch (error) {
412
+ return {
413
+ success: false,
414
+ error: error.message
415
+ };
416
+ }
417
+ }
330
418
  /**
331
419
  * Get gas balance (ETH needed for transactions)
332
420
  */