Skip to content

Commit

Permalink
Merge branch 'main' of github.com:amhedcb/nfc-relayer into justinlee/…
Browse files Browse the repository at this point in the history
…substitute-sender-address
  • Loading branch information
justin-lee-cb committed Oct 1, 2024
2 parents b37dabe + c2bbe5d commit 43c4223
Show file tree
Hide file tree
Showing 9 changed files with 752 additions and 739 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
},
"dependencies": {
"@prisma/client": "^3.6.0",
"cors": "^2.8.5",
"ethers": "5",
"next": "^12.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/jest": "^26.0.24",
"@types/node": "^16.11.6",
"@types/react": "^17.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "ContactlessPaymentTxOrMsg" ADD COLUMN "txHash" TEXT;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ model ContactlessPaymentTxOrMsg {
additionalPayload Json?
dappName String?
dappUrl String?
txHash String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
3 changes: 3 additions & 0 deletions src/pages/api/paymentTxParams/[uuid].ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NextApiRequest, NextApiResponse } from 'next';
import { ethers } from 'ethers';
import { getPaymentTxOrMsg } from '@/services/paymentTxOrMsgService';
import { applyCors } from '@/services/cors';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'GET') {
await applyCors(req, res);

try {
const { uuid, senderAddress } = req.query;

Expand Down
5 changes: 4 additions & 1 deletion src/pages/api/paymentTxParams/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { applyCors } from '@/services/cors';
import { createPaymentTxOrMsg } from '@/services/paymentTxOrMsgService';
import { Payload } from '@/types/paymentTx';
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
await applyCors(req, res);

if (req.method === 'POST') {
try {
// validations
Expand All @@ -28,4 +31,4 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
res.setHeader('Allow', ['POST']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}
};
8 changes: 4 additions & 4 deletions src/pages/api/submitPaymentTx/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { fiatTokenAbi } from "@/FiatTokenAbi";
import { applyCors } from "@/services/cors";
import { appendTxHashToPayment } from "@/services/paymentTxOrMsgService";
import { sponsoredUsdcMapping } from "@/sponsoredUsdcConfig";
import { ethers } from "ethers";
import type { NextApiRequest, NextApiResponse } from "next";
Expand All @@ -19,8 +20,7 @@ export default async function handler(
req: NextApiRequest,
res: NextApiResponse<SponsoredRelayResponse>
) {
// return res.status(500);

await applyCors(req, res);
if (req.method !== "POST") {
return res.status(500);
}
Expand All @@ -35,8 +35,8 @@ export default async function handler(

const provider = new ethers.providers.JsonRpcProvider(sponsoredInfo.rpc);

// TODO (Mike): If possible, use the UUID to communicate to the appropriate websocket that a transaction was received and sent
if (txHash) {
await appendTxHashToPayment(uuid, txHash);
return res.status(200).json({ data: { txHash } as TxHashReceivedResponse });
}

Expand Down
32 changes: 32 additions & 0 deletions src/services/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Cors from 'cors';
import { NextApiRequest, NextApiResponse } from 'next';

const cors = Cors({
methods: ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
origin: '*',
optionsSuccessStatus: 200,
});

// eslint-disable-next-line @typescript-eslint/ban-types
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function) {
return new Promise((resolve, reject) => {
if (!fn) {
return reject(new Error('Middleware function is not defined'));
}
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}

export async function applyCors(req: NextApiRequest, res: NextApiResponse) {
try {
await runMiddleware(req, res, cors);
} catch (error) {
console.error('Error applying CORS middleware:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
12 changes: 12 additions & 0 deletions src/services/paymentTxOrMsgService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ export const getPaymentTxOrMsg = async (uuid: string) => {
...(txParams as Record<string, unknown>),
};
};

// adding a tx hash will let the client know that the transaction was sent
export const appendTxHashToPayment = async (uuid: string, txHash: string) => {
const prisma = getPrismaClient();

return prisma.contactlessPaymentTxOrMsg.update({
where: { uuid },
data: {
txHash,
},
});
};
1,426 changes: 692 additions & 734 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 43c4223

Please sign in to comment.