How to send another token in MetaMask using eth_sendTransaction

Viewed 583

I have this sample code:

ethereum
.request({
  method: 'eth_sendTransaction',
  params: [
    {
      from: accounts[0],
      to: '0x2f318C334780961FB129D2a6c30D0763d9a5C970',
      value: '0x29a2241af62c0000',
      gasPrice: '0x09184e72a000',
      gas: '0x2710',
    },
  ],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error);

I have BSC addresses, so when I try to make transaction, I can send only BNB. How can I transfer another coin?

1 Answers

In order to send coins rather than BNB, you have to find a contract address that has "Transfer" Function to transfer the coin.

For example, if you want to transfer USDC on BSC. You can use web3.js or ethers.js to connect "0x0a385f86059e0b2a048171d78afd1f38558121f3" (this address) and use the Transfer function to USDC.

const tempProvider = new ethers.providers.Web3Provider(window.ethereum);
const tempSigner = tempProvider.getSigner();
const tempContract = new ethers.Contract("0x0a385f86059e0b2a048171d78afd1f38558121f3", abi, tempSigner);

tempContract.transfer(to, amount);

something like this

Related