I'm trying to write a smart contract that takes INR as user input and converts it to equivalent eth ( wei ). I looked into chainlink datafeeds, but no luck there. Need advice on how to implement this. Thanks.
1 Answer
Firstly , you need to have a source from which you can get the exchange rates there are 2 ways for it :
- External API
- Data Feed services
Next , If you have the exchange rate, you can write the conversion function in Solidity.
solidity code :
uint256 convertedEthAmount = inrAmount * exchangeRate / 1e18;
return convertedEthAmount * 1e18; // convert to wei
Lastly , in frontend you have to follow few steps to get these values ,
- Import the ABI
- Create web3 instance :
const contract = new web3.eth.Contract(ABI, contractAddress);
- Call the solidity function :
contract.methods.convert_Inr_to_Eth(inr_Amount).call()