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

Update token input fields #759

Merged
merged 5 commits into from
Feb 1, 2024
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
1 change: 0 additions & 1 deletion projects/sdk/src/lib/swap/Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class Swap {
}

public buildSwap(tokenIn: Token, tokenOut: Token, account: string, _from?: FarmFromMode, _to?: FarmToMode) {

const route = this.router.getRoute(tokenIn.symbol, tokenOut.symbol);
const workflow = Swap.sdk.farm.createAdvancedFarm(`Swap ${tokenIn.symbol}->${tokenOut.symbol}`);

Expand Down
4 changes: 2 additions & 2 deletions projects/sdk/src/lib/swap/SwapOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TokenValue } from "src/TokenValue";
import { Token } from "src/classes/Token";
import { BeanstalkSDK } from "src/lib/BeanstalkSDK";
import { Route } from "src/classes/Router";
import { AdvancedFarmWorkflow } from "../farm";
import { AdvancedFarmWorkflow, FarmFromMode, FarmToMode } from "../farm";

type PathSegment = {
from: string;
Expand All @@ -18,7 +18,7 @@ export class SwapOperation {
sdk: BeanstalkSDK,
readonly tokenIn: Token,
readonly tokenOut: Token,
private readonly workflow: AdvancedFarmWorkflow|Workflow,
private readonly workflow: AdvancedFarmWorkflow | Workflow,
private readonly route: Route
) {
SwapOperation.sdk = sdk;
Expand Down
24 changes: 22 additions & 2 deletions projects/ui/src/components/Chop/Actions/Chop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import TransactionToast from '~/components/Common/TxnToast';
import useFormMiddleware from '~/hooks/ledger/useFormMiddleware';
import useSdk from '~/hooks/sdk';
import useBDV from '~/hooks/beanstalk/useBDV';
import { BalanceFrom } from '~/components/Common/Form/BalanceFromRow';

type ChopFormValues = FormState & {
destination: FarmToMode | undefined;
Expand All @@ -76,7 +77,11 @@ const ChopForm: FC<
const unripeUnderlying = useUnripeUnderlyingMap();
const [quote, setQuote] = useState<BigNumber>(new BigNumber(0));
const [quoteBdv, setQuoteBdv] = useState<BigNumber>(new BigNumber(0));

const [balanceFromIn, setBalanceFromIn] = useState<BalanceFrom>(
values.destination === FarmToMode.EXTERNAL
? BalanceFrom.EXTERNAL
: BalanceFrom.INTERNAL
);
/// Derived values
const state = values.tokens[0];
const inputToken = state.token;
Expand Down Expand Up @@ -147,6 +152,14 @@ const ChopForm: FC<

const isSubmittable = quote?.gt(0) && values.destination;

const changeDestination = (v: BalanceFrom) => {
setBalanceFromIn(v);
setFieldValue(
'destination',
v === BalanceFrom.EXTERNAL ? FarmToMode.EXTERNAL : FarmToMode.INTERNAL
);
};

return (
<Form autoComplete="off">
<TokenSelectDialog
Expand All @@ -157,6 +170,9 @@ const ChopForm: FC<
balances={balances}
tokenList={Object.values(erc20TokenMap)}
mode={TokenSelectMode.SINGLE}
balanceFrom={balanceFromIn}
setBalanceFrom={changeDestination}
balanceFromOptions={[BalanceFrom.INTERNAL, BalanceFrom.EXTERNAL]}
/>
<Stack gap={1}>
<TokenInputField
Expand All @@ -167,7 +183,11 @@ const ChopForm: FC<
fullWidth
InputProps={{
endAdornment: (
<TokenAdornment token={inputToken} onClick={showTokenSelect} />
<TokenAdornment
balanceFrom={balanceFromIn}
token={inputToken}
onClick={showTokenSelect}
/>
),
}}
/>
Expand Down
5 changes: 3 additions & 2 deletions projects/ui/src/components/Common/Form/BalanceFromRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ const options: BalanceFrom[] = [
const BalanceFromRow: React.FC<{
balanceFrom: BalanceFrom;
setBalanceFrom: (v: BalanceFrom) => void;
}> = ({ balanceFrom, setBalanceFrom }) => (
customOptions?: BalanceFrom[];
}> = ({ balanceFrom, setBalanceFrom, customOptions }) => (
<Row gap={1}>
{options.map((option) => {
{(customOptions || options).map((option) => {
const isSelected = balanceFrom === option;
return (
<Button
Expand Down
4 changes: 4 additions & 0 deletions projects/ui/src/components/Common/Form/TokenSelectDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export type TokenSelectDialogProps<K extends keyof TokenBalanceMode> = {
description?: string | JSX.Element;
/** the balance (circulating |farm | combined) being used */
balanceFrom?: BalanceFrom;
/** custom set of options to filter the balance from selection */
balanceFromOptions?: BalanceFrom[];
/** set the balance (circulating |farm | combined) to use */
setBalanceFrom?: (v: BalanceFrom) => void;
/**
Expand Down Expand Up @@ -92,6 +94,7 @@ const TokenSelectDialog: TokenSelectDialogC = React.memo(
title,
description,
balanceFrom = BalanceFrom.TOTAL,
balanceFromOptions,
setBalanceFrom,
// Balances
balancesType = 'farm',
Expand Down Expand Up @@ -210,6 +213,7 @@ const TokenSelectDialog: TokenSelectDialogC = React.memo(
<BalanceFromRow
balanceFrom={balanceFrom}
setBalanceFrom={setBalanceFrom}
customOptions={balanceFromOptions}
/>
<Box
pt={2}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { QuoteHandlerWithParams } from '~/hooks/ledger/useQuoteWithParams';

import FormTxnProvider from '~/components/Common/Form/FormTxnProvider';
import useFormTxnContext from '~/hooks/sdk/useFormTxnContext';
import { BalanceFrom } from '~/components/Common/Form/BalanceFromRow';

export type CreateOrderFormValues = {
placeInLine: BigNumber | null;
Expand Down Expand Up @@ -175,6 +176,7 @@ const CreateOrderV2Form: FC<
selected={values.tokens}
handleSubmit={handleSelectTokens}
balances={balances}
balanceFrom={BalanceFrom.TOTAL}
tokenList={Object.values(erc20TokenMap)}
mode={TokenSelectMode.SINGLE}
/>
Expand Down Expand Up @@ -223,6 +225,7 @@ const CreateOrderV2Form: FC<
? balances.eth
: balances[state.token.address] || ZERO_BN
}
balanceFrom={BalanceFrom.TOTAL}
state={state}
params={quoteHandlerParams}
showTokenSelect={handleOpen}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import useFormMiddleware from '~/hooks/ledger/useFormMiddleware';
import TokenOutput from '~/components/Common/Form/TokenOutput';
import useSdk from '~/hooks/sdk';
import useAccount from '~/hooks/ledger/useAccount';
import { BalanceFrom } from '~/components/Common/Form/BalanceFromRow';

export type FillListingFormValues = FormState & {
settings: SlippageSettingsFragment;
Expand Down Expand Up @@ -197,6 +198,7 @@ const FillListingV2Form: FC<
balances={balances}
tokenList={Object.values(erc20TokenMap)}
mode={TokenSelectMode.SINGLE}
balanceFrom={BalanceFrom.TOTAL}
/>
<Stack gap={1}>
<TokenQuoteProvider
Expand All @@ -212,6 +214,7 @@ const FillListingV2Form: FC<
state={values.tokens[0]}
showTokenSelect={handleOpen}
handleQuote={handleQuote}
balanceFrom={BalanceFrom.TOTAL}
size="small"
/>
{isReady ? (
Expand Down Expand Up @@ -412,7 +415,8 @@ const FillListingForm: FC<{
finalFromMode = optimizeFromMode(amountBeans, balances[Bean.address]);
farm = sdk.farm.create();
tokenInNew = sdk.tokens.BEAN; // FIXME
} else { /// Swap to BEAN and buy
} else {
/// Swap to BEAN and buy
// Require a quote
if (!formData.amountOut)
throw new Error(`No quote available for ${formData.token.symbol}`);
Expand Down Expand Up @@ -497,7 +501,7 @@ const FillListingForm: FC<{
balances,
sdk,
beanstalk.interface,
account
account,
]
);

Expand Down
Loading
Loading