0 votes
1 view
in Blockchain by (12.2k 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 (28.6k points)

 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

Related questions

0 votes
1 answer
asked Jul 17, 2019 in Blockchain by Abhishek_31 (12.2k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !