|
| 1 | +import { AddSubmitProofsLogRequestPayload } from "api/types/admin/transfer/add-log"; |
| 2 | +import dbConnect from "lib/mongodb"; |
| 3 | +import { NextApiRequest, NextApiResponse } from "next"; |
| 4 | +import { verifySignature } from "utils/api/verify-signature"; |
| 5 | +import TransferModel from "models/transfer"; |
| 6 | +import Web3 from "web3"; |
| 7 | +import relayAbi from "@contexts/Transfer/relay-abi"; |
| 8 | +import { RELAY_CONTRACT_ADDRESS } from "@constants"; |
| 9 | +import { |
| 10 | + COMMON_STATUS, |
| 11 | + ITransferLog, |
| 12 | + SYS_TO_ETH_TRANSFER_STATUS, |
| 13 | +} from "@contexts/Transfer/types"; |
| 14 | + |
| 15 | +export const handleSubmitProofs = async ( |
| 16 | + transferId: string, |
| 17 | + payload: AddSubmitProofsLogRequestPayload, |
| 18 | + req: NextApiRequest, |
| 19 | + res: NextApiResponse |
| 20 | +) => { |
| 21 | + await dbConnect(); |
| 22 | + const web3 = new Web3("https://rpc.syscoin.org"); |
| 23 | + |
| 24 | + const { address } = req.session.user!; |
| 25 | + |
| 26 | + const transfer = await TransferModel.findOne({ id: transferId }); |
| 27 | + if (!transfer) { |
| 28 | + return res.status(404).json({ message: "Transfer not found" }); |
| 29 | + } |
| 30 | + |
| 31 | + const { clearAll, signedMessage, txHash, operation } = payload; |
| 32 | + |
| 33 | + const data = { |
| 34 | + operation, |
| 35 | + txHash, |
| 36 | + clearAll, |
| 37 | + }; |
| 38 | + const message = JSON.stringify(data); |
| 39 | + if (!verifySignature(message, signedMessage, address)) { |
| 40 | + return res.status(401).json({ message: "Unauthorized" }); |
| 41 | + } |
| 42 | + |
| 43 | + const receipt = await web3.eth.getTransactionReceipt(txHash); |
| 44 | + |
| 45 | + if (!receipt) { |
| 46 | + return res.status(400).json({ |
| 47 | + message: "Invalid transaction hash: Transaction not found", |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + if ( |
| 52 | + !receipt.to || |
| 53 | + receipt.to.toLowerCase() !== RELAY_CONTRACT_ADDRESS.toLowerCase() |
| 54 | + ) { |
| 55 | + return res.status(400).json({ |
| 56 | + message: "Invalid transaction: To is not the relay contract address", |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + if (receipt.logs.length === 0) { |
| 61 | + return res.status(400).json({ |
| 62 | + message: "Invalid transaction: No logs found", |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + if (clearAll) { |
| 67 | + transfer.logs = transfer.logs.filter( |
| 68 | + (log) => |
| 69 | + !( |
| 70 | + log.status === SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS || |
| 71 | + log.status === COMMON_STATUS.FINALIZING |
| 72 | + ) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const burnSysxLog: ITransferLog = { |
| 77 | + payload: { |
| 78 | + data: { |
| 79 | + hash: receipt.transactionHash, |
| 80 | + }, |
| 81 | + message: "submit-proofs", |
| 82 | + previousStatus: SYS_TO_ETH_TRANSFER_STATUS.GENERATE_PROOFS, |
| 83 | + }, |
| 84 | + status: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS, |
| 85 | + date: Date.now(), |
| 86 | + }; |
| 87 | + |
| 88 | + transfer.logs.push(burnSysxLog); |
| 89 | + |
| 90 | + const confirmLog: ITransferLog = { |
| 91 | + status: COMMON_STATUS.FINALIZING, |
| 92 | + payload: { |
| 93 | + data: receipt, |
| 94 | + message: "Confirm NEVM Transaction", |
| 95 | + previousStatus: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS, |
| 96 | + }, |
| 97 | + date: Date.now(), |
| 98 | + }; |
| 99 | + |
| 100 | + transfer.logs.push(confirmLog); |
| 101 | + |
| 102 | + await transfer.save(); |
| 103 | + |
| 104 | + res.status(200).json({ success: true }); |
| 105 | +}; |
0 commit comments