The contract specifies that it requires Solidity version 0.8.0 and imports the AggregatorV3Interface from the Chainlink contracts library.
The contract is named "TokenSwap" and includes several state variables and mappings:
A uint variable representing the conversion rate from ETH to a fictional token called nUSD.
A uint variable representing the conversion rate from nUSD to ETH.
A uint variable representing the total supply of nUSD tokens.
A mapping that stores the balances of nUSD tokens for each address.
The contract includes an internal instance of the AggregatorV3Interface from the Chainlink library. It is used to obtain the ETH/USD price feed.
The contract defines two events:
Triggered when a user makes a deposit, indicating the amount of ETH and nUSD tokens received.
Triggered when a user redeems nUSD tokens for ETH, indicating the amount of nUSD tokens redeemed and the corresponding ETH amount received.
The constructor sets the initial values for the contract, including the owner, conversion rates, total supply, and the address of the Chainlink ETH/USD price feed contract.
The deposit function allows users to deposit ETH and receive nUSD tokens based on the current ETH/USD exchange rate. The ETH amount is converted to nUSD based on the ethToNusdRate, and the user's balance and the total supply are updated accordingly. The function emits a Deposit event.
The redeem function allows users to redeem a specified amount of nUSD tokens for ETH. The nUSD amount is converted to ETH based on the nusdToEthRate, and the user's balance and the total supply are updated accordingly. The function emits a Redeem event.
The getETHUSDPrice function retrieves the latest ETH/USD price from the Chainlink price feed contract.
The getBalance function returns the nUSD token balance of a given address.
The getTotalSupply function returns the total supply of nUSD tokens
I have used ReactJS for the frontend.There is two one text field and two buttons.
This button is used to depositing the ETH.
This button is used to Reedem the nUSD from the balance
I have Sepolia testnet for this project.
Note : Make sure that you have NodeJS envinornment and Sepolia testnet.
Here you can get test Sepolia eth : https://sepoliafaucet.com/
Run the command "npm start" in project directory.