/
index.ts
397 lines (341 loc) · 16.7 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* eslint-disable no-case-declarations */
import BigNumber from "bignumber.js";
import { Routing } from "../routing";
import { Routing2 } from "../routing/routing.v2";
import { Token2Config } from "../tokens";
import { GlitterBridgeSDK } from "src/GlitterBridgeSDK";
import { BridgeNetworks } from "../networks";
import { CurrentBlock } from "../blocks";
/**
* Enum representing transaction types.
* @enum {string}
*/
export enum TransactionType {
Unknown = "Unknown",
Deposit = "Deposit",
Release = "Release",
Refund = "Refund",
TransferStart = "TransferStart",
TransferEnd = "TransferEnd",
Transfer = "Transfer",
GasDeposit = "GasDeposit",
Finalize = "Finalize",
FeeTransfer = "FeeTransfer",
Error = "Error",
BadRouting = "BadRouting",
}
/**
* Enum representing chain status.
* @enum {string}
*/
export enum ChainStatus {
Unknown = "Unknown",
Pending = "Pending",
Completed = "Completed",
Failed = "Failed",
Cancelled = "Cancelled",
}
export enum BridgeStatus {
Unknown = "Unknown",
DepositReceived = "Deposit Received",
DepositConfirmed = "Deposit Confirmed",
WaitingForInternalTransfer = "Waiting For Internal Transfer",
InternalTransferReceived = "Internal Transfer Received",
InternalTransferConfirmed = "Internal Transfer Confirmed",
WaitingForRelease = "Waiting For Release",
ReleaseReceived = "Release Received",
ReleaseConfirmed = "Release Confirmed",
}
export enum USDCBridgeStatus {
Unknown = 0,
DepositReceived = 1,
DepositConfirmed = 2,
InternalTransferStarted = 3,
InternalTransferReceived = 4,
InternalTransferConfirmed = 5,
Released = 6
}
/**
* Retrieves the description of a USDCBridgeStatus.
* @param {USDCBridgeStatus} status - The USDCBridgeStatus to get the description for.
* @param {GlitterBridgeSDK} [sdk] - (Optional) The GlitterBridgeSDK instance. Used for DepositReceived status confirmation count. if currentBlock is undefined, will try to get current block from CurrentBlock class.
* @param {BridgeNetworks} [chain] - (Optional) The BridgeNetworks value representing the chain. Used for DepositReceived status confirmation count.
* @param {number} [startBlock] - (Optional) The start block number. Used for DepositReceived status confirmation count.
* @param {number} [currentBlock] - (Optional) The current block number. Used for DepositReceived status confirmation count.
* @returns {Promise<string>} A Promise that resolves to the description string.
*/
export async function USDCBridgeStatusDescription(status: USDCBridgeStatus, sdk?:GlitterBridgeSDK, chain?:BridgeNetworks, startBlock?:number, currentBlock?:number): Promise<string> {
let confirmations;
let localcurrentBlock;
let delta;
switch (status) {
case USDCBridgeStatus.DepositReceived:
if(!sdk || !startBlock || !chain){
return "Deposit Received. Waiting for {x} of {y} confirmations.";
}
//Get percent of confirmations
confirmations = sdk.confirmationsRequired(chain);
localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, chain)).block: currentBlock;
delta = localcurrentBlock - startBlock;
if (delta >= confirmations) {
return "Deposit Confirmed. Waiting for internal transfer.";
}
return `Deposit Received. Waiting for ${delta} of ${confirmations} confirmations.`;
case USDCBridgeStatus.DepositConfirmed:
return "Deposit Confirmed. Waiting for internal transfer.";
case USDCBridgeStatus.InternalTransferStarted:
return "Transferred out of deposit chain. Waiting to receive on destination chain.";
case USDCBridgeStatus.InternalTransferReceived:
if(!sdk || !startBlock || !chain){
return "Transfer to destination chain received. Waiting for {x} of {y} confirmations.";
}
//Get percent of confirmations
confirmations = sdk.confirmationsRequired(chain);
localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, chain)).block: currentBlock;
delta = localcurrentBlock - startBlock;
if (delta >= confirmations) {
return "Internal Transfer Confirmed. Waiting for final release";
}
return `Transfer to destination chain received. Waiting for ${delta} of ${confirmations} confirmations.`;
case USDCBridgeStatus.InternalTransferConfirmed:
return "Internal Transfer Confirmed. Waiting for final release";
case USDCBridgeStatus.Released:
return "Released. Funds have arrived at destination.";
default:
return "Unknown";
}
}
/**
* Retrieves the progress of the USDCBridge.
* @param {USDCBridgeProgress} status - The USDCBridgeProgress to get the progress for.
* @param {GlitterBridgeSDK} [sdk] - (Optional) The GlitterBridgeSDK instance. if currentBlock is undefined, will try to get current block from CurrentBlock class.
* @param {BridgeNetworks} [sourceChain] - (Optional) The BridgeNetworks value representing the sourceChain.
* @param {BridgeNetworks} [destinationChain] - (Optional) The BridgeNetworks value representing the destinationChain.
* @param {number} [startBlock] - (Optional) The start block number.
* @param {number} [currentBlock] - (Optional) The current block number.
* @returns {Promise<number>} A Promise that resolves to the progress value.
*/
export async function USDCBridgeProgress(status: USDCBridgeStatus, sdk?:GlitterBridgeSDK, sourceChain?:BridgeNetworks, destinationChain?:BridgeNetworks, startBlock?:number, currentBlock?:number): Promise<number> {
let sourceConfirmations;
let destinationConfirmations;
let percent;
let confirmationRatio;
let weightedPercent;
let minRatio;
let minDelta;
let remainingDelta;
let localcurrentBlock;
switch (status) {
case USDCBridgeStatus.DepositReceived:
if (!sdk || !startBlock || !sourceChain || !destinationChain){
return 10;
} else {
//Get percent of confirmations
sourceConfirmations = sdk.confirmationsRequired(sourceChain);
destinationConfirmations = sdk.confirmationsRequired(destinationChain);
localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, sourceChain)).block: currentBlock;
percent = (localcurrentBlock - startBlock) / sourceConfirmations;
percent = clamp(percent, 0, 1);
confirmationRatio = sourceConfirmations / (sourceConfirmations + destinationConfirmations);
weightedPercent = clamp(10 + (percent)* 80 * confirmationRatio, 10, 90);
return Number((weightedPercent).toFixed(0));
}
case USDCBridgeStatus.DepositConfirmed:
if (!sdk || !startBlock || !sourceChain || !destinationChain){
return 50;
} else {
//Get percent of confirmations
sourceConfirmations = sdk.confirmationsRequired(sourceChain);
destinationConfirmations = sdk.confirmationsRequired(destinationChain);
confirmationRatio = sourceConfirmations / (sourceConfirmations + destinationConfirmations);
minRatio = clamp(10 + (1)* 80 * confirmationRatio, 10, 90);
weightedPercent = minRatio;
return Number((weightedPercent).toFixed(0));
}
case USDCBridgeStatus.InternalTransferStarted:
if (!sdk || !startBlock || !sourceChain || !destinationChain){
return 55;
} else {
//Get percent of confirmations
sourceConfirmations = sdk.confirmationsRequired(sourceChain);
destinationConfirmations = sdk.confirmationsRequired(destinationChain);
confirmationRatio = sourceConfirmations / (sourceConfirmations + destinationConfirmations);
minRatio = clamp(15 + (1)* 80 * confirmationRatio, 10, 95);
weightedPercent = minRatio;
return Number((weightedPercent).toFixed(0));
}
case USDCBridgeStatus.InternalTransferReceived:
if (!sdk || !startBlock || !sourceChain || !destinationChain){
return 95;
} else {
//Get percent of confirmations
sourceConfirmations = sdk.confirmationsRequired(sourceChain);
destinationConfirmations = sdk.confirmationsRequired(destinationChain);
localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, sourceChain)).block: currentBlock;
percent = (localcurrentBlock - startBlock) / destinationConfirmations;
percent = clamp(percent, 0, 1);
minDelta = (1)* 80 * sourceConfirmations / (sourceConfirmations + destinationConfirmations);
remainingDelta = 80 - minDelta;
confirmationRatio = destinationConfirmations / (sourceConfirmations + destinationConfirmations);
weightedPercent = clamp(15 + (percent)* remainingDelta * confirmationRatio, 10, 95);
return Number((weightedPercent).toFixed(0));
}
case USDCBridgeStatus.Released:
return 100;
default:
return 0;
}
}
export enum TokenBridgeStatus {
Unknown = 0,
DepositReceived = 1,
DepositConfirmed = 2,
Released = 3
}
/**
* Retrieves the description of a TokenBridgeStatus.
* @param {TokenBridgeStatus} status - The TokenBridgeStatus to get the description for.
* @param {GlitterBridgeSDK} [sdk] - (Optional) The GlitterBridgeSDK instance. Used for DepositReceived status confirmation count. if currentBlock is undefined, will try to get current block from CurrentBlock class.
* @param {BridgeNetworks} [chain] - (Optional) The BridgeNetworks value representing the chain. Used for DepositReceived status confirmation count.
* @param {number} [startBlock] - (Optional) The start block number. Used for DepositReceived status confirmation count.
* @param {number} [currentBlock] - (Optional) The current block number. Used for DepositReceived status confirmation count.
* @returns {Promise<string>} A Promise that resolves to the description string.
*/
export async function TokenBridgeStatusDescription(status: TokenBridgeStatus, sdk?:GlitterBridgeSDK, chain?:BridgeNetworks, startBlock?:number, currentBlock?:number): Promise<string> {
switch (status) {
case TokenBridgeStatus.DepositReceived:
if(!sdk || !startBlock || !chain){
return "Deposit Received. Waiting for {x} of {y} confirmations.";
}
//Get percent of confirmations
const confirmations = sdk.confirmationsRequired(chain);
const localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, chain)).block: currentBlock;
const delta = localcurrentBlock - startBlock;
if (delta >= confirmations){
return "Deposit Confirmed. Waiting for final release.";
}
return `Deposit Received. Waiting for ${delta} of ${confirmations} confirmations.`;
case TokenBridgeStatus.DepositConfirmed:
return "Deposit Confirmed. Waiting for final release.";
case TokenBridgeStatus.Released:
return "Released. Funds have arrived at destination.";
default:
return "Unknown";
}
}
/**
* Retrieves the progress of the TokenBridge.
* @param {TokenBridgeStatus} status - The TokenBridgeStatus to get the progress for.
* @param {GlitterBridgeSDK} [sdk] - (Optional) The GlitterBridgeSDK instance. if currentBlock is undefined, will try to get current block from CurrentBlock class.
* @param {BridgeNetworks} [chain] - (Optional) The BridgeNetworks value representing the chain.
* @param {number} [startBlock] - (Optional) The start block number.
* @param {number} [currentBlock] - (Optional) The current block number.
* @returns {Promise<number>} A Promise that resolves to the progress value.
*/
export async function TokenBridgeProgress(status: TokenBridgeStatus, sdk?:GlitterBridgeSDK, chain?:BridgeNetworks, startBlock?:number, currentBlock?:number): Promise<number> {
switch (status) {
case TokenBridgeStatus.DepositReceived:
if (!sdk || !startBlock || !chain){
return 20;
} else {
//Get percent of confirmations
const confirmations = sdk.confirmationsRequired(chain);
const localcurrentBlock = !currentBlock? (await CurrentBlock.getCurrentBlock(sdk, chain)).block: currentBlock;
const percent = (localcurrentBlock - startBlock) / confirmations;
const weightedPercent = clamp(20 + (percent)* 70, 20, 70);
//clamp weightedpercent
return Number((weightedPercent).toFixed(0));
}
case TokenBridgeStatus.DepositConfirmed:
return 90;
case TokenBridgeStatus.Released:
return 100;
default:
return 0;
}
}
function clamp(value: number, min: number, max: number): number {
return Math.min(Math.max(value, min), max);
}
/**
* Enum representing bridge type.
* @enum {string}
*/
export enum BridgeType {
Unknown = "Unknown",
Circle = "Circle",
TokenV1 = "TokenV1",
TokenV2 = "TokenV2",
}
/**
* @deprecated Use PartialTxn instead.
*/
export type PartialBridgeTxn = PartialTxn;
/**
* Type representing a partial transaction.
* @typedef {object} PartialTxn
* @property {string} txnID - The transaction ID.
* @property {string} [txnIDHashed] - The hashed transaction ID.
* @property {BridgeType} [bridgeType] - The bridge type.
* @property {Date} [txnTimestamp] - The transaction timestamp.
* @property {number} [block] - The block number.
* @property {number} [confirmations] - The number of confirmations.
* @property {TransactionType} txnType - The transaction type.
* @property {ChainStatus | null} [chainStatus] - The chain status.
* @property {string | null} [network] - The network.
* @property {string | null} [tokenSymbol] - The token symbol.
* @property {string | null} [baseSymbol] - The base symbol.
* @property {string | null} [address] - The address.
* @property {BigNumber | null} [units] - The units.
* @property {BigNumber | number | null} [amount] - The amount.
* @property {Routing | Routing2 | null} [routing] - The routing information.
* @property {string | null} [protocol] - The protocol.
* @property {number | null} [referral_id] - The referral ID.
* @property {BigNumber | null} [gasPaid] - The gas paid.
*/
export type PartialTxn = {
txnID: string;
txnIDHashed?: string;
bridgeType?: BridgeType;
txnTimestamp?: Date;
block?: number;
confirmations?: number;
txnType: TransactionType;
chainStatus?: ChainStatus | null;
network?: string | null;
tokenSymbol?: string | null;
baseSymbol?: string | null;
address?: string | null;
units?: BigNumber | null;
amount?: BigNumber | number | null;
routing?: Routing | Routing2 | null;
protocol?: string | null;
referral_id?: number | null;
gasPaid?: BigNumber | null;
};
export function PartialBridgeTxnEquals(a: PartialBridgeTxn, b: PartialBridgeTxn): boolean {
const differences = [];
if (a.txnID !== b.txnID) differences.push("txnID");
if (a.txnIDHashed !== b.txnIDHashed) differences.push("txnIDHashed");
if (a.bridgeType !== b.bridgeType) differences.push("bridgeType");
if (a.txnTimestamp !== b.txnTimestamp) differences.push("txnTimestamp");
if (a.block !== b.block) differences.push("block");
//if (a.confirmations !== b.confirmations) differences.push("confirmations");
if (a.txnType !== b.txnType) differences.push("txnType");
if (a.chainStatus !== b.chainStatus) differences.push("chainStatus");
if (a.network !== b.network) differences.push("network");
if (a.tokenSymbol !== b.tokenSymbol) differences.push("tokenSymbol");
if (a.baseSymbol !== b.baseSymbol) differences.push("baseSymbol");
if (a.address !== b.address) differences.push("address");
if (a.units !== b.units) differences.push("units");
if (a.amount !== b.amount) differences.push("amount");
if (a.routing !== b.routing) differences.push("routing");
if (a.protocol !== b.protocol) differences.push("protocol");
if (a.referral_id !== b.referral_id) differences.push("referral_id");
if (a.gasPaid !== b.gasPaid) differences.push("gasPaid");
if (differences.length > 0) {
console.log("PartialBridgeTxnEquals: differences", differences);
return false;
}
return true;
}