Skip to content

Commit

Permalink
1026 Thorchain util functions (#1027)
Browse files Browse the repository at this point in the history
* Util functions

* Changeset version file

* Typo
  • Loading branch information
0xp3gasus authored Feb 2, 2024
1 parent ae35c36 commit e1ec010
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-balloons-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-thorchain': patch
---

getDenom, getChainId and getPrefix util functions
1 change: 1 addition & 0 deletions packages/xchain-thorchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@xchainjs/xchain-client": "0.16.1",
"@xchainjs/xchain-cosmos-sdk": "0.2.1",
"@xchainjs/xchain-util": "0.13.2",
"axios": "1.3.6",
"bignumber.js": "9.0.0",
"protobufjs": "6.11.4"
},
Expand Down
17 changes: 4 additions & 13 deletions packages/xchain-thorchain/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
bech32ToBase64,
makeClientPath,
} from '@xchainjs/xchain-cosmos-sdk'
import { Address, Asset, assetFromString, assetToString, isSynthAsset } from '@xchainjs/xchain-util'
import { Address, Asset, assetFromString } from '@xchainjs/xchain-util'
import { BigNumber } from 'bignumber.js'
import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'

Expand All @@ -32,7 +32,7 @@ import {
defaultClientConfig,
} from './const'
import { DepositParam, DepositTx, TxOfflineParams } from './types'
import { getDefaultExplorers, getExplorerAddressUrl, getExplorerTxUrl, isAssetRuneNative as isAssetRune } from './utils'
import { getDefaultExplorers, getDenom, getExplorerAddressUrl, getExplorerTxUrl, getPrefix } from './utils'

/**
* Interface for custom Thorchain client
Expand Down Expand Up @@ -70,14 +70,7 @@ export class Client extends CosmosSDKClient implements ThorchainClient {
* @returns the address prefix
*/
protected getPrefix(network: Network): string {
switch (network) {
case Network.Mainnet:
return 'thor'
case Network.Stagenet:
return 'sthor'
case Network.Testnet:
return 'tthor'
}
return getPrefix(network)
}

/**
Expand Down Expand Up @@ -139,9 +132,7 @@ export class Client extends CosmosSDKClient implements ThorchainClient {
* @returns {string} The denomination of the given asset.
*/
public getDenom(asset: Asset): string | null {
if (isAssetRune(asset)) return RUNE_DENOM
if (isSynthAsset(asset)) return assetToString(asset).toLowerCase()
return asset.symbol.toLowerCase()
return getDenom(asset)
}

/**
Expand Down
50 changes: 48 additions & 2 deletions packages/xchain-thorchain/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Network, RootDerivationPaths, TxHash } from '@xchainjs/xchain-client'
import { Address, Asset, assetToString } from '@xchainjs/xchain-util'
import { Address, Asset, assetToString, isSynthAsset } from '@xchainjs/xchain-util'
import axios from 'axios'

import { AssetRuneNative as AssetRUNE, DEFAULT_EXPLORER_URL } from './const'
import { AssetRuneNative as AssetRUNE, DEFAULT_EXPLORER_URL, RUNE_DENOM } from './const'

export const getDefaultClientUrls = (): Record<Network, string> => {
return {
Expand Down Expand Up @@ -41,3 +42,48 @@ export const getExplorerTxUrl = (tx: TxHash): Record<Network, string> => ({
* @returns {boolean} `true` or `false`
*/
export const isAssetRuneNative = (asset: Asset): boolean => assetToString(asset) === assetToString(AssetRUNE)

/**
* Get denomination from Asset
*
* @param {Asset} asset
* @returns {string} The denomination of the given asset.
*/
export const getDenom = (asset: Asset) => {
if (isAssetRuneNative(asset)) return RUNE_DENOM
if (isSynthAsset(asset)) return assetToString(asset).toLowerCase()
return asset.symbol.toLowerCase()
}

/**
* Get chain id from node url
*
* @param {string} nodeUrl
* @returns the chainId
*/
export const getChainId = async (nodeUrl: string): Promise<string> => {
/**
* TODO: To avoid axios dependency. ChainId can be retrieved from the Stargate client, to achieve this,
* getChainId has to work with JSON-RPC endpoint instead of REST endpoints
*/
const { data } = await axios.get<{ default_node_info?: { network?: string } }>(
`${nodeUrl}/cosmos/base/tendermint/v1beta1/node_info`,
)
return data?.default_node_info?.network || Promise.reject('Could not parse chain id')
}

/**
* Get address prefix by network
* @param {Network} network The network of which return the prefix
* @returns the address prefix
*/
export const getPrefix = (network: Network): string => {
switch (network) {
case Network.Mainnet:
return 'thor'
case Network.Stagenet:
return 'sthor'
case Network.Testnet:
return 'tthor'
}
}
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3578,6 +3578,15 @@ axios@0.27.2:
follow-redirects "^1.14.9"
form-data "^4.0.0"

axios@1.3.6, axios@^1.0.0, axios@^1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.6.tgz#1ace9a9fb994314b5f6327960918406fa92c6646"
integrity sha512-PEcdkk7JcdPiMDkvM4K6ZBRYq9keuVJsToxm2zQIM70Qqo2WHTdJZMXcG9X+RmRp2VPNUQC8W1RAGbgt6b1yMg==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axios@^0.19.0:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
Expand All @@ -3599,15 +3608,6 @@ axios@^0.23.0:
dependencies:
follow-redirects "^1.14.4"

axios@^1.0.0, axios@^1.3.6:
version "1.3.6"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.3.6.tgz#1ace9a9fb994314b5f6327960918406fa92c6646"
integrity sha512-PEcdkk7JcdPiMDkvM4K6ZBRYq9keuVJsToxm2zQIM70Qqo2WHTdJZMXcG9X+RmRp2VPNUQC8W1RAGbgt6b1yMg==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

b4a@^1.6.0:
version "1.6.3"
resolved "https://registry.yarnpkg.com/b4a/-/b4a-1.6.3.tgz#cce6e8a2f0d0774e5fe8062086827e979970266d"
Expand Down

0 comments on commit e1ec010

Please sign in to comment.