Skip to content

Commit

Permalink
fix(useSendTransaction) current account comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
jayeshbhole-rp committed Jan 10, 2025
1 parent 2748298 commit 6292bfa
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 15 deletions.
6 changes: 6 additions & 0 deletions packages/react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @tangled3/react

## 1.16.26

### Patch Changes

- fix(useSendTransaction) current account comparison

## 1.16.25

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tangled3/react",
"private": false,
"version": "1.16.25",
"version": "1.16.26",
"type": "module",
"license": "MIT",
"main": "./src/index.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/actions/cosmos/getCosmosToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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';
import { compareStrings, formatTokenAddress, isNativeOrFactoryToken } from '../../utils/index.js';

export const getCosmosTokenMetadata = async ({
token,
Expand All @@ -17,7 +17,7 @@ export const getCosmosTokenMetadata = async ({

const assetList = registryClient.getChainAssetList(chain.chainName).assets;

const asset = assetList.find((asset) => areTokensEqual(asset.base, token));
const asset = assetList.find((asset) => compareStrings(asset.base, token));

if (!asset) {
throw new Error('Token not found');
Expand Down
20 changes: 10 additions & 10 deletions packages/react/src/actions/getToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { trc20Abi } from '../constants/abi/trc20.js';
import { ETH_ADDRESS, SOL_ADDRESS, SUI_ADDRESS } from '../constants/index.js';
import { TokenMetadata } from '../hooks/useToken.js';
import { ChainData, ChainId, ChainType, ConnectionOrConfig, GetTokenMetadataParams } from '../types/index.js';
import { areTokensEqual } from '../utils/index.js';
import { compareStrings } from '../utils/index.js';
import { getFormattedBalance as getBitcoinBalance } from './bitcoin/balance.js';
import { getCosmosTokenBalanceAndAllowance, getCosmosTokenMetadata } from './cosmos/getCosmosToken.js';
import { getEVMTokenBalanceAndAllowance, getEVMTokenMetadata } from './evm/getEVMToken.js';
Expand All @@ -22,7 +22,7 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
// console.log('[Token] configs', { token, chain, config });
// evm chain
if (chain?.type === 'evm') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
return {
name: chain.nativeCurrency.name,
symbol: chain.nativeCurrency.symbol,
Expand All @@ -38,7 +38,7 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
}

if (chain.type === 'tron') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
return { ...chain.nativeCurrency, address: ETH_ADDRESS, chainId: chain.id, isNative: true };
}
let contractAddress: string = token;
Expand All @@ -59,7 +59,7 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
}

if (chain.type === 'solana') {
if (areTokensEqual(token, SOL_ADDRESS)) {
if (compareStrings(token, SOL_ADDRESS)) {
return { ...chain.nativeCurrency, address: SOL_ADDRESS, chainId: chain.id, isNative: true };
}

Expand All @@ -83,7 +83,7 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
}

if (chain.type === 'cosmos') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
return { ...chain.nativeCurrency, address: ETH_ADDRESS, chainId: chain.id, isNative: true };
}
const res = await getCosmosTokenMetadata({ token, chain, getCosmosClient: config.getCosmosClient });
Expand All @@ -96,13 +96,13 @@ export const getTokenMetadata = async ({ token, chain, config }: GetTokenMetadat
}

if (chain.type === 'bitcoin') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
return { ...chain.nativeCurrency, address: ETH_ADDRESS, chainId: chain.id, isNative: true };
}
}

if (chain.type === 'sui') {
if (areTokensEqual(token, SUI_ADDRESS)) {
if (compareStrings(token, SUI_ADDRESS)) {
return { ...chain.nativeCurrency, address: SUI_ADDRESS, chainId: chain.id, isNative: true };
}

Expand Down Expand Up @@ -167,7 +167,7 @@ export const getTokenBalanceAndAllowance = (async (params) => {

// evm chain
if (chain.type === 'evm') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
const result = {
balance: await getBalance(config.wagmiConfig, {
address: account as EVMAddress,
Expand All @@ -181,7 +181,7 @@ export const getTokenBalanceAndAllowance = (async (params) => {
}

if (chain.type === 'tron') {
if (areTokensEqual(token, ETH_ADDRESS)) {
if (compareStrings(token, ETH_ADDRESS)) {
const balance = BigInt(await config.tronWeb.trx.getBalance(account));
const allowance = BigInt(0);
return { balance, allowance };
Expand All @@ -203,7 +203,7 @@ export const getTokenBalanceAndAllowance = (async (params) => {
const accountPbKey = new PublicKey(account);

// if asset is native solana token
if (areTokensEqual(token, SOL_ADDRESS)) {
if (compareStrings(token, SOL_ADDRESS)) {
const balance = BigInt(await config.solanaConnection.getBalance(accountPbKey));
return { balance, allowance: 0n };
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/hooks/useSendTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation } from '@tanstack/react-query';
import { SendTransactionParams, sendTransactionToChain } from '../actions/sendTransaction.js';
import { ChainData, ChainId } from '../types/index.js';
import { compareStrings } from '../utils/index.js';
import { useConnectionOrConfig } from './useConnectionOrConfig.js';
import { useCurrentAccount } from './useCurrentAccount.js';
import { useCurrentWallet } from './useCurrentWallet.js';
Expand Down Expand Up @@ -50,7 +51,7 @@ export const useSendTransaction = () => {
}

// check if from address matches currentAccount
if (from !== currentAccount.address) {
if (!compareStrings(from, currentAccount.address)) {
throw new Error('From address does not match current account');
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const isMobile = () => {
* @param tokenB The second token.
* @returns True if the two tokens are equal.
*/
export const areTokensEqual = (tokenA: string | undefined, tokenB: string | undefined): boolean => {
export const compareStrings = (tokenA: string | undefined, tokenB: string | undefined): boolean => {
// Early return if either token is undefined
if (!tokenA || !tokenB) return false;

Expand Down

0 comments on commit 6292bfa

Please sign in to comment.