dfinity / nns-dapp

The Dapp of the Internet Computer's Network Nervous System.
https://nns.ic0.app/
Other
112 stars 38 forks source link

NNS1-3281: Add update_exchange_rate for TVL #5379

Closed dskloetd closed 2 months ago

dskloetd commented 2 months ago

Motivation

The NNS dapp displays the USD value of the total amount of ICP locked in neurons. It gets this information from the TVL canister but we want to move this functionality to the nns-dapp canister and remove the TVL canister.

There are 2 inputs to this data:

The amount of ICP locked in neurons The price of ICP in USD. We plan to periodically load both values and cache them in the canister state. When the user requests the TVL, it will be computed from the currently cached state.

In this PR we only add a function which can load the current price of ICP in USD into the nns-dapp state. The function is not yet called, and the state is not yet persisted in this PR.

Changes

  1. Add usd_per_icp_e8s and exchange_rate_timestamp_sec fields to the TvlState type. The timestamp is necessary to offer the same interface from the nns-dapp that the TVL canister offers.
  2. Add code to call the get_exchange_rate method on the exchange rate canister, including an implementation which can be controlled for testing.
  3. Add a test implementation of the time function which gives the current time.
  4. Add update_exchange_rate which calls the exchange rate canister to ask for the ICP price of 5 minutes ago and stores it in the nns-dapp canister state, together with the time at which exchange rate was valid.

Tests

  1. Added unit tests for the success and error cases.
  2. Manually tested the feature end to end in a branch which has all the other changes.

Todos