Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/acpX402.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class AcpX402 {
constructor(
private config: AcpContractConfig,
private sessionKeyClient: ModularAccountV2Client,
private publicClient: ReturnType<typeof createPublicClient>
private publicClient: ReturnType<typeof createPublicClient>,
) {
this.config = config;
this.sessionKeyClient = sessionKeyClient;
Expand All @@ -32,7 +32,7 @@ export class AcpX402 {

async signUpdateJobNonceMessage(
jobId: number,
nonce: string
nonce: string,
): Promise<`0x${string}`> {
const message = `${jobId}-${nonce}`;
const signature = await this.sessionKeyClient.account
Expand Down Expand Up @@ -63,21 +63,24 @@ export class AcpX402 {
if (!response.ok) {
throw new AcpError(
"Failed to update job X402 nonce",
response.statusText
response.statusText,
);
}

const acpJob = await response.json();

return acpJob;
} catch (error) {
if (error instanceof AcpError) {
throw error;
}
throw new AcpError("Failed to update job X402 nonce", error);
}
}

async generatePayment(
payableRequest: X402PayableRequest,
requirements: X402PayableRequirements
requirements: X402PayableRequirements,
): Promise<X402Payment> {
try {
const USDC_CONTRACT = this.config.baseFare.contractAddress;
Expand Down Expand Up @@ -157,7 +160,7 @@ export class AcpX402 {
url: string,
version: string,
budget?: string,
signature?: string
signature?: string,
) {
const baseUrl = this.config.x402Config?.url;
if (!baseUrl) throw new AcpError("X402 URL not configured");
Expand All @@ -176,7 +179,7 @@ export class AcpX402 {
if (!res.ok && res.status !== HTTP_STATUS_CODES.PAYMENT_REQUIRED) {
throw new AcpError(
"Invalid response status code for X402 request",
data
data,
);
}

Expand All @@ -185,6 +188,9 @@ export class AcpX402 {
data,
};
} catch (error) {
if (error instanceof AcpError) {
throw error;
}
throw new AcpError("Failed to perform X402 request", error);
}
}
Expand Down