mail@pastecode.io avatar
2 months ago
2.6 kB
const express = require('express');
const bodyParser = require('body-parser');
const ethers = require('ethers');

const app = express();


// Start with $USDT transaction
// Replace with your MetaMask private key and RPC URL
const privateKey = '[private-key]';

const rpcUrl = '[rpc];

// Create a provider with your RPC URL
const provider = new ethers.JsonRpcProvider(rpcUrl);

// Create a wallet using your private key
const wallet = new ethers.Wallet(privateKey, provider);

// Define an endpoint to handle webhook data
app.post('/webhook', async (req, res) => {
  // Retrieve JSON data from the request body
  const jsonData = req.body;

  // Ensure that the request body contains valid JSON
  if (!jsonData) {
    res.status(400).json({ error: 'Invalid JSON data' });

  // Access individual fields from the JSON data
  const recipientAddress = jsonData.recipientAddress;
  const usdtAmount = jsonData.usdtAmount;

  try {
    // Check the wallet balance using the provider
    const balance = await provider.getBalance(wallet.address);

    // Format the balance using ethers.utils
    const formattedBalance = ethers.formatEther(balance);
    console.log(`Balance: ${formattedBalance} MATIC`);

    // Check if the wallet has sufficient funds
    const usdtAmountWei = ethers.parseUnits(usdtAmount, 6);

    // Create a transaction
    const tx = {
      to: recipientAddress,
      value: usdtAmountWei,

    // Sign and send the transaction
    const txResponse = await wallet.sendTransaction(tx);
    console.log(`Transaction hash: ${txResponse.hash}`);


  // Assuming you have a transaction ID/hash, create the transaction link
    const transactionId = 'your_transaction_id_or_hash'; // Replace this with the actual transaction ID or hash
    const blockchainBaseUrl = 'https://blockchain-explorer.com/tx/'; // Replace this with the base URL of your blockchain explorer

    // Transaction link
    const transactionLink = `${blockchainBaseUrl}${transactionId}`;

    // If everything is successful, send a success response with the transaction link
      message: 'Webhook data received and USDT transaction sent successfully.',
      transactionLink: transactionLink
  } catch (error) {
    // Log the detailed error message including the stack trace
    console.error('Error:', error);

    // Respond with an error message containing the error details
    res.status(500).json({ error: 'Internal server error', message: error.message });

module.exports = {app};