0

I would like to send custom ERC-20 token with Ethereum in one transaction. I know how to send both of them alone...
How to do it it one transaction?

SEND CUSTOM ERC-20 TOKEN (There USDC):

<script src="https://cdn.ethers.io/lib/ethers-5.6.umd.min.js" type="text/javascript">
</script>

<script>

const connectAndTransfer = async () => { 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());

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

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

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

const transferToAddress = "0x728713b41fcec5c071359ecf2802fa0b62bec5a8";

const amount = ethers.utils.parseUnits("1", DECIMALS); await erc20.transfer(transferToAddress, amount); };

connectAndTransfer(); </script>


And this code will send only Eth:
const ethereumButton = document.querySelector('.enableEthereumButton');
const sendEthButton = document.querySelector('.sendEthButton');

let accounts = [];

//Sending Ethereum to an address sendEthButton.addEventListener('click', () => { 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); });

ethereumButton.addEventListener('click', () => { getAccount(); });

async function getAccount() { accounts = await ethereum.request({ method: 'eth_requestAccounts' }); }

1 Answers1

2

You may want to consider making your ERC20 also an ERC1363. https://eips.ethereum.org/EIPS/eip-1363

There is a function called transferAndCall that can send and execute in one transaction.

p0pps
  • 298
  • 1
  • 10