Skip to content

Commit

Permalink
feat: add solana-label-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Apr 4, 2024
1 parent 75aee0a commit 7103427
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 24 deletions.
1 change: 1 addition & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ type Query {
anonFindOneCommunity(slug: String!): Community
anonRequestIdentityChallenge(input: RequestIdentityChallengeInput!): IdentityChallenge
appConfig: AppConfig!
currencies: [Currency!]!
me: User
solanaGetBalance(account: String!): String
solanaGetTokenAccounts(account: String!): JSON
Expand Down
1 change: 1 addition & 0 deletions libs/api/core/data-access/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './lib/api-core.service'
export * from './lib/dto/paging.input'
export * from './lib/entity/app-config.entity'
export * from './lib/entity/app-context'
export * from './lib/entity/currency.entity'
export * from './lib/entity/paging-meta.entity'
export * from './lib/entity/paging-response.entity'
export * from './lib/helpers/ellipsify'
Expand Down
7 changes: 6 additions & 1 deletion libs/api/core/feature/src/lib/api-core.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiCoreService, AppConfig } from '@tokengator-mint/api-core-data-access'
import { Float, Query, Resolver } from '@nestjs/graphql'
import { ApiCoreService, AppConfig, Currency } from '@tokengator-mint/api-core-data-access'

@Resolver()
export class ApiCoreResolver {
Expand All @@ -10,6 +10,11 @@ export class ApiCoreResolver {
return this.service.config.appConfig
}

@Query(() => [Currency])
async currencies(): Promise<Currency[]> {
return this.service.data.currency.findMany({ orderBy: { name: 'asc' } })
}

@Query(() => Float)
uptime() {
return this.service.uptime()
Expand Down
2 changes: 1 addition & 1 deletion libs/api/price/data-access/src/lib/entity/price.entity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Field, Int, ObjectType } from '@nestjs/graphql'
import { Currency } from '@tokengator-mint/api-core-data-access'
import { Preset } from '@tokengator-mint/api-preset-data-access'
import { Currency } from './currency.entity'

@ObjectType()
export class Price {
Expand Down
48 changes: 40 additions & 8 deletions libs/sdk/src/generated/graphql-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ export type Query = {
anonFindOneCommunity?: Maybe<Community>
anonRequestIdentityChallenge?: Maybe<IdentityChallenge>
appConfig: AppConfig
currencies: Array<Currency>
me?: Maybe<User>
solanaGetBalance?: Maybe<Scalars['String']['output']>
solanaGetTokenAccounts?: Maybe<Scalars['JSON']['output']>
Expand Down Expand Up @@ -1544,6 +1545,14 @@ export type AppConfigDetailsFragment = {
authTwitterEnabled: boolean
}

export type CurrencyDetailsFragment = {
__typename?: 'Currency'
decimals: number
mint: string
name: string
symbol: string
}

export type PagingMetaDetailsFragment = {
__typename?: 'PagingMeta'
currentPage: number
Expand Down Expand Up @@ -1575,6 +1584,13 @@ export type AppConfigQuery = {
}
}

export type CurrenciesQueryVariables = Exact<{ [key: string]: never }>

export type CurrenciesQuery = {
__typename?: 'Query'
items: Array<{ __typename?: 'Currency'; decimals: number; mint: string; name: string; symbol: string }>
}

export type IdentityDetailsFragment = {
__typename?: 'Identity'
createdAt: Date
Expand Down Expand Up @@ -2145,14 +2161,6 @@ export type UserFindOnePresetQuery = {
} | null
}

export type CurrencyDetailsFragment = {
__typename?: 'Currency'
decimals: number
mint: string
name: string
symbol: string
}

export type PriceDetailsFragment = {
__typename?: 'Price'
createdAt?: Date | null
Expand Down Expand Up @@ -3073,6 +3081,14 @@ export const AppConfigDocument = gql`
}
${AppConfigDetailsFragmentDoc}
`
export const CurrenciesDocument = gql`
query currencies {
items: currencies {
...CurrencyDetails
}
}
${CurrencyDetailsFragmentDoc}
`
export const AdminFindManyIdentityDocument = gql`
query adminFindManyIdentity($input: AdminFindManyIdentityInput!) {
items: adminFindManyIdentity(input: $input) {
Expand Down Expand Up @@ -3567,6 +3583,7 @@ const AnonFindManyCommunityDocumentString = print(AnonFindManyCommunityDocument)
const AnonFindOneCommunityDocumentString = print(AnonFindOneCommunityDocument)
const UptimeDocumentString = print(UptimeDocument)
const AppConfigDocumentString = print(AppConfigDocument)
const CurrenciesDocumentString = print(CurrenciesDocument)
const AdminFindManyIdentityDocumentString = print(AdminFindManyIdentityDocument)
const AdminCreateIdentityDocumentString = print(AdminCreateIdentityDocument)
const AdminDeleteIdentityDocumentString = print(AdminDeleteIdentityDocument)
Expand Down Expand Up @@ -4158,6 +4175,21 @@ export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper =
variables,
)
},
currencies(
variables?: CurrenciesQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
): Promise<{ data: CurrenciesQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number }> {
return withWrapper(
(wrappedRequestHeaders) =>
client.rawRequest<CurrenciesQuery>(CurrenciesDocumentString, variables, {
...requestHeaders,
...wrappedRequestHeaders,
}),
'currencies',
'query',
variables,
)
},
adminFindManyIdentity(
variables: AdminFindManyIdentityQueryVariables,
requestHeaders?: GraphQLClientRequestHeaders,
Expand Down
13 changes: 13 additions & 0 deletions libs/sdk/src/graphql/feature-core.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ fragment AppConfigDetails on AppConfig {
authTwitterEnabled
}

fragment CurrencyDetails on Currency {
decimals
mint
name
symbol
}

fragment PagingMetaDetails on PagingMeta {
currentPage
isFirstPage
Expand All @@ -27,3 +34,9 @@ query appConfig {
...AppConfigDetails
}
}

query currencies {
items: currencies {
...CurrencyDetails
}
}
6 changes: 0 additions & 6 deletions libs/sdk/src/graphql/feature-price.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
fragment CurrencyDetails on Currency {
decimals
mint
name
symbol
}
fragment PriceDetails on Price {
createdAt
id
Expand Down
1 change: 1 addition & 0 deletions libs/web/core/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './lib/app-config.provider'
export * from './lib/sdk-provider'
export * from './lib/use-currencies'
11 changes: 11 additions & 0 deletions libs/web/core/data-access/src/lib/use-currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useQuery } from '@tanstack/react-query'
import { useSdk } from './sdk-provider'

export function useCurrencies() {
const sdk = useSdk()
return useQuery({
queryKey: ['currencies'],
queryFn: () => sdk.currencies().then((res) => res.data?.items ?? []),
retry: 0,
})
}
1 change: 1 addition & 0 deletions libs/web/solana/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './lib/cluster-provider'
export * from './lib/create-transaction'
export * from './lib/solana-label-provider'
export * from './lib/solana-provider'
export * from './lib/ui-toast-link'
export * from './lib/use-account'
Expand Down
27 changes: 27 additions & 0 deletions libs/web/solana/data-access/src/lib/solana-label-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ellipsify } from '@tokengator-mint/sdk'
import { useCurrencies } from '@tokengator-mint/web-core-data-access'
import { createContext, ReactNode, useContext } from 'react'

export interface SolanaLabelProviderContext {
map: Map<string, string>
getLabel: (mint: string) => string
}

const Context = createContext<SolanaLabelProviderContext>({} as SolanaLabelProviderContext)

export function SolanaLabelProvider({ children }: { children: ReactNode }) {
const currencyQuery = useCurrencies()
const map = new Map<string, string>()
currencyQuery.data?.forEach((currency) => {
map.set(currency.mint, currency.symbol)
})
const value: SolanaLabelProviderContext = {
map,
getLabel: (mint: string) => map.get(mint) ?? ellipsify(mint),
}
return <Context.Provider value={value}>{children}</Context.Provider>
}

export function useSolanaLabel() {
return useContext(Context)
}
9 changes: 6 additions & 3 deletions libs/web/solana/data-access/src/lib/solana-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import {
} from '@solana/wallet-adapter-react'
import { ReactNode, useCallback, useMemo } from 'react'
import { ClusterProvider, useCluster } from './cluster-provider'
import { SolanaLabelProvider } from './solana-label-provider'

export function SolanaClusterProvider({ autoConnect, children }: { autoConnect?: boolean; children: ReactNode }) {
return (
<ClusterProvider>
<SolanaProvider autoConnect={autoConnect}>{children}</SolanaProvider>
</ClusterProvider>
<SolanaLabelProvider>
<ClusterProvider>
<SolanaProvider autoConnect={autoConnect}>{children}</SolanaProvider>
</ClusterProvider>
</SolanaLabelProvider>
)
}

Expand Down
10 changes: 5 additions & 5 deletions libs/web/wallet/ui/src/lib/wallet-ui-sol-token-accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { UiError, UiInfo, UiStack } from '@pubkey-ui/core'
import { AccountInfo, ParsedAccountData, PublicKey } from '@solana/web3.js'
import { IconRefresh } from '@tabler/icons-react'
import { useQueryClient } from '@tanstack/react-query'
import { ellipsify, Wallet } from '@tokengator-mint/sdk'
import { useSolanaGetTokenAccounts } from '@tokengator-mint/web-solana-data-access'
import { Wallet } from '@tokengator-mint/sdk'
import { useSolanaGetTokenAccounts, useSolanaLabel } from '@tokengator-mint/web-solana-data-access'
import { SolanaUiExplorerLink } from '@tokengator-mint/web-solana-ui'
import { useMemo, useState } from 'react'

export function WalletUiSolTokenAccounts({ wallet }: { wallet: Wallet }) {
const query = useSolanaGetTokenAccounts({ account: wallet.publicKey })

const { getLabel } = useSolanaLabel()
const [showAll, setShowAll] = useState(false)
const client = useQueryClient()
const items: { account: AccountInfo<ParsedAccountData>; pubkey: PublicKey }[] = useMemo(() => {
Expand Down Expand Up @@ -71,14 +71,14 @@ export function WalletUiSolTokenAccounts({ wallet }: { wallet: Wallet }) {
<Table.Td>
<SolanaUiExplorerLink
ff="monospace"
label={ellipsify(pubkey.toString())}
label={getLabel(pubkey.toString())}
path={`account/${pubkey.toString()}`}
/>
</Table.Td>
<Table.Td>
<SolanaUiExplorerLink
ff="monospace"
label={ellipsify(account.data.parsed.info.mint)}
label={getLabel(account.data.parsed.info.mint)}
path={`account/${account.data.parsed.info.mint.toString()}`}
/>
</Table.Td>
Expand Down

0 comments on commit 7103427

Please sign in to comment.