-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
387 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { AddSubmitProofsLogRequestPayload } from "api/types/admin/transfer/add-log"; | ||
import dbConnect from "lib/mongodb"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { verifySignature } from "utils/api/verify-signature"; | ||
import TransferModel from "models/transfer"; | ||
import Web3 from "web3"; | ||
import relayAbi from "@contexts/Transfer/relay-abi"; | ||
import { RELAY_CONTRACT_ADDRESS } from "@constants"; | ||
import { | ||
COMMON_STATUS, | ||
ITransferLog, | ||
SYS_TO_ETH_TRANSFER_STATUS, | ||
} from "@contexts/Transfer/types"; | ||
|
||
export const handleSubmitProofs = async ( | ||
transferId: string, | ||
payload: AddSubmitProofsLogRequestPayload, | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) => { | ||
await dbConnect(); | ||
const web3 = new Web3("https://rpc.syscoin.org"); | ||
|
||
const { address } = req.session.user!; | ||
|
||
const transfer = await TransferModel.findOne({ id: transferId }); | ||
if (!transfer) { | ||
return res.status(404).json({ message: "Transfer not found" }); | ||
} | ||
|
||
const { clearAll, signedMessage, txHash, operation } = payload; | ||
|
||
const data = { | ||
operation, | ||
txHash, | ||
clearAll, | ||
}; | ||
const message = JSON.stringify(data); | ||
if (!verifySignature(message, signedMessage, address)) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
|
||
const receipt = await web3.eth.getTransactionReceipt(txHash); | ||
|
||
if (!receipt) { | ||
return res.status(400).json({ | ||
message: "Invalid transaction hash: Transaction not found", | ||
}); | ||
} | ||
|
||
if ( | ||
!receipt.to || | ||
receipt.to.toLowerCase() !== RELAY_CONTRACT_ADDRESS.toLowerCase() | ||
) { | ||
return res.status(400).json({ | ||
message: "Invalid transaction: To is not the relay contract address", | ||
}); | ||
} | ||
|
||
if (receipt.logs.length === 0) { | ||
return res.status(400).json({ | ||
message: "Invalid transaction: No logs found", | ||
}); | ||
} | ||
|
||
if (clearAll) { | ||
transfer.logs = transfer.logs.filter( | ||
(log) => | ||
!( | ||
log.status === SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS || | ||
log.status === COMMON_STATUS.FINALIZING | ||
) | ||
); | ||
} | ||
|
||
const burnSysxLog: ITransferLog = { | ||
payload: { | ||
data: { | ||
hash: receipt.transactionHash, | ||
}, | ||
message: "submit-proofs", | ||
previousStatus: SYS_TO_ETH_TRANSFER_STATUS.GENERATE_PROOFS, | ||
}, | ||
status: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS, | ||
date: Date.now(), | ||
}; | ||
|
||
transfer.logs.push(burnSysxLog); | ||
|
||
const confirmLog: ITransferLog = { | ||
status: COMMON_STATUS.FINALIZING, | ||
payload: { | ||
data: receipt, | ||
message: "Confirm NEVM Transaction", | ||
previousStatus: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS, | ||
}, | ||
date: Date.now(), | ||
}; | ||
|
||
transfer.logs.push(confirmLog); | ||
|
||
await transfer.save(); | ||
|
||
res.status(200).json({ success: true }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
components/Admin/Transfer/AddLogModals/AddNEVMTransactionModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import { | ||
Box, | ||
Button, | ||
FormControlLabel, | ||
TextField, | ||
Typography, | ||
Checkbox, | ||
Alert, | ||
} from "@mui/material"; | ||
import AddLogModalContainer from "./ModalContainer"; | ||
import { useForm } from "react-hook-form"; | ||
import { useUtxoTransaction } from "components/Bridge/v3/hooks/useUtxoTransaction"; | ||
import { useNEVM } from "@contexts/ConnectedWallet/NEVMProvider"; | ||
import { | ||
AddNEVMLogRequestPayload, | ||
AddUTXOLogRequestPayload, | ||
} from "api/types/admin/transfer/add-log"; | ||
import { useState } from "react"; | ||
import { useNevmTransaction } from "components/Bridge/v3/hooks/useNevmTransaction"; | ||
|
||
type Props = { | ||
onClose: (refetch?: boolean) => void; | ||
transferId: string; | ||
operation: AddNEVMLogRequestPayload["operation"]; | ||
}; | ||
|
||
type FormValues = { | ||
txHash: string; | ||
clearAll: boolean; | ||
}; | ||
|
||
const AddNEVMTransactionModal: React.FC<Props> = ({ | ||
onClose, | ||
transferId, | ||
operation, | ||
}) => { | ||
const { handleSubmit, register, watch } = useForm<FormValues>({ | ||
defaultValues: { | ||
txHash: "", | ||
clearAll: true, | ||
}, | ||
}); | ||
|
||
const [submitError, setSubmitError] = useState<string>(); | ||
|
||
const { signMessage } = useNEVM(); | ||
|
||
const txHash = watch("txHash"); | ||
|
||
const { isFetching, isFetched, isSuccess, data, isError } = | ||
useNevmTransaction(txHash, { refetch: false }); | ||
|
||
const isValidTx = isFetched; | ||
|
||
let helperText = | ||
isFetched && !isValidTx ? `Not a valid ${operation} transaction` : ""; | ||
|
||
if (isError) { | ||
helperText = "Error fetching transaction"; | ||
} | ||
|
||
const onSubmit = (values: FormValues) => { | ||
const data = { | ||
operation, | ||
...values, | ||
}; | ||
const message = `0x${Buffer.from(JSON.stringify(data), "utf8").toString( | ||
"hex" | ||
)}`; | ||
signMessage(message) | ||
.then((signedMessage) => { | ||
const payload: AddNEVMLogRequestPayload = { | ||
operation, | ||
clearAll: values.clearAll, | ||
txHash: values.txHash, | ||
signedMessage, | ||
}; | ||
setSubmitError(undefined); | ||
return fetch(`/api/admin/transfer/${transferId}/add-log`, { | ||
body: JSON.stringify(payload), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
method: "POST", | ||
}); | ||
}) | ||
.then((res) => { | ||
if (res.ok) { | ||
onClose(true); | ||
return; | ||
} | ||
return res.json().then(({ message }) => setSubmitError(message)); | ||
}); | ||
}; | ||
|
||
return ( | ||
<AddLogModalContainer component="form" onSubmit={handleSubmit(onSubmit)}> | ||
<Typography variant="body1" sx={{ textTransform: "capitalize" }}> | ||
Add {operation.split("_")} Transaction Log | ||
</Typography> | ||
<Box sx={{ my: 2 }} width="100%"> | ||
<TextField | ||
label="Transaction Hash" | ||
fullWidth | ||
sx={{ mb: 2 }} | ||
{...register("txHash")} | ||
error={Boolean(helperText)} | ||
helperText={helperText} | ||
/> | ||
<FormControlLabel | ||
control={<Checkbox defaultChecked {...register("clearAll")} />} | ||
label={`Clear all other ${operation} logs`} | ||
/> | ||
</Box> | ||
{submitError && <Alert severity="error">{submitError}</Alert>} | ||
<Box display="flex"> | ||
<Button color="secondary" onClick={() => onClose()}> | ||
Cancel | ||
</Button> | ||
<Button | ||
variant="contained" | ||
sx={{ ml: "auto" }} | ||
type="submit" | ||
disabled={!(isSuccess && isValidTx) || isFetching} | ||
> | ||
{isFetching ? "Checking" : "Add"} | ||
</Button> | ||
</Box> | ||
</AddLogModalContainer> | ||
); | ||
}; | ||
|
||
export default AddNEVMTransactionModal; |
21 changes: 21 additions & 0 deletions
21
components/Admin/Transfer/AddLogModals/AddSubmitProofsTransaction.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import AddNEVMTransactionModal from "./AddNEVMTransactionModal"; | ||
|
||
type Props = { | ||
onClose: (refetch?: boolean) => void; | ||
transferId: string; | ||
}; | ||
|
||
const AddSubmitProofsTransaction: React.FC<Props> = ({ | ||
onClose, | ||
transferId, | ||
}) => { | ||
return ( | ||
<AddNEVMTransactionModal | ||
onClose={onClose} | ||
operation="submit-proofs" | ||
transferId={transferId} | ||
/> | ||
); | ||
}; | ||
|
||
export default AddSubmitProofsTransaction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.