Skip to content

Instantly share code, notes, and snippets.

@tgrecojs
Last active August 31, 2021 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgrecojs/e92b521eee549a56fb45af87df9d809f to your computer and use it in GitHub Desktop.
Save tgrecojs/e92b521eee549a56fb45af87df9d809f to your computer and use it in GitHub Desktop.
JS code for calculating price impact of ETH/USDC swaps
export default [
{
amount0: "-30448.617992",
amount1: "9.01",
amountUSD: "30494.73732203450763393772438460871",
id: "0x5db90056a4b2f96ace45738bc83187777c9b84ebbfd0363983173f47893d60de#180388",
sqrtPriceX96: "1360849691735920611140694413818098",
timestamp: "1630430957",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-42244.85148",
amount1: "12.5",
amountUSD: "42309.0042297082217228897520318793",
id: "0xdea2f9a5b9e6f2c0c00a2683417112df8a8ce101a53a73de021f0a399c1300e1#180385",
sqrtPriceX96: "1360822157207058480082398518033526",
timestamp: "1630430207",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-315743.765551",
amount1: "93.40447802611144226",
amountUSD: "316252.0324393724408375240295083951",
id: "0xb8491b2efb898f22279fde6b5c18152c7cc13c8ed912fb3830d761a22d638269#180384",
sqrtPriceX96: "1360783953074584954827668059836983",
timestamp: "1630430096",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-218112.649197",
amount1: "64.5",
amountUSD: "218456.6545053860104269025632209156",
id: "0x607de7cba94be53e52dc45c73b06df55bb7e8b760138ab1f43c9bde0aee81802#180383",
sqrtPriceX96: "1360498478110414551608701428315643",
timestamp: "1630429562",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-121764.783543",
amount1: "36",
amountUSD: "121952.9199951273066794987179102638",
id: "0x0b70173b3cc438ba3ed049665430b4064cddad9c7df4a40671cb00f119bf4be6#180382",
sqrtPriceX96: "1360301344786851161294292264021480",
timestamp: "1630429532",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-359791.102057",
amount1: "106.338934744373466576",
amountUSD: "360375.5369050054265246835013874837",
id: "0x32065bafaae1d48418e56412b0f1022df0a5184fc2b5e04917d9b7a0313d1b08#180381",
sqrtPriceX96: "1360191316885327408560668544415436",
timestamp: "1630429478",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-331298.642445",
amount1: "97.872845859159768028",
amountUSD: "331833.6415460423266944747535279111",
id: "0x550aff82fbb75131e44ce7b7bd98fecdfc9b0ff08a90ad622a0a953eeb055984#180380",
sqrtPriceX96: "1359866309945322000453712409490111",
timestamp: "1630429432",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-500518.840965",
amount1: "147.7688172112373",
amountUSD: "501355.292964325628312752603933107",
id: "0xba6774d88599e8f0773b0f6280a3902138c90f6b8eb5348f3f2da4498422b459#180378",
sqrtPriceX96: "1359504016942352008162981477313047",
timestamp: "1630429357",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-69971.857816",
amount1: "20.66570596538909716",
amountUSD: "70078.7617268697409607324832169937",
id: "0x471deeacac2fe08968a0178b2dbd7dee02fce5f3eccf4ccdd32c12fe1e007c7b#180379",
sqrtPriceX96: "1359567178171820859857436450188617",
timestamp: "1630429357",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-7611.206821",
amount1: "2.2463084906742278",
amountUSD: "7622.677267312431736527109076191045",
id: "0xbe173efa01ca5327685c74bf1c538cb942febefe6357dd1c0f415e5e5a1adbce#180377",
sqrtPriceX96: "1359052386584896468282686016684068",
timestamp: "1630429229",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "-493639.684047",
amount1: "145.640271696128674316",
amountUSD: "494463.4817450609510838431003719604",
id: "0x45570e2019a937b7de5bd399ee50ef969c8d4e12c428d13d878cddc3e71c1891#180376",
sqrtPriceX96: "1359045521123524138461238338048438",
timestamp: "1630429123",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "10000",
amount1: "-2.931718972826589337",
amountUSD: "9984.96702399339590894182755465786",
id: "0x8e9dd5b106ffc00a877d6db69f82ce70099f50306aa445929795b6e8e1af6118#180374",
sqrtPriceX96: "1358600396304855409619920421836539",
timestamp: "1630428589",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "15711.767167",
amount1: "-4.606326936363262056",
amountUSD: "15688.11811101443672098157948890028",
id: "0xff5cd232e10f83efe0fe1eaf9f2f5b395fe0ebcdd4a5c7021ab4e2e5c5c1d108#180373",
sqrtPriceX96: "1358609383569049031638422399021595",
timestamp: "1630428406",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "207807.394605",
amount1: "-60.933333973426503382",
amountUSD: "207481.4429407037828173488238907628",
id: "0x2d02c1848dd84e2e2c459adf5cebe8721dac86f39b54719b18180c287d1cb0fa#180372",
sqrtPriceX96: "1358623504389469748974387633551636",
timestamp: "1630428259",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "2799.417767",
amount1: "-0.82096032323527907",
amountUSD: "2795.216055224268853399139570389919",
id: "0x1a059ee62e846f45baf8893b69ebf736a915cde3805a32442940fe5b619bb557#180371",
sqrtPriceX96: "1358810297180897981120334342549832",
timestamp: "1630428036",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "40764.624462",
amount1: "-11.955023031322049709",
amountUSD: "40702.92946206560386311682687291722",
id: "0xafa6851c1bc0aa9476ced021a95a860e2ef8169ce25f0fe8691317648b182283#180368",
sqrtPriceX96: "1358812813857082084527743024761309",
timestamp: "1630427883",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "1286.5799",
amount1: "-0.377325204612268663",
amountUSD: "1284.649484055145230509451148921962",
id: "0x34cdbeb9f2241d45d97caffbd9c47ba5e34da6d5c104a300a07d76228e8a5c89#180367",
sqrtPriceX96: "1358849462101059640530865319454955",
timestamp: "1630427851",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "86",
amount1: "-0.025221905824146707",
amountUSD: "85.87099756065894562901875733166425",
id: "0x65748b1d835ef63a3828f3995e8fdc8910d1f418a6fbf547c522b08e7e61ebbc#180366",
sqrtPriceX96: "1358850618795289445160057392150016",
timestamp: "1630427803",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "413081.10563",
amount1: "-121.180716100253914351",
amountUSD: "412405.2049796581458457875186238948",
id: "0x791729c3f28c1f03865be1245a870abeedcf4e1fdf8466ca85a07f6b6ddfaad1#180365",
sqrtPriceX96: "1358850696113296763130986405550053",
timestamp: "1630427545",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
{
amount0: "336905.652818",
amount1: "-98.883091485842196612",
amountUSD: "336362.8478624620393816593823038409",
id: "0x6c73788a4f47e8638908d8a50a34cd91ade0a1427da2f56923ab820ca476abe8#180364",
sqrtPriceX96: "1359222176823738015901690723577703",
timestamp: "1630427531",
token0: {
name: "USD Coin",
},
token1: {
name: "Wrapped Ether",
},
},
];
import sampleData from './sampleData.js';
/**
* Uniswaph V3 Subgraph Data
*
* Result of Qu
*/
const queries = {
swaps: `{
swaps(first: 20, where: {pool:
"0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8"},
orderDirection: desc, orderBy: timestamp) {
id
amount0
amount1
amountUSD
token0 {
name
}
token1 {
name
}
sqrtPriceX96
timestamp
}
}`
}
const timestampToDate = timestamp => new Date(timestamp * 1000)
const withEthValue = data.map((x) => {
return {
...x,
ethPriceAtTrade: Math.abs(x.amount0) / Math.abs(x.amount1),
date: timestampToDate(x.timestamp),
};
});
const square = (x) => x * x;
const toExponent = (base) => (power) => base ** power;
const mulitply = (x) => (y) => x * y;
const divide = (x) => (y) => x / y;
const tenToNegTwelve = toExponent(10)(-12);
const mutiplyByExp1 = mulitply(tenToNegTwelve);
const twoToNinetyTwo = toExponent(2)(92);
const diviveByExpX = divide(twoToNinetyTwo)
// Refact this out to be (square(x) * (10 ** -12)) .
const computeFromSqrtPrice = ({ sqrtPriceX96 }) =>
diviveByExpX(mutiplyByExp1(Number(sqrtPriceX96)))
const withPreviousSqrt = withEthValue.reduce((acc, val, index, array) => {
if (acc.length === 0) {
return [{ ...val, isFirstVal: true }];
} else {
const newValue = {
...val,
withComputedVal: computeFromSqrtPrice(val), //?
// Can use computeFromSqrtPrice fn to avoid iterations below
previousSqrt: index > 0 && array[index - 1].sqrtPriceX96,
previousSqrtToNumber: Number(acc[index - 1].sqrtPriceX96),
};
return acc.concat(newValue);
}
}, []); //?
// Can be eliminated
withPreviousSqrt
.map((x) => {
if (!x.isFirstVal) {
return {
...x,
previousSquared:
(square(x.previousSqrtToNumber) * 10 ** -12) / 2 ** 192,
};
}
})
.filter((x) => !x === false)
.map((x) => ({
...x,
priceImpactPercent: x.ethPriceAtTrade - 1 / x.previousSquared,
ethPriceAtPreviousTrade: 1 / x.previousSquared,
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment