I'm working on a project where users approve USDT to the admin wallet address to allow spending on their behalf. In the front end, I've implemented the approval process using a USDT token contract code obtained from Etherscan. You can find it here: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7#code Now, in the backend, I need to fetch the approved USDT amount from the user to the admin wallet. Can anyone guide how to achieve this using the existing contract code? This is my current progress. The 'approve' method is working fine. However, I can't seem to fetch the USDT amount that the user approved from the backend. The transferFrom method seems to transfer from the admin wallet to the admin wallet instead of from the user wallet to the admin wallet.
Frontend
import { Web3 } from 'web3'
import { tokenABI } from '@/contracts/tokenABI'
const tokenAddress = import.meta.env.VITE_TOKEN_CONTRACT_ADDRESS
const web3 = new Web3(window.ethereum)
const tokenContract = new web3.eth.Contract(tokenABI, tokenAddress)
const approveUSDT = async () => {
try {
const approvalAmount = web3.utils.toWei('50', 'ether')
const gasPrice = web3.utils.toWei('5', 'gwei')
const gasLimit = 60000
await tokenContract.methods.approve(adminAddress, approvalAmount).send({
from: userAddress,
gasPrice: gasPrice,
gasLimit: gasLimit
})
} catch (error) {
console.error('Error:', error)
}
}
Backend
const fetchUSDT = async () => {
try {
let transferAmount = await tokenContract.methods.allowance(userAddress, adminAddress).call()
transferAmount = Number(transferAmount.toString())
await tokenContract.methods.transferFrom(userAddress, adminAddress, transferAmount).send({
from: adminAddress
})
} catch (error) {
console.error('Error:', error)
}
}