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

[BlockchainApi] Add ability to get exchange rates from/to cGLD or cUSD #2005

Merged
merged 18 commits into from
Dec 12, 2019

Conversation

jeanregisser
Copy link
Contributor

@jeanregisser jeanregisser commented Dec 3, 2019

Description

Add ability to get exchange rates from/to cGLD or cUSD.

  • It's now possible to get rates from cGLD to any other currency (cUSD, USD, MXN, etc) and vice versa.
  • Supports getting cGLD rate at a specific date/time (with a granularity of 30 mins as that's the current interval at which the rate is being stored)

For now the cUSD/USD and USD/cUSD rates are assumed to be 1.
Also going from cGLD to local currency (or vice versa) is currently assumed to be the same as cGLD -> cUSD -> USD -> local currency.
And similar to cUSD to local currency, but with one less step.

Tested

Added new tests.
Successfully deployed on integration: https://integration-dot-celo-testnet.appspot.com

Other changes

  • Use service account key for all envs. This is so blockchain-api in celo-testnet project can access the firebase db in celo-org-mobile project. This is temporary until we migrate blockchain-api to celo-org-mobile too.
  • Speed up jest tests by mocking contractkit.

WITHOUT mocking contractkit:

  • yarn test (clean cache): ~27secs
  • yarn test (populated cache): 23 secs

WITH mocking contractkit:

  • yarn test (clean cache): ~8secs
  • yarn test (populated cache): ~4secs

Related issues

Backwards compatibility

Yes

WITHOUT mocking contractkit:
- yarn test (clean cache): ~27secs
- yarn test (populated cache): 23 secs
WITH mocking contractkit:
- yarn test (clean cache): ~8secs
- yarn test (populated cache): ~4secs
deploy runs yarn from packages/blockchain-api so ends up with slightly different node_modules
This is so blockchain-api in celo-testnet project
can access the firebase db in celo-org-mobile project

This is temporary until we migrate blockchain-api to celo-org-mobile too.
@codecov
Copy link

codecov bot commented Dec 10, 2019

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2005   +/-   ##
=======================================
  Coverage   74.88%   74.88%           
=======================================
  Files         279      279           
  Lines        7797     7797           
  Branches      690      690           
=======================================
  Hits         5839     5839           
  Misses       1842     1842           
  Partials      116      116
Flag Coverage Δ
#mobile 74.88% <ø> (ø) ⬆️

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 61bfc10...a7abbe6. Read the comment docs.

@jeanregisser jeanregisser marked this pull request as ready for review December 10, 2019 14:58
Copy link
Contributor

@annakaz annakaz left a comment

Choose a reason for hiding this comment

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

LGTM! Left some minor comments

// TODO: move project to celo-org-mobile
// until then, using serviceAccountKey for all envs
// tslint:disable-next-line: no-constant-condition
if (true /* DEPLOY_ENV === 'local' */) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove if (true) ? Or uncomment /* DEPLOY_ENV === 'local' */?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to keep it this way so we can uncomment once we deploy it to celo-org-mobile.
Makes sense?

}

export default class GoldExchangeRateAPI<TContext = any> extends DataSource {
// TODO(jeanregisser): memoize results
Copy link
Contributor

Choose a reason for hiding this comment

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

TODOs here and below for a future PR? Or left in for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left them for a future PR.

expect(mockGoldGetExchangeRate).toHaveBeenCalledTimes(1)
})

it('should retrieve rate for MXN/cGLD', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this test cover any additional logic from the MXN/cGLD one given the mocks?

Copy link
Contributor Author

@jeanregisser jeanregisser Dec 11, 2019

Choose a reason for hiding this comment

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

They look really similar indeed but the idea was to cover both local to cGLD and cGLD to local.

expect(mockGoldGetExchangeRate).toHaveBeenCalledTimes(0)
})

it('should retrieve rate for MXN/USD', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this one- is there anything this test would catch that wouldn't be covered by the USD/MXN one?
Could also consider adding some tests for getConversionSteps

Copy link
Contributor Author

@jeanregisser jeanregisser Dec 11, 2019

Choose a reason for hiding this comment

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

Yes similar to my comment above.
I decided not to test getConversionSteps directly to avoid testing the internals too much.

const pair = `${fromCode}/${toCode}`

if (pair === 'cUSD/USD' || pair === 'USD/cUSD') {
// TODO: use real rates once we have the data
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

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.

A few small comments but mostly looks good!

@@ -23,6 +23,7 @@
"bignumber.js": "^7.2.0",
"dotenv": "^6.1.0",
"express": "^4.16.4",
"firebase-admin": "^8.8.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use the same version we're using in other monorepo packages (8.6.1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

try {
const serviceAccount = require('../serviceAccountKey.json')
return admin.credential.cert(serviceAccount)
} catch {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we catch and log the error object here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes probably, I blindly cut and pasted this from notification-service

@@ -0,0 +1 @@
export { default as CurrencyConversionAPI } from './CurrencyConversionAPI'
Copy link
Contributor

Choose a reason for hiding this comment

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

Why create this index file instead of having other files import from CurrencyConversionAPI directly?

Copy link
Contributor Author

@jeanregisser jeanregisser Dec 11, 2019

Choose a reason for hiding this comment

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

Mostly to hide the internal file organisation of currency conversion with its somewhat private internal modules (ExchangeRateAPI and GoldExchangeRateAPI).
But doesn't really matter.

@jeanregisser jeanregisser added the automerge Have PR merge automatically when checks pass label Dec 11, 2019
@celo-ci-bot-user celo-ci-bot-user merged commit 5b84b23 into master Dec 12, 2019
@celo-ci-bot-user celo-ci-bot-user deleted the jeanregisser/blockchain-api-convert-gold branch December 12, 2019 11:15
aaronmgdr added a commit that referenced this pull request Dec 12, 2019
* master:
  [Wallet] Use Charles proxy to see eth JSON rpc calls when using forno (#2204)
  [Wallet] Disable skip button when the user enable contact access (#2224)
  [Wallet] Redesigning notification lists (#1967)
  [Wallet] Fix crash on iOS when segment is enabled (#2222)
  Update documentation wrt. epoch rewards fractions (#2182)
  Improvement facilitating to run a full node (#2130)
  [BlockchainApi] Add ability to get exchange rates from/to cGLD or cUSD (#2005)
  Improve reliability of e2e governance test (#2208)
  Fix/protocol test flakyness (#2155)
  Fix bignumber display in CLI (#2212)
  Doc changes to address frequently asked questions (#2209)
  Upgrade TS version (#2196)
  Wait for only waitTime - 1 blocks (#2207)
  Minor baklava docs reconnection fixes (#2215)
  Update walletkit gateway fee to fix transactions in forno mode (#2211)
  Update baklava network ID in docs for 1.1 (#2214)
  Support more than 1 attesation bot at a time (#2192)
  Check sync status of attestation service (#2191)
  Indicate to run Twilio globally (#2193)
  Add Twilio and attestation bot envs (#2194)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Have PR merge automatically when checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants