Skip to content

Commit e9cecb1

Browse files
authored
Merge pull request #41 from syscoin/develop
Develop
2 parents 1884e7d + b96d6b1 commit e9cecb1

File tree

10 files changed

+300
-70
lines changed

10 files changed

+300
-70
lines changed

.github/workflows/ci-checks.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: CI Checks
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- develop
8+
pull_request:
9+
branches:
10+
- main
11+
- develop
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: ["16.x", "18.x"]
20+
21+
steps:
22+
- uses: actions/checkout@v4
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- name: Install Yarn
28+
run: npm install -g yarn
29+
- name: Install Dependencies
30+
run: yarn
31+
- name: Build Application
32+
run: yarn build
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { AddFreezeAndBurnLogRequestPayload } 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 {
8+
COMMON_STATUS,
9+
ETH_TO_SYS_TRANSFER_STATUS,
10+
ITransferLog,
11+
} from "@contexts/Transfer/types";
12+
import { validateTransactionReceipt } from "./validate-relay-contract-receipt";
13+
import { ERC20_MANAGER_CONTRACT_ADDRESS } from "@constants";
14+
15+
export const handleFreezeBurn = async (
16+
transferId: string,
17+
payload: AddFreezeAndBurnLogRequestPayload,
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+
try {
44+
const receipt = await validateTransactionReceipt(
45+
web3,
46+
txHash,
47+
ERC20_MANAGER_CONTRACT_ADDRESS
48+
);
49+
50+
if (clearAll) {
51+
transfer.logs = transfer.logs.filter(
52+
(log) =>
53+
!(
54+
log.status === ETH_TO_SYS_TRANSFER_STATUS.FREEZE_BURN_SYS ||
55+
log.status === ETH_TO_SYS_TRANSFER_STATUS.CONFIRM_FREEZE_BURN_SYS
56+
)
57+
);
58+
}
59+
60+
const freezeBurnLog: ITransferLog = {
61+
payload: {
62+
data: {
63+
hash: receipt.transactionHash,
64+
},
65+
message: "Freeze and Burn SYS",
66+
previousStatus: COMMON_STATUS.INITIALIZE,
67+
},
68+
status: ETH_TO_SYS_TRANSFER_STATUS.FREEZE_BURN_SYS,
69+
date: Date.now(),
70+
};
71+
72+
transfer.logs.push(freezeBurnLog);
73+
74+
const confirmLog: ITransferLog = {
75+
status: ETH_TO_SYS_TRANSFER_STATUS.CONFIRM_FREEZE_BURN_SYS,
76+
payload: {
77+
data: receipt,
78+
message: "Confirm NEVM Transaction",
79+
previousStatus: ETH_TO_SYS_TRANSFER_STATUS.FREEZE_BURN_SYS,
80+
},
81+
date: Date.now(),
82+
};
83+
84+
transfer.logs.push(confirmLog);
85+
86+
await transfer.save();
87+
88+
res.status(200).json({ success: true });
89+
} catch (e) {
90+
if (e instanceof Error) {
91+
return res.status(400).json({ message: e.message });
92+
}
93+
94+
return res.status(500).json({
95+
message: "Unknown error",
96+
});
97+
}
98+
};

api/services/admin-transfer/handle-submit-proofs.ts

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { NextApiRequest, NextApiResponse } from "next";
44
import { verifySignature } from "utils/api/verify-signature";
55
import TransferModel from "models/transfer";
66
import Web3 from "web3";
7-
import relayAbi from "@contexts/Transfer/relay-abi";
87
import { RELAY_CONTRACT_ADDRESS } from "@constants";
98
import {
109
COMMON_STATUS,
1110
ITransferLog,
1211
SYS_TO_ETH_TRANSFER_STATUS,
1312
} from "@contexts/Transfer/types";
13+
import { validateTransactionReceipt } from "./validate-relay-contract-receipt";
1414

1515
export const handleSubmitProofs = async (
1616
transferId: string,
@@ -40,66 +40,58 @@ export const handleSubmitProofs = async (
4040
return res.status(401).json({ message: "Unauthorized" });
4141
}
4242

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-
)
43+
try {
44+
const receipt = await validateTransactionReceipt(
45+
web3,
46+
txHash,
47+
RELAY_CONTRACT_ADDRESS
7348
);
74-
}
75-
76-
const burnSysxLog: ITransferLog = {
77-
payload: {
78-
data: {
79-
hash: receipt.transactionHash,
49+
if (clearAll) {
50+
transfer.logs = transfer.logs.filter(
51+
(log) =>
52+
!(
53+
log.status === SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS ||
54+
log.status === COMMON_STATUS.FINALIZING
55+
)
56+
);
57+
}
58+
59+
const submitProofsLogs: ITransferLog = {
60+
payload: {
61+
data: {
62+
hash: receipt.transactionHash,
63+
},
64+
message: "submit-proofs",
65+
previousStatus: SYS_TO_ETH_TRANSFER_STATUS.GENERATE_PROOFS,
8066
},
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);
67+
status: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS,
68+
date: Date.now(),
69+
};
70+
71+
transfer.logs.push(submitProofsLogs);
72+
73+
const confirmLog: ITransferLog = {
74+
status: COMMON_STATUS.FINALIZING,
75+
payload: {
76+
data: receipt,
77+
message: "Confirm NEVM Transaction",
78+
previousStatus: SYS_TO_ETH_TRANSFER_STATUS.SUBMIT_PROOFS,
79+
},
80+
date: Date.now(),
81+
};
8982

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-
};
83+
transfer.logs.push(confirmLog);
9984

100-
transfer.logs.push(confirmLog);
85+
await transfer.save();
10186

102-
await transfer.save();
87+
res.status(200).json({ success: true });
88+
} catch (e) {
89+
if (e instanceof Error) {
90+
return res.status(400).json({ message: e.message });
91+
}
10392

104-
res.status(200).json({ success: true });
93+
return res.status(500).json({
94+
message: "Unknown error",
95+
});
96+
}
10597
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import Web3 from "web3";
2+
3+
export const validateTransactionReceipt = async (
4+
web3: Web3,
5+
txHash: string,
6+
contractAddress: string
7+
) => {
8+
const receipt = await web3.eth.getTransactionReceipt(txHash);
9+
10+
if (!receipt) {
11+
throw new Error("Invalid transaction hash: Transaction not found");
12+
}
13+
14+
if (
15+
!receipt.to ||
16+
receipt.to.toLowerCase() !== contractAddress.toLowerCase()
17+
) {
18+
throw new Error(
19+
"Invalid transaction: To is not the expected contract address"
20+
);
21+
}
22+
23+
if (receipt.logs.length === 0) {
24+
throw new Error("Invalid transaction: No logs found");
25+
}
26+
return receipt;
27+
};

api/types/admin/transfer/add-log.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ export type AddBurnSysxLogRequestPayload = {
1919
operation: "burn-sysx";
2020
} & BaseUtxoTransaction;
2121

22+
export type AddMintSysxLogRequestPayload = {
23+
operation: "mint-sysx";
24+
} & BaseUtxoTransaction;
25+
2226
export type AddSubmitProofsLogRequestPayload = {
2327
operation: "submit-proofs";
2428
} & BaseEVMTransaction;
2529

30+
export type AddFreezeAndBurnLogRequestPayload = {
31+
operation: "freeze-burn-sys";
32+
} & BaseEVMTransaction;
33+
2634
export type AddUTXOLogRequestPayload =
2735
| AddBurnSysLogRequestPayload
28-
| AddBurnSysxLogRequestPayload;
36+
| AddBurnSysxLogRequestPayload
37+
| AddMintSysxLogRequestPayload;
2938

30-
export type AddNEVMLogRequestPayload = AddSubmitProofsLogRequestPayload;
39+
export type AddNEVMLogRequestPayload =
40+
| AddSubmitProofsLogRequestPayload
41+
| AddFreezeAndBurnLogRequestPayload;
3142

3243
export type AddLogRequestPayload =
3344
| AddUTXOLogRequestPayload

0 commit comments

Comments
 (0)