Skip to content

Commit

Permalink
feat: tests and lsat details
Browse files Browse the repository at this point in the history
  • Loading branch information
fringlesinthestreet committed Apr 30, 2024
1 parent cc97fbf commit 8d8cf7b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 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.2",
"version": "0.19.0",
"name": "@rainbow-me/swaps",
"license": "GPL-3.0",
"main": "dist/index.js",
Expand Down
18 changes: 15 additions & 3 deletions sdk/src/quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,14 @@ export const getCrosschainQuote = async (

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

const to = sanityCheckAddress(
let to = quote.to;
const { expectedAddress, shouldOverride } = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
);
if (shouldOverride) {
to = expectedAddress;
}

let txData = data;
if (referrer) {
Expand Down Expand Up @@ -611,11 +618,16 @@ export const getCrosschainQuoteExecutionDetails = (
provider: StaticJsonRpcProvider
): CrosschainQuoteExecutionDetails => {
const { from, data, value } = quote;
const to = sanityCheckAddress(

let to = quote.to;
const { expectedAddress, shouldOverride } = sanityCheckAddress(
quote.source,
quote.fromChainId,
extractDestinationAddress(quote)
);
if (shouldOverride) {
to = expectedAddress;
}

return {
method: provider.estimateGas({
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from '@ethersproject/bignumber';
import { ChainId, EthereumAddress } from '../types';
export const ETH_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
export const API_BASE_URL = 'https://swap.s.rainbow.me';
export const API_BASE_URL = 'https://swap.p.rainbow.me';
export const RAINBOW_ROUTER_CONTRACT_ADDRESS =
'0x00000000009726632680fb29d3f7a9734e3010e2';

Expand Down
10 changes: 7 additions & 3 deletions sdk/src/utils/sanity_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
* @param quoteSource The aggregator used for the quote.
* @param chainID The origin network chain ID for the quote.
* @param assertedAddress The destination address provided by the quote.
* @returns {string} The destination address stored in the SDK for the provided (source, chainID) combination.
* @returns {string, boolean} The destination address stored in the SDK for the provided (source, chainID) combination.
* And if it should be overridden in the quote.
* @throws {Error} Throws an error if any of the following conditions are met:
* - The quote's destination address is undefined.
* - No destination address is defined in the SDK for the provided (source, chainID) combination.
Expand All @@ -23,7 +24,10 @@ export function sanityCheckAddress(
quoteSource: Source | undefined,
chainID: ChainId,
assertedAddress: string | undefined
): string {
): {
expectedAddress: string;
shouldOverride: boolean;
} {
if (assertedAddress === undefined || assertedAddress === '') {
throw new Error(
`quote's destination addresses must be defined (API Response)`
Expand All @@ -43,7 +47,7 @@ export function sanityCheckAddress(
`source ${quoteSource}'s destination address '${assertedAddress}' on chainID ${chainID} is not consistent, expected: '${expectedAddress}'`
);
}
return shouldOverride ? expectedAddress!.toString() : assertedAddress;
return { expectedAddress, shouldOverride };
}

/**
Expand Down
54 changes: 54 additions & 0 deletions sdk/tests/utils/sanity_check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ChainId, CrosschainQuote, EthereumAddress, Source } from '../../src';
import {
decodeERC20TransferToData,
extractDestinationAddress,
getExpectedDestinationAddress,
sanityCheckAddress,
} from '../../src/utils/sanity_check';

const okERC20Data =
Expand Down Expand Up @@ -97,3 +99,55 @@ describe('decodeERC20TransferData', () => {
);
});
});

describe('getExpectedDestinationAddress', () => {
it('should return expected and true for socket', () => {
expect(
getExpectedDestinationAddress(
Source.CrosschainAggregatorSocket,
ChainId.mainnet
)
).toEqual({
expectedAddress: '0x3a23F943181408EAC424116Af7b7790c94Cb97a5',
shouldOverride: true,
});
});
it('should return expected and false for relay', () => {
expect(
getExpectedDestinationAddress(
Source.CrosschainAggregatorRelay,
ChainId.mainnet
)
).toEqual({
expectedAddress: '0xf70da97812CB96acDF810712Aa562db8dfA3dbEF',
shouldOverride: false,
});
});
});

describe('sanityCheckAddress', () => {
it('should return expected and true for socket', () => {
expect(
sanityCheckAddress(
Source.CrosschainAggregatorSocket,
ChainId.mainnet,
'0x3a23F943181408EAC424116Af7b7790c94Cb97a5'
)
).toEqual({
expectedAddress: '0x3a23F943181408EAC424116Af7b7790c94Cb97a5',
shouldOverride: true,
});
});
it('should return expected and false for relay', () => {
expect(
sanityCheckAddress(
Source.CrosschainAggregatorRelay,
ChainId.mainnet,
'0xf70da97812CB96acDF810712Aa562db8dfA3dbEF'
)
).toEqual({
expectedAddress: '0xf70da97812CB96acDF810712Aa562db8dfA3dbEF',
shouldOverride: false,
});
});
});

0 comments on commit 8d8cf7b

Please sign in to comment.