Skip to content

Commit

Permalink
call smart contracts for payout amounts (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Sep 30, 2024
1 parent 9a9d4fd commit 233cead
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/app/api/_utils/chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ethers, JsonRpcProvider, Signer } from "ethers";

export async function getBackendVoidSigner(): Promise<Signer> {
const provider = new JsonRpcProvider(process.env.BACKEND_CHAIN_RPC_URL);
return new ethers.VoidSigner("0x0000000000000000000000000000000000000000", provider);
}
8 changes: 8 additions & 0 deletions src/app/api/quote/[carrier]/[flightNumber]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { NextRequest } from "next/server";
import { LOGGER } from "../../../../../utils/logger_backend";
import { FLIGHTSTATS_BASE_URL } from "../../../_utils/config";
import { sendRequestAndReturnResponse } from "../../../_utils/proxy";
import { FlightLib__factory } from "../../../../../contracts/gif";
import { getBackendVoidSigner } from "../../../_utils/chain";

/**
* get rating from flightstats and calculate quote via contract call
Expand All @@ -26,8 +28,14 @@ export async function GET(request: NextRequest, { params } : { params: { carrier
LOGGER.debug(`[${reqId}] Ratings:ontime ${rating.ontime}, observations: ${rating.observations}, late15 ${rating.late15}, late30 ${rating.late30}, `
+ `late45 ${rating.late45}, cancelled ${rating.cancelled}, diverted ${rating.diverted} ontimepercent ${rating.ontimepercent}`);

const signer = await getBackendVoidSigner();

// TODO: calculate premium based on rating by calling onchain quote function (#45)
// TODO: generate signature for flight rating data
const productAddress = process.env.NEXT_PUBLIC_PRODUCT_CONTRACT_ADDRESS!;
const flightLib = FlightLib__factory.connect(process.env.NEXT_PUBLIC_FLIGHT_LIB_ADDRESS!, signer);
const result = await flightLib.calculatePayoutAmounts(productAddress, 30 * 10 ** 6, [rating.observations, rating.late15, rating.late30, rating.late45, rating.cancelled, rating.diverted]);
LOGGER.info(`calculatePayoutAmounts ${result[0].toString()}, ${result[1].toString()}, ${result[2].toString()}`);

return Response.json({
premium: 30 * 10 ** 6, // FIXME: return actual premium
Expand Down

0 comments on commit 233cead

Please sign in to comment.