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: exposing relay source on crosschain swaps #76

Merged
merged 2 commits into from
Jun 21, 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
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
24 changes: 23 additions & 1 deletion sdk/tests/utils/build_url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
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 +39,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'
);
});
});
Loading