0

Is there any function in go-ethereum, which can convert values from ether to wei and vice-versa?

I should notice, that I'm not using geth as standalone app, I'm using go-ethereum as imported libraries to my golang app, so using console is not an option

I want to ask if go-etherum has some converter function inside it, or, probably, someone know other non-official library at go, which can convert Ethereum values.

1

Go-ethereum exposes the web3 API:

https://geth.ethereum.org/docs/interface/javascript-console

So, if amt is in ETH, you can do:

web3.utils.toWei(amt,'ether')

and

web3.utils.fromWei(amt, 'ether')

See:

https://web3js.readthedocs.io/en/v1.2.6/web3-utils.html#towei

| improve this answer | |
  • Ok, I should add that I'm not using geth as standalone app, I'm using go-ethereum modules as imported libraries to my golang app (for interaction with smart-contracts, transaction, event's, etc) Therefore I cannot use java-script console So, Im asking -- is there anything inside go-ethereum for making such conversions? Or probably you know any other go-lib for doing so? – Jack Bekket Jun 15 at 13:38
  • @JackBekket I'm not sure of the arithmetic precision in go, but could you just divide or multiply by 1000000000000000000? I don't know re. the go-ethereum internals. Why not update your question with this context? – atomh33ls Jun 15 at 14:18
  • Yes, currently the only solution is to do amt*10^18, but it feels unsafety and not really convinient as such simple functions from web3 :c Also, probably you know, why there are no implementation of web3 in go? – Jack Bekket Jun 15 at 18:39
  • @JackBekket I don't know, and I also don't know why there'd need to be one either. – atomh33ls Jun 19 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.