-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from terminal-fi/tests
[core] add a way to test all pairs
- Loading branch information
Showing
18 changed files
with
198 additions
and
98 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ContractKit } from '@celo/contractkit'; | ||
import { | ||
mainnetRegistryCeloDex, | ||
mainnetRegistryCurve, | ||
mainnetRegistryMisc, | ||
mainnetRegistryMobius, | ||
mainnetRegistryMoola, | ||
mainnetRegistryMoolaV2, | ||
mainnetRegistrySavingsCELO, | ||
mainnetRegistryStCelo, | ||
mainnetRegistrySushiswap, | ||
mainnetRegistrySymmetric, | ||
mainnetRegistryUbeswap, | ||
mainnetRegistryUniswapV3, | ||
} from '../registry-cfg'; | ||
import { RegistryMento } from '../registries/mento'; | ||
import { Registry } from '../registry'; | ||
|
||
export const registriesByName: {[name: string]: (kit: ContractKit) => Registry} = { | ||
// Sorted by importance based on TVL. | ||
"mento": (kit: ContractKit) => new RegistryMento(kit), | ||
"curve": mainnetRegistryCurve, | ||
"uniswap-v3": mainnetRegistryUniswapV3, | ||
"moola-v2": mainnetRegistryMoolaV2, | ||
"stcelo": mainnetRegistryStCelo, | ||
"ubeswap": mainnetRegistryUbeswap, | ||
"sushiswap": mainnetRegistrySushiswap, | ||
"mobius": mainnetRegistryMobius, | ||
"misc": mainnetRegistryMisc, | ||
|
||
// DEPRECATED stuff: | ||
"savingscelo": mainnetRegistrySavingsCELO, | ||
"moola": mainnetRegistryMoola, | ||
"celodex": mainnetRegistryCeloDex, | ||
"symmetric": mainnetRegistrySymmetric, | ||
} |
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,87 @@ | ||
#!/usr/bin/env node | ||
import commander from 'commander'; | ||
import { ContractKit, newKit, setImplementationOnProxy } from '@celo/contractkit'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { address as swappaRouterV1Address} from '../../tools/deployed/mainnet.SwappaRouterV1.addr.json'; | ||
|
||
import { SwappaManager } from '../swappa-manager'; | ||
import { initAllTokens, tokenByAddrOrSymbol } from './tokens'; | ||
import { registriesByName } from './registries'; | ||
|
||
const program = commander.program | ||
.option("--network <network>", "Celo client URL to connect to.", "http://localhost:8545") | ||
.option("--registry <registry>", "Registry to use for testing.", "") | ||
.option("--amount <amount>", "Input amount.", "0.001") | ||
.parse(process.argv) | ||
|
||
process.on('unhandledRejection', (reason: any, _promise: any) => { | ||
// @ts-ignore | ||
console.error('Unhandled Rejection for promise:', _promise, 'at:', reason.stack || reason) | ||
process.exit(1) | ||
}) | ||
|
||
async function main() { | ||
const opts = program.opts() | ||
const kit = await newKit(opts.network) | ||
const chainId = await kit.web3.eth.getChainId() | ||
const allTokens = await initAllTokens(chainId) | ||
|
||
const tokenWhitelist = allTokens.filter((v) => v.chainId === chainId).map((v) => v.address) | ||
|
||
const registries = [registriesByName[opts.registry](kit)] | ||
const manager = new SwappaManager(kit, swappaRouterV1Address, registries) | ||
console.info(`Finding & initializing pairs...`) | ||
const pairs = await manager.reinitializePairs(tokenWhitelist) | ||
console.info(`Pairs (${pairs.length}):`) | ||
|
||
const initBlockN = await kit.web3.eth.getBlockNumber() | ||
console.info("Waiting for new block before running tests...") | ||
while (true) { | ||
const blockN = await kit.web3.eth.getBlockNumber() | ||
if (blockN > initBlockN) { | ||
break | ||
} | ||
await new Promise(resolve => setTimeout(resolve, 100)) | ||
} | ||
|
||
console.info("Running tests...") | ||
let passedN = 0 | ||
let failedN = 0 | ||
let highN = 0 | ||
for (const pair of pairs) { | ||
const inputAmountA = new BigNumber(opts.amount).shiftedBy(tokenByAddrOrSymbol(pair.tokenA).decimals) | ||
const inputAmountB = new BigNumber(opts.amount).shiftedBy(tokenByAddrOrSymbol(pair.tokenB).decimals) | ||
const [ | ||
expectedOutputB, | ||
expectedOutputA, | ||
_ | ||
] = await Promise.all([ | ||
pair.outputAmountAsync(pair.tokenA, inputAmountA).catch(() => { return 0 }), | ||
pair.outputAmountAsync(pair.tokenB, inputAmountB).catch(() => { return 0 }), | ||
pair.refresh() | ||
]) | ||
const outputB = pair.outputAmount(pair.tokenA, inputAmountA) | ||
const outputA = pair.outputAmount(pair.tokenB, inputAmountB) | ||
const passed = outputB.eq(expectedOutputB) && outputA.eq(expectedOutputA) | ||
const highOutput = outputB.gt(expectedOutputB) || outputA.gt(expectedOutputA) | ||
if (!passed) { | ||
console.warn( | ||
`Mismatch (HIGH?: ${highOutput}): ${tokenByAddrOrSymbol(pair.tokenA).symbol}/${tokenByAddrOrSymbol(pair.tokenB).symbol}: ` + | ||
`${outputB.toFixed(0)} vs ${expectedOutputB} (${outputB.eq(expectedOutputB)}), ` + | ||
`${outputA.toFixed(0)} vs ${expectedOutputA} (${outputA.eq(expectedOutputA)})`) | ||
failedN += 1 | ||
if (highOutput) { | ||
highN += 1 | ||
} | ||
} else { | ||
passedN += 1 | ||
} | ||
} | ||
|
||
console.info(`--------------------------------------------------------------------------------`) | ||
console.info(`PASSED: ${passedN}, FAILED: ${failedN}, HIGH?: ${highN}`) | ||
kit.stop() | ||
} | ||
|
||
main() |
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,36 @@ | ||
import axios from 'axios'; | ||
import * as ubeswapTokens from '@ubeswap/default-token-list/ubeswap-experimental.token-list.json' | ||
|
||
export interface Token { | ||
chainId: number, | ||
address: string, | ||
symbol: string, | ||
decimals: number, | ||
} | ||
let _CHAIN_ID: number | ||
let _ALL_TOKENS: Token[] | ||
|
||
export async function initAllTokens(chainId: number) { | ||
_CHAIN_ID = chainId | ||
const celoTokenListURI = "https://celo-org.github.io/celo-token-list/celo.tokenlist.json" | ||
const celoTokenList = (await axios.get<{tokens: Token[]}>(celoTokenListURI)).data | ||
_ALL_TOKENS = [ | ||
...ubeswapTokens.tokens, | ||
...celoTokenList.tokens, | ||
{ | ||
chainId: 42220, | ||
address: "0x617f3112bf5397D0467D315cC709EF968D9ba546", | ||
symbol: "USDTxWormhole", | ||
decimals: 6, | ||
} | ||
] | ||
return _ALL_TOKENS | ||
} | ||
|
||
export function tokenByAddrOrSymbol(addressOrSymbol: string) { | ||
const t = _ALL_TOKENS.find((t) => t.chainId === _CHAIN_ID && (t.address === addressOrSymbol || t.symbol === addressOrSymbol)) | ||
if (!t) { | ||
throw new Error(`Unrecognized token: ${addressOrSymbol}!`) | ||
} | ||
return t | ||
} |
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
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
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
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
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
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
Oops, something went wrong.