Skip to content

Commit

Permalink
feat: added bonfida sol domains
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Feb 14, 2024
1 parent 861ee85 commit 2e67999
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 71 deletions.
1 change: 1 addition & 0 deletions packages/exceptions/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const isCommonLockedError = (error: string) => {
'Failed to open the device',
'Failed to open the device',
'Ledger Device is busy',
'Ledger device',
'UNKNOWN_ERROR',
]

Expand Down
4 changes: 0 additions & 4 deletions packages/sdk-ts/src/core/utils/Denom/DenomClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export class DenomClient {
return this.tokenMetaUtils.getMetaByAddress(address)
}

getTokenMetaDataByName(name: string): TokenMetaBase | undefined {
return this.tokenMetaUtils.getMetaByName(name)
}

getCoinGeckoId(denom: string): string {
return this.tokenMetaUtils.getCoinGeckoIdFromSymbol(denom)
}
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk-ts/src/core/utils/Denom/DenomClientSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export class DenomClientSync {
return this.tokenMetaUtils.getMetaByAddress(address)
}

getTokenMetaDataByName(name: string): TokenMetaBase | undefined {
return this.tokenMetaUtils.getMetaByName(name)
}

getCoinGeckoId(denom: string): string {
return this.tokenMetaUtils.getCoinGeckoIdFromSymbol(denom)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-ui-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"scripts": {
"postinstall": "link-module-alias",
"build": "tsc --build tsconfig.build.json && tsc --build tsconfig.build.esm.json && yarn build:post && link-module-alias && yarn copy-files && yarn get-ibc-tokens",
"build": "tsc --build tsconfig.build.json && tsc --build tsconfig.build.esm.json && yarn build:post && link-module-alias && yarn copy-files",
"build:watch": "tsc --build -w tsconfig.build.json && tsc -w --build tsconfig.build.esm.json && yarn build:post && link-module-alias",
"build:post": "shx cp ../../etc/stub/package.json.stub dist/cjs/package.json && shx cp ../../etc/stub/package.esm.json.stub dist/esm/package.json",
"clean": "tsc --build tsconfig.build.json --clean && tsc --build tsconfig.build.esm.json --clean && shx rm -rf coverage *.log junit.xml dist && jest --clearCache && shx mkdir -p dist",
Expand Down
4 changes: 0 additions & 4 deletions packages/sdk-ui-ts/src/denom/DenomClientAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,6 @@ export class DenomClientAsync {
return this.denomClient.getTokenMetaDataByAddress(address)
}

getTokenMetaDataByName(name: string): TokenMetaBase | undefined {
return this.denomClient.getTokenMetaDataByName(name)
}

getCoinGeckoId(denom: string): string {
return this.denomClient.getCoinGeckoId(denom)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { InjBonfidaNameService } from './InjBonfidaNameService'

describe('InjBonfidaNameService', () => {
test('fetchInjAddress', () => {
const service = new InjBonfidaNameService()
const name = 'randomname.sol'
const result = service.fetchInjAddress(name)

expect(result).toBeDefined()
expect(result).toEqual('inj1...')
})
})
47 changes: 47 additions & 0 deletions packages/sdk-ui-ts/src/services/bonfida/InjBonfidaNameService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { GeneralException } from '@injectivelabs/exceptions'
import {
Network,
NetworkEndpoints,
getNetworkEndpoints,
} from '@injectivelabs/networks'
import {
toBase64,
fromBase64,
ChainGrpcWasmApi,
binaryToBase64,
} from '@injectivelabs/sdk-ts'
import { getBonfidaContractAddress } from './utils'

export class InjBonfidaNameService {
protected client: ChainGrpcWasmApi

private contractAddress: string

constructor(
network: Network = Network.Testnet,
endpoints?: NetworkEndpoints,
) {
const networkEndpoints = endpoints || getNetworkEndpoints(network)

this.client = new ChainGrpcWasmApi(networkEndpoints.grpc)
this.contractAddress = getBonfidaContractAddress(network)
}

async fetchInjAddress(name: string) {
const query = {
resolve: {
domain_name: name,
},
}
const response = await this.client.fetchSmartContractState(
this.contractAddress,
toBase64(query),
)

return fromBase64(binaryToBase64(response.data))
}

async fetchInjName(_address: string) {
throw new GeneralException(new Error(`Not suported for this name service`))
}
}
1 change: 1 addition & 0 deletions packages/sdk-ui-ts/src/services/bonfida/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './InjBonfidaNameService'
9 changes: 9 additions & 0 deletions packages/sdk-ui-ts/src/services/bonfida/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Network, isTestnet } from '@injectivelabs/networks'

export function getBonfidaContractAddress(network: Network): string {
if (isTestnet(network)) {
return 'inj1q79ujqyh72p43mhr2ldaly3x6d50rzp3354at3'
}

return 'inj1v7chmgm7vmuwldjt80utmw9c95jkrch979ps8z'
}
1 change: 1 addition & 0 deletions packages/sdk-ui-ts/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './web3'
export * from './gas'
export * from './nameservice'
export * from './bonfida'
6 changes: 0 additions & 6 deletions packages/token-metadata/src/TokenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ export class TokenFactory {
return getTokenFromMeta(byAddress, denom)
}

const byName = this.tokenMetaUtils.getMetaByName(denom)

if (byName) {
return getTokenFromMeta(byName, denom)
}

return
}

Expand Down
25 changes: 2 additions & 23 deletions packages/token-metadata/src/TokenMetaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
getMappedTokensByErc20Address,
getMappedTokensByCw20Address,
} from './tokens/mappings/mapByAddress'
import { getMappedTokensByName } from './tokens/mappings/mapByName'
import { getMappedTokensByHash } from './tokens/mappings/mapByHash'
import { getMappedTokensBySymbol } from './tokens/mappings/mapBySymbol'
import { TokenMetaBase, TokenVerification, TokenType } from './types'
Expand All @@ -16,14 +15,11 @@ export class TokenMetaUtils {

protected tokensByHash: Record<string, TokenMetaBase>

protected tokensByName: Record<string, TokenMetaBase>

constructor(tokens: Record<string, TokenMetaBase>) {
this.tokens = getMappedTokensBySymbol(tokens)
this.tokensByErc20Address = getMappedTokensByErc20Address(this.tokens)
this.tokensByCw20Address = getMappedTokensByCw20Address(this.tokens)
this.tokensByHash = getMappedTokensByHash(this.tokens)
this.tokensByName = getMappedTokensByName(this.tokens)
}

/**
Expand All @@ -49,9 +45,8 @@ export class TokenMetaUtils {
}

getMetaByFactory(denom: string): TokenMetaBase | undefined {
const [symbolOrName, creatorAddress] = denom.split('/').reverse()
const tokenMeta =
this.getMetaByName(symbolOrName) || this.getMetaBySymbol(symbolOrName)
const [symbol, creatorAddress] = denom.split('/').reverse()
const tokenMeta = this.getMetaBySymbol(symbol)

if (!tokenMeta) {
return
Expand Down Expand Up @@ -185,22 +180,6 @@ export class TokenMetaUtils {
: undefined
}

getMetaByName(name: string): TokenMetaBase | undefined {
const { tokensByName } = this
const tokenName = name.toLowerCase() as keyof typeof tokensByName

if (!tokensByName[tokenName] && !tokensByName[name]) {
return
}

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

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

getCoinGeckoIdFromSymbol(symbol: string): string {
const { tokens: tokensBySymbol } = this
const symbolToUppercase =
Expand Down
1 change: 0 additions & 1 deletion packages/token-metadata/src/tokens/mappings/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './mapByAddress'
export * from './mapByHash'
export * from './mapByName'
export * from './mapBySymbol'
10 changes: 0 additions & 10 deletions packages/token-metadata/src/tokens/mappings/mapByName.ts

This file was deleted.

37 changes: 19 additions & 18 deletions packages/token-metadata/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ export const getPeggyDenomFromSymbolOrName = (
) => {
const tokenMetaUtils = TokenMetaUtilsFactory.make(network)
const metaFromSymbol = tokenMetaUtils.getMetaBySymbol(symbolOrName)
const metaFromName = tokenMetaUtils.getMetaByName(symbolOrName)

if (!metaFromSymbol && !metaFromName) {
if (!metaFromSymbol) {
return
}

if (!metaFromSymbol?.erc20 && !metaFromName?.erc20) {
if (!metaFromSymbol?.erc20) {
return
}

return `peggy${(metaFromSymbol || metaFromName)?.erc20?.address}`
return `peggy${metaFromSymbol?.erc20?.address}`
}

export const getIbcDenomFromSymbolOrName = (
Expand All @@ -52,26 +51,27 @@ export const getIbcDenomFromSymbolOrName = (
source?: TokenSource,
) => {
const tokenMetaUtils = TokenMetaUtilsFactory.make(network)
const metaFromName = tokenMetaUtils.getMetaBySymbol(symbolOrName)
const metaFromSymbol = tokenMetaUtils.getMetaByName(symbolOrName)
const metaFromSymbol = tokenMetaUtils.getMetaBySymbol(symbolOrName)

if (!metaFromSymbol && !metaFromName) {
if (!metaFromSymbol) {
return
}

if (!metaFromSymbol?.ibcs && !metaFromName?.ibcs) {
if (!metaFromSymbol?.ibcs) {
return
}

const meta = (metaFromSymbol || metaFromName)!
const meta = metaFromSymbol

if (source) {
if (source) {
const ibcHash = meta?.ibcs?.find((ibc) => ibc.source === source)?.hash

return `ibc/${ibcHash}`
}

const defaultIbcHash = meta.ibcs?.find((ibc) => ibc.source === TokenSource.Cosmos)?.hash
const defaultIbcHash = meta.ibcs?.find(
(ibc) => ibc.source === TokenSource.Cosmos,
)?.hash
const [ibc] = meta.ibcs || []
const ibcHash = defaultIbcHash || ibc?.hash

Expand All @@ -84,18 +84,17 @@ export const getCw20FromSymbolOrName = (
source?: TokenSource,
) => {
const tokenMetaUtils = TokenMetaUtilsFactory.make(network)
const metaFromName = tokenMetaUtils.getMetaBySymbol(symbolOrName)
const metaFromSymbol = tokenMetaUtils.getMetaByName(symbolOrName)
const metaFromSymbol = tokenMetaUtils.getMetaBySymbol(symbolOrName)

if (!metaFromSymbol && !metaFromName) {
if (!metaFromSymbol) {
return
}

if (!metaFromSymbol?.cw20s && !metaFromName?.cw20s) {
if (!metaFromSymbol?.cw20s) {
return
}

const meta = (metaFromName || metaFromSymbol)!
const meta = metaFromSymbol

if (source) {
const cw20 = meta?.cw20s?.find((cw20) => cw20.source === source)
Expand Down Expand Up @@ -128,9 +127,11 @@ export const getIbcMeta = (
const ibcMetaFromIbcs = token.ibcs?.find((meta) =>
denomToLowerCase.includes(meta.hash.toLowerCase()),
)
const defaultIbcMeta = token.ibcs?.find((meta) => meta.source === TokenSource.Cosmos)
const defaultIbcMeta = token.ibcs?.find(
(meta) => meta.source === TokenSource.Cosmos,
)

return ibcMetaFromIbcs || defaultIbcMeta || token.ibcs?.[0] || undefined
return ibcMetaFromIbcs || defaultIbcMeta || token.ibcs?.[0] || undefined
}

export const getNativeTokenFactoryMeta = (
Expand Down

0 comments on commit 2e67999

Please sign in to comment.