Skip to content

Commit

Permalink
⚡️ server: simulate credit for installments authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Nov 12, 2024
1 parent 4814281 commit af278d5
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions server/hooks/cryptomate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,6 @@ export default new Hono().post(
);

async function prepareCollection(payload: v.InferOutput<typeof Payload>) {
const previewPromise = startSpan({ name: "query onchain state", op: "exa.preview" }, () =>
publicClient.readContract({
abi: installmentsPreviewerAbi,
address: installmentsPreviewerAddress,
functionName: "preview",
}),
);
const card = await database.query.cards.findFirst({
columns: { mode: true },
where: and(eq(cards.id, payload.data.card_id), eq(cards.status, "ACTIVE")),
Expand All @@ -248,7 +241,19 @@ async function prepareCollection(payload: v.InferOutput<typeof Payload>) {
args: [BigInt(firstMaturity), amount, BigInt(timestamp), signature],
} as const;
}
const preview = await startSpan({ name: "await preview", op: "exa.wait" }, () => previewPromise);
if (payload.event_type === "AUTHORIZATION") {
return {
functionName: "collectCredit",
args: [BigInt(firstMaturity + card.mode * MATURITY_INTERVAL), amount, BigInt(timestamp), signature],
} as const;
}
const preview = await startSpan({ name: "query onchain state", op: "exa.preview" }, () =>
publicClient.readContract({
abi: installmentsPreviewerAbi,
address: installmentsPreviewerAddress,
functionName: "preview",
}),
);
setContext("preview", preview);
const installments = startSpan({ name: "split installments", op: "exa.split" }, () =>
splitInstallments(
Expand Down

0 comments on commit af278d5

Please sign in to comment.