0

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) ."

1
  • Please, can you add more details about your question? Or can you explain better the problem?
    – Kerry99
    Mar 31 at 15:54

3 Answers 3

1

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

1
  • Sorry for crying, it works nice. Apr 7 at 13:07
1

If you want to transfer ERC20 from one wallet to another you can just use the "transfer" method, however the sender must have the ERC20 in the wallet. An example of this in javascript you can use:

let contract = new web3.eth.Contract("CONTRACT_ABI HERE", "ADRESSS HERE");
contract.methods
.transfer("USER RECEIVE ADDRESS", web3.utils.toWei("1000"))
.send({
  from: USER_SENDER,
  gas: 200000
}).on("receipt", async (response) => {
   console.log(response);
})

I hope it could have helped.

1
  • I will try it ! Looks nice Apr 1 at 13:40
0

you don't have to approve here. Why don't you just initiate a transaction with Web3.js in your frontend code. web3.eth.sendTransaction({from : "senderAddress", to : "ERC20TokenAddress", data :"abiEncodedTransferData"})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.