Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query and record exchange rate using notification service #1020

Merged
merged 31 commits into from
Sep 24, 2019

Conversation

annakaz
Copy link
Contributor

@annakaz annakaz commented Sep 17, 2019

Description

To allow for a time series of the gold/dollar exchange rate, query the Exchange contract exchange rate using the notification service polling every 30 minutes, and store the result in Firebas. This allows developers to build like showing a time series of the gold/dollar exchange rate to app users.

The view calls are made by a web3 instance that uses a transaction node as a provider.

Tested

Tested locally with yarn start:local.

Not yet deployed to GCP, so storage in firebase has not been tested.

Other changes

Updated readme deploy script.

Related issues

Backwards compatibility

Yes, this builds on top of notification service but does not modify what is already there.

@codecov
Copy link

codecov bot commented Sep 17, 2019

Codecov Report

Merging #1020 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1020   +/-   ##
=======================================
  Coverage   66.45%   66.45%           
=======================================
  Files         256      256           
  Lines        7349     7349           
  Branches      424      424           
=======================================
  Hits         4884     4884           
  Misses       2377     2377           
  Partials       88       88
Flag Coverage Δ
#mobile 66.45% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bd4657d...702df1b. Read the comment docs.

Copy link
Contributor

@jmrossy jmrossy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice first pass! A few comments below :)

@@ -19,6 +19,8 @@
"deploy": "./deploy.sh"
},
"dependencies": {
"@celo/utils": "^0.0.6-beta5",
"@celo/walletkit": "^0.0.14",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we shouldn't add a dependency on walletkit here given that we want to deprecate that in favor of contract kit anyway. Can you change this?

Copy link
Contributor Author

@annakaz annakaz Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to be consistent with the mobile app, which uses walletkit: https://github.com/celo-org/celo-monorepo/blob/master/packages/mobile/src/exchange/actions.ts#L6
contractkit doesn't have the same getExchangeRate() function, so using contractkit would mean pasting over the code in walletkit.

I created an issue to migrate this and the mobile app away from walletkit after the exchange (or another util where this and the mobile app can share logic) is implemented in contractkit. #1026

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ContractKit with the needed functionality
#1083

packages/notification-service/package.json Show resolved Hide resolved
@@ -36,6 +36,10 @@ export const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE
export const POLLING_INTERVAL = Number(process.env.POLLING_INTERVAL) || 1000
export const NOTIFICATIONS_TTL_MS = Number(process.env.NOTIFICATION_TTL_MS) || 3600 * 1000 * 24 * 7 // 1 week in milliseconds

export const EXCHANGE_POLLING_INTERVAL =
Number(process.env.EXCHANGE_POLLING_INTERVAL) || 30 * 60 * 1000 // 30 minutes in milliseconds
export const WEB3_PROVIDER_URL = process.env.WEB3_PROVIDER_URL || 'http://34.83.137.48:8545' // Default to integration transaction node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this ip seems likely to change as integration churns right? I don't feel strongly but I'd prefer to have it just error if it can find a web3 provider value. Or, even better, have it skip exchange rate polling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Updated to skip exchange polling if it can't find a web3 provider value

let web3: Web3
export function getWeb3Instance(): Web3 {
if (web3) {
if (web3.eth.net.isListening()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


export const exchangePolling = AsyncPolling(async (end) => {
try {
const web3 = await getWeb3Instance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, any reason to get web3 here given that it's in the same file as handleExchangeQuery?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@@ -13,3 +14,14 @@ export const notificationPolling = AsyncPolling(async (end) => {
end()
}
}, POLLING_INTERVAL)

export const exchangePolling = AsyncPolling(async (end) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm slightly concerned about the impact of having two things polling at the same time given that JS is single threaded, though admittedly this one doesn't run often. If you can confirm that it doesn't seem to interfere with the notification polling then I think we're good

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From running locally, this doesn't seem to interfere, but I'm going to test deploying this to integration to confirm

let dollarMakerRate: number
let goldMakerRate: number
it('should fetch a working web3 instance', async () => {
web3 = await getWeb3Instance()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you mocking web3 for this? if not, I recommend we do.
If you need a semi-realistic web3 mock, you can use the one in mobile/__mocks__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After moving the getExchangeRate function to ContractKit, these tests don't improve coverage, as they are directly testing a function in ContractKit. So I removed this

@jmrossy jmrossy assigned annakaz and unassigned jmrossy Sep 17, 2019
…o-org/celo-monorepo into annakaz/ex-rate-notification-service
@annakaz annakaz assigned jmrossy and unassigned annakaz Sep 23, 2019
@ashishb ashishb closed this Sep 24, 2019
@ashishb ashishb deleted the annakaz/ex-rate-notification-service branch September 24, 2019 01:21
@ashishb ashishb restored the annakaz/ex-rate-notification-service branch September 24, 2019 02:15
@ashishb ashishb reopened this Sep 24, 2019
@annakaz
Copy link
Contributor Author

annakaz commented Sep 24, 2019

Note that since integration is having issues with view functions (running into same Error: Returned values aren't valid, did it run Out of Gas? as in wallet), this was tested with an alfajores web3 provider in the local config, then running yarn start:local.

@annakaz
Copy link
Contributor Author

annakaz commented Sep 24, 2019

Added instructions to update WEB3_PROVIDER_URL to the Celo Operations Runbook.

@annakaz
Copy link
Contributor Author

annakaz commented Sep 24, 2019

Note that the build is failing, but this is happening on master right now and will be fixed by #1088

@annakaz annakaz merged commit 8f2fb7f into master Sep 24, 2019
aaronmgdr added a commit that referenced this pull request Sep 25, 2019
* master: (61 commits)
  Remove locales as website is now just in English (#1050)
  Add MetadataURL to account struct (#1103)
  Allow validators to use any valid combination of gold commitments as stake (#885)
  Fix blockscout websocket jsonrpc url (#1096)
  [Wallet] Preliminary iOS support (#1098)
  [Wallet] Set security fee description translation in Spanish (#1097)
  Exclude generated in vscode file watcher setting (#1082)
  Update .env and .env.integration files (#1087)
  Allow a testnet to run without ethstats (#1085)
  Collect exchange rate time series using notification service (#1020)
  Return to preview view when Fee Education is closed (#1068)
  [Wallet] Pin Setup Flow v2 (#1054)
  Added a variable for electoral threshold (#1023)
  [celotool]Store .env config on GCS after deployment (#1086)
  Group size limit (#1035)
  Fix governance unit tests (#1084)
  Add getExchangeRate to ContractKit (#1083)
  [CLI]Unlock till the geth exits (#1070)
  Add Quorum and Refactor Governance (#430)
  Shuffle elected validators using block randomness (#1033)
  ...
@annakaz annakaz deleted the annakaz/ex-rate-notification-service branch October 15, 2019 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Notification service SBAT make view calls to contracts
3 participants