-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test slippage calculation with real data (#85)
* Remove test-data * Add test using realistic data to help the slippage match our expectations * Make it easier to test in local
- Loading branch information
Showing
44 changed files
with
35,241 additions
and
35,105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
import cors from "@fastify/cors"; | ||
import fp from "fastify-plugin"; | ||
import cors, { FastifyCorsOptions } from '@fastify/cors'; | ||
import fp from 'fastify-plugin'; | ||
|
||
export default fp(async (fastify, opts) => { | ||
const options = { | ||
const options: FastifyCorsOptions = { | ||
...opts, | ||
origin: false, | ||
delegator: (req, callback) => { | ||
const corsOptions = { | ||
origin: false, | ||
}; | ||
|
||
// do not include CORS headers for requests from localhost | ||
const origin = req.headers.origin; | ||
console.log('delegator', origin); | ||
if (origin && /^http:\/\/localhost/.test(origin)) { | ||
corsOptions.origin = true; | ||
} | ||
|
||
// callback expects two parameters: error and options | ||
callback(null, corsOptions); | ||
}, | ||
}; | ||
fastify.register(cors, options); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
libs/services/src/SlippageService/SlippageServiceMain.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { SlippageServiceMain } from './SlippageServiceMain'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { | ||
ChainNames, | ||
SupportedChainId, | ||
toSupportedChainId, | ||
} from '../../../shared/src'; | ||
|
||
const getUsdPrice = jest.fn(); | ||
const getUsdPrices = jest.fn(); | ||
|
||
/** | ||
* Test the SlippageService main implementation using realistic test data. | ||
* One difference between these tests and the ones in SlippageServiceMain.spec.ts is that in these tests we will use | ||
* real data for prices and USD we received from Coingecko and we save into test data files. | ||
* | ||
* These will allow to refine the slippage calculation algorithm to make it work well with real data. | ||
*/ | ||
describe('SlippageServiceMain: Real test data', () => { | ||
let slippageService: SlippageServiceMain; | ||
|
||
// Read all files in test-data folder | ||
const testDataDir = path.join(__dirname, 'test-data'); | ||
const testDataFiles = fs.readdirSync(testDataDir); | ||
|
||
for (const fileName of testDataFiles) { | ||
const { | ||
pair, | ||
baseToken: baseTokenAddress, | ||
quoteToken: quoteTokenAddress, | ||
chainId: chainIdValue, | ||
volatilityDetails, | ||
slippageBps, | ||
} = readTestFile(path.join(testDataDir, fileName)); | ||
|
||
const chainId = toSupportedChainId(chainIdValue); | ||
const chainName = ChainNames[chainId]; | ||
|
||
// Uncomment to tests a single pair | ||
// if (pair !== 'WETH-xDAI' || chainId !== SupportedChainId.GNOSIS_CHAIN) | ||
// continue; | ||
|
||
test(`Expect ${chainName} ${pair} slippage to be ${slippageBps} BPS`, async () => { | ||
const { | ||
quoteToken: quoteTokenVolatilityDetails, | ||
baseToken: baseTokenVolatilityDetails, | ||
} = volatilityDetails; | ||
|
||
// GIVEN: USD price for the base and quote tokens | ||
getUsdPrice.mockImplementation(async (_chainId, tokenAddress) => { | ||
if (tokenAddress === baseTokenAddress) { | ||
return baseTokenVolatilityDetails.usdPrice; | ||
} else { | ||
return quoteTokenVolatilityDetails.usdPrice; | ||
} | ||
}); | ||
|
||
// GIVEN: USD prices for the base and quote tokens | ||
getUsdPrices.mockImplementation(async (_chainId, tokenAddress) => { | ||
if (tokenAddress === baseTokenAddress) { | ||
return baseTokenVolatilityDetails.prices; | ||
} else { | ||
return quoteTokenVolatilityDetails.prices; | ||
} | ||
}); | ||
|
||
// WHEN: Get the slippage | ||
const slippage = await slippageService.getSlippageBps({ | ||
chainId, | ||
baseTokenAddress, | ||
quoteTokenAddress, | ||
}); | ||
|
||
// THEN: The slippage should be as expected | ||
expect(slippage).toBe(slippageBps); | ||
}); | ||
} | ||
|
||
beforeEach(() => { | ||
slippageService = new SlippageServiceMain({ | ||
getUsdPrice, | ||
getUsdPrices, | ||
}); | ||
}); | ||
}); | ||
|
||
function readTestFile(filePath: string) { | ||
const testContent = JSON.parse(fs.readFileSync(filePath, 'utf-8')); | ||
const volatilityDetails = testContent.volatilityDetails; | ||
|
||
volatilityDetails.quoteToken.prices = | ||
volatilityDetails.quoteToken.prices.map(fixDateForPrices); | ||
volatilityDetails.baseToken.prices = | ||
volatilityDetails.baseToken.prices.map(fixDateForPrices); | ||
|
||
return testContent; | ||
} | ||
|
||
function fixDateForPrices(price: any) { | ||
return { | ||
...price, | ||
date: new Date(price.date), | ||
}; | ||
} |
Oops, something went wrong.