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

intercept and mock e2e swaps calls #1754

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
db0810f
prevent useSwapSlippage from making unnecessary requests
greg-schrammel Oct 31, 2024
9164172
test
greg-schrammel Nov 1, 2024
1cd7b09
response
greg-schrammel Nov 4, 2024
2119c2d
popular in rainbow
greg-schrammel Nov 5, 2024
a5e1c8f
dad can you come pick me up im scared
greg-schrammel Nov 12, 2024
8624638
Merge branch 'master' into greg-mock-swap-e2e
BrodyHughes Nov 13, 2024
1752d7f
yarn setup ran lol
BrodyHughes Nov 13, 2024
5efb232
hmm
BrodyHughes Nov 13, 2024
becb035
fix test 1, test test 2
BrodyHughes Nov 14, 2024
06942ad
logging
BrodyHughes Nov 14, 2024
52efb0b
skip test
BrodyHughes Nov 19, 2024
37d9f68
okay
BrodyHughes Nov 19, 2024
5675d1a
audit fix
BrodyHughes Nov 19, 2024
74e8084
yarn install
BrodyHughes Nov 19, 2024
e9d33be
fix send
BrodyHughes Nov 19, 2024
1c3e162
reenable retries
BrodyHughes Nov 19, 2024
37d7bfc
add all mocks, fix unit tests by making them use hardhat now
BrodyHughes Nov 21, 2024
64d38ff
rm retry:0
BrodyHughes Nov 21, 2024
4030edf
clean up
BrodyHughes Nov 21, 2024
235db69
check vlock number add new hashed responses
BrodyHughes Dec 3, 2024
0de1dfa
Merge branch 'master' into greg-mock-swap-e2e
BrodyHughes Dec 3, 2024
25d7ebd
tweak script and get new responses + yarn install
BrodyHughes Dec 3, 2024
924befc
okay point the script at the anvil instance instead of our prod key
BrodyHughes Dec 3, 2024
aff68a2
undo the removal of chainIds from testIds (#1779)
BrodyHughes Dec 12, 2024
c3a70cc
Merge branch 'master' into greg-mock-swap-e2e
BrodyHughes Dec 12, 2024
6995691
bad merge oops
BrodyHughes Dec 12, 2024
d988f48
Merge branch 'master' into greg-mock-swap-e2e
BrodyHughes Dec 12, 2024
60196a9
failOnError: false
BrodyHughes Dec 12, 2024
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
29 changes: 29 additions & 0 deletions e2e/fetchResponses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-var-requires */

// this file is ran locally with ts-node to fetch the responses from the swap quotes urls
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran locally to get swap_quotes outputs

require('dotenv').config();
const { writeFile } = require('fs/promises');

const { createClient, http, sha256 } = require('viem');
const { getBlockNumber } = require('viem/actions');

const urls = require('./mocks/mock_swap_quotes_urls.json');

(async () => {
const client = createClient({ transport: http(process.env.ETH_MAINNET_RPC) });
const blockNumber = await getBlockNumber(client);

// check initial blockNumber
console.log('OUTSIDE FUNCTION', blockNumber.toString());

await Promise.all(
urls.map(async (url: RequestInfo | URL) => {
const res = await fetch(url).then((r) => r.text());
const hash = sha256(url);

// check if blockNumber is the same the entire time
console.log('INSIDE FUNCTION', blockNumber.toString());
BrodyHughes marked this conversation as resolved.
Show resolved Hide resolved
await writeFile(`e2e/mocks/swap_quotes/${hash}.json`, res);
}),
);
})();
38 changes: 38 additions & 0 deletions e2e/mockFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Hex, sha256 } from 'viem';

export function mockFetch() {
const nativeFetch = window.fetch;
window.fetch = async function mockedFetch(
input: RequestInfo | URL,
init?: RequestInit,
) {
if (input instanceof Request || !URL.canParse(input))
return nativeFetch(input, init);

const url = new URL(input);

if (url.hostname === 'swap.p.rainbow.me') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intercepts requests to swap.p.rainbow.me and responds with our mocked responses

console.log('Intercepting swap request:', {
url: url.href,
params: Object.fromEntries(url.searchParams),
});

const hash = sha256(url.href as Hex);
console.log('Looking for mock file with hash:', hash);

const response = await import(`./mocks/swap_quotes/${hash}.json`);
console.log('Mock response:', response);

if (!response)
throw new Error('no response for request', {
cause: { url: url.href, hash },
});

return new Response(JSON.stringify(response), {
headers: { 'Content-Type': 'application/json' },
});
}

return nativeFetch(input, init);
};
}
169 changes: 169 additions & 0 deletions e2e/mocks/mock_swap_quotes_urls.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"error":true,"error_code":504,"message":"no routes found"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"slippagePercent":2}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"sellTokenAddress":"0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee","sellTokenAsset":{"assetCode":"eth","decimals":18,"iconUrl":"https://rainbowme-res.cloudinary.com/image/upload/v1668565116/assets/ethereum/eth.png","name":"Ethereum","network":"mainnet","symbol":"ETH","networks":{"1":{"address":"eth","decimals":18},"10":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"42161":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"56":{"address":"0x2170ed0880ac9a755fd29b2688956bd959f933f8","decimals":18},"7777777":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"81457":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"8453":{"address":"0x0000000000000000000000000000000000000000","decimals":18}},"chainId":1,"price":{"value":3582.92,"available":true},"totalPrice":{"value":358292002.19248796,"available":true}},"buyTokenAddress":"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599","buyTokenAsset":{"assetCode":"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599","decimals":8,"iconUrl":"https://rainbowme-res.cloudinary.com/image/upload/v1693266228/assets/ethereum/0x2260fac5e5542a773aa44fbcfedf7c193bc2c599.png","name":"Wrapped Bitcoin","network":"mainnet","symbol":"WBTC","networks":{"1":{"address":"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599","decimals":8},"10":{"address":"0x68f180fcce6836688e9084f035309e29bf0a2095","decimals":8},"137":{"address":"0x1bfd67037b42cf73acf2047067bd4f2c47d9bfd6","decimals":8},"42161":{"address":"0x2f2a2543b76a4166549f7aab2e75bef0aefc5b0f","decimals":8},"43114":{"address":"0x50b7545627a5162f82a992c33b87adc75187b218","decimals":8},"81457":{"address":"0xf7bc58b8d8f97adc129cfc4c9f45ce3c0e1d2692","decimals":8}},"chainId":1,"price":{"value":95075.26989325,"available":true},"totalPrice":{"value":73751688.77119744,"available":true}},"allowanceTarget":"0x00000000009726632680fb29d3f7a9734e3010e2","allowanceNeeded":false,"to":"0xdef1c0ded9bec7f1a1670819833240f027b25eff","data":"0x415565b0000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000000000000000000000000014feeeab6899a69b0be700000000000000000000000000000000000000000000000000000011e1570d3300000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000e400000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000014feeeab6899a69b0be7000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000ca000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000c600000000000000000000000000000000000000000000000000000000000000c600000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000014feeeab6899a69b0be700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c60000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000006200000000000000000000000000000000000000000000000000000000000000740000000000000000000000000000000000000000000000000000000000000088000000000000000000000000000000012556e69737761705633000000000000000000000000000000000000000000000000000000000001931eb34171ec79f218000000000000000000000000000000000000000000000000000000055f94a0ef000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000e592427a0aece92de3edee1f18e0157c0586156400000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb82260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000000000000000000000000000000000000000000000012556e69737761705633000000000000000000000000000000000000000000000000000000000001931eb34171ec79f21800000000000000000000000000000000000000000000000000000004dc50b4fc000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000e592427a0aece92de3edee1f18e0157c0586156400000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002bc02aaa39b223fe8d0a0e5c4f27ead9083c756cc20001f42260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000000000000000000000000000000000000000000000000000000253757368695377617000000000000000000000000000000000000000000000000000000000000865f9115d0a42b3112a000000000000000000000000000000000000000000000000000000038c8b93f7000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000d9e1ce17f2641f24ae83637ab66a2cca9c378b9f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000002556e6973776170563200000000000000000000000000000000000000000000000000000000000432fc88ae852159889500000000000000000000000000000000000000000000000000000001add32015000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000f164fc0ec4e93095b804a4795bbe1e041497b92a00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000014437572766556320000000000000000000000000000000000000000000000000000000000000003263d6682e3d8f3e430000000000000000000000000000000000000000000000000000000016c3bff3900000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000080000000000000000000000000d51a44d3fae010294c616388b506acda1bfaae465b41b90800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000001c42616e636f72563300000000000000000000000000000000000000000000000000000000000001931eb34171ec79f21800000000000000000000000000000000000000000000000000000000b9111930000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000eef417e1d5cc832e619ae18d2f140de2999dd4fb00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000012556e69737761705633000000000000000000000000000000000000000000000000000000000000865f9115d0a42cb7b3000000000000000000000000000000000000000000000000000000004ca6419d000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000e592427a0aece92de3edee1f18e0157c05861564000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000042c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb8dac17f958d2ee523a2206206994597c13d831ec7000bb82260fac5e5542a773aa44fbcfedf7c193bc2c599000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c5990000000000000000000000000000000000000000000000000000000006e056ce000000000000000000000000ad01c20d5886137e056775af56915de824c8fce5000000000000000000000000000000000000000000000000000000000000001d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000002260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000000000000000000000000000012168593e400000000000000000000000069d6d375de8c7ade7e44446df97f49e661fdad7d000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee0000000000000000000000000000000000000000000000000000000000000000869584cd000000000000000000000000100000000000000000000000000000000000001100000000000000000000000000000000000000000aef01038b44e7f2cedf16dd","sellAmount":"100000000611927694623603","sellAmountMinusFees":"99150000606726309219303","sellAmountDisplay":"100000000611927694623603","sellAmountInEth":"100000000611927694622720","buyAmount":"77571895251","buyAmountMinusFees":"77571895251","buyAmountDisplay":"77571895251","buyAmountDisplayMinimum":"76796176299","buyAmountInEth":"20584241002086966155264","tradeAmountUSD":358292002.19248796,"tradeFeeAmountUSD":3045482.018636148,"value":"100000000611927694623603","gasPrice":"30015947302","source":"0x","protocols":[{"name":"SushiSwap","part":40},{"name":"Uniswap_V3","part":17.5},{"name":"Curve_V2","part":15},{"name":"Uniswap_V2","part":20},{"name":"BancorV3","part":7.5}],"feeTokenAsset":{"assetCode":"eth","decimals":18,"iconUrl":"https://rainbowme-res.cloudinary.com/image/upload/v1668565116/assets/ethereum/eth.png","name":"Ethereum","network":"mainnet","symbol":"ETH","networks":{"1":{"address":"eth","decimals":18},"10":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"42161":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"56":{"address":"0x2170ed0880ac9a755fd29b2688956bd959f933f8","decimals":18},"7777777":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"81457":{"address":"0x0000000000000000000000000000000000000000","decimals":18},"8453":{"address":"0x0000000000000000000000000000000000000000","decimals":18}},"chainId":1,"price":{"value":3582.92,"available":true},"totalPrice":{"value":3045482.018636148,"available":true}},"fee":"850000005201385404300","feeInEth":"850000005201385404288","feePercentageBasisPoints":"8500000000000000","tradeType":"exact_input","from":"0xa0Ee7A142d267C1f36714E4a8F75612F20a79720","defaultGasLimit":"350000","swapType":"normal","txTarget":"0x00000000009726632680fb29d3f7a9734e3010e2","chainId":1}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"slippagePercent":2}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"data":{"slippagePercent":2}}
Loading
Loading