-
Notifications
You must be signed in to change notification settings - Fork 370
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
Conversation
Because of the file rename, it can be difficult to see the diff. The only function that changed is |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
Updated with proper yield call, new diff can be seen here |
There was a problem hiding this 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?
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
packages/mobile/src/exchange/saga.ts
Outdated
try { | ||
yield call(getConnectedAccount) | ||
|
||
const contractKit = newKitFromWeb3(web3) |
There was a problem hiding this comment.
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?
packages/mobile/src/exchange/saga.ts
Outdated
|
||
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() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated!
tokenContract = await contractKit.contracts.getStableToken() | ||
break | ||
default: | ||
throw new Error(`Could not fetch contract for unknown token ${token}`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woot!
packages/mobile/src/exchange/saga.ts
Outdated
yield call(getConnectedAccount) | ||
const exchange = yield call([contractKit.contracts, contractKit.contracts.getExchange]) | ||
|
||
const dollarMakerExchangeRate: BigNumber = yield call( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great call
packages/mobile/src/exchange/saga.ts
Outdated
try { | ||
const account: string = yield call(getConnectedUnlockedAccount) | ||
const exchangeRatePair: ExchangeRatePair = yield select( | ||
(state: RootState) => state.exchange.exchangeRatePair |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
…norepo into annakaz/app-contractkit
@@ -93,16 +108,6 @@ describe('stableToken saga', () => { | |||
.run() | |||
}) | |||
|
|||
// TODO(cmcewen): Figure out how to mock this so we can get actual contract calls |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 } = { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
packages/mobile/src/tokens/saga.ts
Outdated
|
||
export async function getTokenContract(token: CURRENCY_ENUM) { | ||
Logger.debug(TAG + '@getTokenContract', `Fetching contract for ${token}`) | ||
await getConnectedAccount() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
…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) ...
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 ensurefetchTokenBalanceWithRetry
used in import works.Other changes
Add contract decimal caching
Moved the logic in
actions.ts
tosaga.ts
to follow redux saga conventionRelated issues
Backwards compatibility
Yes