I am building a dApp where supermarkets send Ethereum tokens to customers as change after purchases. I want to make my frontend user-friendly by allowing users to input amounts in USD instead of ETH.
Here is my code for sending ETH to customers for my smart contract:
function sendMoneyToCustomer(address payable customer) public payable {
balances[customer] += msg.value;
customer.transfer(msg.value);
}
and this is code where I interact with the smart contract
async sendMoneyToCustomer(signer, customerAddress, amount) {
const contract = new ethers.Contract(this.contractAddress, this.abi, signer);
const ethValue = ethers.parseEther(amount);
const tx = await contract.sendMoneyToCustomer(customerAddress, { value: ethValue });
await tx.wait();
}
I would like guidance on how to:
-
Fetch the current ETH to USD exchange rate.
-
Convert a user-provided USD amount to ETH before sending it.
-
Display user balances in USD instead of ETH.
Any help or code snippets would be appreciated!