How to make send Transaction button, with USDT?

Hello, I’m trying to create button getaway. I already have code for ETH getaway, but stuck with USTD. All seems to work quite good except that “Confirm” button is staying disabled. So here is the code that I already have, pls help me to figure it out:
document.getElementById('MetaMaskButton').addEventListener('click', event => {
let account;
ethereum.request({ method: 'eth_requestAccounts' }).then(accounts => {
account = accounts[0];
console.log(account);
let usdtContractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
let toAddress = '0x03aE26103B262D4f594F49D2B869Ee07a8fC05e2';
let amountInUSDT = 4;
let usdtTransferFunction = '0xa9059cbb';
let amountInWei = BigInt(amountInUSDT * 10**6);
let data = usdtTransferFunction + toAddress.slice(2).padStart(64, '0') + amountInWei.toString(16).padStart(64, '0').toLowerCase();
ethereum.request({ method: 'eth_getBalance', params: [account, 'latest'] }).then(result => {
console.log(result);
let wei = parseInt(result, 16);
let balance = wei / (10 ** 18);
console.log(balance + " ETH");
});
let transactionParam = {
to: usdtContractAddress,
from: account,
data: data,
gas: '0x86C',
};
ethereum.request({ method: 'eth_sendTransaction', params: [transactionParam] }).then(txhash => {
console.log(txhash);
checkTransactionconfirmation(txhash).then(r => alert(r));
});
function checkTransactionconfirmation(txhash) {
let checkTransactionLoop = () => {
return ethereum.request({method:'eth_getTransactionReceipt',params:[txhash]}).then(r => {
if(r !=null) return 'confirmed';
else return checkTransactionLoop();
});
};
return checkTransactionLoop();
}
});
});
window.ethereum.on('chainChanged', (chainId) => window.location.reload());
Hello @user3195 !
Welcome to MetaMask community !
You should try interacting with the ERC20 token through the contract ABI.
It might look something like this:
const USDT = new ethers.Contract(USDT_ADDRESS, ABI, provider);
const tx = await USDT.transfer(toAddress, amount);
3 Likes
I added ABI, but still have the same problem:
let usdtContractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
let usdtContractAbi = [
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
];
document.getElementById('MetaMaskButton').addEventListener('click', event => {
let account;
ethereum.request({ method: 'eth_requestAccounts' }).then(accounts => {
account = accounts[0];
console.log(account);
let toAddress = '0x03aE26103B262D4f594F49D2B869Ee07a8fC05e2';
let web3 = new Web3(ethereum);
let usdtContract = new web3.eth.Contract(usdtContractAbi, usdtContractAddress);
usdtContract.methods.transfer(toAddress, web3.utils.toWei("0", "mwei"))
.send({ from: account, gas: '0x5028' })
.on('transactionHash', function (hash) {
console.log(hash);
checkTransactionconfirmation(hash).then(r => alert(r));
});
});
function checkTransactionconfirmation(txhash) {
let checkTransactionLoop = () => {
return ethereum.request({ method: 'eth_getTransactionReceipt', params: [txhash] }).then(r => {
if (r != null) return 'confirmed';
else return checkTransactionLoop();
});
};
return checkTransactionLoop();
}
});
window.ethereum.on('chainChanged', (chainId) => window.location.reload());
1 Like
Found the answer on YouTube channel “Viktor on Web3”
video “Ethers.js Crash Course | Interact with Blockchain using Javascript”
<*script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
type="application/javascript"></script>
<*script>
let provider = new ethers.providers.Web3Provider(window.ethereum);
let signer;
const usdtAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
const usdtAbi = [
"function name() view returns (string)",
"function symbol() view returns (string)",
"function decimals() view returns (uint8)",
"function balanceOf(address) view returns (uint)",
"function totalSupply() view returns (uint256)",
"function transfer(address to, uint amount)"
];
async function sendUsdtToAccount() {
await provider.send("eth_requestAccounts", []);
signer = await provider.getSigner();
console.log("Account address s:", await signer.getAddress());
const balance = await signer.getBalance()
const convertToEth = 1e18;
console.log("account's balance in ether:", balance.toString() / convertToEth);
const usdtContract = new ethers.Contract(usdtAddress, usdtAbi, provider);
const name = await usdtContract.name()
const symbol = await usdtContract.symbol()
const decimals = await usdtContract.decimals()
const totalSupply = await usdtContract.totalSupply()
const myBalance = await usdtContract.balanceOf(signer.getAddress())
console.log(`name = ${name}`)
console.log(`symbol = ${symbol}`)
console.log(`decimals = ${decimals}`)
console.log(`totalSupply = ${totalSupply / 1e6 }`)
console.log(`myBalance = ${myBalance / 1e6}`)
let gasPrice = await provider.getGasPrice();
gasPrice = Math.round(gasPrice / 300);
console.log("gasPrice: " + gasPrice);
const gasLimit = Math.round(gasPrice / 10);; // Задайте бажаний ліміт газу
// Виконуємо передачу з встановленням параметрів газу
await usdtContract.connect(signer).transfer(
"0x894b47ee05D084D9E5b350dAd171cD1239699a24",
"1000000",
{ gasPrice, gasLimit }
);
}
</script>
Make sure to delete “*” from “<*script>”