Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Blockchain by (12.7k points)

If I have an ERC20 contract on my blockchain, and a user wants to buy tokens as in the following scenario:

User gives the number of tokens they want to buy

An info box will popup to show the user how many ether they will pay and the remaining balance

Once confirmed, a the amount of tokens in balance should be shown

To obtain tokens, I have to call a method in a smart contract which looks like this. obtainTok(BigInteger weiValue)

What is the formula to calculate the weiValue in this scenario using web3j?

EDIT Since there is no Javadocs for the library the parameters are not really clear for me as a beginner.

ethGetBalance(String address, DefaultBlockParameter defaultBlockParameter)

Does the address here represent the wallet address or user address?

1 Answer

0 votes
by (29.5k points)
edited by

 Hi,If you are attempting to do this in web3j, you would use org.web3j.utils.toWei(valueInEther, Unit.ETHER); where valueInEther is a String or BigDecimal.you wouldn't typically send ether to your smart contract as a parameter to obtainTok(). You would send it in as part of the transaction object and read it in your smart contract with msg.value

Hope this helps

Know how Blockchain works by enrolling in Blockchain Course.

Related questions

0 votes
1 answer
asked Jul 17, 2019 in Blockchain by Abhishek_31 (12.7k points)
0 votes
1 answer

Browse Categories

...