Skip to content

Commit 0d059da

Browse files
committed
refactor: implement payableAmount in sdk level
and refactor acp job type
1 parent f83419e commit 0d059da

2 files changed

Lines changed: 52 additions & 8 deletions

File tree

src/acpJob.ts

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AcpJob {
3232
public phase: AcpJobPhases,
3333
public context: Record<string, any>,
3434
public contractAddress: Address,
35-
public netPayableAmount?: number
35+
public deliverable?: DeliverablePayload
3636
) {
3737
const content = this.memos.find(
3838
(m) => m.nextPhase === AcpJobPhases.NEGOTIATION
@@ -85,11 +85,6 @@ class AcpJob {
8585
return this.acpContractClient.config.baseFare;
8686
}
8787

88-
public get deliverable() {
89-
return this.memos.find((m) => m.nextPhase === AcpJobPhases.COMPLETED)
90-
?.content;
91-
}
92-
9388
public get rejectionReason() {
9489
const rejectedMemo = this.memos.find(
9590
(m) =>
@@ -125,6 +120,56 @@ class AcpJob {
125120
return this.memos[this.memos.length - 1];
126121
}
127122

123+
public get netPayableAmount(): number | undefined {
124+
const payableMemo = this.memos.find(
125+
(m) =>
126+
m.nextPhase === AcpJobPhases.TRANSACTION &&
127+
m.type === MemoType.PAYABLE_REQUEST &&
128+
m.payableDetails
129+
);
130+
131+
if (!payableMemo || !payableMemo.payableDetails) {
132+
return undefined;
133+
}
134+
135+
const decimals =
136+
payableMemo.payableDetails.token.toLowerCase() ===
137+
this.baseFare.contractAddress.toLowerCase()
138+
? this.baseFare.decimals
139+
: 18;
140+
141+
const formattedAmount = formatUnits(
142+
payableMemo.payableDetails.amount,
143+
decimals
144+
);
145+
146+
const payableAmount = parseFloat(formattedAmount);
147+
148+
if (!Number.isFinite(payableAmount)) {
149+
throw new AcpError(
150+
`Payable amount overflow: ${formattedAmount} exceeds safe number range`
151+
);
152+
}
153+
154+
if (payableAmount > Number.MAX_SAFE_INTEGER) {
155+
throw new AcpError(
156+
`Payable amount ${formattedAmount} exceeds MAX_SAFE_INTEGER (${Number.MAX_SAFE_INTEGER}). Precision may be lost.`
157+
);
158+
}
159+
160+
if (this.priceType === PriceType.PERCENTAGE) {
161+
const netAmount = payableAmount * (1 - this.priceValue);
162+
if (!Number.isFinite(netAmount)) {
163+
throw new AcpError(
164+
`Net payable amount calculation overflow for percentage pricing`
165+
);
166+
}
167+
return netAmount;
168+
}
169+
170+
return payableAmount;
171+
}
172+
128173
async createRequirement(content: string) {
129174
const operations: OperationPayload[] = [];
130175

src/interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ export interface IAcpJob {
7575
evaluatorAddress: Address;
7676
price: number;
7777
priceTokenAddress: Address;
78-
deliverable: DeliverablePayload | null;
7978
memos: IAcpMemoData[];
8079
context: Record<string, any>;
8180
createdAt: string;
8281
contractAddress: Address;
82+
deliverable?: DeliverablePayload;
8383
memoToSign?: number;
84-
netPayableAmount?: number;
8584
};
8685
error?: Error;
8786
}

0 commit comments

Comments
 (0)