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

fetch common tokens #732

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/containers/EventsHandlers/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ import { useDispatch, useSelector } from 'react-redux'
import { useEffect, useState } from 'react'
import { network, rpcAddress, status } from '@selectors/solanaConnection'
import { actions } from '@reducers/pools'
import { actions as walletActions } from '@reducers/solanaWallet'
import { getMarketProgramSync } from '@web3/programs/amm'
import { poolsArraySortedByFees, poolTicks, tickMaps } from '@selectors/pools'
import {
getNetworkTokensList,
findPairs,
getFullNewTokensData,
getPoolsVolumeRanges
getPoolsVolumeRanges,
getMainnetCommonTokens
} from '@consts/utils'
import { swap } from '@selectors/swap'
import { findTickmapChanges, Pair } from '@invariant-labs/sdk'
import { PublicKey } from '@solana/web3.js'
import { getCurrentSolanaConnection } from '@web3/connection'
import { Status, actions as solanaConnectionActions } from '@reducers/solanaConnection'
import { NetworkType } from '@consts/static'

const MarketEvents = () => {
const dispatch = useDispatch()
Expand All @@ -29,6 +32,25 @@ const MarketEvents = () => {
const poolTicksArray = useSelector(poolTicks)
const [subscribedTick, _setSubscribeTick] = useState<Set<string>>(new Set())
const [subscribedTickmap, _setSubscribedTickmap] = useState<Set<string>>(new Set())

useEffect(() => {
if (networkType !== NetworkType.MAINNET) {
return
}
const getCommonTokens = async () => {
try {
const mainnetCommonTokens = await getMainnetCommonTokens()
dispatch(
walletActions.setCommonTokens({ network: networkType, tokens: mainnetCommonTokens })
)
} catch (error) {
console.log(error)
}
}

void getCommonTokens()
}, [networkType])

useEffect(() => {
const connection = getCurrentSolanaConnection()
if (networkStatus !== Status.Initialized || !connection) {
Expand Down
10 changes: 4 additions & 6 deletions src/containers/NewPositionWrapper/NewPositionWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { ProgressState } from '@components/AnimatedButton/AnimatedButton'
import NewPosition from '@components/NewPosition/NewPosition'
import { TickPlotPositionData } from '@components/PriceRangePlot/PriceRangePlot'
import {
ALL_FEE_TIERS_DATA,
PositionOpeningMethod,
bestTiers,
commonTokensForNetworks
} from '@consts/static'
import { ALL_FEE_TIERS_DATA, PositionOpeningMethod, bestTiers } from '@consts/static'
import {
TokenPriceData,
addNewTokenToLocalStorage,
Expand Down Expand Up @@ -40,6 +35,7 @@ import {
balanceLoading,
canCreateNewPool,
canCreateNewPosition,
commonTokens,
status,
swapTokensDict
} from '@selectors/solanaWallet'
Expand Down Expand Up @@ -85,6 +81,8 @@ export const NewPositionWrapper: React.FC<IProps> = ({

const isTimeoutError = useSelector(timeoutError)

const commonTokensForNetworks = useSelector(commonTokens)

const [poolIndex, setPoolIndex] = useState<number | null>(null)

const [progress, setProgress] = useState<ProgressState>('none')
Expand Down
6 changes: 3 additions & 3 deletions src/containers/WrappedSwap/WrappedSwap.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ProgressState } from '@components/AnimatedButton/AnimatedButton'
import { Swap } from '@components/Swap/Swap'
import { commonTokensForNetworks } from '@consts/static'
import {
addNewTokenToLocalStorage,
TokenPriceData,
Expand All @@ -19,12 +18,12 @@ import {
} from '@selectors/pools'
import { network, timeoutError } from '@selectors/solanaConnection'
import { actions as connectionActions } from '@reducers/solanaConnection'
import { status, swapTokensDict, balanceLoading } from '@selectors/solanaWallet'
import { status, swapTokensDict, balanceLoading, commonTokens } from '@selectors/solanaWallet'
import { swap as swapPool } from '@selectors/swap'
import { PublicKey } from '@solana/web3.js'
import { getCurrentSolanaConnection } from '@web3/connection'
import { openWalletSelectorModal } from '@web3/selector'
import { useEffect, useState } from 'react'
import React, { useEffect, useState } from 'react'
import { useDispatch, useSelector } from 'react-redux'

export const WrappedSwap = () => {
Expand All @@ -43,6 +42,7 @@ export const WrappedSwap = () => {
const isFetchingNewPool = useSelector(isLoadingLatestPoolsForTransaction)
const networkType = useSelector(network)
const isTimeoutError = useSelector(timeoutError)
const commonTokensForNetworks = useSelector(commonTokens)

const [progress, setProgress] = useState<ProgressState>('none')
const [tokenFrom, setTokenFrom] = useState<PublicKey | null>(null)
Expand Down
23 changes: 0 additions & 23 deletions src/store/consts/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,6 @@ export const bestTiers: Record<NetworkType, BestTier[]> = {
Localnet: []
}

export const commonTokensForNetworks: Record<NetworkType, PublicKey[]> = {
Devnet: [
USDC_DEV.address,
USDT_DEV.address,
BTC_DEV.address,
WSOL_DEV.address,
MSOL_DEV.address,
USDH_DEV.address
],
Mainnet: [
new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
new PublicKey('Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'),
new PublicKey('USDH1SM1ojwWUga67PGrgFWUHibbjqMvuMaDkRJTgkX'),
new PublicKey('4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R'),
new PublicKey('So11111111111111111111111111111111111111112'),
new PublicKey('3NZ9JMVBmGAqocybic2c7LQCJScmgsAZ6vQqTDzcqmJh'),
new PublicKey('7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs'),
new PublicKey('mSoLzYCxHdYgdzU16g5QSh3i5K3z3KZK7ytfqcJm7So')
],
Testnet: [],
Localnet: []
}

export const airdropTokens: Record<NetworkType, PublicKey[]> = {
Devnet: [
USDC_DEV.address,
Expand Down
10 changes: 10 additions & 0 deletions src/store/consts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1188,3 +1188,13 @@ export const solToPriorityFee = (sol: number) => {
}

export const createLoaderKey = () => (new Date().getMilliseconds() + Math.random()).toString()

export const getMainnetCommonTokens = async (): Promise<PublicKey[]> => {
const { data } = await axios.get('https://tokens.jup.ag/tokens?tags=verified')
Copy link
Member

Choose a reason for hiding this comment

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

have you checked if jup handles the limit in the query (to fetch only 8 tokens)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, i have checked. there is no option to limit number of tokens in query. here is link to API https://station.jup.ag/docs/token-list/token-list-api


const commonTokens = data
.slice(0, 8)
.map((token: { address: string }) => new PublicKey(token.address))

return commonTokens
}
49 changes: 47 additions & 2 deletions src/store/reducers/solanaWallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { DEFAULT_PUBLICKEY } from '@consts/static'
import {
BTC_DEV,
DEFAULT_PUBLICKEY,
MC2_DEV,
MC3_DEV,
MCK_DEV,
MSOL_DEV,
NetworkType,
RENDOGE_DEV,
SOL_DEV,
USDC_DEV,
USDH_DEV,
USDT_DEV
} from '@consts/static'
import { BN } from '@project-serum/anchor'
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { PublicKey } from '@solana/web3.js'
Expand Down Expand Up @@ -36,15 +49,43 @@ export interface ISolanaWallet {
balance: BN
accounts: { [key in string]: ITokenAccount }
balanceLoading: boolean
commonTokens: Record<NetworkType, PublicKey[]>
}

export const defaultState: ISolanaWallet = {
status: Status.Uninitialized,
address: DEFAULT_PUBLICKEY,
balance: new BN(0),
accounts: {},
balanceLoading: false
balanceLoading: false,
commonTokens: {
Devnet: [
USDC_DEV.address,
USDT_DEV.address,
SOL_DEV.address,
MSOL_DEV.address,
BTC_DEV.address,
RENDOGE_DEV.address,
MCK_DEV.address,
MC2_DEV.address,
MC3_DEV.address,
USDH_DEV.address
],
Mainnet: [
new PublicKey('So11111111111111111111111111111111111111112'),
new PublicKey('JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN'),
new PublicKey('85VBFQZC9TZkfaptBWjvUw7YbZjy52A6mjtPGjstQAmQ'),
new PublicKey('HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3'),
new PublicKey('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'),
new PublicKey('Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB'),
new PublicKey('EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm'),
new PublicKey('CKaKtYvz6dKPyMvYq9Rh3UBrnNqYZAyd7iF4hJtjUvks')
],
Testnet: [],
Localnet: []
}
}

export const solanaWalletSliceName = 'solanaWallet'
const solanaWalletSlice = createSlice({
name: solanaWalletSliceName,
Expand Down Expand Up @@ -89,6 +130,10 @@ const solanaWalletSlice = createSlice({
state.accounts[action.payload.programId].balance = action.payload.balance
return state
},
setCommonTokens(state, action: PayloadAction<{ network: NetworkType; tokens: PublicKey[] }>) {
state.commonTokens[action.payload.network] = action.payload.tokens
return state
},
// Triggers rescan for tokens that we control
rescanTokens() {},
airdrop() {},
Expand Down
14 changes: 6 additions & 8 deletions src/store/selectors/solanaWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import {

const store = (s: AnyProps) => s[solanaWalletSliceName] as ISolanaWallet

export const { address, balance, accounts, status, balanceLoading } = keySelectors(store, [
'address',
'balance',
'accounts',
'status',
'balanceLoading'
])
export const { address, balance, accounts, status, balanceLoading, commonTokens } = keySelectors(
store,
['address', 'balance', 'accounts', 'status', 'balanceLoading', 'commonTokens']
)

export const tokenBalance = (tokenAddress: PublicKey) =>
createSelector(accounts, balance, (tokensAccounts, solBalance) => {
Expand Down Expand Up @@ -116,6 +113,7 @@ export const solanaWalletSelectors = {
accounts,
status,
tokenAccount,
balanceLoading
balanceLoading,
commonTokens
}
export default solanaWalletSelectors
Loading