[SOLVED] How To Transfer Token Via Backend Side (nodejs)

hi, is there any way to transfer USDT token from backend to users?

Hi @syamsoul

You can use libraries like ether.js or web3.js.
To make smart contract token transfer on backend you will need to use a rpc node to send a transaction to blockchain and also use a private key of a wallet to sign the transaction and paying the gas fees.

sorry @johnversus … i don’t understand… is Moralis also RPC node?

No, Moralis does not have any RPC nodes. You will have to get node url from other node providers.

thank you for giving me hints…

now i managed to transfer USDT token via backend (using web3.js library)…

this is my code… hope will helps others too…

    import Web3 from 'web3'

    const providerUrl = "<provider url>";
    const contractAddress = "<contract address>";
    const jsonInterface = "<ABI>";

    const web3 = new Web3(providerUrl);
    const contract = new web3.eth.Contract(jsonInterface , contractAddress);

    const privateKey = "<private key of sender's wallet>";
    const senderAddress = "<sender's wallet address>";
    const receiverAddress = "<receiver's wallet address>";

    let amount = 4.5;
    let value = web3.utils.toWei(amount, 'ether');

    const query = contract.methods.transfer(receiverAddress, value);
    const encodedABI = query.encodeABI();

    let signedTxn = await web3.eth.accounts.signTransaction({
        nonce: await web3.eth.getTransactionCount(senderAddress),
        to: contractAddress,
        data: encodedABI,
        gasPrice: await web3.eth.getGasPrice(),
        gas: 2000000,
    }, privateKey);

    web3.eth.sendSignedTransaction(signedTxn.rawTransaction).then((receipt) => {
        console.log(receipt);
    })
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.