Skip to content

Commit

Permalink
Merge branch 'main' into add/hyperliquid-evm
Browse files Browse the repository at this point in the history
  • Loading branch information
jayeshbhole-rp committed Jan 3, 2025
2 parents 6ed8a7b + 66859c0 commit 83beaca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
6 changes: 6 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

- feat: add hyperliquid

## 1.15.15

### Patch Changes

- fix tron switch network call

## 1.15.14

### Patch Changes
Expand Down
32 changes: 19 additions & 13 deletions packages/react/src/actions/cosmos/getCosmosToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate';
import { maxInt256 } from 'viem';
import { RouterLCDBalancesResponse } from '../../types/cosmos.js';
import { ChainData, ConnectionOrConfig, CosmsosChainType } from '../../types/index.js';
import { areTokensEqual, formatTokenAddress, isNativeOrFactoryToken } from '../../utils/index.js';

Expand Down Expand Up @@ -45,9 +46,14 @@ export const getCosmosTokenBalanceAndAllowance = async ({
allowance: bigint;
}> => {
if (chain.id === 'router_9600-1') {
const { getNetworkInfo, ChainGrpcWasmApi, ChainGrpcBankApi, getRouterSignerAddress, toUtf8 } = await import(
'@routerprotocol/router-chain-sdk-ts'
);
const {
getNetworkInfo,
getEndpointsForNetwork,
getNetworkType,
restFetcher,
ChainGrpcBankApi,
getRouterSignerAddress,
} = await import('@routerprotocol/router-chain-sdk-ts');

const network = getNetworkInfo(chain.extra?.environment);
if (token === 'route') {
Expand All @@ -68,24 +74,24 @@ export const getCosmosTokenBalanceAndAllowance = async ({
allowance: maxInt256,
};
} else {
const wasmClient = new ChainGrpcWasmApi(network.grpcEndpoint);

const networkEnvironment = chain.id === 'router_9600-1' ? 'mainnet' : 'testnet';
const lcdEndpoint = getEndpointsForNetwork(getNetworkType(networkEnvironment)).lcdEndpoint;
const address = getRouterSignerAddress(account);
if (!address) {
throw new Error(`Invalid address to convert to Router: ${account}`);
}

const balance = await wasmClient.fetchSmartContractState(
token,
toUtf8(
JSON.stringify({
balance: { address },
}),
),
const balancesData: RouterLCDBalancesResponse = await restFetcher(
`${lcdEndpoint}/cosmos/bank/v1beta1/balances/${address}?pagination.limit=1000`,
);
const tokenBalance = balancesData.balances.find((balance) => balance.denom === token);

if (!tokenBalance) {
throw new Error(`Failed to fetch balance for token: ${token}`);
}

return {
balance: BigInt(balance.data.balance),
balance: BigInt(tokenBalance.amount),
allowance: maxInt256,
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/hooks/useNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ export const useNetwork = () => {
return chains.find((chain) => chain.id === switchedChain.id);
}
if (chain.type === 'tron') {
// convert chain id
try {
await (connector as WalletInstance<'tron'>).switchChain(chain.tronName);
await (connector as WalletInstance<'tron'>).switchChain(chain.trxId);
} catch (e) {
if (e === WalletSwitchChainError) {
console.error('Switch chain is not supported');
Expand Down
9 changes: 9 additions & 0 deletions packages/react/src/types/cosmos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Coin } from '@routerprotocol/router-chain-sdk-ts';

export type RouterLCDBalancesResponse = {
balances: Coin[];
pagination: {
next_string: string | null;
total: string;
};
};

0 comments on commit 83beaca

Please sign in to comment.