Run following command to query weather data from URL, and report weather to contract, then read the weather from contract.
node report-weather.js
Run following command to use multicall to batch got weather for a given batchId.
node multicall-weather.js
The multicall contract is deployed here.
Question 1: If the API returns the temperature in a decimal form (like 27.5 C), how to submit this decimal number to the smart contract while keeping its precision.
Answer: Let's say the temperature's precision is 2 decimals, we can multiple the weather by 100 when we write the weather to contract storage, and divide it by 100 when we read the weather. uint32 is large enough to save the temperature even after mulitpled by 100, because the number of real temperature usually is not that large.
Question 2: How to store a negative temperature while keeping the current smart contract interface unchanged?
Answer: We can mimic the two's complement to store negative value into a uint32 type.
How to store a number:
- To store a number
N, where0 <= N <= 2^31-1, we store it as is. - To store a negative number
-N, where0 <= N <= 2^31, we store the value as2^32-N.
How to read a number:
- If we read a number
Nfrom contract, and0 <= N <= 2^31-1, we keep it as is. - If we read a number
Nfrom contract, and2^31 <= N <= 2^32-1, we calculateN-2^32to recover the negative value.