Skip to content

Commit d3f2635

Browse files
committed
allow rotating obligated token
1 parent 5d116e1 commit d3f2635

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ import React from 'react';
3737
import SwapWidget from '@ensofinance/shortcuts-widget';
3838

3939
const App = () => {
40-
return (
41-
<div>
42-
<SwapWidget apiKey={"YOUR_API_KEY"} />
40+
return (
41+
<div>
42+
<SwapWidget apiKey={"YOUR_API_KEY"} />
4343
</div>
44-
);
44+
);
4545
};
4646

4747
export default App;
@@ -60,7 +60,8 @@ The `SwapWidget` component accepts the following props:
6060
- `themeConfig` (SystemConfig): Optional theme configuration
6161
- `enableShare` (boolean): Enable route sharing functionality (copy with button)
6262
- `obligateSelection` (boolean): Obligate token selection
63-
- `indicateRoute` (boolean): Indicate the route of the swap
63+
- `rotateObligated` (boolean | 0 | 1): For use with `obligateSelection`, displays arrow to rotate the obligated token
64+
- `indicateRoute` (boolean): Displays shortcut execution route
6465

6566
## License
6667

widget/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ The `SwapWidget` component accepts the following props:
6060
- `themeConfig` (SystemConfig): Optional theme configuration
6161
- `enableShare` (boolean): Enable route sharing functionality (copy with button)
6262
- `obligateSelection` (boolean): Obligate token selection
63-
- `indicateRoute` (boolean): Indicate the route of the swap
63+
- `rotateObligated` (boolean | 0 | 1): For use with `obligateSelection`, displays arrow to rotate the obligated token
64+
- `indicateRoute` (boolean): Displays shortcut execution route
6465

6566
## License
6667

widget/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default ({
3535
obligateSelection,
3636
indicateRoute,
3737
adaptive,
38+
rotateObligated,
3839
}: WidgetProps & {
3940
apiKey: string;
4041
themeConfig?: SystemConfig;
@@ -101,6 +102,7 @@ export default ({
101102
<CacheProvider value={cache}>
102103
<ChakraProvider value={system}>
103104
<SwapWidget
105+
rotateObligated={rotateObligated}
104106
indicateRoute={indicateRoute}
105107
obligateSelection={obligateSelection}
106108
tokenIn={tokenIn?.toLowerCase() as Address}

widget/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ensofinance/shortcuts-widget",
3-
"version": "0.2.55",
3+
"version": "0.2.56",
44
"type": "module",
55
"homepage": "https://www.enso.build/",
66
"repository": {

widget/src/components/SwapWidget.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Flex,
77
IconButton,
88
Link,
9-
Tabs,
109
Text,
1110
useDisclosure,
1211
} from "@chakra-ui/react";
@@ -44,8 +43,8 @@ import Notification from "@/components/Notification";
4443
import { ClipboardLink, ClipboardRoot } from "@/components/ui/clipboard";
4544
import RouteIndication from "@/components/RouteIndication";
4645
import { Tooltip } from "@/components/ui/tooltip";
47-
import { NotifyType, WidgetProps } from "@/types";
4846
import Slippage from "@/components/Slippage";
47+
import { NotifyType, ObligatedToken, WidgetProps } from "@/types";
4948

5049
const SwapWidget = ({
5150
tokenOut: providedTokenOut,
@@ -54,12 +53,16 @@ const SwapWidget = ({
5453
enableShare,
5554
indicateRoute,
5655
adaptive,
56+
rotateObligated,
5757
}: WidgetProps) => {
5858
const [tokenIn, setTokenIn] = useState<Address>();
5959
const [valueIn, setValueIn] = useState("");
6060
const [warningAccepted, setWarningAccepted] = useState(false);
6161
const [tokenOut, setTokenOut] = useState<Address>();
6262
const [slippage, setSlippage] = useState(DEFAULT_SLIPPAGE);
63+
const [obligatedToken, setObligatedToken] = useState(
64+
obligateSelection && (rotateObligated ?? ObligatedToken.TokenOut),
65+
);
6366

6467
const chainId = usePriorityChainId();
6568
const wagmiChainId = useChainId();
@@ -218,6 +221,10 @@ const SwapWidget = ({
218221

219222
const limitInputTokens =
220223
chainId === mainnet.id && tokenOutInfo?.symbol === "UNI-V2";
224+
const displayTokenRotation =
225+
!obligateSelection ||
226+
rotateObligated ||
227+
typeof rotateObligated === "number";
221228

222229
return (
223230
<Box
@@ -250,7 +257,7 @@ const SwapWidget = ({
250257
<SwapInput
251258
title={"You pay"}
252259
limitTokens={limitInputTokens && MAINNET_ZAP_INPUT_TOKENS}
253-
obligatedToken={providedTokenIn && obligateSelection}
260+
obligatedToken={obligatedToken === ObligatedToken.TokenIn}
254261
portalRef={portalRef}
255262
tokenValue={tokenIn}
256263
tokenOnChange={setTokenIn}
@@ -259,7 +266,7 @@ const SwapWidget = ({
259266
usdValue={tokenInUsdPrice}
260267
/>
261268

262-
{!obligateSelection && (
269+
{displayTokenRotation && (
263270
<Flex justifyContent="center" alignItems="center">
264271
<IconButton
265272
borderRadius={"full"}
@@ -272,6 +279,13 @@ const SwapWidget = ({
272279
onClick={() => {
273280
const tempTokenIn = tokenIn;
274281

282+
if (obligateSelection)
283+
setObligatedToken((val) =>
284+
val === ObligatedToken.TokenIn
285+
? ObligatedToken.TokenOut
286+
: ObligatedToken.TokenIn,
287+
);
288+
275289
setTokenIn(tokenOut);
276290
setTokenOut(tempTokenIn);
277291
setValueIn(valueOut);
@@ -284,7 +298,7 @@ const SwapWidget = ({
284298

285299
<SwapInput
286300
disabled
287-
obligatedToken={providedTokenOut && obligateSelection}
301+
obligatedToken={obligatedToken === ObligatedToken.TokenOut}
288302
title={"You receive"}
289303
loading={routerLoading}
290304
portalRef={portalRef}

widget/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type WidgetProps = {
77
obligateSelection?: boolean;
88
enableShare?: boolean;
99
indicateRoute?: boolean;
10+
rotateObligated?: boolean | ObligatedToken;
1011
};
1112

1213
export enum NotifyType {
@@ -17,3 +18,8 @@ export enum NotifyType {
1718
Warning = "warning",
1819
Blocked = "blocked",
1920
}
21+
22+
export enum ObligatedToken {
23+
TokenIn,
24+
TokenOut,
25+
}

0 commit comments

Comments
 (0)