Skip to content

Commit 2a5317f

Browse files
authored
feat: fee router (#83)
* feat: taking feeParams as widget props and passing it to quote call * fix: types * v1.2.5
1 parent 9458fe8 commit 2a5317f

8 files changed

Lines changed: 135 additions & 96 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socket.tech/plugin",
3-
"version": "1.2.4",
3+
"version": "1.2.5",
44
"main": "dist/index.js",
55
"module": "dist/index.es.js",
66
"types": "dist/index.d.ts",

src/components/RouteDetails/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const RouteDetails = () => {
4444
const excludeBridges = useSelector(
4545
(state: any) => state.customSettings.excludeBridges
4646
);
47+
const feeParams = useSelector((state: any) => state.customSettings.feeParams);
4748
const isEnoughBalance = useSelector(
4849
(state: any) => state.amount.isEnoughBalance
4950
);
@@ -63,7 +64,9 @@ export const RouteDetails = () => {
6364
includeBridges,
6465
excludeBridges,
6566
singleTxOnly,
66-
swapSlippage
67+
swapSlippage,
68+
feeParams?.feeTakerAddress,
69+
feeParams?.feePercent
6770
);
6871

6972
// Boolean variable to fill all condition before the api call is made to fetch quotes.

src/hooks/apis/useRoutes.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export const useRoutes = (
1515
includeBridges,
1616
excludeBridges,
1717
singleTxOnly,
18-
swapSlippage
18+
swapSlippage,
19+
feeTakerAddress,
20+
feePercent
1921
) => {
2022
const isTxModalOpen = useSelector((state: any) => state.modals.isTxModalOpen);
2123
const shouldFetch =
@@ -36,7 +38,9 @@ export const useRoutes = (
3638
includeBridges,
3739
excludeBridges,
3840
singleTxOnly,
39-
swapSlippage
41+
swapSlippage,
42+
_feeTakerAddress,
43+
_feePercent,
4044
) {
4145
const { routes: quotes } = await socket.getAllQuotes(
4246
{
@@ -51,6 +55,8 @@ export const useRoutes = (
5155
excludeBridges,
5256
singleTxOnly,
5357
defaultSwapSlippage: swapSlippage,
58+
feeTakerAddress: _feeTakerAddress,
59+
feePercent: _feePercent
5460
}
5561
);
5662
return quotes;
@@ -69,6 +75,8 @@ export const useRoutes = (
6975
excludeBridges,
7076
singleTxOnly,
7177
swapSlippage,
78+
feeTakerAddress,
79+
feePercent,
7280
"quotes",
7381
]
7482
: null,

src/hooks/useCustomSettings.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
setExludeBridges,
1414
setSingleTxOnly,
1515
setApiKey,
16+
setFeeParams,
1617
} from "../state/customSettingsSlice";
1718
import { setSingleTxOnly as setSingleTxOnlyFromUser } from "../state/quotesSlice";
1819
import { formatRGB } from "../utils";
@@ -31,6 +32,7 @@ export const useCustomSettings = (props: WidgetProps) => {
3132
includeBridges,
3233
excludeBridges,
3334
singleTxOnly,
35+
feeParams,
3436
API_KEY,
3537
} = props;
3638
const dispatch = useDispatch();
@@ -50,6 +52,10 @@ export const useCustomSettings = (props: WidgetProps) => {
5052
dispatch(setExludeBridges(excludeBridges));
5153
dispatch(setSingleTxOnly(singleTxOnly));
5254

55+
if (feeParams?.feePercent && feeParams?.feeTakerAddress) {
56+
dispatch(setFeeParams(feeParams));
57+
}
58+
5359
// if singleTxOnly is set to true in the plugin config,
5460
// additionally set the in-plugin singleTxOnly value to true
5561
if (singleTxOnly) {

src/state/customSettingsSlice.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const initialState = {
1414
excludeBridges: null,
1515
singleTxOnly: false,
1616
apiKey: null,
17+
feeParams: null,
1718
};
1819

1920
const customSettingsSlice = createSlice({
@@ -58,6 +59,9 @@ const customSettingsSlice = createSlice({
5859
},
5960
setApiKey: (state, action) => {
6061
state.apiKey = action.payload
62+
},
63+
setFeeParams: (state, action) => {
64+
state.feeParams = action.payload
6165
}
6266
},
6367
});
@@ -76,7 +80,8 @@ export const {
7680
setIncludeBridges,
7781
setExludeBridges,
7882
setSingleTxOnly,
79-
setApiKey
83+
setApiKey,
84+
setFeeParams
8085
} = customSettingsSlice.actions;
8186

8287
// Note - Custom token list is not set here. Check out hooks/useTokenLists.ts

src/stories/Test.stories.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const Template = (args: WidgetProps) => {
107107
provider={provider}
108108
// defaultDestNetwork={_defaultDestNetwork}
109109
// destNetworks={_destNetworks}
110+
// feeParams={{feePercent: 2.5, feeTakerAddress: ''}}
110111
/>
111112
)}
112113
</div>

src/types/index.d.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface transactionDetails {
2121
destinationAmount: string;
2222
sourceToken: Currency; // 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee denotes native token
2323
destinationToken: Currency;
24-
txData: txData[] // tx hashes will be passed here
24+
txData: txData[]; // tx hashes will be passed here
2525
bridgeName?: string; // will be passed only in case of cross chain swaps
2626
estimatedServiceTime?: string; // (in ms) will be passed only in case of cross chain swaps
2727
dexName?: string; // will be passed only in case of same chain swaps
@@ -30,6 +30,11 @@ export interface transactionDetails {
3030
export type onNetworkChange = (network: Network) => void;
3131
export type onTokenChange = (token: Currency) => void;
3232

33+
export interface FeeParams {
34+
feePercent: number;
35+
feeTakerAddress: string;
36+
}
37+
3338
export interface WidgetProps {
3439
API_KEY: string;
3540
provider?: any;
@@ -93,10 +98,21 @@ export interface WidgetProps {
9398
// These messages are usually more human readable. Hence on our frontend we check for e.data.message || e.message
9499
onError?: (error: any) => void;
95100

96-
// Will be called when the cross-chain swap or same chain swap transaction is submitted.
101+
// Will be called when the cross-chain swap or same chain swap transaction is submitted.
97102
// This excludes the source and/or destination swap transactions in case of cross-chain swaps and only the bridging transaction will be considered
98103
onSubmit?: (data: transactionDetails) => void;
99104

105+
/**
106+
* Fee Params to collect fees
107+
*
108+
* feePercent
109+
* The % of fee to be cut from the source input token amount.
110+
* NOTE : Fee Percent can be up to three decimal places and cannot be more than 5%
111+
*
112+
* feeTakerAddress: the address where the fee will be sent to in the transaction
113+
*/
114+
feeParams?: FeeParams;
115+
100116
locale?: string;
101117
title?: ReactNode | string;
102118
customize?: Customize;

0 commit comments

Comments
 (0)