-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
138 lines (123 loc) · 4.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
export { Address } from './base/Address'
export { Token, TokenQuantity } from './entities/Token'
export { ArbitrumUniverse, arbiConfig, PROTOCOL_CONFIGS as arbiProtocolConfigs } from './configuration/arbitrum'
export { setupArbitrumZapper } from './configuration/setupArbitrumZapper'
export { BaseUniverse, baseConfig, PROTOCOL_CONFIGS as baseProtocolConfigs } from './configuration/base'
export { setupBaseZapper } from './configuration/setupBaseZapper'
export { ethereumConfig, PROTOCOL_CONFIGS as ethereumProtocolConfigs } from './configuration/ethereum'
export { setupEthereumZapper } from './configuration/setupEthereumZapper'
import { ArbitrumUniverse, arbiConfig } from './configuration/arbitrum'
import { setupArbitrumZapper } from './configuration/setupArbitrumZapper'
import { BaseUniverse, baseConfig } from './configuration/base'
import { setupBaseZapper } from './configuration/setupBaseZapper'
import { EthereumUniverse, ethereumConfig } from './configuration/ethereum'
import { setupEthereumZapper } from './configuration/setupEthereumZapper'
export { TokenFlowGraph, TokenFlowGraphSearcher, TokenFlowGraphBuilder, ITokenFlowGraphRegistry } from './searcher/TokenFlowGraph'
export { ZapTransaction } from './searcher/ZapTransaction'
import { loadTokens } from './configuration/loadTokens'
import { makeConfig } from './configuration/ChainConfiguration'
import { JsonRpcProvider } from '@ethersproject/providers'
import {
ChainId,
ChainIds,
isChainIdSupported,
} from './configuration/ReserveAddresses'
import { Universe } from './Universe'
import { createKyberswap } from './aggregators/Kyberswap'
import { createEnso } from './aggregators/Enso'
export { createParaswap } from './aggregators/Paraswap'
export { type Config } from './configuration/ChainConfiguration'
export {
makeCustomRouterSimulator,
makeCallManySimulator,
createSimulateZapTransactionUsingProvider,
SimulateParams,
} from './configuration/ZapSimulation'
export const configuration = {
utils: {
loadTokens,
},
makeConfig,
}
export { Universe } from './Universe'
export { createKyberswap } from './aggregators/Kyberswap'
export { createEnso } from './aggregators/Enso'
const CHAIN_ID_TO_CONFIG: Record<
ChainId,
{
config: ReturnType<typeof makeConfig>
blockTime: number
setup: (uni: any) => Promise<void>
setupWithDexes: (uni: any) => Promise<void>
}
> = {
[ChainIds.Mainnet]: {
config: ethereumConfig,
blockTime: 12,
setup: setupEthereumZapper,
setupWithDexes: async (uni: EthereumUniverse) => {
uni.addTradeVenue(createKyberswap('Kyberswap', uni))
uni.addTradeVenue(createEnso('Enso', uni, 1))
await setupEthereumZapper(uni)
},
},
[ChainIds.Arbitrum]: {
config: arbiConfig,
blockTime: 0.25,
setup: setupArbitrumZapper,
setupWithDexes: async (uni: ArbitrumUniverse) => {
uni.addTradeVenue(createKyberswap('Kyberswap', uni))
uni.addTradeVenue(createEnso('Enso', uni, 1))
await setupArbitrumZapper(uni)
},
},
[ChainIds.Base]: {
config: baseConfig,
blockTime: 2,
setup: setupBaseZapper,
setupWithDexes: async (uni: BaseUniverse) => {
uni.addTradeVenue(createKyberswap('Kyberswap', uni))
uni.addTradeVenue(createEnso('Enso', uni, 1))
await setupBaseZapper(uni)
},
},
}
type ChainIdToUni = {
[ChainIds.Arbitrum]: ArbitrumUniverse
[ChainIds.Base]: BaseUniverse
[ChainIds.Mainnet]: EthereumUniverse
}
export const fromProvider = async <
const ID extends keyof typeof CHAIN_ID_TO_CONFIG
>(
provider: JsonRpcProvider,
withDexes: boolean = true
): Promise<ChainIdToUni[ID]> => {
const network = await provider.getNetwork()
const chainId = network.chainId
if (isChainIdSupported(chainId) === false) {
throw new Error(`chainId ${chainId} is not supported`)
}
if (chainId !== network.chainId) {
throw new Error(
`provider chainId (${network.chainId}) does not match requested chainId (${chainId})`
)
}
const { config, setup, setupWithDexes } = CHAIN_ID_TO_CONFIG[chainId]
const universe = await Universe.createWithConfig(
provider,
config,
async (uni) => {
if (withDexes) {
await setupWithDexes(uni as any)
} else {
await setup(uni as any)
}
}
)
await universe.updateBlockState(
await provider.getBlockNumber(),
(await provider.getGasPrice()).toBigInt()
)
return universe as ChainIdToUni[ID]
}