@clawback/sdk 1.0.0 → 1.1.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +163 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clawback/sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "SDK for Clawback - on-chain escrow payments for AI agents on Solana",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -502,6 +502,169 @@ export class ClawbackClient {
502
502
  });
503
503
  return response.data;
504
504
  }
505
+
506
+ /**
507
+ * Set up webhooks with human-readable threshold
508
+ * @param {string} url - Webhook URL
509
+ * @param {Array<string>} events - Events to subscribe to (defaults to all payment events)
510
+ * @param {Object} options - Options
511
+ * @param {number} options.lowBalanceThreshold - Low balance threshold in human-readable form (e.g., 5 for 5 USDC)
512
+ * @param {string} options.lowBalanceToken - Token for threshold (default: USDC)
513
+ * @returns {Promise<Object>}
514
+ *
515
+ * @example
516
+ * // Subscribe to all events, alert when USDC balance < 5
517
+ * await clawback.setupWebhooks('https://myagent.com/webhook', null, {
518
+ * lowBalanceThreshold: 5,
519
+ * lowBalanceToken: 'USDC'
520
+ * });
521
+ */
522
+ async setupWebhooks(url, events = null, options = {}) {
523
+ const defaultEvents = [
524
+ 'payment.created',
525
+ 'payment.accepted',
526
+ 'payment.released',
527
+ 'payment.disputed',
528
+ 'balance.low'
529
+ ];
530
+
531
+ const threshold = options.lowBalanceThreshold
532
+ ? this.toBaseUnits(options.lowBalanceThreshold, options.lowBalanceToken || 'USDC')
533
+ : 0;
534
+
535
+ return this.setWebhook(url, events || defaultEvents, threshold);
536
+ }
537
+
538
+ // =========================================================================
539
+ // Funding Helpers
540
+ // =========================================================================
541
+
542
+ /**
543
+ * Get funding instructions for your wallet
544
+ * @returns {Promise<{walletAddress: string, network: string, usdcMint: string, instructions: string}>}
545
+ */
546
+ async getFundingInstructions() {
547
+ const walletAddress = await this.getWalletAddress();
548
+
549
+ return {
550
+ walletAddress,
551
+ network: 'Solana Mainnet',
552
+ usdcMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
553
+ instructions: `
554
+ ACTION REQUIRED: Fund your Clawback wallet
555
+
556
+ Your Clawback wallet needs funding to make payments.
557
+
558
+ Wallet Address: ${walletAddress}
559
+ Network: Solana Mainnet
560
+
561
+ Send USDC or SOL to this address.
562
+ USDC Contract: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
563
+
564
+ You can send funds from any Solana wallet (Phantom, Solflare, etc.)
565
+ or from a centralized exchange that supports Solana withdrawals.
566
+ `.trim()
567
+ };
568
+ }
569
+
570
+ /**
571
+ * Check balance and return funding instructions if needed
572
+ * @param {Object} thresholds - Minimum balance thresholds
573
+ * @param {number} thresholds.usdc - Minimum USDC balance (default: 1)
574
+ * @param {number} thresholds.sol - Minimum SOL balance (default: 0.01)
575
+ * @returns {Promise<{needsFunding: boolean, balance: Object, fundingInstructions: string|null}>}
576
+ *
577
+ * @example
578
+ * const status = await clawback.checkFundingStatus({ usdc: 5, sol: 0.1 });
579
+ * if (status.needsFunding) {
580
+ * // Notify human with status.fundingInstructions
581
+ * console.log(status.fundingInstructions);
582
+ * }
583
+ */
584
+ async checkFundingStatus(thresholds = {}) {
585
+ const minUsdc = thresholds.usdc ?? 1;
586
+ const minSol = thresholds.sol ?? 0.01;
587
+
588
+ const balance = await this.getBalance();
589
+ const needsFunding = balance.usdc < minUsdc && balance.sol < minSol;
590
+
591
+ let fundingInstructions = null;
592
+ if (needsFunding) {
593
+ const info = await this.getFundingInstructions();
594
+ fundingInstructions = info.instructions;
595
+ }
596
+
597
+ return {
598
+ needsFunding,
599
+ balance,
600
+ fundingInstructions,
601
+ };
602
+ }
603
+
604
+ /**
605
+ * Initialize agent with recommended setup (webhooks + funding check)
606
+ * Returns a status object and any actions needed from the human operator
607
+ *
608
+ * @param {Object} options - Setup options
609
+ * @param {string} options.webhookUrl - URL to receive webhook notifications
610
+ * @param {number} options.lowBalanceThreshold - USDC amount to trigger low balance alert (default: 5)
611
+ * @param {number} options.minUsdc - Minimum USDC to consider "funded" (default: 1)
612
+ * @param {number} options.minSol - Minimum SOL to consider "funded" (default: 0.01)
613
+ * @returns {Promise<{ready: boolean, balance: Object, webhooksConfigured: boolean, humanActionRequired: string|null}>}
614
+ *
615
+ * @example
616
+ * const status = await clawback.initialize({
617
+ * webhookUrl: 'https://myagent.com/clawback-webhook',
618
+ * lowBalanceThreshold: 10
619
+ * });
620
+ *
621
+ * if (!status.ready) {
622
+ * // Send status.humanActionRequired to your human operator
623
+ * notifyHuman(status.humanActionRequired);
624
+ * }
625
+ */
626
+ async initialize(options = {}) {
627
+ const {
628
+ webhookUrl,
629
+ lowBalanceThreshold = 5,
630
+ minUsdc = 1,
631
+ minSol = 0.01,
632
+ } = options;
633
+
634
+ // Ensure registered
635
+ await this._ensureRegistered();
636
+
637
+ // Set up webhooks if URL provided
638
+ let webhooksConfigured = false;
639
+ if (webhookUrl) {
640
+ try {
641
+ await this.setupWebhooks(webhookUrl, null, {
642
+ lowBalanceThreshold,
643
+ lowBalanceToken: 'USDC'
644
+ });
645
+ webhooksConfigured = true;
646
+ } catch (e) {
647
+ console.error('Failed to configure webhooks:', e.message);
648
+ }
649
+ }
650
+
651
+ // Check funding status
652
+ const fundingStatus = await this.checkFundingStatus({ usdc: minUsdc, sol: minSol });
653
+
654
+ const ready = !fundingStatus.needsFunding;
655
+ let humanActionRequired = null;
656
+
657
+ if (fundingStatus.needsFunding) {
658
+ humanActionRequired = fundingStatus.fundingInstructions;
659
+ }
660
+
661
+ return {
662
+ ready,
663
+ balance: fundingStatus.balance,
664
+ webhooksConfigured,
665
+ humanActionRequired,
666
+ };
667
+ }
505
668
  }
506
669
 
507
670
  // Export errors for type checking