Skip to content

Commit

Permalink
feat: exposing relay source on crosschain swaps
Browse files Browse the repository at this point in the history
  • Loading branch information
fringlesinthestreet committed Jun 21, 2024
1 parent 1932c6f commit 06783c0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.20.0",
"version": "0.21.0",
"name": "@rainbow-me/swaps",
"license": "GPL-3.0",
"main": "dist/index.js",
Expand Down
87 changes: 85 additions & 2 deletions sdk/src/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,45 @@ export const buildRainbowCrosschainQuoteUrl = ({
return `${API_BASE_URL}/v1/quote?bridgeVersion=3&` + searchParams.toString();
};

/**
* Function to build the swap API URI to get the claim bridge quote
*/
export const buildRainbowClaimBridgeQuoteUrl = ({
chainId,
toChainId,
sellTokenAddress,
buyTokenAddress,
sellAmount,
fromAddress,
slippage,
refuel,
}: {
chainId: number;
toChainId?: number;
sellTokenAddress: EthereumAddress;
buyTokenAddress: EthereumAddress;
sellAmount?: BigNumberish;
fromAddress: EthereumAddress;
slippage: number;
refuel?: boolean;
}) => {
const searchParams = new URLSearchParams({
buyToken: buyTokenAddress,
chainId: String(chainId),
claim: String(true),
feePercentageBasisPoints: '0',
fromAddress,
refuel: String(refuel),
sellAmount: String(sellAmount),
sellToken: sellTokenAddress,
slippage: String(slippage),
source: Source.CrosschainAggregatorRelay.toString(),
swapType: SwapType.crossChain,
toChainId: String(toChainId),
});
return `${API_BASE_URL}/v1/quote?bridgeVersion=3&` + searchParams.toString();
};

/**
* Function to get a minimum amount of source chain gas token to perform a refuel swap
*
Expand Down Expand Up @@ -322,7 +361,51 @@ export const getCrosschainQuote = async (
toChainId,
});

const response = await fetch(url);
return fetchAndSanityCheckCrosschainQuote(url);
};

/**
* Function to get a crosschain swap quote from rainbow's swap aggregator backend
*/
export const getClaimBridgeQuote = async (
params: QuoteParams
): Promise<CrosschainQuote | QuoteError | null> => {
const {
chainId = ChainId.optimism,
toChainId,
fromAddress,
sellTokenAddress,
buyTokenAddress,
sellAmount,
slippage,
refuel = false,
} = params;

if (!sellAmount || !toChainId) {
return null;
}

const url = buildRainbowClaimBridgeQuoteUrl({
buyTokenAddress,
chainId,
fromAddress,
refuel,
sellAmount,
sellTokenAddress,
slippage,
toChainId,
});

return fetchAndSanityCheckCrosschainQuote(url);
};

/**
* Function to encapsulate logic to fetch and check a crosschain quote
*/
const fetchAndSanityCheckCrosschainQuote = async (
crosschainQuoteURL: string
): Promise<CrosschainQuote | QuoteError | null> => {
const response = await fetch(crosschainQuoteURL);
const quote = await response.json();
if (quote.error) {
return quote as QuoteError;
Expand Down Expand Up @@ -363,7 +446,7 @@ const calculateDeadline = async (wallet: Wallet) => {
* @param {TransactionOptions} transactionOptions
* @param {Signer} wallet
* @param {boolean} permit
* @param {number} ChainId
* @param {number} chainId
* @param {string} referrer
* @returns {Promise<Transaction>}
*/
Expand Down
21 changes: 20 additions & 1 deletion sdk/tests/utils/build_url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildRainbowCrosschainQuoteUrl } from '../../src';
import {buildRainbowClaimBridgeQuoteUrl, buildRainbowCrosschainQuoteUrl} from '../../src';

describe('when creating crosschain swap URL', () => {
it('should consider feePercentageBasisPoints if passed', () => {
Expand Down Expand Up @@ -36,3 +36,22 @@ describe('when creating crosschain swap URL', () => {
);
});
});

describe('when creating claim bridge swap URL', () => {
it('should set feePercentageBasisPoints to 0 and claim as true if passed', () => {
expect(
buildRainbowClaimBridgeQuoteUrl({
buyTokenAddress: '0x456',
chainId: 1,
fromAddress: '0x789',
refuel: false,
sellAmount: '100',
sellTokenAddress: '0x123',
slippage: 2,
toChainId: 137,
})
).toEqual(
'https://swap.p.rainbow.me/v1/quote?bridgeVersion=3&buyToken=0x456&chainId=1&claim=true&feePercentageBasisPoints=0&fromAddress=0x789&refuel=false&sellAmount=100&sellToken=0x123&slippage=2&source=relay&swapType=cross-chain&toChainId=137'
);
});
});

0 comments on commit 06783c0

Please sign in to comment.