|
1 | | -import { Interface } from '@ethersproject/abi'; |
2 | | -import { DeepReadonly } from 'ts-essentials'; |
3 | | -import { Log, Logger } from '../../types'; |
4 | | -import { bigIntify, catchParseLogError } from '../../utils'; |
5 | | -import { StatefulEventSubscriber } from '../../stateful-event-subscriber'; |
| 1 | +import { Logger } from '../../types'; |
| 2 | +import { bigIntify, Utils } from '../../utils'; |
6 | 3 | import { IDexHelper } from '../../dex-helper/idex-helper'; |
7 | | -import ResolverABI from '../../abi/fluid-dex/resolver.abi.json'; |
8 | | -import LiquidityABI from '../../abi/fluid-dex/liquidityUserModule.abi.json'; |
9 | 4 | import { |
10 | 5 | CommonAddresses, |
11 | 6 | FluidDexLiquidityProxyState, |
12 | 7 | PoolReserve, |
13 | 8 | PoolReserveResponse, |
14 | 9 | } from './types'; |
15 | | -import { Address } from '../../types'; |
16 | 10 | import { Contract } from 'ethers'; |
| 11 | +import ResolverABI from '../../abi/fluid-dex/resolver.abi.json'; |
17 | 12 |
|
18 | | -export class FluidDexLiquidityProxy extends StatefulEventSubscriber<FluidDexLiquidityProxyState> { |
19 | | - handlers: { |
20 | | - [event: string]: ( |
21 | | - event: any, |
22 | | - state: DeepReadonly<FluidDexLiquidityProxyState>, |
23 | | - log: Readonly<Log>, |
24 | | - ) => Promise<DeepReadonly<FluidDexLiquidityProxyState> | null>; |
25 | | - } = {}; |
26 | | - |
27 | | - logDecoder: (log: Log) => any; |
28 | | - |
29 | | - addressesSubscribed: Address[]; |
30 | | - |
31 | | - readonly liquidityIface = new Interface(LiquidityABI); |
32 | | - |
33 | | - readonly resolverIface = new Interface(ResolverABI); |
| 13 | +const STATE_CACHE_KEY = 'liquidity_proxy_state'; |
| 14 | +const STATE_TTL_SECONDS = 600; // 10 minutes |
| 15 | +const LOCAL_CACHE_TTL_SECONDS = 5; // 5 seconds |
34 | 16 |
|
35 | | - resolverContract: Contract; |
| 17 | +export class FluidDexLiquidityProxy { |
| 18 | + readonly resolverContract: Contract; |
36 | 19 |
|
37 | 20 | constructor( |
38 | | - readonly parentName: string, |
| 21 | + readonly dexKey: string, |
39 | 22 | readonly commonAddresses: CommonAddresses, |
40 | 23 | protected network: number, |
41 | 24 | readonly dexHelper: IDexHelper, |
42 | | - logger: Logger, |
| 25 | + readonly logger: Logger, |
43 | 26 | ) { |
44 | | - super(parentName, 'liquidity proxy', dexHelper, logger); |
45 | | - |
46 | | - this.logDecoder = (log: Log) => this.liquidityIface.parseLog(log); |
47 | | - |
48 | 27 | this.resolverContract = new Contract( |
49 | 28 | this.commonAddresses.resolver, |
50 | 29 | ResolverABI, |
51 | 30 | this.dexHelper.provider, |
52 | 31 | ); |
53 | | - |
54 | | - this.addressesSubscribed = [commonAddresses.liquidityProxy]; |
55 | | - |
56 | | - // Add handlers |
57 | | - this.handlers['LogOperate'] = this.handleOperate.bind(this); |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Handle a trade rate change on the pool. |
62 | | - */ |
63 | | - async handleOperate( |
64 | | - event: any, |
65 | | - state: DeepReadonly<FluidDexLiquidityProxyState>, |
66 | | - log: Readonly<Log>, |
67 | | - ): Promise<DeepReadonly<FluidDexLiquidityProxyState> | null> { |
68 | | - return this.generateState(log.blockNumber); |
69 | | - } |
70 | | - |
71 | | - /** |
72 | | - * The function is called every time any of the subscribed |
73 | | - * addresses release log. The function accepts the current |
74 | | - * state, updates the state according to the log, and returns |
75 | | - * the updated state. |
76 | | - * @param state - Current state of event subscriber |
77 | | - * @param log - Log released by one of the subscribed addresses |
78 | | - * @returns Updates state of the event subscriber after the log |
79 | | - */ |
80 | | - async processLog( |
81 | | - state: DeepReadonly<FluidDexLiquidityProxyState>, |
82 | | - log: Readonly<Log>, |
83 | | - ): Promise<DeepReadonly<FluidDexLiquidityProxyState> | null> { |
84 | | - try { |
85 | | - const event = this.logDecoder(log); |
86 | | - if (event.name in this.handlers) { |
87 | | - return await this.handlers[event.name](event, state, log); |
88 | | - } |
89 | | - } catch (e) { |
90 | | - catchParseLogError(e, this.logger); |
91 | | - } |
92 | | - |
93 | | - return null; |
94 | 32 | } |
95 | 33 |
|
96 | | - async getStateOrGenerate( |
97 | | - blockNumber: number, |
98 | | - ): Promise<FluidDexLiquidityProxyState> { |
99 | | - let state = this.getState(blockNumber); |
100 | | - if (!state) { |
101 | | - state = await this.generateState(blockNumber); |
102 | | - this.setState(state, blockNumber); |
103 | | - } |
104 | | - return state; |
105 | | - } |
106 | | - |
107 | | - /** |
108 | | - * The function generates state using on-chain calls. This |
109 | | - * function is called to regenerate state if the event based |
110 | | - * system fails to fetch events and the local state is no |
111 | | - * more correct. |
112 | | - * @param blockNumber - Blocknumber for which the state should |
113 | | - * should be generated |
114 | | - * @returns state of the event subscriber at blocknumber |
115 | | - */ |
116 | | - async generateState( |
117 | | - blockNumber: number, |
118 | | - ): Promise<DeepReadonly<FluidDexLiquidityProxyState>> { |
| 34 | + async fetchAndSetState(blockNumber: number): Promise<void> { |
119 | 35 | const rawResult = |
120 | 36 | await this.resolverContract.callStatic.getAllPoolsReservesAdjusted({ |
121 | 37 | blockTag: blockNumber, |
122 | 38 | }); |
123 | 39 |
|
124 | | - const convertedResult = this.convertToFluidDexPoolState(rawResult); |
125 | | - this.logger.info(`${this.parentName}: ${this.name}: generating state...`); |
| 40 | + const state = this.convertToFluidDexPoolState(rawResult); |
| 41 | + |
| 42 | + await this.dexHelper.cache.setex( |
| 43 | + this.dexKey, |
| 44 | + this.network, |
| 45 | + STATE_CACHE_KEY, |
| 46 | + STATE_TTL_SECONDS, |
| 47 | + Utils.Serialize(state), |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + async getState(): Promise<FluidDexLiquidityProxyState | null> { |
| 52 | + const cached = await this.dexHelper.cache.getAndCacheLocally( |
| 53 | + this.dexKey, |
| 54 | + this.network, |
| 55 | + STATE_CACHE_KEY, |
| 56 | + LOCAL_CACHE_TTL_SECONDS, |
| 57 | + ); |
126 | 58 |
|
127 | | - return convertedResult; |
| 59 | + if (cached) { |
| 60 | + return Utils.Parse(cached) as FluidDexLiquidityProxyState; |
| 61 | + } |
| 62 | + |
| 63 | + return null; |
128 | 64 | } |
129 | 65 |
|
130 | 66 | private convertToFluidDexPoolState( |
|
0 commit comments