-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataRequestBatch.ts
More file actions
74 lines (62 loc) · 2.2 KB
/
DataRequestBatch.ts
File metadata and controls
74 lines (62 loc) · 2.2 KB
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
import Big from "big.js";
import { Block } from "./Block";
import { DataRequest, DataRequestResolved } from "./DataRequest";
import DBLogger from "./DBLoggerModule";
import { INetwork } from "./INetwork";
export interface DataRequestBatch {
internalId: string;
targetNetwork: INetwork;
requests: DataRequest[];
}
export interface DataRequestBatchResolved extends DataRequestBatch {
targetAddress: string;
requests: DataRequestResolved[];
db?: DBLogger;
}
let nonce = 0;
export function createDataRequestBatch(requests: DataRequest[]): DataRequestBatch {
// Making sure no two requests will match each other
nonce++;
// We assume that batches are all on the same network.
// Otherwise that would defeat the purpose of batches..
const network = requests[0].targetNetwork;
return {
requests,
targetNetwork: network,
internalId: nonce + '-' + requests.map(r => r.internalId).join(','),
}
}
export interface EnoughConfirmationsDetails {
confirmed: boolean;
confirmationsNeeded: Big;
currentConfirmations: Big;
}
/**
* Makes sure all requests in the batch are confirmed enough to start executing
* NOTICE: this does not check if the block number still equal the block number of the request
*
* @export
* @param {DataRequestBatch} batch
* @param {Block} currentBlock
* @return {boolean}
*/
export function hasBatchEnoughConfirmations(batch: DataRequestBatch, currentBlock: Block): EnoughConfirmationsDetails {
let confirmed = true;
let mostConfirmationsNeeded = new Big(0);
let mostCurrentConfirmations = new Big(0);
batch.requests.forEach((request) => {
const currentConfirmations = currentBlock.number.minus(request.createdInfo.block.number);
if (!currentConfirmations.gte(request.confirmationsRequired)) {
confirmed = false;
}
if (mostConfirmationsNeeded.lt(request.confirmationsRequired)) {
mostConfirmationsNeeded = request.confirmationsRequired;
mostCurrentConfirmations = currentConfirmations;
}
});
return {
confirmed,
confirmationsNeeded: mostConfirmationsNeeded,
currentConfirmations: mostCurrentConfirmations,
};
}