From abccd88cd9d3e6dc55a0ad6e886f2cc57f1bd558 Mon Sep 17 00:00:00 2001 From: Aren Date: Fri, 20 Dec 2024 15:22:46 +0400 Subject: [PATCH] Implement basic fuel wallet withdrawal --- .../Withdraw/Wallet/FuelWalletWithdrawal.tsx | 75 +++++++++++++++++++ .../Withdraw/Wallet/WalletTransferContent.tsx | 13 ++++ 2 files changed, 88 insertions(+) create mode 100644 components/Swap/Withdraw/Wallet/FuelWalletWithdrawal.tsx diff --git a/components/Swap/Withdraw/Wallet/FuelWalletWithdrawal.tsx b/components/Swap/Withdraw/Wallet/FuelWalletWithdrawal.tsx new file mode 100644 index 000000000..2f2edbef9 --- /dev/null +++ b/components/Swap/Withdraw/Wallet/FuelWalletWithdrawal.tsx @@ -0,0 +1,75 @@ +import { FC, useCallback, useState } from 'react' +import toast from 'react-hot-toast'; +import { BackendTransactionStatus } from '../../../../lib/layerSwapApiClient'; +import useWallet from '../../../../hooks/useWallet'; +import { useSwapTransactionStore } from '../../../../stores/swapTransactionStore'; +import WalletIcon from '../../../icons/WalletIcon'; +import { WithdrawPageProps } from './WalletTransferContent'; +import { ButtonWrapper, ConnectWalletButton } from './WalletTransfer/buttons'; +import { useSettingsState } from '../../../../context/settings'; +import { + useWallet as useFuelWallet, +} from '@fuels/react'; +import { bn } from 'fuels'; + +const FuelWalletWithdrawStep: FC = ({ network, callData, swapId, token, amount }) => { + const [loading, setLoading] = useState(false); + const { setSwapTransaction } = useSwapTransactionStore() + + const { provider } = useWallet(network, 'withdrawal'); + const { wallet: fuelWallet } = useFuelWallet() + + const wallet = provider?.activeWallet + const networkName = network?.name + + const { networks } = useSettingsState() + const networkWithTokens = networks.find(n => n.name === networkName) + + const handleTransfer = useCallback(async () => { + setLoading(true) + try { + + if (!fuelWallet) throw Error("Fuel wallet not connected") + + // The amount of coins to transfer. + const bnAmount = bn(amount); + + // Create a transaction request using wallet helper + const transactionRequest = token?.contract ? await fuelWallet.createTransfer('0x9E22044B082B1ff5B2b824De1068F9A04A02ff0E1d36807B2b9Dda8bB65071C3', bnAmount, token?.contract) : await fuelWallet.createTransfer('0x9E22044B082B1ff5B2b824De1068F9A04A02ff0E1d36807B2b9Dda8bB65071C3', bnAmount); + + // Broadcast the transaction to the network + const transactionResponse = await fuelWallet.sendTransaction( + transactionRequest, // The transaction to send + ) + + if (swapId && transactionResponse) setSwapTransaction(swapId, BackendTransactionStatus.Completed, transactionResponse.id) + + } + catch (e) { + if (e?.message) { + toast(e.message) + return + } + } + finally { + setLoading(false) + } + }, [swapId, callData, network, token, amount, fuelWallet]) + + if (!fuelWallet) { + return + } + + return ( +
+ { + fuelWallet && + + } +
+ ) +} + +export default FuelWalletWithdrawStep; \ No newline at end of file diff --git a/components/Swap/Withdraw/Wallet/WalletTransferContent.tsx b/components/Swap/Withdraw/Wallet/WalletTransferContent.tsx index 5cb40698c..28d4c9be4 100644 --- a/components/Swap/Withdraw/Wallet/WalletTransferContent.tsx +++ b/components/Swap/Withdraw/Wallet/WalletTransferContent.tsx @@ -10,6 +10,7 @@ import LoopringWalletWithdraw from "./Loopring"; import { Network, Token } from "../../../../Models/Network"; import TonWalletWithdrawStep from "./TonWalletWithdraw"; import ParadexWalletWithdrawStep from "./paradex/index"; +import FuelWalletWithdrawStep from "./FuelWalletWithdrawal"; //TODO have separate components for evm and none_evm as others are sweepless anyway export const WalletTransferContent: FC = () => { @@ -43,6 +44,9 @@ export const WalletTransferContent: FC = () => { const sourceIsParadex = source_network_internal_name?.toUpperCase() === KnownInternalNames.Networks.ParadexMainnet?.toUpperCase() || source_network_internal_name?.toUpperCase() === KnownInternalNames.Networks.ParadexTestnet?.toUpperCase(); + const sourceIsFuel = source_network_internal_name?.toUpperCase() === KnownInternalNames.Networks.FuelMainnet?.toUpperCase() + || source_network_internal_name?.toUpperCase() === KnownInternalNames.Networks.FuelTestnet?.toUpperCase(); + const depositAddress = depositActionsResponse?.find(da => true)?.to_address; const amount = depositActionsResponse?.find(da => true)?.amount || 0; const callData = depositActionsResponse?.find(da => true)?.call_data; @@ -106,6 +110,15 @@ export const WalletTransferContent: FC = () => { swapId={swap?.id} callData={callData} />; + else if (sourceIsFuel) + return ; else return <> {