Skip to content

Commit

Permalink
feat: fee router (#83)
Browse files Browse the repository at this point in the history
* feat: taking feeParams as widget props and passing it to quote call

* fix: types

* v1.2.5
  • Loading branch information
salil-naik authored Sep 7, 2023
1 parent 9458fe8 commit 2a5317f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@socket.tech/plugin",
"version": "1.2.4",
"version": "1.2.5",
"main": "dist/index.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/components/RouteDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const RouteDetails = () => {
const excludeBridges = useSelector(
(state: any) => state.customSettings.excludeBridges
);
const feeParams = useSelector((state: any) => state.customSettings.feeParams);
const isEnoughBalance = useSelector(
(state: any) => state.amount.isEnoughBalance
);
Expand All @@ -63,7 +64,9 @@ export const RouteDetails = () => {
includeBridges,
excludeBridges,
singleTxOnly,
swapSlippage
swapSlippage,
feeParams?.feeTakerAddress,
feeParams?.feePercent
);

// Boolean variable to fill all condition before the api call is made to fetch quotes.
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/apis/useRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const useRoutes = (
includeBridges,
excludeBridges,
singleTxOnly,
swapSlippage
swapSlippage,
feeTakerAddress,
feePercent
) => {
const isTxModalOpen = useSelector((state: any) => state.modals.isTxModalOpen);
const shouldFetch =
Expand All @@ -36,7 +38,9 @@ export const useRoutes = (
includeBridges,
excludeBridges,
singleTxOnly,
swapSlippage
swapSlippage,
_feeTakerAddress,
_feePercent,
) {
const { routes: quotes } = await socket.getAllQuotes(
{
Expand All @@ -51,6 +55,8 @@ export const useRoutes = (
excludeBridges,
singleTxOnly,
defaultSwapSlippage: swapSlippage,
feeTakerAddress: _feeTakerAddress,
feePercent: _feePercent
}
);
return quotes;
Expand All @@ -69,6 +75,8 @@ export const useRoutes = (
excludeBridges,
singleTxOnly,
swapSlippage,
feeTakerAddress,
feePercent,
"quotes",
]
: null,
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useCustomSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
setExludeBridges,
setSingleTxOnly,
setApiKey,
setFeeParams,
} from "../state/customSettingsSlice";
import { setSingleTxOnly as setSingleTxOnlyFromUser } from "../state/quotesSlice";
import { formatRGB } from "../utils";
Expand All @@ -31,6 +32,7 @@ export const useCustomSettings = (props: WidgetProps) => {
includeBridges,
excludeBridges,
singleTxOnly,
feeParams,
API_KEY,
} = props;
const dispatch = useDispatch();
Expand All @@ -50,6 +52,10 @@ export const useCustomSettings = (props: WidgetProps) => {
dispatch(setExludeBridges(excludeBridges));
dispatch(setSingleTxOnly(singleTxOnly));

if (feeParams?.feePercent && feeParams?.feeTakerAddress) {
dispatch(setFeeParams(feeParams));
}

// if singleTxOnly is set to true in the plugin config,
// additionally set the in-plugin singleTxOnly value to true
if (singleTxOnly) {
Expand Down
7 changes: 6 additions & 1 deletion src/state/customSettingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const initialState = {
excludeBridges: null,
singleTxOnly: false,
apiKey: null,
feeParams: null,
};

const customSettingsSlice = createSlice({
Expand Down Expand Up @@ -58,6 +59,9 @@ const customSettingsSlice = createSlice({
},
setApiKey: (state, action) => {
state.apiKey = action.payload
},
setFeeParams: (state, action) => {
state.feeParams = action.payload
}
},
});
Expand All @@ -76,7 +80,8 @@ export const {
setIncludeBridges,
setExludeBridges,
setSingleTxOnly,
setApiKey
setApiKey,
setFeeParams
} = customSettingsSlice.actions;

// Note - Custom token list is not set here. Check out hooks/useTokenLists.ts
1 change: 1 addition & 0 deletions src/stories/Test.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const Template = (args: WidgetProps) => {
provider={provider}
// defaultDestNetwork={_defaultDestNetwork}
// destNetworks={_destNetworks}
// feeParams={{feePercent: 2.5, feeTakerAddress: ''}}
/>
)}
</div>
Expand Down
20 changes: 18 additions & 2 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface transactionDetails {
destinationAmount: string;
sourceToken: Currency; // 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee denotes native token
destinationToken: Currency;
txData: txData[] // tx hashes will be passed here
txData: txData[]; // tx hashes will be passed here
bridgeName?: string; // will be passed only in case of cross chain swaps
estimatedServiceTime?: string; // (in ms) will be passed only in case of cross chain swaps
dexName?: string; // will be passed only in case of same chain swaps
Expand All @@ -30,6 +30,11 @@ export interface transactionDetails {
export type onNetworkChange = (network: Network) => void;
export type onTokenChange = (token: Currency) => void;

export interface FeeParams {
feePercent: number;
feeTakerAddress: string;
}

export interface WidgetProps {
API_KEY: string;
provider?: any;
Expand Down Expand Up @@ -93,10 +98,21 @@ export interface WidgetProps {
// These messages are usually more human readable. Hence on our frontend we check for e.data.message || e.message
onError?: (error: any) => void;

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

/**
* Fee Params to collect fees
*
* feePercent
* The % of fee to be cut from the source input token amount.
* NOTE : Fee Percent can be up to three decimal places and cannot be more than 5%
*
* feeTakerAddress: the address where the fee will be sent to in the transaction
*/
feeParams?: FeeParams;

locale?: string;
title?: ReactNode | string;
customize?: Customize;
Expand Down
Loading

0 comments on commit 2a5317f

Please sign in to comment.