-
Notifications
You must be signed in to change notification settings - Fork 302
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
Migrate composables to a provider pattern, so watchers and composables register once. #537
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<script lang="ts"> | ||
import { defineComponent, h } from 'vue'; | ||
|
||
import Provider from '@/plugins/providers/Provider.vue'; | ||
import provideTokenStore from '@/plugins/providers/tokenstore.provider'; | ||
import provideTokens from '@/plugins/providers/tokens.provider'; | ||
import provideAccountBalances from '@/plugins/providers/balances.provider'; | ||
import provideAccountAllowances from '@/plugins/providers/allowances.provider'; | ||
|
||
const providers = [ | ||
provideTokenStore, | ||
provideAccountBalances, | ||
provideTokens, | ||
provideAccountAllowances | ||
]; | ||
|
||
import App from './App.vue'; | ||
export default defineComponent({ | ||
components: { | ||
App, | ||
Provider | ||
}, | ||
render() { | ||
const renderProvider = (providerFunctions: Function[]) => { | ||
if (!providerFunctions.length) { | ||
return h(App, {}); | ||
} | ||
const [providerFunction, ...remainder] = providerFunctions; | ||
return h( | ||
Provider, | ||
{ providerFunction: providerFunction as Function }, | ||
{ | ||
default() { | ||
return [renderProvider(remainder)]; | ||
} | ||
} | ||
); | ||
}; | ||
return renderProvider(providers); | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,22 @@ | ||
import { Token, TokenMap } from '@/types'; | ||
import { getAddress } from '@ethersproject/address'; | ||
import { keyBy, orderBy, uniqBy } from 'lodash'; | ||
import { computed } from 'vue'; | ||
import { useStore } from 'vuex'; | ||
import useAccountBalances from './useAccountBalances'; | ||
import useTokenStore from './useTokensStore'; | ||
|
||
type TokenRequest = { | ||
query?: string; | ||
queryAddress?: string; | ||
}; | ||
import { | ||
TokenRequest, | ||
TokensProviderPayload, | ||
TokensProviderSymbol | ||
} from '@/plugins/providers/tokens.provider'; | ||
import { inject, onBeforeMount } from 'vue'; | ||
|
||
export default function useTokens(request?: TokenRequest) { | ||
const store = useStore(); | ||
const prices = computed(() => store.state.market.prices); | ||
const { allTokens: _allTokens } = useTokenStore(); | ||
const { balances } = useAccountBalances(); | ||
|
||
const tokensList = computed(() => { | ||
const _tokens = uniqBy<Token>( | ||
orderBy( | ||
// populate token data into list of tokens | ||
Object.values(_allTokens.value).map(token => { | ||
const balance = | ||
(balances.value || {})[token.address.toLowerCase()]?.balance || '0'; | ||
const price = prices.value[token.address.toLowerCase()]?.price || 0; | ||
const value = balance * price; | ||
const price24HChange = | ||
prices.value[token.address.toLowerCase()]?.price24HChange || 0; | ||
const value24HChange = (value / 100) * price24HChange; | ||
return { | ||
...token, | ||
address: getAddress(token.address), // Enforce that we use checksummed addresses | ||
value, | ||
price, | ||
price24HChange, | ||
balance, | ||
value24HChange | ||
}; | ||
}), | ||
['value', 'balance'], | ||
['desc', 'desc'] | ||
), | ||
'address' | ||
); | ||
|
||
if (request?.queryAddress) { | ||
const queryAddressLC = request?.queryAddress?.toLowerCase(); | ||
|
||
return _tokens.filter( | ||
token => token.address?.toLowerCase() === queryAddressLC | ||
); | ||
} | ||
|
||
// search functionality, this can be better | ||
if (request?.query) { | ||
const queryLC = request?.query?.toLowerCase(); | ||
const { tokens, updateTokenRequest } = inject( | ||
TokensProviderSymbol | ||
) as TokensProviderPayload; | ||
|
||
return _tokens.filter( | ||
token => | ||
token.name.toLowerCase().includes(queryLC) || | ||
token.symbol.toLowerCase().includes(queryLC) | ||
); | ||
onBeforeMount(() => { | ||
if (request) { | ||
updateTokenRequest(request); | ||
} | ||
|
||
return _tokens; | ||
}); | ||
|
||
const tokens = computed(() => keyBy(tokensList.value, 'address') as TokenMap); | ||
|
||
return { tokens }; | ||
return { | ||
tokens | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 probably will move to a better home that isn't inside plugins