moltspay 0.2.8 → 0.2.10

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,145 @@ 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
+ }
418
+ /**
419
+ * Pay for a service (transfer USDC to service provider)
420
+ *
421
+ * This is the main method for Agent-to-Agent payments.
422
+ * Returns payment data ready to submit to service API.
423
+ *
424
+ * @example
425
+ * ```typescript
426
+ * const wallet = new AgentWallet({ chain: 'base' });
427
+ *
428
+ * // Pay for Zen7 video service
429
+ * const payment = await wallet.payService({
430
+ * provider: '0xb8d6f2441e8f8dfB6288A74Cf73804cDd0484E0C',
431
+ * amount: 0.99,
432
+ * service: 'video_generation'
433
+ * });
434
+ *
435
+ * if (payment.success) {
436
+ * // Submit to service API
437
+ * await fetch('/v1/video/generate', {
438
+ * body: JSON.stringify({
439
+ * prompt: "...",
440
+ * payment: payment.paymentData // Ready to use!
441
+ * })
442
+ * });
443
+ * }
444
+ * ```
445
+ */
446
+ async payService(params) {
447
+ const result = await this.transfer({
448
+ to: params.provider,
449
+ amount: params.amount
450
+ });
451
+ if (result.success && result.txHash) {
452
+ return {
453
+ success: true,
454
+ txHash: result.txHash,
455
+ explorerUrl: result.explorerUrl,
456
+ paymentData: {
457
+ method: "transfer",
458
+ chain: this.chain,
459
+ tx_hash: result.txHash
460
+ }
461
+ };
462
+ } else {
463
+ return {
464
+ success: false,
465
+ error: result.error
466
+ };
467
+ }
468
+ }
330
469
  /**
331
470
  * Get gas balance (ETH needed for transactions)
332
471
  */