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

[Wallet] Migrate app view functions to contractkit #1381

Merged
merged 31 commits into from
Oct 25, 2019

Conversation

annakaz
Copy link
Contributor

@annakaz annakaz commented Oct 17, 2019

Description

Use contractkit in app, and use it instead of walletkit to view exchange rate and fetch balances in wallet.

Tested

Tested on device using alfajores to ensure that exchange rate and balances can be fetched. Also tested from cold start to ensure fetchTokenBalanceWithRetry used in import works.

Other changes

Add contract decimal caching
Moved the logic in actions.ts to saga.ts to follow redux saga convention

Related issues

Backwards compatibility

Yes

@annakaz
Copy link
Contributor Author

annakaz commented Oct 17, 2019

Because of the file rename, it can be difficult to see the diff. The only function that changed is doFetchExchangeRate(), which I have compared in an online diff checker to make it easier to diff here:
https://www.diffchecker.com/A3OPDvZG

@codecov
Copy link

codecov bot commented Oct 17, 2019

Codecov Report

Merging #1381 into master will decrease coverage by 0.15%.
The diff coverage is 25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1381      +/-   ##
==========================================
- Coverage   65.49%   65.34%   -0.16%     
==========================================
  Files         271      271              
  Lines        8167     8160       -7     
  Branches      501      491      -10     
==========================================
- Hits         5349     5332      -17     
- Misses       2699     2714      +15     
+ Partials      119      114       -5
Flag Coverage Δ
#mobile 65.34% <25%> (-0.16%) ⬇️
Impacted Files Coverage Δ
packages/mobile/src/invite/saga.ts 80.53% <ø> (ø) ⬆️
packages/mobile/src/stableToken/saga.ts 81.81% <ø> (ø) ⬆️
packages/mobile/src/goldToken/saga.ts 0% <ø> (ø) ⬆️
packages/mobile/src/exchange/actions.ts 100% <ø> (+71.84%) ⬆️
packages/mobile/src/exchange/saga.ts 0% <0%> (ø) ⬆️
packages/mobile/src/exchange/reducer.ts 66.66% <100%> (+6.66%) ⬆️
packages/mobile/src/import/saga.ts 87.75% <100%> (ø) ⬆️
packages/mobile/src/web3/contracts.ts 56.52% <100%> (+1.97%) ⬆️
packages/mobile/src/tokens/saga.ts 87.17% <81.81%> (-5.42%) ⬇️
...kages/mobile/src/transactions/TransferFeedItem.tsx 82.45% <0%> (-0.88%) ⬇️
... and 7 more

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 167d6c4...76228df. Read the comment docs.

@annakaz
Copy link
Contributor Author

annakaz commented Oct 17, 2019

Updated with proper yield call, new diff can be seen here
https://www.diffchecker.com/A3OPDvZG

Copy link
Contributor

@cmcewen cmcewen left a comment

Choose a reason for hiding this comment

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

Nice!!!! As I'm looking, I see that we don't have any tests for these functions - would you mind adding one or two?

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.

The changes mostly lgtm so far, thanks for moving this logic to the saga file.
Just a few questions below


const goldTokenContract: GoldTokenType = yield call(getGoldTokenContract, web3)
const stableTokenContract: StableTokenType = yield call(getStableTokenContract, web3)
const exchangeContract: ExchangeType = yield call(getExchangeContract, web3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be converted to contact kit too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These contracts are used for the exchange transaction. I plan to migrate these along with other state changing functions in a future PR

try {
yield call(getConnectedAccount)

const contractKit = newKitFromWeb3(web3)
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 have the kit live in web3/saga or somewhere else where more components can reach for it?


async function convertToContractDecimals(value: BigNumber | string | number, contract: any) {
// TODO(Rossy): Move this function to SDK and cache this decimals amount
const decimals = await contract.methods.decimals().call()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this use contract kit too?

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!

@jmrossy jmrossy assigned annakaz and unassigned jmrossy and cmcewen Oct 19, 2019
tokenContract = await contractKit.contracts.getStableToken()
break
default:
throw new Error(`Could not fetch contract for unknown token ${token}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error is caught in tokenFetchFactory

@annakaz annakaz removed their assignment Oct 22, 2019
Copy link
Contributor

@cmcewen cmcewen left a comment

Choose a reason for hiding this comment

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

woot!

yield call(getConnectedAccount)
const exchange = yield call([contractKit.contracts, contractKit.contracts.getExchange])

const dollarMakerExchangeRate: BigNumber = yield call(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could parallelize these calls for speed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great call

try {
const account: string = yield call(getConnectedUnlockedAccount)
const exchangeRatePair: ExchangeRatePair = yield select(
(state: RootState) => state.exchange.exchangeRatePair
Copy link
Contributor

Choose a reason for hiding this comment

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

nit - move to a selector

@@ -13,6 +14,7 @@ import { Provider } from 'web3/providers'
const tag = 'web3/contracts'

export const web3: Web3 = getWeb3()
export const contractKit = newKitFromWeb3(web3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this be okay when toggling to zero sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may need to update the contractkit provider as well, but I can deal with that in the zeroSync PR. Thanks for flagging this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Follow up on this, tested after merging in the ZeroSync PR and everything works as expected

@annakaz annakaz assigned annakaz and unassigned jmrossy and cmcewen Oct 23, 2019
@@ -93,16 +108,6 @@ describe('stableToken saga', () => {
.run()
})

// TODO(cmcewen): Figure out how to mock this so we can get actual contract calls
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that I removed this test as a getStableToken call is already tested in the 'should fetch the balance and put the new balance' test above.


async function getWeiPerUnit(token: CURRENCY_ENUM) {
let weiPerUnit = contractWeiPerUnit[token]
if (!weiPerUnit) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice to see this cached 👍

import { getConnectedAccount, getConnectedUnlockedAccount } from 'src/web3/saga'
import * as utf8 from 'utf8'

const TAG = 'tokens/saga'

// The number of wei that represent one unit in a contract
const contractWeiPerUnit: { [key in CURRENCY_ENUM]: BigNumber | null } = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can a wei per unit differ for two tokens? If not probably not need to cache two diff values for it

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 it can differ and is set different for gold and stabletokens, it's a configurable parameter here: https://github.com/celo-org/celo-monorepo/blob/master/packages/protocol/contracts/stability/StableToken.sol#L113


export async function getTokenContract(token: CURRENCY_ENUM) {
Logger.debug(TAG + '@getTokenContract', `Fetching contract for ${token}`)
await getConnectedAccount()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does the account need to be connected for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to wait for web3 sync, which is needed for contractkit to get the token contracts

@annakaz annakaz merged commit a6a7269 into master Oct 25, 2019
aaronmgdr added a commit that referenced this pull request Oct 29, 2019
…repo into aaronmgdr/invite-page

* 'aaronmgdr/invite-page' of github.com:celo-org/celo-monorepo: (63 commits)
  Fix compile error during local clean build (#1506)
  Update to RN 61 and AndroidX (#1343)
  Set usage of shuffled round robin in the genesis block (#1464)
  Add spanish backup key for backup key flow (#1500)
  Fix sync tests by pulling genesis block to determine epoch length (#1504)
  [Wallet] fix missing full name error alert (#1496)
  [Wallet + Verification Pool] Add details about generating an app-signature (#1482)
  Deploy celo's image of ethstats (#1421)
  Storing previous randomness values (#1197)
  [Wallet] Fix wei invite bug (#1489)
  Point all packages to latest ganache-cli master (#1488)
  Point end-to-end tests back to master (#1469)
  [Wallet] Migrate app view functions to contractkit (#1381)
  [Wallet] Add script to translate locale strings (#1485)
  [Wallet] Update wallet celo client version and add missing translations for backup flow (#1483)
  [Wallet] Hotfix local currency (#1481)
  [Wallet] Remove QR debouncing to improve responsiveness (#1480)
  [Wallet] Upgrade app version to v1.5.1 (#1463)
  Update governance end-to-end tests to work with changed precompile (#1476)
  Fixes key_placer.sh when encrypting files (#1465)
  ...
@mcortesi mcortesi deleted the annakaz/app-contractkit branch December 4, 2019 16:49
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.

3 participants