@agentxpay/sdk 0.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.
@@ -0,0 +1,96 @@
1
+ import { ethers } from "ethers";
2
+ import PaymentManagerABI from "../abi/PaymentManager.json";
3
+ import { PaymentResult } from "../types";
4
+
5
+ export class PaymentsModule {
6
+ private contract: ethers.Contract | null = null;
7
+ private address: string;
8
+ private signerOrProvider: ethers.Signer | ethers.Provider;
9
+
10
+ constructor(address: string, signerOrProvider: ethers.Signer | ethers.Provider) {
11
+ this.address = address;
12
+ this.signerOrProvider = signerOrProvider;
13
+ if (address && address !== "") {
14
+ this.contract = new ethers.Contract(address, PaymentManagerABI, signerOrProvider);
15
+ }
16
+ }
17
+
18
+ private getContract(): ethers.Contract {
19
+ if (!this.contract) {
20
+ throw new Error("PaymentManager contract address not configured");
21
+ }
22
+ return this.contract;
23
+ }
24
+
25
+ async payPerUse(serviceId: bigint, amount: bigint): Promise<PaymentResult> {
26
+ const contract = this.getContract();
27
+ const tx = await contract.payPerUse(serviceId, { value: amount });
28
+ const receipt = await tx.wait();
29
+ return {
30
+ txHash: receipt.hash,
31
+ serviceId,
32
+ amount,
33
+ provider: "",
34
+ };
35
+ }
36
+
37
+ async batchPay(serviceIds: bigint[], totalAmount: bigint): Promise<string> {
38
+ const contract = this.getContract();
39
+ const tx = await contract.batchPay(serviceIds, { value: totalAmount });
40
+ const receipt = await tx.wait();
41
+ return receipt.hash;
42
+ }
43
+
44
+ async deposit(amount: bigint): Promise<string> {
45
+ const contract = this.getContract();
46
+ const tx = await contract.deposit({ value: amount });
47
+ const receipt = await tx.wait();
48
+ return receipt.hash;
49
+ }
50
+
51
+ async withdraw(amount: bigint): Promise<string> {
52
+ const contract = this.getContract();
53
+ const tx = await contract.withdraw(amount);
54
+ const receipt = await tx.wait();
55
+ return receipt.hash;
56
+ }
57
+
58
+ async payFromBalance(serviceId: bigint): Promise<string> {
59
+ const contract = this.getContract();
60
+ const tx = await contract.payFromBalance(serviceId);
61
+ const receipt = await tx.wait();
62
+ return receipt.hash;
63
+ }
64
+
65
+ async withdrawProviderEarnings(): Promise<string> {
66
+ const contract = this.getContract();
67
+ const tx = await contract.withdrawProviderEarnings();
68
+ const receipt = await tx.wait();
69
+ return receipt.hash;
70
+ }
71
+
72
+ async getUserBalance(address: string): Promise<bigint> {
73
+ const contract = this.getContract();
74
+ return contract.getUserBalance(address);
75
+ }
76
+
77
+ async getProviderEarnings(address: string): Promise<bigint> {
78
+ const contract = this.getContract();
79
+ return contract.getProviderEarnings(address);
80
+ }
81
+
82
+ async getPaymentCount(address: string): Promise<bigint> {
83
+ const contract = this.getContract();
84
+ return contract.getPaymentCount(address);
85
+ }
86
+
87
+ async getTotalSpent(address: string): Promise<bigint> {
88
+ const contract = this.getContract();
89
+ return contract.getTotalSpent(address);
90
+ }
91
+
92
+ getContractAddress(): string {
93
+ const contract = this.getContract();
94
+ return contract.target as string;
95
+ }
96
+ }
@@ -0,0 +1,97 @@
1
+ import { ethers } from "ethers";
2
+ import ServiceRegistryABI from "../abi/ServiceRegistry.json";
3
+ import { Service, SubscriptionPlan, DiscoverFilter } from "../types";
4
+
5
+ export class ServicesModule {
6
+ private contract: ethers.Contract;
7
+
8
+ constructor(address: string, signerOrProvider: ethers.Signer | ethers.Provider) {
9
+ this.contract = new ethers.Contract(address, ServiceRegistryABI, signerOrProvider);
10
+ }
11
+
12
+ async registerService(
13
+ name: string,
14
+ description: string,
15
+ endpoint: string,
16
+ category: string,
17
+ pricePerCall: bigint
18
+ ): Promise<{ txHash: string; serviceId: bigint }> {
19
+ const tx = await this.contract.registerService(name, description, endpoint, category, pricePerCall);
20
+ const receipt = await tx.wait();
21
+ const event = receipt.logs.find(
22
+ (log: any) => this.contract.interface.parseLog(log)?.name === "ServiceRegistered"
23
+ );
24
+ const parsed = event ? this.contract.interface.parseLog(event) : null;
25
+ const serviceId = parsed ? parsed.args[0] : 0n;
26
+ return { txHash: receipt.hash, serviceId };
27
+ }
28
+
29
+ async addSubscriptionPlan(
30
+ serviceId: bigint,
31
+ name: string,
32
+ price: bigint,
33
+ duration: bigint
34
+ ): Promise<{ txHash: string; planId: bigint }> {
35
+ const tx = await this.contract.addSubscriptionPlan(serviceId, name, price, duration);
36
+ const receipt = await tx.wait();
37
+ return { txHash: receipt.hash, planId: 0n };
38
+ }
39
+
40
+ async getServiceDetails(serviceId: bigint): Promise<Service> {
41
+ const s = await this.contract.getServiceDetails(serviceId);
42
+ return this._mapService(s);
43
+ }
44
+
45
+ async discoverServices(filter?: DiscoverFilter): Promise<Service[]> {
46
+ if (filter?.category) {
47
+ const services = await this.contract.getServicesByCategory(filter.category);
48
+ let result = services.map((s: any) => this._mapService(s));
49
+ if (filter.maxPrice) {
50
+ result = result.filter((s: Service) => s.pricePerCall <= filter.maxPrice!);
51
+ }
52
+ return result;
53
+ }
54
+
55
+ const count = await this.contract.getServiceCount();
56
+ const services: Service[] = [];
57
+ for (let i = 1n; i <= count; i++) {
58
+ const s = await this.contract.getServiceDetails(i).catch(() => null);
59
+ if (s && s.isActive) {
60
+ const service = this._mapService(s);
61
+ if (!filter?.maxPrice || service.pricePerCall <= filter.maxPrice) {
62
+ services.push(service);
63
+ }
64
+ }
65
+ }
66
+ return services;
67
+ }
68
+
69
+ async getSubscriptionPlans(serviceId: bigint): Promise<SubscriptionPlan[]> {
70
+ const plans = await this.contract.getSubscriptionPlans(serviceId);
71
+ return plans.map((p: any) => ({
72
+ planId: p.planId,
73
+ serviceId: p.serviceId,
74
+ price: p.price,
75
+ duration: p.duration,
76
+ name: p.name,
77
+ }));
78
+ }
79
+
80
+ async getServiceCount(): Promise<bigint> {
81
+ return this.contract.getServiceCount();
82
+ }
83
+
84
+ private _mapService(s: any): Service {
85
+ return {
86
+ id: s.id,
87
+ provider: s.provider,
88
+ name: s.name,
89
+ description: s.description,
90
+ endpoint: s.endpoint,
91
+ category: s.category,
92
+ pricePerCall: s.pricePerCall,
93
+ isActive: s.isActive,
94
+ createdAt: s.createdAt,
95
+ };
96
+ }
97
+ }
@@ -0,0 +1,78 @@
1
+ import { ethers } from "ethers";
2
+ import SubscriptionManagerABI from "../abi/SubscriptionManager.json";
3
+ import { Subscription } from "../types";
4
+
5
+ export class SubscriptionsModule {
6
+ private contract: ethers.Contract | null = null;
7
+
8
+ constructor(address: string, signerOrProvider: ethers.Signer | ethers.Provider) {
9
+ if (address && address !== "") {
10
+ this.contract = new ethers.Contract(address, SubscriptionManagerABI, signerOrProvider);
11
+ }
12
+ }
13
+
14
+ private getContract(): ethers.Contract {
15
+ if (!this.contract) {
16
+ throw new Error("SubscriptionManager contract address not configured");
17
+ }
18
+ return this.contract;
19
+ }
20
+
21
+ async subscribe(serviceId: bigint, planId: bigint, amount: bigint): Promise<{ txHash: string; subscriptionId: bigint }> {
22
+ const contract = this.getContract();
23
+ const tx = await contract.subscribe(serviceId, planId, { value: amount });
24
+ const receipt = await tx.wait();
25
+ return { txHash: receipt.hash, subscriptionId: 0n };
26
+ }
27
+
28
+ async cancelSubscription(subscriptionId: bigint): Promise<string> {
29
+ const contract = this.getContract();
30
+ const tx = await contract.cancelSubscription(subscriptionId);
31
+ const receipt = await tx.wait();
32
+ return receipt.hash;
33
+ }
34
+
35
+ async renewSubscription(subscriptionId: bigint, amount: bigint): Promise<string> {
36
+ const contract = this.getContract();
37
+ const tx = await contract.renewSubscription(subscriptionId, { value: amount });
38
+ const receipt = await tx.wait();
39
+ return receipt.hash;
40
+ }
41
+
42
+ async toggleAutoRenew(subscriptionId: bigint): Promise<string> {
43
+ const contract = this.getContract();
44
+ const tx = await contract.toggleAutoRenew(subscriptionId);
45
+ const receipt = await tx.wait();
46
+ return receipt.hash;
47
+ }
48
+
49
+ async getSubscription(subscriptionId: bigint): Promise<Subscription> {
50
+ const contract = this.getContract();
51
+ const s = await contract.getSubscription(subscriptionId);
52
+ return {
53
+ subscriptionId: s.subscriptionId,
54
+ serviceId: s.serviceId,
55
+ planId: s.planId,
56
+ subscriber: s.subscriber,
57
+ startTime: s.startTime,
58
+ endTime: s.endTime,
59
+ autoRenew: s.autoRenew,
60
+ isActive: s.isActive,
61
+ };
62
+ }
63
+
64
+ async getUserSubscriptions(address: string): Promise<bigint[]> {
65
+ const contract = this.getContract();
66
+ return contract.getUserSubscriptions(address);
67
+ }
68
+
69
+ async isActive(subscriptionId: bigint): Promise<boolean> {
70
+ const contract = this.getContract();
71
+ return contract.isSubscriptionActive(subscriptionId);
72
+ }
73
+
74
+ async checkAccess(userAddress: string, serviceId: bigint): Promise<boolean> {
75
+ const contract = this.getContract();
76
+ return contract.checkAccess(userAddress, serviceId);
77
+ }
78
+ }
@@ -0,0 +1,111 @@
1
+ import { ethers } from "ethers";
2
+ import AgentWalletABI from "../abi/AgentWallet.json";
3
+ import AgentWalletFactoryABI from "../abi/AgentWalletFactory.json";
4
+
5
+ export class WalletModule {
6
+ private factoryContract: ethers.Contract | null = null;
7
+ private signerOrProvider: ethers.Signer | ethers.Provider;
8
+
9
+ constructor(factoryAddress: string, signerOrProvider: ethers.Signer | ethers.Provider) {
10
+ if (factoryAddress && factoryAddress !== "") {
11
+ this.factoryContract = new ethers.Contract(factoryAddress, AgentWalletFactoryABI, signerOrProvider);
12
+ }
13
+ this.signerOrProvider = signerOrProvider;
14
+ }
15
+
16
+ private getContract(): ethers.Contract {
17
+ if (!this.factoryContract) {
18
+ throw new Error("AgentWalletFactory contract address not configured");
19
+ }
20
+ return this.factoryContract;
21
+ }
22
+
23
+ async createWallet(dailyLimit: bigint): Promise<{ txHash: string; walletAddress: string }> {
24
+ const contract = this.getContract();
25
+ const tx = await contract.createWallet(dailyLimit);
26
+ const receipt = await tx.wait();
27
+ const event = receipt.logs.find(
28
+ (log: any) => contract.interface.parseLog(log)?.name === "WalletCreated"
29
+ );
30
+ const parsed = event ? contract.interface.parseLog(event) : null;
31
+ const walletAddress = parsed ? parsed.args[1] : "";
32
+ return { txHash: receipt.hash, walletAddress };
33
+ }
34
+
35
+ async getWallets(owner: string): Promise<string[]> {
36
+ const contract = this.getContract();
37
+ return contract.getWallets(owner);
38
+ }
39
+
40
+ getWalletInstance(walletAddress: string): AgentWalletClient {
41
+ return new AgentWalletClient(walletAddress, this.signerOrProvider);
42
+ }
43
+ }
44
+
45
+ export class AgentWalletClient {
46
+ private contract: ethers.Contract;
47
+
48
+ constructor(address: string, signerOrProvider: ethers.Signer | ethers.Provider) {
49
+ this.contract = new ethers.Contract(address, AgentWalletABI, signerOrProvider);
50
+ }
51
+
52
+ async execute(to: string, value: bigint, data: string = "0x"): Promise<string> {
53
+ const tx = await this.contract.execute(to, value, data);
54
+ const receipt = await tx.wait();
55
+ return receipt.hash;
56
+ }
57
+
58
+ async setDailySpendingLimit(limit: bigint): Promise<string> {
59
+ const tx = await this.contract.setDailySpendingLimit(limit);
60
+ const receipt = await tx.wait();
61
+ return receipt.hash;
62
+ }
63
+
64
+ async authorizeAgent(agent: string): Promise<string> {
65
+ const tx = await this.contract.authorizeAgent(agent);
66
+ const receipt = await tx.wait();
67
+ return receipt.hash;
68
+ }
69
+
70
+ async revokeAgent(agent: string): Promise<string> {
71
+ const tx = await this.contract.revokeAgent(agent);
72
+ const receipt = await tx.wait();
73
+ return receipt.hash;
74
+ }
75
+
76
+ async withdrawFunds(amount: bigint): Promise<string> {
77
+ const tx = await this.contract.withdrawFunds(amount);
78
+ const receipt = await tx.wait();
79
+ return receipt.hash;
80
+ }
81
+
82
+ async deposit(amount: bigint): Promise<string> {
83
+ const signer = this.contract.runner as ethers.Signer;
84
+ const tx = await signer.sendTransaction({
85
+ to: await this.contract.getAddress(),
86
+ value: amount,
87
+ });
88
+ const receipt = await tx.wait();
89
+ return receipt!.hash;
90
+ }
91
+
92
+ async isAuthorizedAgent(agent: string): Promise<boolean> {
93
+ return this.contract.isAuthorizedAgent(agent);
94
+ }
95
+
96
+ async getDailySpendingLimit(): Promise<bigint> {
97
+ return this.contract.getDailySpendingLimit();
98
+ }
99
+
100
+ async getDailySpent(): Promise<bigint> {
101
+ return this.contract.getDailySpent();
102
+ }
103
+
104
+ async getRemainingDailyAllowance(): Promise<bigint> {
105
+ return this.contract.getRemainingDailyAllowance();
106
+ }
107
+
108
+ async getOwner(): Promise<string> {
109
+ return this.contract.getOwner();
110
+ }
111
+ }
@@ -0,0 +1,93 @@
1
+ export interface AgentXPayConfig {
2
+ rpcUrl: string;
3
+ privateKey?: string;
4
+ signer?: any; // ethers.Signer
5
+ contracts?: ContractAddresses;
6
+ network?: "local" | "testnet" | "mainnet";
7
+ }
8
+
9
+ export interface ContractAddresses {
10
+ serviceRegistry?: string;
11
+ paymentManager?: string;
12
+ subscriptionManager?: string;
13
+ escrow?: string;
14
+ agentWalletFactory?: string;
15
+ }
16
+
17
+ export interface Service {
18
+ id: bigint;
19
+ provider: string;
20
+ name: string;
21
+ description: string;
22
+ endpoint: string;
23
+ category: string;
24
+ pricePerCall: bigint;
25
+ isActive: boolean;
26
+ createdAt: bigint;
27
+ }
28
+
29
+ export interface SubscriptionPlan {
30
+ planId: bigint;
31
+ serviceId: bigint;
32
+ price: bigint;
33
+ duration: bigint;
34
+ name: string;
35
+ }
36
+
37
+ export interface Subscription {
38
+ subscriptionId: bigint;
39
+ serviceId: bigint;
40
+ planId: bigint;
41
+ subscriber: string;
42
+ startTime: bigint;
43
+ endTime: bigint;
44
+ autoRenew: boolean;
45
+ isActive: boolean;
46
+ }
47
+
48
+ export enum EscrowStatus {
49
+ Created = 0,
50
+ Funded = 1,
51
+ Released = 2,
52
+ Disputed = 3,
53
+ Refunded = 4,
54
+ Resolved = 5,
55
+ }
56
+
57
+ export interface EscrowData {
58
+ escrowId: bigint;
59
+ serviceId: bigint;
60
+ payer: string;
61
+ provider: string;
62
+ amount: bigint;
63
+ status: EscrowStatus;
64
+ createdAt: bigint;
65
+ deadline: bigint;
66
+ description: string;
67
+ }
68
+
69
+ export interface DiscoverFilter {
70
+ category?: string;
71
+ maxPrice?: bigint;
72
+ isActive?: boolean;
73
+ }
74
+
75
+ export interface PaymentResult {
76
+ txHash: string;
77
+ serviceId: bigint;
78
+ amount: bigint;
79
+ provider: string;
80
+ }
81
+
82
+ export interface X402PaymentInfo {
83
+ address: string;
84
+ amount: string;
85
+ token: string;
86
+ serviceId: string;
87
+ chainId: string;
88
+ }
89
+
90
+ export interface X402FetchOptions extends RequestInit {
91
+ autoPayment?: boolean;
92
+ maxRetries?: number;
93
+ }
@@ -0,0 +1,25 @@
1
+ import { ContractAddresses } from "../types";
2
+
3
+ export const MONAD_TESTNET_CHAIN_ID = 10143;
4
+ export const MONAD_TESTNET_RPC = "https://testnet-rpc.monad.xyz/";
5
+
6
+ export const DEFAULT_CONTRACTS: Record<string, ContractAddresses> = {
7
+ local: {
8
+ serviceRegistry: "",
9
+ paymentManager: "",
10
+ subscriptionManager: "",
11
+ escrow: "",
12
+ agentWalletFactory: "",
13
+ },
14
+ testnet: {
15
+ serviceRegistry: "",
16
+ paymentManager: "",
17
+ subscriptionManager: "",
18
+ escrow: "",
19
+ agentWalletFactory: "",
20
+ },
21
+ };
22
+
23
+ export const DEFAULT_GAS_LIMIT = 500000n;
24
+ export const MAX_RETRIES = 3;
25
+ export const RETRY_DELAY_MS = 1000;
@@ -0,0 +1,30 @@
1
+ import { ethers } from "ethers";
2
+
3
+ export function shortenAddress(address: string, chars = 4): string {
4
+ return `${address.slice(0, chars + 2)}...${address.slice(-chars)}`;
5
+ }
6
+
7
+ export function formatEther(wei: bigint): string {
8
+ return ethers.formatEther(wei);
9
+ }
10
+
11
+ export function parseEther(ether: string): bigint {
12
+ return ethers.parseEther(ether);
13
+ }
14
+
15
+ export function isValidAddress(address: string): boolean {
16
+ return ethers.isAddress(address);
17
+ }
18
+
19
+ export async function waitForTx(
20
+ tx: ethers.TransactionResponse,
21
+ confirmations = 1
22
+ ): Promise<ethers.TransactionReceipt> {
23
+ const receipt = await tx.wait(confirmations);
24
+ if (!receipt) throw new Error("Transaction receipt is null");
25
+ return receipt;
26
+ }
27
+
28
+ export function sleep(ms: number): Promise<void> {
29
+ return new Promise((resolve) => setTimeout(resolve, ms));
30
+ }