ERC-20 Transfer – Simple Transfer from Address to Address on Web Page

erc-20

This is questions:

ETC…..

No single answer show who to do it [not just here].
I just looking for most simple way to make transfer from one address to another on web page.

I need frontend part. Is it possible to do it?

I have clone of USDT on testnet.
I Use metamask also web3.

I understand "The transferFrom method will only work if the owner (the _from address) has allowed the receiver ( _to address) to withdraw the funds, this is done by calling function approve(address _spender, uint256 _value) ."

Best Answer

You will first need to establish connect your wallet to the webpage (and the library). I like to use Ethers as the web3 client library.

const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
// Prompt user for account connections
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
console.log("Account:", await signer.getAddress());

After this, you need to create the interface for your contract. For this, you'll need two things, the address and the ABI

The address is the deployed address of the ERC20 token. Using the USDC token address here, and specifying the amount of decimals in the token.

const CONTRACT_ADDRESS = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const DECIMALS = 6

The full ABI of a contract is created when building from Solidity. If you are lucky, you can get it from Etherscan.

Luckily, ERC20 contracts are standardized, so you can use a general ERC20 abi, or just provide the ABI of the single method you are using like this:

const abi = ["function transfer(address to, uint amount)"];

Now you can initiate the contract and call the Transfer method.

const erc20 = new ethers.Contract(CONTRACT_ADDRESS, abi, provider);

const transferToAddress = "0x..."
const amount = ethers.utils.parseUnits("1", DECIMALS);

await erc20.transfer(transferToAddress, amount)

Here is a working codesandbox

codesandbox screenshot

Related Topic