-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1196 from layerswap/dev
Dev to main
- Loading branch information
Showing
11 changed files
with
219 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 { | ||
useWallet as useFuelWallet, | ||
} from '@fuels/react'; | ||
|
||
const FuelWalletWithdrawStep: FC<WithdrawPageProps> = ({ network, callData, swapId }) => { | ||
const [loading, setLoading] = useState(false); | ||
const { setSwapTransaction } = useSwapTransactionStore() | ||
|
||
const { provider } = useWallet(network, 'withdrawal'); | ||
const { wallet: fuelWallet } = useFuelWallet() | ||
const wallet = provider?.activeWallet | ||
|
||
const handleTransfer = useCallback(async () => { | ||
try { | ||
setLoading(true) | ||
|
||
if (!fuelWallet) throw Error("Fuel wallet not connected") | ||
if (!callData) throw Error("Call data not found") | ||
|
||
const tx = JSON.parse(callData) | ||
const transactionResponse = await fuelWallet.sendTransaction(tx) | ||
|
||
if (swapId && transactionResponse) setSwapTransaction(swapId, BackendTransactionStatus.Completed, transactionResponse.id) | ||
|
||
} | ||
catch (e) { | ||
if (e?.message) { | ||
toast(e.message) | ||
return | ||
} | ||
} | ||
finally { | ||
setLoading(false) | ||
} | ||
}, [swapId, callData, fuelWallet]) | ||
|
||
if (!wallet) { | ||
return <ConnectWalletButton /> | ||
} | ||
|
||
return ( | ||
<div className="w-full space-y-5 flex flex-col justify-between h-full text-primary-text"> | ||
{ | ||
fuelWallet && | ||
<ButtonWrapper isDisabled={!!loading} isSubmitting={!!loading} onClick={handleTransfer} icon={<WalletIcon className="stroke-2 w-6 h-6" aria-hidden="true" />} > | ||
Send from wallet | ||
</ButtonWrapper> | ||
} | ||
</div> | ||
) | ||
} | ||
|
||
export default FuelWalletWithdrawStep; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { ArrowLeftRight } from 'lucide-react'; | ||
import { FC, useCallback, useState } from 'react' | ||
import toast from 'react-hot-toast'; | ||
import { useSwapTransactionStore } from '../../../../stores/swapTransactionStore'; | ||
import { BackendTransactionStatus } from '../../../../lib/layerSwapApiClient'; | ||
import { ButtonWrapper, ChangeNetworkButton, ConnectWalletButton } from './WalletTransfer/buttons'; | ||
import { useAccount } from 'wagmi'; | ||
import useWallet from '../../../../hooks/useWallet'; | ||
import { WithdrawPageProps } from './WalletTransferContent'; | ||
import { sophon, sophonTestnet } from 'viem/chains'; | ||
import { createWalletClient, custom, JsonRpcAccount } from 'viem'; | ||
import { eip712WalletActions, getGeneralPaymasterInput } from 'viem/zksync'; | ||
import KnownInternalNames from '../../../../lib/knownIds'; | ||
|
||
const SophonWalletWithdraw: FC<WithdrawPageProps> = ({ amount, depositAddress, network, token, swapId, callData }) => { | ||
const [loading, setLoading] = useState(false); | ||
|
||
const { setSwapTransaction } = useSwapTransactionStore(); | ||
const { chain: activeChain, connector } = useAccount(); | ||
|
||
const networkChainId = Number(network?.chain_id) ?? undefined | ||
const { provider } = useWallet(network, 'withdrawal') | ||
const wallet = provider?.activeWallet | ||
|
||
const handleTransfer = useCallback(async () => { | ||
|
||
if (!wallet?.address || !swapId || !depositAddress || !token || amount == undefined || !callData || !network?.metadata.zks_paymaster_contract) return | ||
|
||
try { | ||
setLoading(true) | ||
|
||
const walletProvider = await connector?.getProvider() as any | ||
|
||
if (!walletProvider) throw new Error('Could not get provider') | ||
|
||
const account = { | ||
address: wallet?.address, | ||
type: 'json-rpc' | ||
} as JsonRpcAccount | ||
|
||
const walletClient = createWalletClient({ | ||
chain: network.name === KnownInternalNames.Networks.SophonSepolia ? sophonTestnet : sophon, | ||
transport: custom(walletProvider), | ||
account: account | ||
}).extend(eip712WalletActions()); | ||
|
||
const request = await walletClient.prepareTransactionRequest({ | ||
to: depositAddress, | ||
data: callData as `0x${string}`, | ||
paymaster: network?.metadata.zks_paymaster_contract, | ||
paymasterInput: getGeneralPaymasterInput({ innerInput: "0x" }), | ||
}) | ||
|
||
const signature = await walletClient.signTransaction(request as any) | ||
const hash = await walletClient.sendRawTransaction({ | ||
serializedTransaction: signature | ||
}) | ||
|
||
if (hash) { | ||
setSwapTransaction(swapId, BackendTransactionStatus.Pending, hash); | ||
} | ||
} | ||
catch (e) { | ||
if (e?.message) { | ||
if (e.name.includes("EstimateGasExecutionError")) return toast("You don't have enough funds") | ||
toast(e.message) | ||
return | ||
} | ||
} | ||
finally { | ||
setLoading(false) | ||
} | ||
}, [swapId, depositAddress, token, amount, callData]) | ||
|
||
if (!wallet) { | ||
return <ConnectWalletButton /> | ||
} | ||
|
||
else if (activeChain?.id !== networkChainId && network) { | ||
return <ChangeNetworkButton | ||
chainId={networkChainId} | ||
network={network.display_name} | ||
/> | ||
} | ||
|
||
return ( | ||
<> | ||
<ButtonWrapper isDisabled={!!(loading)} isSubmitting={!!loading} onClick={handleTransfer} icon={<ArrowLeftRight className="h-5 w-5 ml-2" aria-hidden="true" />} > | ||
Send from wallet | ||
</ButtonWrapper> | ||
</> | ||
) | ||
} | ||
export default SophonWalletWithdraw; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.