Skip to content

Commit

Permalink
update token inputs to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalecks committed Jan 29, 2024
1 parent 24bdd5a commit cf4f7b2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 60 deletions.
20 changes: 12 additions & 8 deletions projects/sdk/src/lib/swap/Swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ export class Swap {

for (let i = 0; i < route.length; i++) {
if (route.getStep(i - 1)) {
if (route.getStep(i - 1).from === "BEAN" && route.getStep(i - 1).to === "WETH" &&
route.getStep(i).from === "WETH" && route.getStep(i).to === "ETH") {
useAdvancedFarm = true;
break;
};
};
};
if (
route.getStep(i - 1).from === "BEAN" &&
route.getStep(i - 1).to === "WETH" &&
route.getStep(i).from === "WETH" &&
route.getStep(i).to === "ETH"
) {
useAdvancedFarm = true;
break;
}
}
}

if (useAdvancedFarm) {
workflow = Swap.sdk.farm.createAdvancedFarm(`Swap ${tokenIn.symbol}->${tokenOut.symbol}`);
Expand Down Expand Up @@ -81,7 +85,7 @@ export class Swap {
}
}

const op = new SwapOperation(Swap.sdk, tokenIn, tokenOut, workflow, route);
const op = new SwapOperation(Swap.sdk, tokenIn, tokenOut, workflow, route, _from, _to);

return op;
}
Expand Down
8 changes: 5 additions & 3 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,8 +18,10 @@ export class SwapOperation {
sdk: BeanstalkSDK,
readonly tokenIn: Token,
readonly tokenOut: Token,
private readonly workflow: AdvancedFarmWorkflow|Workflow,
private readonly route: Route
private readonly workflow: AdvancedFarmWorkflow | Workflow,
private readonly route: Route,
readonly from?: FarmFromMode,
readonly to?: FarmToMode
) {
SwapOperation.sdk = sdk;
sdk.debug(`new SwapOperation(): ${this.getDisplay()}`);
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) => {
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
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

0 comments on commit cf4f7b2

Please sign in to comment.