javascriptphpsoliditysmartcontractscryptocurrency

How to automate USDT sending transactions?


I want to make utility on my website so people who don't like product can get their money back in usdt. By clicking on submit button I want to send usdt (TRC or ERC) back to customer. Is it even possible. I think its possible with Solana blockchain. There is autoapprove in phantom wallet for example. But I need in USDT

Customer will type his address in Post form and it will automatically send usdt to him


Solution

  • You can pass the private key of the sender address to web3.js or any other wrapper (ethersjs for JS, web3.php for PHP, ...) of JSON-RPC API of an Ethereum node. You can run your own node but more common scenario is using a 3rd party node provider such as Infura.

    The web3.js instance builds the transaction, signs it with the private key, and sends to the node to broadcast to the rest of the network (Ethereum, Tron, or other depending on which network is the node connected to).

    Example using web3js:

    const Web3 = require("web3");
    const web3 = new Web3(NODE_URL);
    
    const USDTAddress = "0xdAC17F958D2ee523a2206206994597C13D831ec7";
    
    // just the `transfer()` function is sufficient in this case
    const ERC20_ABI = [
        {"constant":true,"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},
        
    ];
    
    // you're passing the private key just to the local web3js instance
    // it's not broadcasted to the node
    web3.eth.accounts.wallet.add(PRIVATE_KEY_OF_SENDER_ADDRESS);
    
    async function run() {
        const tokenContract = new Web3.eth.Contract(USDTAddress, ERC20_ABI);
    
        const to = "0x123";
        const amount = "1000000"; // don't forget to account for the decimals
    
        // invoking the `transfer()` function of the contract
        const transaction = await tokenContract.methods.transfer(to, amount).send({from: SENDER_ADDRESS});
    }
    
    run();