Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bridge-ui-v2): fixing getFilterLogs errors #15468

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions packages/bridge-ui-v2/src/components/Bridge/Amount.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { getLogger } from '$libs/util/logger';
import { uid } from '$libs/util/uid';
import { account } from '$stores/account';
import { ethBalance } from '$stores/balance';
import { network } from '$stores/network';

import {
Expand Down Expand Up @@ -59,6 +60,8 @@

const to = $recipientAddress || $account?.address;

let balanceForGasCalculation = $ethBalance;

// We need all these guys to validate
if (
!to ||
Expand All @@ -67,17 +70,19 @@
!$destNetwork ||
!$tokenBalance ||
!$selectedToken ||
!(balanceForGasCalculation && balanceForGasCalculation > BigInt(0)) ||
$enteredAmount === BigInt(0) // no need to check if the amount is 0
)
) {
return;
}

try {
await checkBalanceToBridge({
to,
token,
amount: $enteredAmount,
fee,
balance,
balance: balanceForGasCalculation,
srcChainId: $network.id,
destChainId: $destNetwork.id,
tokenIds:
Expand Down Expand Up @@ -192,6 +197,8 @@

computingMaxAmount = true;

const balance = determineBalance();

try {
let maxAmount;

Expand Down Expand Up @@ -228,15 +235,27 @@
}
}

$: balance = $tokenBalance
? typeof $tokenBalance === 'bigint'
? $tokenBalance > BigInt(0)
? $tokenBalance
: BigInt(0) // ERC721/1155
: 'value' in $tokenBalance && $tokenBalance.value > BigInt(0)
? $tokenBalance.value
: BigInt(0) // ERC20
: BigInt(0);
const determineBalance = () => {
let balance = 0n;
if (!$selectedToken) return balance;
const type = $selectedToken.type;
switch (type) {
case TokenType.ERC20:
if (typeof $tokenBalance === 'bigint') break;
if ($tokenBalance?.value) balance = $tokenBalance.value;
break;
case TokenType.ETH:
balance = $ethBalance;
break;
case TokenType.ERC721:
case TokenType.ERC1155:
if (typeof $tokenBalance === 'bigint') balance = $tokenBalance;
break;
default:
break;
}
return balance;
};

$: if (inputBox && sanitizedValue !== inputBox.getValue()) {
inputBox.setValue(sanitizedValue); // Update InputBox value if sanitizedValue changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
$: isERC1155 = $selectedNFTs ? $selectedNFTs.some((nft) => nft.type === 'ERC1155') : false;
</script>

<div class="container mx-auto inline-block align-middle space-y-[25px] mt-[30px]">
<div class="container mx-auto inline-block align-middle space-y-[25px] w-full md:w-[524px] mt-[30px]">
<div class="flex justify-between mb-2 items-center">
<div class="font-bold text-primary-content">{$t('bridge.nft.step.review.transfer_details')}</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { getLogger } from '$libs/util/logger';
import { uid } from '$libs/util/uid';
import { account } from '$stores/account';
import { network } from '$stores/network';

import { destNetwork } from '../Bridge/state';

Expand All @@ -44,7 +45,7 @@
tokenService.storeToken(customToken, $account?.address as Address);
customTokens = tokenService.getTokens($account?.address as Address);

const { chain: srcChain } = getNetwork();
const srcChain = $network;
const destChain = $destNetwork;

if (!srcChain || !destChain) return;
Expand Down Expand Up @@ -119,7 +120,7 @@
return;
}

const { chain: srcChain } = getNetwork();
const srcChain = $network;
if (!srcChain) return;
try {
const token = await getTokenWithInfoFromAddress({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import { type Address, getNetwork } from '@wagmi/core';
import type { Address } from '@wagmi/core';
import { onDestroy } from 'svelte';
import { t } from 'svelte-i18n';

Expand All @@ -12,6 +12,7 @@
import { getCrossChainAddress } from '$libs/token/getCrossChainAddress';
import { uid } from '$libs/util/uid';
import { account } from '$stores/account';
import { network } from '$stores/network';

import { destNetwork } from '../Bridge/state';
import DialogView from './DialogView.svelte';
Expand Down Expand Up @@ -64,12 +65,12 @@
};

const selectToken = async (token: Token) => {
const { chain } = getNetwork();
const srcChain = $network;
const destChain = $destNetwork;

// In order to select a token, we only need the source chain to be selected,
// unless it's an imported token...
if (!chain) {
if (!srcChain) {
warningToast({ title: $t('messages.network.required') });
return;
}
Expand All @@ -88,7 +89,7 @@
try {
bridgedAddress = await getCrossChainAddress({
token,
srcChainId: chain.id,
srcChainId: srcChain.id,
destChainId: destChain.id,
});
} catch (error) {
Expand Down
6 changes: 2 additions & 4 deletions packages/bridge-ui-v2/src/libs/relayer/RelayerAPIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { apiService } from '$config';
import type { BridgeTransaction, MessageStatus } from '$libs/bridge';
import { isSupportedChain } from '$libs/chain';
import { TokenType } from '$libs/token';
import { fetchTransactionReceipt } from '$libs/util/fetchTransactionReceipt';
import { getLogger } from '$libs/util/logger';
import { publicClient } from '$libs/wagmi';

import type {
APIRequestParams,
Expand All @@ -36,9 +36,7 @@ export class RelayerAPIService {
//Todo: duplicate code in BridgeTxService
private static async _getTransactionReceipt(chainId: number, hash: Hash) {
try {
const client = publicClient({ chainId });
const receipt = await client.getTransactionReceipt({ hash });
return receipt;
return await fetchTransactionReceipt(hash, chainId);
} catch (error) {
log(`Error getting transaction receipt for ${hash}: ${error}`);
return null;
Expand Down
6 changes: 2 additions & 4 deletions packages/bridge-ui-v2/src/libs/storage/BridgeTxService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { routingContractsMap } from '$bridgeConfig';
import { pendingTransaction, storageService } from '$config';
import { type BridgeTransaction, MessageStatus } from '$libs/bridge';
import { isSupportedChain } from '$libs/chain';
import { fetchTransactionReceipt } from '$libs/util/fetchTransactionReceipt';
import { jsonParseWithDefault } from '$libs/util/jsonParseWithDefault';
import { getLogger } from '$libs/util/logger';
import { publicClient } from '$libs/wagmi';
Expand All @@ -23,11 +24,8 @@ export class BridgeTxService {

//Todo: duplicate code in RelayerAPIService
private static async _getTransactionReceipt(chainId: number, hash: Hash) {
log(`Getting transaction receipt for ${hash} on chain ${chainId}`);
try {
const client = publicClient({ chainId });
const receipt = await client.getTransactionReceipt({ hash });
return receipt;
return await fetchTransactionReceipt(hash, chainId);
} catch (error) {
log(`Error getting transaction receipt for ${hash}: ${error}`);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class CustomTokenService implements TokenService {
doesTokenAlreadyExist = tokens.findIndex((tokenFromStorage) => tokenFromStorage.symbol === token.symbol) >= 0;
}
if (!doesTokenAlreadyExist) {
token.imported = true;
tokens.push(token);
}

Expand Down
4 changes: 1 addition & 3 deletions packages/bridge-ui-v2/src/libs/token/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const log = getLogger('token:getBalance');

export async function getBalance({ userAddress, token, srcChainId, destChainId }: GetBalanceArgs) {
let tokenBalance: FetchBalanceResult;

log('getBalance', { userAddress, token, srcChainId, destChainId });
if (!token || token.type === TokenType.ETH) {
// If no token is passed in, we assume is ETH
tokenBalance = await fetchBalance({ address: userAddress, chainId: srcChainId });
Expand All @@ -37,8 +37,6 @@ export async function getBalance({ userAddress, token, srcChainId, destChainId }
chainId: srcChainId,
});
}

log('Token balance', tokenBalance);

return tokenBalance;
}
31 changes: 31 additions & 0 deletions packages/bridge-ui-v2/src/libs/util/fetchTransactionReceipt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Hash } from 'viem';

import { chains } from '$libs/chain';

export async function fetchTransactionReceipt(transactionHash: Hash, chainId: number) {
try {
const nodeUrl = chains.find((c) => c.id === chainId)?.rpcUrls?.public?.http[0];
if (!nodeUrl) {
throw new Error('Node URL not found');
}

const response = await fetch(nodeUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
method: 'eth_getTransactionReceipt',
params: [transactionHash],
id: 1,
}),
});

const data = await response.json();
return data.result;
} catch (error) {
console.error('Error fetching transaction receipt:', error);
throw error;
}
}
Loading