Skip to content

Commit

Permalink
feat: add slippage endpoint (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
welps authored Apr 16, 2024
1 parent 84dd595 commit 10383b0
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
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;
}

0 comments on commit 10383b0

Please sign in to comment.