Skip to content

Commit

Permalink
Fuel disconnect fix, query balance provider fix, different account tr…
Browse files Browse the repository at this point in the history
…ansfer error handling
  • Loading branch information
arentant committed Dec 13, 2024
1 parent dc18c67 commit 67616e7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 19 deletions.
11 changes: 4 additions & 7 deletions components/Swap/Withdraw/Wallet/WalletTransfer/TransferToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
useAccount,
useSendTransaction
} from "wagmi";
import { createPublicClient, http, parseEther } from 'viem'
import { parseEther } from 'viem'
import SubmitButton from "../../../../buttons/submitButton";
import { BackendTransactionStatus } from "../../../../../lib/layerSwapApiClient";
import WalletIcon from "../../../../icons/WalletIcon";
Expand All @@ -29,19 +29,14 @@ const TransferTokenButton: FC<BaseTransferButtonProps> = ({

const { selectedSourceAccount } = useSwapDataState()

const { chain } = useAccount();
const { address } = useAccount();
const { setSwapTransaction } = useSwapTransactionStore();
const { depositActionsResponse } = useSwapDataState()

const callData = depositActionsResponse?.find(da => true)?.call_data as `0x${string}` | undefined

const transaction = useSendTransaction()

const publicClient = createPublicClient({
chain: chain,
transport: http()
})

useEffect(() => {
(async () => {
if (selectedSourceAccount?.address && depositAddress) {
Expand Down Expand Up @@ -111,6 +106,8 @@ const TransferTokenButton: FC<BaseTransferButtonProps> = ({
<TransactionMessage
transaction={transaction}
applyingTransaction={applyingTransaction}
activeAddress={address}
selectedSourceAddress={selectedSourceAccount?.address}
/>
}
{
Expand Down
1 change: 0 additions & 1 deletion components/Swap/Withdraw/Wallet/WalletTransfer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FC, useEffect, useState } from "react";
import { useAccount } from "wagmi";
import { useSwitchChain } from 'wagmi'
import { PublishedSwapTransactions } from "../../../../../lib/layerSwapApiClient";
import { ChangeNetworkButton, ConnectWalletButton } from "./buttons";
import TransferTokenButton from "./TransferToken";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import resolveError from "./resolveError"
import { ActionData } from "./sharedTypes"
import { BaseError } from 'viem'
import { datadogRum } from '@datadog/browser-rum';
import { addressFormat } from "../../../../../lib/address/formatter"

type TransactionMessageProps = {
wait?: ActionData,
transaction: ActionData,
applyingTransaction: boolean
applyingTransaction: boolean,
activeAddress: string | undefined
selectedSourceAddress: string | undefined
}

const TransactionMessage: FC<TransactionMessageProps> = ({
wait, transaction, applyingTransaction
wait, transaction, applyingTransaction, activeAddress, selectedSourceAddress
}) => {
const transactionResolvedError = resolveError(transaction?.error as BaseError)
const hasError = transaction?.isError || wait?.isError

if (wait?.isPending || applyingTransaction) {
return <TransactionInProgressMessage />
}
Expand All @@ -28,7 +31,11 @@ const TransactionMessage: FC<TransactionMessageProps> = ({
}
else if (transaction?.isError && transactionResolvedError === "transaction_rejected") {
return <TransactionRejectedMessage />
} else if (hasError) {
}
else if (transaction.isError && activeAddress && selectedSourceAddress && (activeAddress?.toLowerCase() !== selectedSourceAddress?.toLowerCase())) {
return <UnknowndMessage />
}
else if (hasError) {
const unexpectedError = transaction?.error?.['data']?.message || transaction?.error
|| wait?.error

Expand Down Expand Up @@ -77,6 +84,13 @@ const TransactionRejectedMessage: FC = () => {
details={`You've rejected the transaction in your wallet. Click “Try again” to open the prompt again.`} />
}

const UnknowndMessage: FC = () => {
return <WalletMessage
status="error"
header='Unknown Message'
details={`Lorem ipsum`} />
}

const UexpectedErrorMessage: FC<{ message: string }> = ({ message }) => {
return <WalletMessage
status="error"
Expand Down
1 change: 0 additions & 1 deletion components/Wallet/WalletsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export const WalletItem: FC<HTMLAttributes<HTMLDivElement> & WalletItemProps> =

const isSelected = selectable && (wallet.addresses.length == 1 && wallet.address == selectedAddress)
const walletBalanceAmount = walletBalance?.amount && truncateDecimals(walletBalance?.amount, token?.precision)
console.log(wallet)
return (
<div {...props} className="rounded-md outline-none text-primary-tex">
<div
Expand Down
4 changes: 1 addition & 3 deletions lib/newbalances/balanceResolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Token } from "@imtbl/imx-sdk";
import { NetworkWithTokens } from "../../Models/Network";
import { EVMBalanceProvider } from "./providers/evmBalanceProvider";
import { FuelBalanceProvider } from "./providers/fuelBalanceProvider";
Expand All @@ -12,19 +11,18 @@ import { ZkSyncBalanceProvider } from "./providers/zkSyncBalanceProvider";

export class BalanceResolver {
private providers = [
new QueryBalanceProvider(),
new StarknetBalanceProvider(),
new EVMBalanceProvider(),
new FuelBalanceProvider(),
new ImmutableXBalanceProvider(),
new LoopringBalanceProvider(),
new QueryBalanceProvider(),
new SolanaBalanceProvider(),
new TonBalanceProvider(),
new ZkSyncBalanceProvider()
];

getBalance(address: string, network: NetworkWithTokens) {

const provider = this.providers.find(p => p.supportsNetwork(network));
//TODO: create interface for balance providers in case of empty state they shoudl throw error
//never return undefined as SWR does not set loading state if undefined is returned
Expand Down
5 changes: 3 additions & 2 deletions lib/newbalances/providers/queryBalanceProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class QueryBalanceProvider {
fromAsset: null,
};
}
const urlParams = new URLSearchParams(location.search);

const urlParams = new URLSearchParams(location.search);
return {
from: urlParams.get('from'),
to: urlParams.get('to'),
Expand All @@ -34,6 +34,7 @@ export class QueryBalanceProvider {
}

supportsNetwork(network: NetworkWithTokens): boolean {
if (!this.query.balances) return false
return network?.name?.toLocaleLowerCase() === this.query.from?.toLowerCase() || network?.name?.toLocaleLowerCase() === this.query.to?.toLowerCase()
}

Expand Down
3 changes: 2 additions & 1 deletion lib/wallets/fuel/useFuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ export default function useFuel(): WalletProvider {
if (!fuelConnector) throw new Error('Connector not found')

await fuelConnector.disconnect()
removeWallet(name, connectorName)
}
catch (e) {
console.log(e)
} finally {
removeWallet(name, connectorName)
}
}

Expand Down

0 comments on commit 67616e7

Please sign in to comment.