-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinality.ts
155 lines (137 loc) · 4.94 KB
/
finality.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { Connection } from '@solana/web3.js';
import { ethers } from 'ethers6';
import {
CHAIN_ID_ARBITRUM,
CHAIN_ID_AVAX,
CHAIN_ID_BASE,
CHAIN_ID_BSC,
CHAIN_ID_ETH,
CHAIN_ID_OPTIMISM,
CHAIN_ID_POLYGON,
CHAIN_ID_SOLANA,
} from '../config/chains';
import { ContractsConfig } from '../config/contracts';
import { RpcConfig } from '../config/rpc';
import { EvmProviders } from './evm-providers';
import logger from './logger';
import { delay } from './util';
export class ChainFinality {
private readonly blockGenerationTimeSecond: {
[chainId: number]: number;
};
private readonly minimumBlocksToFinality: {
[chainId: number]: number;
};
private readonly finalizedBlocks: { [chainId: number]: number } = {
[CHAIN_ID_ETH]: 20,
[CHAIN_ID_BSC]: 8,
[CHAIN_ID_POLYGON]: 80,
[CHAIN_ID_AVAX]: 2,
[CHAIN_ID_ARBITRUM]: 1,
[CHAIN_ID_OPTIMISM]: 1,
[CHAIN_ID_BASE]: 1,
};
private readonly minSwapValueUsd = 100;
private readonly maxSwapValueUsd = 1_000_000;
constructor(
private readonly solanaConnection: Connection,
private readonly contracts: ContractsConfig,
private readonly rpcConfig: RpcConfig,
private readonly evmProviders: EvmProviders,
private readonly secondaryEvmProviders: EvmProviders,
) {
this.blockGenerationTimeSecond = {
[CHAIN_ID_ETH]: 13,
[CHAIN_ID_BSC]: 3,
[CHAIN_ID_POLYGON]: 2,
[CHAIN_ID_AVAX]: 2,
[CHAIN_ID_ARBITRUM]: 0.3,
[CHAIN_ID_OPTIMISM]: 3,
[CHAIN_ID_BASE]: 3,
};
this.minimumBlocksToFinality = {
[CHAIN_ID_ETH]: 1,
[CHAIN_ID_BSC]: 3,
[CHAIN_ID_POLYGON]: 16,
[CHAIN_ID_AVAX]: 1,
[CHAIN_ID_ARBITRUM]: 1,
[CHAIN_ID_OPTIMISM]: 1,
[CHAIN_ID_BASE]: 1,
};
}
async waitForEvm(chainId: number, sourceTxHash: string, swapValueUsd: number): Promise<void> {
const startTime = Date.now();
let iteration = 0;
while (true) {
try {
const timeToFinalize = await this.timeToFinalizeSeconds(chainId, sourceTxHash, swapValueUsd);
if (timeToFinalize <= 0) {
if ([CHAIN_ID_ETH, CHAIN_ID_POLYGON].includes(chainId)) {
const [tx, tx2] = await Promise.all([
await this.evmProviders[chainId].getTransactionReceipt(sourceTxHash),
await this.secondaryEvmProviders[chainId].getTransactionReceipt(sourceTxHash),
]);
if (!tx || tx.status !== 1 || !tx2 || tx2.status !== 1) {
throw new Error('Transaction (eth)not found or has error in waiting for chain finality');
}
return;
} else {
const tx = await this.evmProviders[chainId].getTransactionReceipt(sourceTxHash);
if (!tx || tx.status !== 1) {
throw new Error('Transaction not found or has error in waiting for chain finality');
}
return;
}
}
await delay(timeToFinalize * 1000);
} catch (err: any) {
logger.error(`Error while waiting for finality for tx: ${sourceTxHash} ${err.message}`);
await delay(3000);
} finally {
iteration++;
if (Date.now() - startTime > 60000) {
logger.warn(`Waiting for finality for tx: ${sourceTxHash} for more than 60 seconds`);
}
if (Date.now() - startTime > 3600_000) {
logger.error(`Waiting for finality for tx: ${sourceTxHash} took more than 1 hour. Giving up`);
throw new Error('Waiting for finality for tx took more than 1 hour');
}
}
}
}
async waitForFinality(sourceChain: number, sourceTxHash: string, swapValueUsd: number): Promise<void> {
if (sourceChain === CHAIN_ID_SOLANA) {
await this.solanaConnection.getTransaction(sourceTxHash, {
commitment: 'finalized',
maxSupportedTransactionVersion: 2,
});
} else {
await this.waitForEvm(sourceChain, sourceTxHash, swapValueUsd);
}
}
private async getEvmLatestBlockNumber(provider: ethers.JsonRpcProvider): Promise<number> {
const resOpt = await provider.send('eth_getBlockByNumber', ['latest', false]);
return parseInt(resOpt.number);
}
private async timeToFinalizeSeconds(wChainId: number, txHash: string, swapValueUsd: number): Promise<number> {
const tx = await this.evmProviders[wChainId].getTransaction(txHash);
if (!tx) {
throw new Error('Transaction not found in timeToFinalizeSeconds');
}
const finalizedBlockNumber = tx.blockNumber! + this.finalizedBlocks[wChainId];
let safeBlockForDriver = finalizedBlockNumber;
if (swapValueUsd < this.minSwapValueUsd) {
safeBlockForDriver = tx.blockNumber! + this.minimumBlocksToFinality[wChainId];
} else {
const factor = (swapValueUsd - this.minSwapValueUsd) / (this.maxSwapValueUsd - this.minSwapValueUsd);
const blocksToSemiFinalize =
this.minimumBlocksToFinality[wChainId] +
(this.finalizedBlocks[wChainId] - this.minimumBlocksToFinality[wChainId]) * factor;
safeBlockForDriver = tx.blockNumber! + blocksToSemiFinalize;
}
const lastBlockNumber = await this.getEvmLatestBlockNumber(this.evmProviders[wChainId]);
const remainingBlocks = safeBlockForDriver - lastBlockNumber;
// every tx is polled at most 10 times so rpc usage is controlled
return (remainingBlocks * this.blockGenerationTimeSecond[wChainId]) / 10;
}
}