Skip to content

Commit

Permalink
chore: remove unused code, remove guardrails that cause issues with n…
Browse files Browse the repository at this point in the history
…ew providers (#88)

* chore: remove unused code, remove guardrails that cause issues with new providers

* introduce new field for approvals

* remove no_approval field instead of deprecating
  • Loading branch information
welps authored Oct 3, 2024
1 parent f894bf1 commit b19ae3c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 457 deletions.
1 change: 1 addition & 0 deletions sdk/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.DS_Store
node_modules
dist
.idea
91 changes: 10 additions & 81 deletions sdk/src/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
QuoteParams,
SocketChainsData,
Source,
SwapType,
TransactionOptions,
} from './types';
import {
Expand All @@ -26,14 +25,10 @@ import {
PERMIT_EXPIRATION_TS,
RAINBOW_ROUTER_CONTRACT_ADDRESS,
RAINBOW_ROUTER_CONTRACT_ADDRESS_ZORA,
WRAPPED_ASSET,
} from './utils/constants';
import { signPermit } from './utils/permit';
import { getReferrerCode } from './utils/referrer';
import {
extractDestinationAddress,
sanityCheckAddress,
} from './utils/sanity_check';
import { sanityCheckAddress } from './utils/sanity_check';

/**
* Function to get the rainbow router contract address based on the chainId
Expand Down Expand Up @@ -94,16 +89,10 @@ const buildRainbowQuoteUrl = ({
fromAddress,
sellToken: sellTokenAddress,
slippage: String(slippage),
swapType: SwapType.normal,
...(source ? { source } : {}),
...(sellAmount
? { sellAmount: String(sellAmount) }
: { buyAmount: String(buyAmount) }),
// When buying ETH, we need to tell the aggregator
// to return the funds to the contract if we need to take a fee
...(buyTokenAddress === ETH_ADDRESS
? { destReceiver: getRainbowRouterContractAddress(chainId) }
: {}),
...(feePercentageBasisPoints !== undefined
? { feePercentageBasisPoints: String(feePercentageBasisPoints) }
: {}),
Expand Down Expand Up @@ -157,13 +146,12 @@ export const buildRainbowCrosschainQuoteUrl = ({
sellAmount: String(sellAmount),
sellToken: sellTokenAddress,
slippage: String(slippage),
swapType: SwapType.crossChain,
toChainId: String(toChainId),
...(feePercentageBasisPoints !== undefined
? { feePercentageBasisPoints: String(feePercentageBasisPoints) }
: {}),
});
return `${API_BASE_URL}/v1/quote?bridgeVersion=3&` + searchParams.toString();
return `${API_BASE_URL}/v1/quote?bridgeVersion=4&` + searchParams.toString();
};

/**
Expand Down Expand Up @@ -202,10 +190,9 @@ export const buildRainbowClaimBridgeQuoteUrl = ({
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();
return `${API_BASE_URL}/v1/quote?bridgeVersion=4&` + searchParams.toString();
};

/**
Expand Down Expand Up @@ -270,51 +257,6 @@ export const getQuote = async (
currency,
} = params;

const sellTokenAddressLowercase = sellTokenAddress.toLowerCase();
const buyTokenAddressLowercase = buyTokenAddress.toLowerCase();
const ethAddressLowerCase = ETH_ADDRESS.toLowerCase();
const wrappedAssetLowercase = WRAPPED_ASSET[chainId]?.toLowerCase();
const isWrap =
sellTokenAddressLowercase === ethAddressLowerCase &&
buyTokenAddressLowercase === wrappedAssetLowercase;
const isUnwrap =
sellTokenAddressLowercase === wrappedAssetLowercase &&
buyTokenAddressLowercase === ethAddressLowerCase;

// When wrapping or unwrapping ETH, the quote is always 1:1
// so we don't need to call our backend.
if (isWrap || isUnwrap) {
const amount = sellAmount || buyAmount;
// For wrapping/unwrapping, we need either sell amount or buy amount
if (!amount) {
return null;
}

return {
buyAmount: amount,
buyAmountDisplay: amount,
buyAmountDisplayMinimum: amount,
buyAmountInEth: amount,
buyAmountMinusFees: amount,
buyTokenAddress,
chainId,
defaultGasLimit: isWrap ? '30000' : '40000',
fee: 0,
feeInEth: 0,
feePercentageBasisPoints: 0,
from: fromAddress,
inputTokenDecimals: 18,
outputTokenDecimals: 18,
sellAmount: amount,
sellAmountDisplay: amount,
sellAmountInEth: amount,
sellAmountMinusFees: amount,
sellTokenAddress,
tradeAmountUSD: 0,
tradeFeeAmountUSD: 0,
};
}

if (isNaN(Number(sellAmount)) && isNaN(Number(buyAmount))) {
return null;
}
Expand Down Expand Up @@ -441,24 +383,19 @@ const fetchAndSanityCheckCrosschainQuote = async (
return quote as QuoteError;
}

const quoteWithRestrictedAllowanceTarget = quote as CrosschainQuote;
try {
quoteWithRestrictedAllowanceTarget.allowanceTarget = sanityCheckAddress(
quoteWithRestrictedAllowanceTarget.source,
quoteWithRestrictedAllowanceTarget.chainId,
quoteWithRestrictedAllowanceTarget.allowanceTarget
);
sanityCheckAddress(quote?.to);
} catch (e) {
return {
error: true,
message:
e instanceof Error
? e.message
: `unexpected error happened while checking crosschain quote's address: ${quoteWithRestrictedAllowanceTarget.allowanceTarget}`,
: `unexpected error happened while checking crosschain quote's address: ${quote?.to}`,
} as QuoteError;
}

return quoteWithRestrictedAllowanceTarget;
return quote;
};

const calculateDeadline = async (wallet: Wallet) => {
Expand Down Expand Up @@ -629,11 +566,7 @@ export const fillCrosschainQuote = async (
): Promise<Transaction> => {
const { data, from, value } = quote;

const to = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
);
sanityCheckAddress(quote?.to);

let txData = data;
if (referrer) {
Expand All @@ -643,7 +576,7 @@ export const fillCrosschainQuote = async (
const swapTx = await wallet.sendTransaction({
data: txData,
from,
to,
to: quote.to,
...{
...transactionOptions,
value,
Expand Down Expand Up @@ -733,17 +666,13 @@ export const getCrosschainQuoteExecutionDetails = (
): CrosschainQuoteExecutionDetails => {
const { from, data, value } = quote;

const to = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
);
sanityCheckAddress(quote?.to);

return {
method: provider.estimateGas({
data,
from,
to,
to: quote.to,
value,
}),
params: {
Expand Down
17 changes: 9 additions & 8 deletions sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ export enum Source {
export enum SwapType {
normal = 'normal',
crossChain = 'cross-chain',
wrap = 'wrap',
unwrap = 'unwrap',
}

export type EthereumAddress = string;

// QuoteParams are the parameters required to get a quote from the Swap API
export interface QuoteParams {
source?: Source;
chainId: number;
Expand All @@ -49,7 +52,6 @@ export interface QuoteParams {
slippage: number;
destReceiver?: EthereumAddress;
refuel?: boolean;
swapType: SwapType;
feePercentageBasisPoints?: number;
toChainId?: number;
currency: string;
Expand All @@ -60,12 +62,14 @@ export interface ProtocolShare {
part: number;
}

// QuoteError is returned when a swap quote failed
export interface QuoteError {
error: boolean;
error_code?: number;
message: string;
}

// Quote is the response from the Swap API
export interface Quote {
source?: Source;
from: EthereumAddress;
Expand Down Expand Up @@ -93,11 +97,13 @@ export interface Quote {
inputTokenDecimals?: number;
outputTokenDecimals?: number;
defaultGasLimit?: string;
swapType?: string;
swapType: SwapType;
tradeAmountUSD: number;
tradeFeeAmountUSD: number;
rewards?: Reward[];
chainId: number;
allowanceTarget: string;
allowanceNeeded: boolean;
}

export interface TokenAsset {
Expand Down Expand Up @@ -257,15 +263,10 @@ export interface SocketChainsData {
}[];
}

// CrosschainQuote holds additional fields relevant for crosschain swaps
export interface CrosschainQuote extends Quote {
fromAsset: SocketAsset;
fromChainId: number;
toAsset: SocketAsset;
toChainId: number;
allowanceTarget?: string;
routes: SocketRoute[];
refuel: SocketRefuelData | null;
no_approval: boolean | undefined;
}

export interface TransactionOptions {
Expand Down
23 changes: 3 additions & 20 deletions sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,13 @@ export const RAINBOW_ROUTER_CONTRACT_ADDRESS =
export const RAINBOW_ROUTER_CONTRACT_ADDRESS_ZORA =
'0xa61550e9ddd2797e16489db09343162be98d9483';

// SOCKET_GATEWAY_CONTRACT_ADDRESSES is mapped by int chain ID to avoid a breaking change
export const SOCKET_GATEWAY_CONTRACT_ADDRESSESS = new Map([
[ChainId.mainnet, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.optimism, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.polygon, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.arbitrum, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.bsc, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.zora, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.base, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.avalanche, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.blast, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
[ChainId.apechain, '0x3a23F943181408EAC424116Af7b7790c94Cb97a5'],
]);

// RELAY_LINK_BRIDGING_RELAYER_ADDRESS is the EOA used by relay link as relayer on all chains
export const RELAY_LINK_BRIDGING_RELAYER_ADDRESS =
'0xf70da97812CB96acDF810712Aa562db8dfA3dbEF';

export const ERC20_TRANSFER_SIGNATURE = `0xa9059cbb`;

export type MultiChainAsset = {
[key: string]: EthereumAddress;
};

/**
* @deprecated: Should not use this anymore to reduce friction for new network support
*/
export const WRAPPED_ASSET: MultiChainAsset = {
[`${ChainId.mainnet}`]: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
[`${ChainId.optimism}`]: '0x4200000000000000000000000000000000000006',
Expand Down
Loading

0 comments on commit b19ae3c

Please sign in to comment.