my smart contract uses an Oracle to retrieve the price. The price is returned as a string in scientific notation, for example, "3e-8". How I can convert this to number (uint)? The token has 18 decimals, so the final value in this example should be 30000000000.
2 Answers
You're either going to have to find a library that does this, or write some string parsing yourself, to extract the signed number after the "e", add that to the ERC20's decimals() return value, and multiply the number before the "e" by 10**result. Other answers have suggested available Solidity code for parsing, but if you roll your own you'll need to do string slicing and character access. Search around.
- 51
- 2
You can use the Provable's Computation Datasource, to do that https://docs.provable.xyz/#data-sources-computation.
Your use case resembles this example here https://github.com/provable-things/ethereum-examples/tree/master/solidity/computation-datasource/url-requests
Just make the relevant request to Coingecko inside a python script and then make the conversion you would expect onchain.
Please note that ARG0, ARG1, ARG2 you find inside the url-requests.py script
arg = [os.environ['ARG0'], os.environ['ARG1']]
parse 3rd arg into kwargs if available
if 'ARG2' in os.environ: kwargs = ast.literal_eval(os.environ['ARG2'])
else: kwargs = {}
are environment variables defined at Docker level, relative to the values defined into the array given to the oraclize_query function defined inside UrlRequests.sol code.
The first element of the array specified should contain the IPFS multihash of the uploaded archive. (The value returned by IPFS add archive.zip) Then ARG0, ARG1, ... follows
- 1
3e8 == 300000000.3e-8 == 0.00000003, which is simply zero if you tried to represent that as auint256. – Luke Hutchison Jun 05 '22 at 01:43