How to Automatically Transfer ERC20 Tokens From the Server

soliditytokensweb3.php

I deployed an ERC20 contract. I want to send tokens to other addresses without my manual sign.

How can I transfer tokens from my wallet automatically from the server? (via php for example)

Thanks.

Best Answer

You can sign a transaction using Node.js web server as this:

const contractAddress = '0x000000'; //Yor token contract address
const privateKey      = '';         //The private key of your contract Owner  
const toAddress       = '0x000000'; //The address to transfer the tokens    

//Creating Web3 Objct
const web3js = new web3(new web3.providers.HttpProvider("https://infura.node.url....."));
    

var contractABI = [];//Contract Token ABI

//creating Contract Object
var contract = new web3js.eth.Contract(contractABI,contractAddress, {from: ownerAddress} ); 

var data = contract.methods.transfer(toAddress, value).encodeABI(); //Create the data for token transaction.

var rawTransaction = {"to": contractAddress, "gas": 100000, "data": data }; 

web3js.eth.accounts.signTransaction(rawTransaction, privateKey)
    .then(signedTx => web3js.eth.sendSignedTransaction(signedTx.rawTransaction))
    //.then(function(receipt){ console.log("Transaction receipt: ", receipt); getETHBalanceOf();  })
    .then(req => { 
            /* The trx was done. Write your acctions here. For example getBalance */
            getTOKENBalanceOf(toAddress).then ( balance => { console.log(toAddress + " Token Balance: " + balance); });
            return true;  
    })      
    

//GET TOKEN BALANCE FUNCTION ////////////////////////////////
async function getTOKENBalanceOf(address){
    return await contract.methods.balanceOf(address).call();                        
}   
Related Topic