forked from filecoin-project/raas-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDealStatus.sol
136 lines (112 loc) · 4.47 KB
/
DealStatus.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
// Uncomment this line to use console.log
// import "hardhat/console.sol";
import "./interfaces/IAggregatorOracle.sol";
import "./data-segment/Proof.sol";
// Delta that implements the AggregatorOracle interface
contract DealStatus is IAggregatorOracle, Proof {
uint256 private transactionId;
mapping(uint256 => bytes) private txIdToCid;
mapping(bytes => Deal[]) private cidToDeals;
constructor() {
transactionId = 0;
}
function submit(bytes memory _cid) external returns (uint256) {
// Increment the transaction ID
transactionId++;
// Save _cid
txIdToCid[transactionId] = _cid;
// Emit the event
emit SubmitAggregatorRequest(transactionId, _cid);
return transactionId;
}
function submitRaaS(
bytes memory _cid,
uint256 _replication_target,
uint256 _repair_threshold,
uint256 _renew_threshold
) external returns (uint256) {
// Increment the transaction ID
transactionId++;
// Save _cid
txIdToCid[transactionId] = _cid;
// Emit the event
emit SubmitAggregatorRequestWithRaaS(
transactionId,
_cid,
_replication_target,
_repair_threshold,
_renew_threshold
);
return transactionId;
}
function complete(
uint256 _id,
uint64 _dealId,
uint64 _minerId,
InclusionProof memory _proof,
InclusionVerifierData memory _verifierData
) external returns (InclusionAuxData memory) {
require(_id <= transactionId, "Delta.complete: invalid tx id");
// Emit the event
emit CompleteAggregatorRequest(_id, _dealId);
// save the _dealId if it is not already saved
bytes memory cid = txIdToCid[_id];
for (uint256 i = 0; i < cidToDeals[cid].length; i++) {
if (cidToDeals[cid][i].dealId == _dealId) {
return computeExpectedAuxData(_proof, _verifierData);
}
}
Deal memory deal = Deal(_dealId, _minerId);
cidToDeals[cid].push(deal);
// Perform validation logic
// return this.computeExpectedAuxDataWithDeal(_dealId, _proof, _verifierData);
return computeExpectedAuxData(_proof, _verifierData);
}
// allDealIds should return all the deal ids created by the aggregator
function getAllDeals(bytes memory _cid) external view returns (Deal[] memory) {
return cidToDeals[_cid];
}
function getAllCIDs() external view returns (bytes[] memory) {
bytes[] memory cids = new bytes[](transactionId);
for (uint256 i = 0; i < transactionId; i++) {
cids[i] = txIdToCid[i + 1];
}
return cids;
}
// getActiveDeals should return all the _cid's active dealIds
function getActiveDeals(bytes memory _cid) external returns (Deal[] memory) {
// get all the deal ids for the cid
Deal[] memory activeDealIds;
activeDealIds = this.getAllDeals(_cid);
for (uint256 i = 0; i < activeDealIds.length; i++) {
uint64 dealID = activeDealIds[i].dealId;
// get the deal's expiration epoch
MarketTypes.GetDealActivationReturn memory dealActivationStatus = MarketAPI
.getDealActivation(dealID);
if (dealActivationStatus.terminated > 0 || dealActivationStatus.activated == -1) {
delete activeDealIds[i];
}
}
return activeDealIds;
}
// getExpiringDeals should return all the deals' dealIds if they are expiring within `epochs`
function getExpiringDeals(bytes memory _cid, uint64 epochs) external returns (Deal[] memory) {
// the logic is similar to the above, but use this api call:
// https://github.com/Zondax/filecoin-solidity/blob/master/contracts/v0.8/MarketAPI.sol#LL110C9-L110C9
Deal[] memory expiringDealIds;
expiringDealIds = this.getAllDeals(_cid);
for (uint256 i = 0; i < expiringDealIds.length; i++) {
uint64 dealId = expiringDealIds[i].dealId;
// get the deal's expiration epoch
MarketTypes.GetDealTermReturn memory dealTerm = MarketAPI.getDealTerm(dealId);
if (
block.number < uint64(dealTerm.end) - epochs || block.number > uint64(dealTerm.end)
) {
delete expiringDealIds[i];
}
}
return expiringDealIds;
}
}