Reality Check Please - ethers Send USDT

Nice to be around the brainiacs!

Total noob here, strong background in php and trying to add some script code to automate paying out Commissions in USDT (ERC20).

I picked up this code snipped for ethers.js and was hoping someone could give it a quick check. Not sure about the provider format and whether to use JsonRpcProvider or InfuraProvider and if I should be using Mainnet for network. All assistance and suggestions greatly appreciated and once working will create a quick github repo to share.

function commpay(pk, pd, pa) {
const pkey = pk;
const radd = pd;
const amt = pa;

const ethers = require(‘ethers’);

// Set up provider (e.g., Infura or Alchemy)
const provider = new ethers.providers.JsonRpcProvider(‘https://mainnet.infura.io/v3/my_api_key’);
//const provider = new ethers.providers.InfuraProvider(network = “mainnet”, MY_API_KEY)
// Set up the sender’s wallet
const wallet = new ethers.Wallet(pkey, provider);

// USDT contract address and ABI (for ERC-20 tokens)
const USDT_CONTRACT_ADDRESS = ‘0xAB0874D3e7Cd256Cd3F1A9480c3b0C01109E2117’;
const ERC20_ABI = [
“function transfer(address to, uint256 amount) public returns (bool)”
];

// Create contract instance
const usdtContract = new ethers.Contract(USDT_CONTRACT_ADDRESS, ERC20_ABI, wallet);

// Send USDT
async function sendUSDT(toAddress, amount) {
const tx = await usdtContract.transfer(toAddress, ethers.utils.parseUnits(amount, 6)); // USDT has 6 decimals
await tx.wait(); // Wait for the transaction to be confirmed
//console.log(‘Transaction successful:’, tx.hash);
alert('Tx successful: ’ tx.hash);
}

// Call the function to send USDT
sendUSDT(radd, amt); // Send 10 USDT

}

Hey, it depends on the ethers version there are some changes between v5 and v6, see Documentation
Check this working example done with v5 Ethers.js: How to send ERC-20 Tokens

1 Like