diff --git a/.gitignore b/.gitignore index bcf1851..36420af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules -src/config.json \ No newline at end of file +config.json \ No newline at end of file diff --git a/app.js b/app.js index 340f3b3..9c8b4ae 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,8 @@ const ETH = require("./src/chain/eth") const eth = new ETH() const app = express() +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); const port = process.argv[2] || 12345 app.get("/", (req, res) =>{ @@ -15,7 +17,7 @@ app.post("/checkPayment/eth", async(req, res) =>{ let result switch (coin) { case "eth": - // 校验eth + result = await eth.checkETHPaymentStatus(address, amount, ts) break case "usdt": result = await eth.checkUSDTPaymentStatus(address, amount, ts) diff --git a/config.json b/config.json deleted file mode 100644 index d0908b8..0000000 --- a/config.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "nodesUrl": { - "ETH": { - "url":"https://api.etherscan.io/v2/api", - "key":"2KKTKZ8MSG6DNUPQ4K9JVWPQ4H33DSUQA3" - } - }, - "abiPath": { - "usdt_erc20_abi": "./src/abi/erc20usdt.json", - "usdc_erc20_abi": "./src/abi/erc20usdc.json", - "usdt_trc20_abi": "./src/abi/trc20usdt.json" - } -} \ No newline at end of file diff --git a/src/chain/eth.js b/src/chain/eth.js index 40f34cf..ffc0054 100644 --- a/src/chain/eth.js +++ b/src/chain/eth.js @@ -79,6 +79,49 @@ class ETH { // return result; // } + async checkETHPaymentStatus(address, amount, ts) { + const url = `${app.ethNodeUrl} + ?chainid=1 + &module=account + &action=txlist + &address=${address} + &startblock=0 + &endblock=99999999 + &page=1 + &offset=100 + &sort=desc + &apikey=${this.#key}` + try { + const response = await axios.get(url) + const transactions = response.data.result + if (!Array.isArray(transactions) || transactions.length === 0) { + return { code: -1, result: false, data: [] }; + } + + // 筛选时间戳 >= ts 的交易 + const afterTimestampTxs = transactions.filter(tx => parseInt(tx.timeStamp) >= ts); + + if (afterTimestampTxs.length === 0) { + return { code: -1, result: false, data: [] }; + } + + // 筛选出金额等于指定值的交易(ETH 最小单位是 18 位) + const matchedTxs = afterTimestampTxs.filter(tx => parseFloat(tx.value) / 10 ** 18 === amount && address === tx.to); + if (matchedTxs.length === 0) { + return { code: -1, result: false, data: [] }; + } + + return { + code: 0, + result: true, + data: matchedTxs.length === 1 ? matchedTxs[0] : matchedTxs, + }; + } catch (error){ + console.error("Error fetching transactions:", error); + return {code:-1, result: false, data:[]}; + } + } + async checkUSDTPaymentStatus(address, amount, ts){ const url = `${app.ethNodeUrl} ?chainid=1 @@ -108,7 +151,7 @@ class ETH { } // 筛选出金额等于指定值的交易(USDT 最小单位是 6 位) - const matchedTxs = afterTimestampTxs.filter(tx => parseFloat(tx.value) / 1_000_000 === amount && tx.functionName === "transfer(address _to, uint256 _value)"); + const matchedTxs = afterTimestampTxs.filter(tx => parseFloat(tx.value) / 1_000_000 === amount && tx.functionName === "transfer(address _to, uint256 _value)" && address === tx.to); if (matchedTxs.length === 0) { return { code: -1, result: false, data: [] }; @@ -154,7 +197,7 @@ class ETH { } // 筛选出金额等于指定值的交易(USDT 最小单位是 6 位) - const matchedTxs = afterTimestampTxs.filter(tx => parseFloat(tx.value) / 1_000_000 === amount && tx.functionName === "transfer(address _to, uint256 _value)"); + const matchedTxs = afterTimestampTxs.filter(tx => parseFloat(tx.value) / 1_000_000 === amount && tx.functionName === "transfer(address _to, uint256 _value)" && address === tx.to); if (matchedTxs.length === 0) { return { code: -1, result: false, data: [] };