Skip to content

Commit

Permalink
feat: token verification intro
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Jul 28, 2023
1 parent f0d146f commit 0967a5a
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 29 deletions.
14 changes: 13 additions & 1 deletion packages/sdk-ui-ts/src/denom/DenomClientAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { Token } from '@injectivelabs/token-metadata'
import {
TokenInfo,
TokenMeta,
getUnknownTokenWithSymbol,
getIbcTokenMetaFromDenomTrace,
} from '@injectivelabs/token-metadata'
import { getTokenFromAlchemyTokenMetaResponse } from '../utils/alchemy'
Expand Down Expand Up @@ -69,12 +70,23 @@ export class DenomClientAsync {
}

/**
* Used to get tracked tokens only (those in the token-metadata package)
* Used to get tracked tokens only
* (those in the token-metadata package)
*/
getDenomTokenStatic(denom: string): Token | undefined {
return this.denomClient.getDenomToken(denom)
}

/**
* Used to get tracked tokens only
* (those in the token-metadata package)
*/
getDenomTokenStaticOrUnknown(denom: string): Token {
const token = this.denomClient.getDenomToken(denom)

return token || getUnknownTokenWithSymbol(denom)
}

/**
* Used to get all tokens even if they are not tracked on the token-metadata package
* ERC20, CW20, IBC, etc
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-ui-ts/src/token/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import {
Token,
TokenType,
getUntrackedToken,
getUnknownTokenWithSymbol,
} from '@injectivelabs/token-metadata'
import { awaitForAll } from '@injectivelabs/utils'
import { DenomClientAsync } from '../denom/DenomClientAsync'
Expand Down Expand Up @@ -320,7 +320,7 @@ export class TokenService {

return {
...transaction,
token: getUntrackedToken(transaction.denom),
token: getUnknownTokenWithSymbol(transaction.denom),
} as UiBridgeTransactionWithToken
}

Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-ui-ts/src/utils/alchemy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Token } from '@injectivelabs/token-metadata'
import { TokenType } from '@injectivelabs/token-metadata'
import { TokenType, TokenVerification } from '@injectivelabs/token-metadata'
import { TokenMetadataResponse } from 'alchemy-sdk'

export const getKeyFromRpcUrl = (rpcUrl: string) => {
Expand All @@ -24,6 +24,7 @@ export const getTokenFromAlchemyTokenMetaResponse = (
logo: response.logo || 'untracked.svg',
coinGeckoId: '',
tokenType: TokenType.Erc20,
tokenVerification: TokenVerification.External,

erc20: {
decimals: response.decimals || 18,
Expand Down
9 changes: 7 additions & 2 deletions packages/sdk-ui-ts/src/utils/cw20.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ContractStateWithPagination } from '@injectivelabs/sdk-ts'
import { TokenType, type Token } from '@injectivelabs/token-metadata'
import {
TokenType,
TokenVerification,
type Token,
} from '@injectivelabs/token-metadata'

export const getTokenFromContractStateResponse = (
denom: string,
Expand All @@ -15,9 +19,10 @@ export const getTokenFromContractStateResponse = (
name: tokenInfo.name || contractInfo.label || 'Unknown',
symbol: tokenInfo.symbol || tokenInfo.name || 'Unknown',
decimals: tokenInfo.decimals || 18,
logo: 'untracked.svg',
logo: 'unknown.svg',
coinGeckoId: '',
tokenType: TokenType.Cw20,
tokenVerification: TokenVerification.Internal,

cw20: {
decimals: tokenInfo.decimals || 18,
Expand Down
18 changes: 11 additions & 7 deletions packages/sdk-ui-ts/src/utils/exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
GrpcOrderTypeMap,
} from '@injectivelabs/sdk-ts'
import { OrderSide } from '@injectivelabs/ts-types'
import { TokenType, type Token } from '@injectivelabs/token-metadata'
import {
TokenType,
TokenVerification,
type Token,
} from '@injectivelabs/token-metadata'

export const orderSideToOrderType = (orderType: OrderSide): GrpcOrderType => {
switch (orderType) {
Expand Down Expand Up @@ -40,14 +44,14 @@ export const orderSideToOrderType = (orderType: OrderSide): GrpcOrderType => {
export const getTokenFromInsuranceFund = (
denom: string,
response: InsuranceFund,
): Token => {
return {
): Token =>
({
denom,
name: (response.marketTicker || 'Unknown') + 'Insurance Fund',
symbol: denom || 'Unknown',
name: (response.marketTicker || denom) + ' Insurance Fund',
symbol: denom,
decimals: 18,
logo: 'injective-v3.svg',
coinGeckoId: '',
tokenType: TokenType.InsuranceFund,
} as Token
}
tokenVerification: TokenVerification.Verified,
} as Token)
13 changes: 9 additions & 4 deletions packages/sdk-ui-ts/src/utils/factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Metadata } from '@injectivelabs/sdk-ts'
import { TokenType, type Token } from '@injectivelabs/token-metadata'
import {
TokenType,
TokenVerification,
type Token,
} from '@injectivelabs/token-metadata'

export const getTokenFromDenomsMetadata = (
denom: string,
Expand All @@ -9,11 +13,12 @@ export const getTokenFromDenomsMetadata = (

return {
denom,
name: response.name || 'Unknown',
name: response.name || response.symbol || denom,
symbol: response.symbol || response.name || 'Unknown',
decimals: denomUnit.exponent || 18,
logo: 'injective-v3.svg',
decimals: denomUnit.exponent || 0,
logo: 'unknown.svg',
coinGeckoId: '',
tokenType: TokenType.TokenFactory,
tokenVerification: TokenVerification.Internal,
} as Token
}
20 changes: 17 additions & 3 deletions packages/token-metadata/src/TokenMetaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { getMappedTokensByName } from './tokens/mappings/mapByName'
import { getMappedTokensByHash } from './tokens/mappings/mapByHash'
import { getMappedTokensBySymbol } from './tokens/mappings/mapBySymbol'
import { TokenMeta, TokenType } from './types'
import { TokenMeta, TokenVerification, TokenType } from './types'

export class TokenMetaUtils {
protected tokens: Record<string, TokenMeta>
Expand Down Expand Up @@ -40,7 +40,12 @@ export class TokenMetaUtils {
return
}

return tokensBySymbol[tokenSymbol] || tokensBySymbol[symbol]
const tokenMeta = tokensBySymbol[tokenSymbol] || tokensBySymbol[symbol]

return {
...tokenMeta,
tokenVerification: TokenVerification.Verified,
}
}

getMetaByAddress(address: string): TokenMeta | undefined {
Expand Down Expand Up @@ -68,6 +73,7 @@ export class TokenMetaUtils {
? {
...tokenMeta,
tokenType: TokenType.Cw20,
tokenVerification: TokenVerification.Verified,
}
: undefined
}
Expand All @@ -93,6 +99,7 @@ export class TokenMetaUtils {
return {
...tokenMeta,
tokenType: tokenMeta.erc20 ? TokenType.Erc20 : TokenType.Evm,
tokenVerification: TokenVerification.Verified,
}
}

Expand All @@ -106,6 +113,7 @@ export class TokenMetaUtils {
? {
...tokenMeta,
tokenType: tokenMeta.erc20 ? TokenType.Erc20 : TokenType.Evm,
tokenVerification: TokenVerification.Verified,
}
: undefined
}
Expand All @@ -126,6 +134,7 @@ export class TokenMetaUtils {
? {
...tokenMeta,
tokenType: TokenType.Ibc,
tokenVerification: TokenVerification.Verified,
}
: undefined
}
Expand All @@ -138,7 +147,12 @@ export class TokenMetaUtils {
return
}

return tokensByName[tokenName] || tokensByName[name]
const tokenMeta = tokensByName[tokenName] || tokensByName[name]

return {
...tokenMeta,
tokenVerification: TokenVerification.Verified,
}
}

getCoinGeckoIdFromSymbol(symbol: string): string {
Expand Down
5 changes: 4 additions & 1 deletion packages/token-metadata/src/images/unknown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 9 additions & 2 deletions packages/token-metadata/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ export enum TokenType {
Native = 'native',
TokenFactory = 'tokenFactory',
InsuranceFund = 'insuranceFund',
Untracked = 'untracked',
Unverified = 'unverified',
Unknown = 'unknown',
}

export enum TokenVerification {
Verified = 'verified' /** verified on token-metadata package */,
Internal = 'internal' /** verified from on-chain data */,
External = 'external' /** verified on external source */,
Untracked = 'untracked' /** unverified on any source */,
}

export enum Cw20TokenSource {
Expand Down Expand Up @@ -70,6 +76,7 @@ export interface TokenMeta {
symbol: string
decimals: number
tokenType?: TokenType
tokenVerification?: TokenVerification
coinGeckoId: string

ibc?: IbcTokenMeta
Expand Down
15 changes: 9 additions & 6 deletions packages/token-metadata/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
Token,
TokenMeta,
TokenType,
IbcTokenMeta,
Cw20TokenMeta,
Cw20TokenSingle,
Cw20TokenSource,
Cw20TokenMeta,
TokenVerification,
Cw20TokenMetaWithSource,
IbcTokenMeta,
} from './types'
import { ibcBaseDenoms } from './tokens/tokens'
import { getChannelIdFromPath } from './ibc'
Expand Down Expand Up @@ -262,26 +263,28 @@ export const getTokenFromMeta = (meta: TokenMeta, denom?: string): Token => {
return tokenWithDecimalsAndSymbol
}

export const getUntrackedToken = (denom: string): Token => {
export const getUnknownToken = (denom: string): Token => {
return {
denom,
name: denom,
symbol: denom,
decimals: 18,
logo: 'untracked.svg',
coinGeckoId: '',
tokenType: TokenType.Untracked,
tokenType: TokenType.Unknown,
tokenVerification: TokenVerification.Untracked,
} as Token
}

export const getUntrackedTokenWithSymbol = (denom: string): Token => {
export const getUnknownTokenWithSymbol = (denom: string): Token => {
return {
denom,
name: denom,
symbol: 'UNTRACKED',
decimals: 0,
logo: 'untracked.svg',
coinGeckoId: '',
tokenType: TokenType.Untracked,
tokenType: TokenType.Unknown,
tokenVerification: TokenVerification.Untracked,
} as Token
}

0 comments on commit 0967a5a

Please sign in to comment.