Skip to content

Commit

Permalink
fix: should not replace to address
Browse files Browse the repository at this point in the history
  • Loading branch information
fringlesinthestreet committed Apr 30, 2024
1 parent ae95fc5 commit cc97fbf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 53 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.19.0-beta.1",
"version": "0.19.0-beta.2",
"name": "@rainbow-me/swaps",
"license": "GPL-3.0",
"main": "dist/index.js",
Expand Down
18 changes: 8 additions & 10 deletions sdk/src/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import { signPermit } from './utils/permit';
import { getReferrerCode } from './utils/referrer';
import {
extractDestinationAddress,
sanityCheckAllowanceAddress,
sanityCheckDestinationAddress,
sanityCheckAddress,
} from './utils/sanity_check';

/**
Expand Down Expand Up @@ -323,12 +322,11 @@ export const getCrosschainQuote = async (

const quoteWithRestrictedAllowanceTarget = quote as CrosschainQuote;
try {
quoteWithRestrictedAllowanceTarget.allowanceTarget =
sanityCheckAllowanceAddress(
quoteWithRestrictedAllowanceTarget.source,
quoteWithRestrictedAllowanceTarget.chainId,
quoteWithRestrictedAllowanceTarget.allowanceTarget
);
quoteWithRestrictedAllowanceTarget.allowanceTarget = sanityCheckAddress(
quoteWithRestrictedAllowanceTarget.source,
quoteWithRestrictedAllowanceTarget.chainId,
quoteWithRestrictedAllowanceTarget.allowanceTarget
);
} catch (e) {
return {
error: true,
Expand Down Expand Up @@ -510,7 +508,7 @@ export const fillCrosschainQuote = async (
): Promise<Transaction> => {
const { data, from, value } = quote;

const to = sanityCheckDestinationAddress(
const to = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
Expand Down Expand Up @@ -613,7 +611,7 @@ export const getCrosschainQuoteExecutionDetails = (
provider: StaticJsonRpcProvider
): CrosschainQuoteExecutionDetails => {
const { from, data, value } = quote;
const to = sanityCheckDestinationAddress(
const to = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
Expand Down
67 changes: 25 additions & 42 deletions sdk/src/utils/sanity_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,6 @@ import {
SOCKET_GATEWAY_CONTRACT_ADDRESSESS,
} from './constants';

/**
* Sanity checks the quote's allowance address against the expected address stored in the SDK.
* Relay works with an EOA an direct transfers, so no allowance is expected.
*
* @param quoteSource The aggregator used for the quote.
* @param chainID The origin network chain ID for the quote.
* @param assertedAddress The allowance address provided by the quote.
* @returns {string} The allowance address expected in the SDK for the provided (source, chainID) combination.
* @throws {Error} Throws an error if any of the following conditions are met:
* - It's a relay's quote and the quote's allowance address is not empty/undefined.
* - It's a socket's quote and the quote's allowance address is undefined or different to the expected one.
* - No destination address is defined in the SDK for the provided (source, chainID) combination.
*/
export function sanityCheckAllowanceAddress(
quoteSource: Source | undefined,
chainID: ChainId,
assertedAddress: string | undefined
): string {
const validSource = quoteSource !== undefined;
if (validSource && quoteSource === Source.CrosschainAggregatorSocket) {
return sanityCheckDestinationAddress(quoteSource, chainID, assertedAddress);
}
if (validSource && quoteSource === Source.CrosschainAggregatorRelay) {
if (assertedAddress === undefined || assertedAddress === '') {
return '';
}
throw new Error(
`relay should not bring allowance address: ${assertedAddress}`
);
}
throw new Error(`unknown crosschain swap source ${quoteSource}`);
}

/**
* Sanity checks the quote's returned address against the expected address stored in the SDK.
* This function ensures the integrity and correctness of the destination address provided by the quote source.
Expand All @@ -52,7 +19,7 @@ export function sanityCheckAllowanceAddress(
* - No destination address is defined in the SDK for the provided (source, chainID) combination.
* - The provided quote's destination address does not case-insensitively match the SDK's stored destination address.
*/
export function sanityCheckDestinationAddress(
export function sanityCheckAddress(
quoteSource: Source | undefined,
chainID: ChainId,
assertedAddress: string | undefined
Expand All @@ -62,7 +29,10 @@ export function sanityCheckDestinationAddress(
`quote's destination addresses must be defined (API Response)`
);
}
let expectedAddress = getExpectedDestinationAddress(quoteSource, chainID);
const { expectedAddress, shouldOverride } = getExpectedDestinationAddress(
quoteSource,
chainID
);
if (expectedAddress === undefined || expectedAddress === '') {
throw new Error(
`expected source ${quoteSource}'s destination address on chainID ${chainID} must be defined (Swap SDK)`
Expand All @@ -73,7 +43,7 @@ export function sanityCheckDestinationAddress(
`source ${quoteSource}'s destination address '${assertedAddress}' on chainID ${chainID} is not consistent, expected: '${expectedAddress}'`
);
}
return expectedAddress!.toString();
return shouldOverride ? expectedAddress!.toString() : assertedAddress;
}

/**
Expand Down Expand Up @@ -163,18 +133,31 @@ export function decodeERC20TransferToData(
*
* @param quoteSource The aggregator used for the quote.
* @param chainID The origin network chain ID for the quote.
* @returns {string | undefined} The destination address stored in the SDK for the provided (source, chainID) combination.
* Returns `undefined` if no address is stored for the specified combination.
* @returns {string | undefined, boolean} The destination address stored in the SDK for the provided (source, chainID)
* combination and if we need to overwrite it on the quote.
* Returns `undefined` if there is no address for the specified combination.
*/
export function getExpectedDestinationAddress(
quoteSource: Source | undefined,
chainID: ChainId
): string | undefined {
): {
expectedAddress: string | undefined;
shouldOverride: boolean;
} {
const validSource = quoteSource !== undefined;
if (validSource && quoteSource === Source.CrosschainAggregatorSocket) {
return SOCKET_GATEWAY_CONTRACT_ADDRESSESS.get(chainID);
return {
expectedAddress: SOCKET_GATEWAY_CONTRACT_ADDRESSESS.get(chainID),
shouldOverride: true,
};
} else if (validSource && quoteSource === Source.CrosschainAggregatorRelay) {
return RELAY_LINK_BRIDGING_RELAYER_ADDRESS;
return {
expectedAddress: RELAY_LINK_BRIDGING_RELAYER_ADDRESS,
shouldOverride: false,
};
}
return undefined;
return {
expectedAddress: undefined,
shouldOverride: false,
};
}

0 comments on commit cc97fbf

Please sign in to comment.