const express = require("express");
const bodyParser = require("body-parser");
const { Web3 } = require("web3");
const { contract_abi, contract_address } = require("./contract.js");
const app = express();
app.use(bodyParser.json());
const privateKey = 'myownprivate';
const rpcUrl = 'myownrpc';
console.log("privateKey is:", privateKey);
console.log("rpcUrl is:", rpcUrl);
const web3 = new Web3(rpcUrl);
// Replace with the USDT contract address and ABI
const usdtContractAddress = contract_address;
const usdtContractABI = contract_abi;
app.get("/", async (req, res) => {
res.send("okay......");
});
app.post("/webhook", async (req, res) => {
const jsonData = req.body;
if (!jsonData) {
res.status(400).json({ error: "Invalid JSON data" });
return;
}
const recipientAddress = jsonData.recipientAddress;
const usdtAmount = jsonData.usdtAmount;
try {
const usdtContract = new web3.eth.Contract(
usdtContractABI,
usdtContractAddress
);
const usdtAmountWei = web3.utils.toWei(usdtAmount.toString(), "ether");
const gasPriceWei = web3.utils.toWei("20", "gwei"); // Example gas price (20 Gwei)
const txObject = {
from: web3.eth.accounts.privateKeyToAccount(privateKey).address,
to: recipientAddress,
value: "0x0", // Set the value to 0 if you're not sending Ether
data: usdtContract.methods
.transfer(recipientAddress, usdtAmountWei)
.encodeABI(),
gasPrice: gasPriceWei, // Set the gas price
};
const signedTx = await web3.eth.accounts.signTransaction(
txObject,
privateKey
);
const txReceipt = await web3.eth.sendSignedTransaction(
signedTx.rawTransaction
);
console.log("txReceipt is:", txReceipt);
const transactionLink = `https://polygonscan.com/tx/${txReceipt.transactionHash}`; // Link to the transaction on PolygonScan
console.log("transactionLink is:", transactionLink);
res.status(200).json({
message: "Webhook data received and USDT transaction sent successfully.",
transactionLink: transactionLink,
});
} catch (error) {
console.error("Error:", error);
res
.status(500)
.json({ error: "Internal server error", message: error.message });
}
});
module.exports = { app };