forked from p2p-org/p2p-wallet-web
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): tokens provider for optimize, sequence of primary tokens
- Loading branch information
1 parent
c0dd10b
commit 0fd6eab
Showing
15 changed files
with
79 additions
and
49 deletions.
There are no files selected for viewing
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,4 +1,5 @@ | ||
export * from './seed'; | ||
export * from './solana'; | ||
export * from './tokens'; | ||
export * from './tokenAccounts'; | ||
export * from './transactionsHistory'; |
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,2 @@ | ||
export * from './useToken'; | ||
export * from './useTokens'; |
File renamed without changes.
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,2 @@ | ||
export * from './hooks'; | ||
export * from './provider'; |
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,61 @@ | ||
import { createContainer } from 'unstated-next'; | ||
import { useConnectionContext } from "../solana"; | ||
import { networkToChainId, Token } from "@saberhq/token-utils"; | ||
import { useMemo } from "react"; | ||
import { makeTokenMap } from "./utils/makeTokenMap"; | ||
|
||
const PRIMARY_SYMBOLS = ['SOL', 'USDC', 'BTC', 'USDT', 'ETH']; | ||
|
||
export type TokenMap = Record<string, Token>; | ||
|
||
export interface UseTokens { | ||
tokens: readonly Token[]; | ||
tokenMap: TokenMap; | ||
} | ||
|
||
const useTokensInternal = (): UseTokens => { | ||
const { network } = useConnectionContext(); | ||
const chainId = networkToChainId(network); | ||
|
||
const standardTokenMap = useMemo(() => makeTokenMap(chainId), [chainId]); | ||
|
||
const tokenMap = useMemo(() => { | ||
if (!chainId) { | ||
return {}; | ||
} | ||
|
||
return standardTokenMap; | ||
}, [chainId, standardTokenMap]); | ||
|
||
const tokens = useMemo(() => { | ||
const newTokenMap = { ...tokenMap }; | ||
|
||
// Get Token Mints | ||
const newTokens = Object.values(newTokenMap); | ||
const primaryMints = PRIMARY_SYMBOLS.map((symbol) => { | ||
const token = newTokens.find((itemToken) => itemToken.symbol === symbol); | ||
return token?.address; | ||
}).filter(Boolean) as string[]; | ||
|
||
// Get Primary Tokens and remove it from Token Map | ||
const primaryTokens = primaryMints | ||
.map((mint) => { | ||
const token = newTokenMap[mint]; | ||
delete newTokenMap[mint]; | ||
|
||
return token; | ||
}) | ||
.filter(Boolean) as Token[]; | ||
|
||
// Make tokens list with primary and other tokens | ||
return [...primaryTokens, ...Object.values(newTokenMap)]; | ||
}, [tokenMap]); | ||
|
||
return { | ||
tokens, | ||
tokenMap, | ||
}; | ||
}; | ||
|
||
export const { Provider: TokensProvider, useContainer: useTokensContext } = | ||
createContainer(useTokensInternal); |
File renamed without changes.
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,4 +1 @@ | ||
export * from './useAllTokens'; | ||
export * from './useStorage'; | ||
export * from './useToken'; | ||
export * from './useTokens'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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