Skip to content

Commit

Permalink
feat: cw20 addr validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Sep 22, 2023
1 parent d7f79d8 commit c4332e0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions packages/sdk-ts/src/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ export const getChecksumAddress = (ethAddress: string) => {

return checksumAddress
}

export const isCw20ContractAddress = (address: string) =>
address.length === 42 && address.startsWith('inj')
13 changes: 7 additions & 6 deletions packages/sdk-ui-ts/src/denom/DenomClientAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
fromUtf8,
DenomClient,
InsuranceFund,
ChainGrpcIbcApi,
ChainGrpcBankApi,
ChainGrpcWasmApi,
isCw20ContractAddress,
ChainGrpcInsuranceFundApi,
ChainGrpcIbcApi,
} from '@injectivelabs/sdk-ts'
import { Web3Client } from '../services/web3/Web3Client'
import type { Token } from '@injectivelabs/token-metadata'
Expand Down Expand Up @@ -116,9 +117,7 @@ export class DenomClientAsync {
return getTokenFromAlchemyTokenMetaResponse(denom, response)
}

const isCW20 = denom.startsWith('inj')

if (isCW20) {
if (isCw20ContractAddress(denom)) {
const contractAddress = denom

const response = await this.chainWasmApi.fetchContractState({
Expand All @@ -137,7 +136,7 @@ export class DenomClientAsync {
const tokenFactoryAddress = denom.split('/')[2]

// CW20 contract (ex: from Wormhole)
if (tokenFactoryAddress.startsWith('inj')) {
if (isCw20ContractAddress(tokenFactoryAddress)) {
const response = await this.chainWasmApi.fetchContractState({
contractAddress: tokenFactoryAddress,
pagination: {
Expand Down Expand Up @@ -218,7 +217,9 @@ export class DenomClientAsync {
return this.metadatas.find((metadata) => metadata.base === denom)
}

const { metadatas } = await this.chainBankApi.fetchDenomsMetadata({ limit: 1000 })
const { metadatas } = await this.chainBankApi.fetchDenomsMetadata({
limit: 1000,
})

this.metadatas = metadatas

Expand Down
6 changes: 3 additions & 3 deletions packages/token-metadata/src/TokenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from './tokens/network'
import { Token, TokenMeta, TokenType } from './types'
import tokensBySymbol from './tokens/tokens'
import { getTokenFromMeta } from './utils'
import { getTokenFromMeta, isCw20ContractAddress } from './utils'

export class TokenFactory {
public tokenMetaUtils: TokenMetaUtils
Expand Down Expand Up @@ -129,7 +129,7 @@ export class TokenFactory {
}

getCw20DenomTokenMeta(address: string): TokenMeta | undefined {
if (!address.startsWith('inj')) {
if (!isCw20ContractAddress(address)) {
throw new GeneralException(
new Error(`The address ${address} is not a valid CW20 address`),
)
Expand All @@ -151,7 +151,7 @@ export class TokenFactory {
)
}

if (address.startsWith('inj') && address.length === 42) {
if (isCw20ContractAddress(address)) {
const tokenMeta = this.tokenMetaUtils.getMetaByAddress(address)

return tokenMeta
Expand Down
7 changes: 5 additions & 2 deletions packages/token-metadata/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const getTokenTypeFromDenom = (denom: string) => {
return TokenType.Native
}

if (denom.startsWith('inj')) {
if (isCw20ContractAddress(denom)) {
return TokenType.Cw20
}

Expand Down Expand Up @@ -94,7 +94,7 @@ export const getTokenDecimals = (token: Token) => {
return token.decimals
}

if (token.denom.startsWith('inj')) {
if (isCw20ContractAddress(token.denom)) {
return token.cw20?.decimals || token.decimals
}

Expand Down Expand Up @@ -288,3 +288,6 @@ export const getUnknownTokenWithSymbol = (denom: string): Token => {
tokenVerification: TokenVerification.Unverified,
} as Token
}

export const isCw20ContractAddress = (address: string) =>
address.length === 42 && address.startsWith('inj')

0 comments on commit c4332e0

Please sign in to comment.