import { Button, Typography } from 'antd' import BigNumber from 'bignumber.js' import { ethers } from 'ethers' import { Sdk } from 'etherspot' import { useState } from 'react' import { useKlimaStakingExecutor } from '../../hooks/Etherspot/klimaStakingExecutor' import { ChainId, CoinKey, ExtendedRouteOptional, findDefaultToken, Token } from '../../types' import LoadingIndicator from '../LoadingIndicator' import { MinimalEtherspotStep } from '../SwappingEtherspot/StepRenderers/MinimalStepRenderers/MinimalEtherspotStep' const TOKEN_POLYGON_USDC = findDefaultToken(CoinKey.USDC, ChainId.POL) interface Props { etherSpotSDK: Sdk residualRoute: ExtendedRouteOptional etherspotWalletBalance: BigNumber setResidualRoute: Function setEtherspotWalletBalance: Function tokenPolygonSKLIMA: Token } export const ResidualRouteKlimaStakeModal = ({ etherSpotSDK, residualRoute, etherspotWalletBalance, setEtherspotWalletBalance, setResidualRoute, tokenPolygonSKLIMA, }: Props) => { const [isSwapping, setIsSwapping] = useState(false) const { etherspotStepExecution, executeEtherspotStep, resetEtherspotExecution, handlePotentialEtherSpotError, finalizeEtherSpotExecution, } = useKlimaStakingExecutor() const stakeResidualFunds = async () => { setIsSwapping(true) try { finalizeEtherSpotExecution( await executeEtherspotStep( etherSpotSDK!, residualRoute!.gasStep!, residualRoute!.stakingStep!, ethers.utils .parseUnits(etherspotWalletBalance?.toString()!, TOKEN_POLYGON_USDC.decimals) .toString(), ), residualRoute!.stakingStep!.estimate.toAmountMin, ) } catch (e) { // eslint-disable-next-line no-console console.warn(e) setIsSwapping(false) handlePotentialEtherSpotError(e, residualRoute!) } } return ( <> You still have {etherspotWalletBalance.toFixed(2)} USDC in your smart contract based wallet, do you want to swap and stake it to sKLIMA?
{MinimalEtherspotStep({ etherspotStepExecution, stakingStep: residualRoute?.stakingStep, isSwapping: isSwapping, index: 0, alternativeToToken: tokenPolygonSKLIMA, previousStepInfo: { amount: ethers.BigNumber.from( etherspotWalletBalance?.shiftedBy(TOKEN_POLYGON_USDC.decimals).toString(), ).toString(), token: TOKEN_POLYGON_USDC, }, })}
{!etherspotStepExecution ? ( <> ) : etherspotStepExecution.status === 'FAILED' ? ( ) : etherspotStepExecution.status === 'DONE' ? ( <> Transaction Successful! ) : ( )}
) }