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

feat: add slippage endpoint #67

Merged
merged 1 commit into from
Apr 16, 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: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './wrappedAsset';
export * from './types';
export * from './quotes';
export * from './slippage';
export * from './utils/constants';
export * from './utils/permit';
export * from './utils/sources';
92 changes: 92 additions & 0 deletions sdk/src/slippage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { BigNumberish } from '@ethersproject/bignumber';
import {
ChainId,
EthereumAddress,
Slippage,
SlippageError,
SlippageParams,
} from './types';
import { API_BASE_URL } from './utils/constants';

/**
* Function to get a slippage formatted quote url to use with backend
*
* @param {ChainId} params.chainId
* @param {ChainId} params.toChainId
* @param {EthereumAddress} params.sellTokenAddress
* @param {EthereumAddress} params.buyTokenAddress
* @param {BigNumberish} params.buyAmount
* @param {BigNumberish} params.sellAmount
* @returns {string}
*/
const buildRainbowSlippageUrl = ({
chainId,
toChainId,
sellTokenAddress,
buyTokenAddress,
buyAmount,
sellAmount,
}: {
chainId: number;
toChainId?: number;
sellTokenAddress: EthereumAddress;
buyTokenAddress: EthereumAddress;
buyAmount?: BigNumberish;
sellAmount?: BigNumberish;
}) => {
const searchParams = new URLSearchParams({
buyToken: buyTokenAddress,
chainId: String(chainId),
sellToken: sellTokenAddress,
toChainId: String(toChainId),
...(sellAmount
? { sellAmount: String(sellAmount) }
: { buyAmount: String(buyAmount) }),
});
return `${API_BASE_URL}/v1/slippage?` + searchParams.toString();
};

/**
* Function to get slippage from rainbow's swap aggregator backend
*
* @param {SlippageParams} params
* @param {ChainId} params.chainId
* @param {ChainId} params.toChainId
* @param {EthereumAddress} params.sellTokenAddress
* @param {EthereumAddress} params.buyTokenAddress
* @param {BigNumberish} params.sellAmount
* @param {BigNumberish} params.buyAmount
* @returns {Promise<Slippage | SlippageError | null>}
*/
export const getSlippage = async (
params: SlippageParams
): Promise<Slippage | SlippageError | null> => {
const {
chainId = ChainId.mainnet,
toChainId,
sellTokenAddress,
buyTokenAddress,
sellAmount,
buyAmount,
} = params;

if (isNaN(Number(sellAmount)) && isNaN(Number(buyAmount))) {
return null;
}

const url = buildRainbowSlippageUrl({
buyAmount,
buyTokenAddress,
chainId,
sellAmount,
sellTokenAddress,
toChainId,
});

const response = await fetch(url);
const slippage = await response.json();
if (slippage.error) {
return slippage as SlippageError;
}
return slippage?.data as Slippage;
};
19 changes: 19 additions & 0 deletions sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,22 @@ export interface CrosschainQuoteExecutionDetails {
method: any;
params: TransactionOptions;
}

export interface SlippageParams {
chainId: number;
sellTokenAddress: EthereumAddress;
buyTokenAddress: EthereumAddress;
sellAmount?: BigNumberish;
buyAmount?: BigNumberish;
toChainId?: number;
}

export interface SlippageError {
error: boolean;
error_code?: number;
message: string;
}

export interface Slippage {
slippagePercent: number;
}
Loading