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

Silo Withdraw as X #548

Merged
merged 8 commits into from
Jul 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import useFarmerFormTxnsSummary from '~/hooks/farmer/form-txn/useFarmerFormTxnsS
import MergeIcon from '~/img/misc/merge-icon.svg';

import { FormTxnsFormState } from '~/components/Common/Form';
import { FormTxn } from '~/lib/Txn';
import { FormTxn, PlantAndDoX } from '~/lib/Txn';
import SiloVestingWarningAlert from '~/components/Silo/SiloVestingWarningAlert';

const sx = {
Expand All @@ -51,7 +51,9 @@ const sx = {
*
* NOTE: Used within Formik Context
*/
const AddPlantTxnToggle: React.FC<{}> = () => {
const AddPlantTxnToggle: React.FC<{
plantAndDoX: PlantAndDoX | undefined;
}> = ({ plantAndDoX }) => {
/// Local State
const [open, show, hide] = useToggle();

Expand Down Expand Up @@ -95,7 +97,7 @@ const AddPlantTxnToggle: React.FC<{}> = () => {
}, [open, isPlant, isPlanting, show, hide]);

/// If there is nothing to plant or if the preset isn't plant, return nothing
if (!isPlant || !plantEnabled) return null;
if (!isPlant || !plantEnabled || !plantAndDoX) return null;

return (
<>
Expand Down
16 changes: 4 additions & 12 deletions projects/ui/src/components/Common/Form/FormTxnProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { useFetchFarmerBalances } from '~/state/farmer/balances/updater';
import { useFetchFarmerBarn } from '~/state/farmer/barn/updater';
import { useFetchFarmerField } from '~/state/farmer/field/updater';
import { useFetchFarmerSilo } from '~/state/farmer/silo/updater';
import useSeason from '~/hooks/beanstalk/useSeason';
import {
FormTxnBundler,
ClaimFarmStep,
Expand All @@ -21,9 +20,9 @@ import {
MowFarmStep,
PlantFarmStep,
RinseFarmStep,
PlantAndDoX,
FormTxn,
} from '~/lib/Txn';
import usePlantAndDoX from '~/hooks/farmer/form-txn/usePlantAndDoX';

// -------------------------------------------------------------------------

Expand Down Expand Up @@ -68,7 +67,6 @@ const useInitFormTxnContext = () => {
const farmerSilo = useFarmerSilo();
const farmerField = useFarmerField();
const farmerBarn = useFarmerFertilizer();
const season = useSeason();

/// Refetch functions
const [refetchFarmerSilo] = useFetchFarmerSilo();
Expand All @@ -79,17 +77,11 @@ const useInitFormTxnContext = () => {

/// Helpers
const getBDV = useBDV();
const plantAndDoX = usePlantAndDoX();

/// Context State
const [txnBundler, setTxnBundler] = useState(new FormTxnBundler(sdk, {}));

const plantAndDoX = useMemo(() => {
const earnedBeans = sdk.tokens.BEAN.amount(
farmerSilo.beans.earned.toString()
);

return new PlantAndDoX(sdk, earnedBeans, season.toNumber());
}, [farmerSilo.beans.earned, sdk, season]);

/// On any change, update the txn bundler
useEffect(() => {
const { BEAN } = sdk.tokens;
Expand Down Expand Up @@ -204,7 +196,7 @@ const useInitFormTxnContext = () => {
txnBundler,
plantAndDoX,
refetch,
};
} as const;
};

export const FormTxnBuilderContext = React.createContext<
Expand Down
40 changes: 26 additions & 14 deletions projects/ui/src/components/Silo/Actions/Convert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ import WarningAlert from '~/components/Common/Alert/WarningAlert';
import TokenOutput from '~/components/Common/Form/TokenOutput';
import TxnAccordion from '~/components/Common/TxnAccordion';

import useFarmerDepositCrateFromPlant from '~/hooks/farmer/useFarmerDepositCrateFromPlant';
import AdditionalTxnsAccordion from '~/components/Common/Form/FormTxn/AdditionalTxnsAccordion';
import useFarmerFormTxnsActions from '~/hooks/farmer/form-txn/useFarmerFormTxnActions';
import useAsyncMemo from '~/hooks/display/useAsyncMemo';
import AddPlantTxnToggle from '~/components/Common/Form/FormTxn/AddPlantTxnToggle';
import FormTxnProvider from '~/components/Common/Form/FormTxnProvider';
import useFormTxnContext from '~/hooks/sdk/useFormTxnContext';
import { FormTxn, ConvertFarmStep } from '~/lib/Txn';
import usePlantAndDoX from '~/hooks/farmer/form-txn/usePlantAndDoX';

// -----------------------------------------------------------------------

Expand Down Expand Up @@ -90,12 +90,14 @@ const ConvertForm: FC<
/** other */
sdk: BeanstalkSDK;
conversion: ConvertDetails;
plantAndDoX: ReturnType<typeof usePlantAndDoX>;
}
> = ({
tokenList,
siloBalances,
handleQuote,
currentSeason,
plantAndDoX,
sdk,
// Formik
values,
Expand All @@ -107,7 +109,7 @@ const ConvertForm: FC<
const [isTokenSelectVisible, showTokenSelect, hideTokenSelect] = useToggle();
const getBDV = useBDV();

const { crate: plantCrate } = useFarmerDepositCrateFromPlant();
const plantCrate = plantAndDoX?.crate?.bn;

/// Extract values from form state
const tokenIn = values.tokens[0].token; // converting from token
Expand All @@ -129,9 +131,10 @@ const ConvertForm: FC<
sdk.tokens.BEAN.equals(tokenIn)
);

const totalAmountIn = isUsingPlanted
? (amountIn || ZERO_BN).plus(plantCrate.asBN.amount)
: amountIn;
const totalAmountIn =
isUsingPlanted && plantCrate
? (amountIn || ZERO_BN).plus(plantCrate.amount)
: amountIn;

/// Derived form state
let isReady = false;
Expand Down Expand Up @@ -268,7 +271,9 @@ const ConvertForm: FC<
}
params={quoteHandlerParams}
/>
{!canConvert && tokenOut && maxAmountIn ? null : <AddPlantTxnToggle />}
{!canConvert && tokenOut && maxAmountIn ? null : (
<AddPlantTxnToggle plantAndDoX={plantAndDoX.plantAction} />
)}

{/* User Input: destination token */}
{depositedAmount.gt(0) ? (
Expand Down Expand Up @@ -490,6 +495,9 @@ const ConvertPropProvider: FC<{
if (!farmerBalances?.deposits) {
throw new Error('No balances found');
}
const { plantAction } = plantAndDoX;

const includePlant = !!(isConvertingPlanted && plantAction);

const result = await ConvertFarmStep._handleConversion(
sdk,
Expand All @@ -499,7 +507,7 @@ const ConvertPropProvider: FC<{
tokenIn.amount(_amountIn.toString()),
season.toNumber(),
slippage,
isConvertingPlanted ? plantAndDoX : undefined
includePlant ? plantAction : undefined
);

setConversion(result.conversion);
Expand Down Expand Up @@ -540,8 +548,11 @@ const ConvertPropProvider: FC<{
success: 'Convert successful.',
});

const { plantAction } = plantAndDoX;

const amountIn = tokenIn.amount(_amountIn.toString()); // amount of from token
const isPlanting = values.farmActions.primary?.includes(FormTxn.PLANT);
const isPlanting =
plantAndDoX && values.farmActions.primary?.includes(FormTxn.PLANT);

const convertTxn = new ConvertFarmStep(
sdk,
Expand All @@ -553,7 +564,7 @@ const ConvertPropProvider: FC<{
const { getEncoded, minAmountOut } = await convertTxn.handleConversion(
amountIn,
slippage,
isPlanting ? plantAndDoX : undefined
isPlanting ? plantAction : undefined
);

convertTxn.build(getEncoded, minAmountOut);
Expand Down Expand Up @@ -602,17 +613,17 @@ const ConvertPropProvider: FC<{
}
},
[
middleware,
account,
farmerBalances,
sdk,
season,
plantAndDoX,
account,
txnBundler,
middleware,
plantAndDoX,
initialValues,
farmerBalances,
refetch,
refetchPools,
refetchFarmerBalances,
initialValues,
]
);

Expand All @@ -634,6 +645,7 @@ const ConvertPropProvider: FC<{
currentSeason={season}
sdk={sdk}
conversion={conversion}
plantAndDoX={plantAndDoX}
{...formikProps}
/>
</>
Expand Down
23 changes: 14 additions & 9 deletions projects/ui/src/components/Silo/Actions/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const TransferForm: FC<
token: Token;
siloBalance: TokenSiloBalance | undefined;
season: BigNumber;
plantAndDoX: PlantAndDoX;
plantAndDoX: PlantAndDoX | undefined;
}
> = ({
// Formik
Expand All @@ -84,7 +84,8 @@ const TransferForm: FC<
const txnActions = useFarmerFormTxnsActions();
const isUsingPlant = Boolean(
values.farmActions.primary?.includes(FormTxn.PLANT) &&
BEAN.equals(whitelistedToken)
BEAN.equals(whitelistedToken) &&
plantAndDoX
);

// Results
Expand All @@ -93,7 +94,7 @@ const TransferForm: FC<
const deposits = siloBalance?.deposits || [];

if (!isUsingPlant && (amount.lte(0) || !deposits.length)) return null;
if (isUsingPlant && plantAndDoX.getAmount().lte(0)) return null;
if (isUsingPlant && plantAndDoX?.getAmount().lte(0)) return null;

// FIXME: stems
return WithdrawFarmStep.calculateWithdraw(
Expand Down Expand Up @@ -187,7 +188,7 @@ const TransferForm: FC<
balanceLabel="Deposited Balance"
InputProps={InputProps}
/>
<AddPlantTxnToggle />
<AddPlantTxnToggle plantAndDoX={plantAndDoX} />
{depositedBalance?.gt(0) && (
<>
<FieldWrapper label="Transfer to">
Expand Down Expand Up @@ -343,15 +344,19 @@ const TransferPropProvider: FC<{
const formData = values.tokens[0];
const primaryActions = values.farmActions.primary;

const { plantAction } = plantAndDoX;

const isPlanting =
plantAndDoX &&
primaryActions?.includes(FormTxn.PLANT) &&
sdk.tokens.BEAN.equals(token);

const baseAmount = token.amount((formData?.amount || 0).toString());

const totalAmount = isPlanting
? baseAmount.add(plantAndDoX.getAmount())
: baseAmount;
const totalAmount =
isPlanting && plantAction
? baseAmount.add(plantAction.getAmount())
: baseAmount;

if (totalAmount.lte(0)) throw new Error('Invalid amount.');

Expand All @@ -363,7 +368,7 @@ const TransferPropProvider: FC<{
values.to,
baseAmount,
season.toNumber(),
isPlanting ? plantAndDoX : undefined
isPlanting ? plantAction : undefined
);

if (!transferTxn.withdrawResult) {
Expand Down Expand Up @@ -435,7 +440,7 @@ const TransferPropProvider: FC<{
token={token}
siloBalance={farmerBalances}
season={season}
plantAndDoX={plantAndDoX}
plantAndDoX={plantAndDoX.plantAction}
{...formikProps}
/>
)}
Expand Down
Loading
Loading