@agether/sdk 1.2.0 → 1.3.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 (41) hide show
  1. package/dist/cli.js +295 -5
  2. package/dist/index.d.mts +205 -1
  3. package/dist/index.d.ts +205 -1
  4. package/dist/index.js +460 -17
  5. package/dist/index.mjs +458 -16
  6. package/package.json +1 -1
  7. package/dist/cli.d.mts +0 -2
  8. package/dist/cli.d.ts +0 -19
  9. package/dist/cli.d.ts.map +0 -1
  10. package/dist/cli.mjs +0 -0
  11. package/dist/clients/AgentIdentityClient.d.ts +0 -163
  12. package/dist/clients/AgentIdentityClient.d.ts.map +0 -1
  13. package/dist/clients/AgentIdentityClient.js +0 -293
  14. package/dist/clients/AgetherClient.d.ts +0 -101
  15. package/dist/clients/AgetherClient.d.ts.map +0 -1
  16. package/dist/clients/AgetherClient.js +0 -272
  17. package/dist/clients/ScoringClient.d.ts +0 -138
  18. package/dist/clients/ScoringClient.d.ts.map +0 -1
  19. package/dist/clients/ScoringClient.js +0 -135
  20. package/dist/clients/VaultClient.d.ts +0 -62
  21. package/dist/clients/VaultClient.d.ts.map +0 -1
  22. package/dist/clients/VaultClient.js +0 -157
  23. package/dist/clients/WalletClient.d.ts +0 -73
  24. package/dist/clients/WalletClient.d.ts.map +0 -1
  25. package/dist/clients/WalletClient.js +0 -174
  26. package/dist/clients/X402Client.d.ts +0 -61
  27. package/dist/clients/X402Client.d.ts.map +0 -1
  28. package/dist/clients/X402Client.js +0 -303
  29. package/dist/index.d.ts.map +0 -1
  30. package/dist/types/index.d.ts +0 -220
  31. package/dist/types/index.d.ts.map +0 -1
  32. package/dist/types/index.js +0 -52
  33. package/dist/utils/abis.d.ts +0 -21
  34. package/dist/utils/abis.d.ts.map +0 -1
  35. package/dist/utils/abis.js +0 -134
  36. package/dist/utils/config.d.ts +0 -31
  37. package/dist/utils/config.d.ts.map +0 -1
  38. package/dist/utils/config.js +0 -117
  39. package/dist/utils/format.d.ts +0 -44
  40. package/dist/utils/format.d.ts.map +0 -1
  41. package/dist/utils/format.js +0 -75
package/dist/index.d.ts CHANGED
@@ -729,6 +729,10 @@ interface X402Config {
729
729
  backendUrl: string;
730
730
  agentId?: string;
731
731
  accountAddress?: string;
732
+ /** Auto-borrow from Morpho credit line when USDC balance is insufficient for payment */
733
+ autoDraw?: boolean;
734
+ /** MorphoCredit contract address (required for autoDraw) */
735
+ morphoCreditAddress?: string;
732
736
  }
733
737
  interface X402Response<T = unknown> {
734
738
  success: boolean;
@@ -761,9 +765,209 @@ declare class X402Client {
761
765
  private request;
762
766
  private parsePaymentRequired;
763
767
  private buildPaymentPayload;
768
+ private static readonly MORPHO_DRAW_ABI;
769
+ private static readonly AGENT_ACCOUNT_EXEC_ABI;
770
+ private static readonly ERC20_BALANCE_ABI;
771
+ private static readonly AUTO_DRAW_COLLATERALS;
772
+ private ensureBalance;
764
773
  private riskCheck;
765
774
  }
766
775
 
776
+ /**
777
+ * MorphoCreditClient — SDK client for Morpho-backed overcollateralized credit
778
+ *
779
+ * Covers all 9 flows:
780
+ * 1. Registration (via AgentIdentityClient) + existing 8004 detection
781
+ * 2. Deposit collateral + borrow in one call
782
+ * 3. Deposit collateral only (no borrow)
783
+ * 4. Sponsor agent by agentId + borrow
784
+ * 5. Sponsor agent by agentId, no borrow
785
+ * 6. Sponsor agent by address + borrow
786
+ * 7. Sponsor agent by address, no borrow
787
+ * 8. Borrow against existing collateral (for x402 payments)
788
+ * 9. (Handled by X402Client.autoDraw)
789
+ */
790
+ interface MorphoCreditConfig {
791
+ /** Private key for signing */
792
+ privateKey: string;
793
+ /** Base RPC URL */
794
+ rpcUrl: string;
795
+ /** Agent's ERC-8004 ID */
796
+ agentId: string | bigint;
797
+ /** Contract addresses */
798
+ contracts: {
799
+ morphoCredit: string;
800
+ accountFactory: string;
801
+ usdc: string;
802
+ };
803
+ }
804
+ interface CollateralToken {
805
+ symbol: string;
806
+ address: string;
807
+ decimals: number;
808
+ }
809
+ interface MorphoPosition {
810
+ token: string;
811
+ collateralAmount: bigint;
812
+ borrowedAmount: bigint;
813
+ borrowShares: bigint;
814
+ isActive: boolean;
815
+ }
816
+ interface DepositResult {
817
+ tx: string;
818
+ amount: bigint;
819
+ token: string;
820
+ agentAccount: string;
821
+ totalCollateral: bigint;
822
+ }
823
+ interface BorrowResult {
824
+ tx: string;
825
+ amount: bigint;
826
+ agentAccount: string;
827
+ totalDebt: bigint;
828
+ collateralToken: string;
829
+ }
830
+ interface DepositAndBorrowResult {
831
+ depositTx: string;
832
+ borrowTx: string;
833
+ collateral: {
834
+ amount: bigint;
835
+ token: string;
836
+ };
837
+ borrowed: bigint;
838
+ agentAccount: string;
839
+ totalDebt: bigint;
840
+ }
841
+ interface SponsorResult {
842
+ depositTx: string;
843
+ borrowTx?: string;
844
+ targetAccount: string;
845
+ targetAgentId?: string;
846
+ collateral: {
847
+ amount: bigint;
848
+ token: string;
849
+ };
850
+ borrowed?: bigint;
851
+ totalCollateral: bigint;
852
+ totalDebt: bigint;
853
+ }
854
+ interface RepayResult {
855
+ tx: string;
856
+ amount: bigint;
857
+ remainingDebt: bigint;
858
+ }
859
+ interface WithdrawResult {
860
+ tx: string;
861
+ amount: bigint;
862
+ token: string;
863
+ destination: string;
864
+ remainingCollateral: bigint;
865
+ }
866
+ declare class MorphoCreditClient {
867
+ private signer;
868
+ private config;
869
+ private morpho;
870
+ private factory;
871
+ private accountAddress?;
872
+ private collaterals;
873
+ constructor(config: MorphoCreditConfig, collaterals?: Record<string, CollateralToken>);
874
+ private resolveToken;
875
+ /**
876
+ * Get the AgentAccount address for the configured agentId.
877
+ */
878
+ getAccountAddress(): Promise<string>;
879
+ /**
880
+ * Get the AgentAccount address for any agentId.
881
+ */
882
+ getAccountForAgent(agentId: string | bigint): Promise<string>;
883
+ private ensureCreditProvider;
884
+ /**
885
+ * Approve ERC-20 token from EOA → MorphoCredit, then deposit for account.
886
+ * This is 2 EOA txs (approve + depositCollateralFor) — cannot be batched
887
+ * because both are called from EOA, not from AgentAccount.
888
+ */
889
+ private approveAndDeposit;
890
+ private morphoIface;
891
+ private erc20Iface;
892
+ /**
893
+ * Execute multiple calls via AgentAccount.executeBatch() in a single tx.
894
+ * Each call is { target, value, data }.
895
+ */
896
+ private batch;
897
+ /**
898
+ * Deposit collateral from EOA into Morpho for own AgentAccount.
899
+ * Does NOT borrow — use `borrow()` or `depositAndBorrow()` for that.
900
+ *
901
+ * @param tokenSymbol - Collateral token (WETH, wstETH, cbETH)
902
+ * @param amount - Human-readable amount (e.g. "0.05")
903
+ */
904
+ deposit(tokenSymbol: string, amount: string): Promise<DepositResult>;
905
+ /**
906
+ * Deposit collateral AND borrow USDC in a single SDK call.
907
+ * USDC lands in the AgentAccount.
908
+ *
909
+ * @param tokenSymbol - Collateral token (WETH, wstETH, cbETH)
910
+ * @param collateralAmount - Human-readable collateral amount (e.g. "0.05")
911
+ * @param borrowAmount - Human-readable USDC amount to borrow (e.g. "50")
912
+ */
913
+ depositAndBorrow(tokenSymbol: string, collateralAmount: string, borrowAmount: string): Promise<DepositAndBorrowResult>;
914
+ /**
915
+ * Borrow USDC against already-deposited collateral.
916
+ * Auto-detects which collateral token has a position.
917
+ * USDC lands in AgentAccount — ready for x402 payments.
918
+ *
919
+ * @param amount - Human-readable USDC amount (e.g. "100")
920
+ */
921
+ borrow(amount: string): Promise<BorrowResult>;
922
+ /**
923
+ * Deposit collateral for another agent. Caller pays from their wallet.
924
+ * Optionally borrow USDC for the agent (only works if caller owns the AgentAccount).
925
+ *
926
+ * Supports both agentId lookup and direct address.
927
+ *
928
+ * @param target - `{ agentId: "17676" }` or `{ address: "0x..." }`
929
+ * @param tokenSymbol - Collateral token
930
+ * @param amount - Human-readable collateral amount
931
+ * @param borrowAmount - Optional: USDC to borrow (only if caller is owner)
932
+ */
933
+ sponsor(target: {
934
+ agentId: string | bigint;
935
+ } | {
936
+ address: string;
937
+ }, tokenSymbol: string, amount: string, borrowAmount?: string): Promise<SponsorResult>;
938
+ /**
939
+ * Repay borrowed USDC from AgentAccount back to Morpho.
940
+ *
941
+ * @param amount - Human-readable USDC amount (e.g. "50")
942
+ */
943
+ repay(amount: string): Promise<RepayResult>;
944
+ /**
945
+ * Withdraw collateral from Morpho back to EOA.
946
+ *
947
+ * @param tokenSymbol - Collateral token
948
+ * @param amount - Human-readable amount or "all"
949
+ */
950
+ withdraw(tokenSymbol: string, amount: string): Promise<WithdrawResult>;
951
+ /**
952
+ * Get position for a specific collateral token.
953
+ */
954
+ getPosition(tokenSymbol: string): Promise<MorphoPosition>;
955
+ /**
956
+ * Get all active positions across all collateral tokens.
957
+ */
958
+ getAllPositions(): Promise<MorphoPosition[]>;
959
+ /**
960
+ * Get total debt across all positions.
961
+ */
962
+ getTotalDebt(): Promise<bigint>;
963
+ /**
964
+ * Get USDC balance in the AgentAccount.
965
+ */
966
+ getAccountUSDC(): Promise<bigint>;
967
+ /** Get the wallet (signer) address */
968
+ getAddress(): string;
969
+ }
970
+
767
971
  /**
768
972
  * WalletClient - SDK for AgentAccount (smart wallet) operations (v2)
769
973
  *
@@ -930,4 +1134,4 @@ declare const VALIDATION_REGISTRY_ABI: string[];
930
1134
  declare const MORPHO_CREDIT_ABI: string[];
931
1135
  declare const ERC20_ABI: string[];
932
1136
 
933
- export { ACCOUNT_FACTORY_ABI, AGENT_ACCOUNT_ABI, AGENT_REPUTATION_ABI, AgentIdentityClient, type AgentIdentityClientOptions, type AgentReputation, AgetherClient, type AgetherClientOptions, type AgetherConfig, AgetherError, type BayesianScore, CREDIT_PROVIDER_ABI, ChainId, type ContractAddresses, type CreditApplication, type CreditAppliedEvent, type CreditApprovedEvent, type CreditDrawnEvent, type CreditInfo, type CreditLine, CreditNotActiveError, type CreditRejectedEvent, type CreditRepaidEvent, CreditStatus, type DrawRequest, ERC20_ABI, type GraduationStatus, IDENTITY_REGISTRY_ABI, InsufficientCreditError, type LPPosition, LP_VAULT_ABI, type LoanPosition, MORPHO_CREDIT_ABI, type MorphoMarketParams, type PaymentProof, type PaymentRequirements, type ProviderStatus, REPUTATION_CREDIT_ABI, type RepayRequest, type RiskCheckResponse, type RiskFactor, type ScoreExplanation, type ScoredLimitPreview, ScoringClient, type ScoringContext, ScoringRejectedError, type ScoringRequest, type ScoringResult, type TransactionResult, VALIDATION_REGISTRY_ABI, VaultClient, type VaultClientOptions, type VaultStats, WalletClient, type WalletClientConfig, type WalletInfo, X402Client, type X402Config, type X402PaymentRequest, type X402PaymentResult, type X402Response, bpsToRate, createConfig, formatAPR, formatAddress, formatHealthFactor, formatPercent, formatTimestamp, formatUSD, formatUnits, getDefaultConfig, getUSDCAddress, parseUnits, rateToBps };
1137
+ export { ACCOUNT_FACTORY_ABI, AGENT_ACCOUNT_ABI, AGENT_REPUTATION_ABI, AgentIdentityClient, type AgentIdentityClientOptions, type AgentReputation, AgetherClient, type AgetherClientOptions, type AgetherConfig, AgetherError, type BayesianScore, type BorrowResult, CREDIT_PROVIDER_ABI, ChainId, type CollateralToken, type ContractAddresses, type CreditApplication, type CreditAppliedEvent, type CreditApprovedEvent, type CreditDrawnEvent, type CreditInfo, type CreditLine, CreditNotActiveError, type CreditRejectedEvent, type CreditRepaidEvent, CreditStatus, type DepositAndBorrowResult, type DepositResult, type DrawRequest, ERC20_ABI, type GraduationStatus, IDENTITY_REGISTRY_ABI, InsufficientCreditError, type LPPosition, LP_VAULT_ABI, type LoanPosition, MORPHO_CREDIT_ABI, MorphoCreditClient, type MorphoCreditConfig, type MorphoMarketParams, type MorphoPosition, type PaymentProof, type PaymentRequirements, type ProviderStatus, REPUTATION_CREDIT_ABI, type RepayRequest, type RepayResult, type RiskCheckResponse, type RiskFactor, type ScoreExplanation, type ScoredLimitPreview, ScoringClient, type ScoringContext, ScoringRejectedError, type ScoringRequest, type ScoringResult, type SponsorResult, type TransactionResult, VALIDATION_REGISTRY_ABI, VaultClient, type VaultClientOptions, type VaultStats, WalletClient, type WalletClientConfig, type WalletInfo, type WithdrawResult, X402Client, type X402Config, type X402PaymentRequest, type X402PaymentResult, type X402Response, bpsToRate, createConfig, formatAPR, formatAddress, formatHealthFactor, formatPercent, formatTimestamp, formatUSD, formatUnits, getDefaultConfig, getUSDCAddress, parseUnits, rateToBps };