-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewFacet.sol
More file actions
205 lines (174 loc) · 7.99 KB
/
ViewFacet.sol
File metadata and controls
205 lines (174 loc) · 7.99 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
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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.25;
import {CommonTypes} from "filecoin-solidity/types/CommonTypes.sol";
import {VerifRegTypes} from "filecoin-solidity/types/VerifRegTypes.sol";
import {VerifRegAPI} from "filecoin-solidity/VerifRegAPI.sol";
import {IFacet} from "../interfaces/IFacet.sol";
import {Storage} from "../libraries/Storage.sol";
import {Types} from "../libraries/Types.sol";
import {ErrorLib} from "../libraries/Errors.sol";
import {FilecoinConverter} from "../libraries/FilecoinConverter.sol";
contract ViewFacet is IFacet {
// get the function selectors for this facet for deployment and update scripts
function selectors() external pure returns (bytes4[] memory selectors_) {
selectors_ = new bytes4[](7);
selectors_[0] = this.getAllocationPackage.selector;
selectors_[1] = this.getClientPackagesWithClaimStatus.selector;
selectors_[2] = this.getPackageWithClaimStatus.selector;
selectors_[3] = this.checkProviderClaims.selector;
selectors_[4] = this.getAppConfig.selector;
selectors_[5] = this.getStorageEntity.selector;
selectors_[6] = this.getStorageEntities.selector;
}
function getAppConfig() external view returns (Storage.AppConfig memory appConfig) {
appConfig = Storage.getAppConfig();
}
function getAllocationPackage(uint256 packageId) external view returns (Types.AllocationPackageReturn memory ret) {
Storage.AllocationPackage storage package = Storage.s().allocationPackages[packageId];
if (package.client == address(0)) {
revert ErrorLib.InvalidPackageId();
}
ret.client = package.client;
ret.storageProviders = package.storageProviders;
ret.spAllocationIds = new uint64[][](package.storageProviders.length);
for (uint256 sp = 0; sp < package.storageProviders.length; sp++) {
uint64 provider = package.storageProviders[sp];
ret.spAllocationIds[sp] = package.spAllocationIds[provider];
}
ret.claimed = package.claimed;
ret.collateral = package.collateral;
}
function getPackageWithClaimStatus(uint256 packageId)
external
view
returns (Types.ClientPackageWithClaimStatus memory package)
{
return _getPackageWithClaimStatus(packageId);
}
// Function to get all client packages with claim status
function getClientPackagesWithClaimStatus(address client)
external
view
returns (Types.ClientPackageWithClaimStatus[] memory packages)
{
packages = new Types.ClientPackageWithClaimStatus[](Storage.s().clientAllocationPackages[client].length);
for (uint256 i = 0; i < Storage.s().clientAllocationPackages[client].length; i++) {
packages[i] = _getPackageWithClaimStatus(Storage.s().clientAllocationPackages[client][i]);
}
return packages;
}
function _getPackageWithClaimStatus(uint256 packageId)
internal
view
returns (Types.ClientPackageWithClaimStatus memory)
{
// slither-disable-next-line uninitialized-local
Types.PackageContext memory ctx;
ctx.packageId = packageId;
(ctx.packageInfo,) = _getPackageBasicInfo(ctx);
return _populateProviderInfo(ctx);
}
function _getPackageBasicInfo(Types.PackageContext memory ctx)
internal
view
returns (Types.ClientPackageWithClaimStatus memory, bool)
{
Storage.AllocationPackage storage p = Storage.s().allocationPackages[ctx.packageId];
if (p.client == address(0)) {
return (ctx.packageInfo, false);
}
ctx.packageInfo.packageId = ctx.packageId;
ctx.packageInfo.client = p.client;
ctx.packageInfo.claimed = p.claimed;
ctx.packageInfo.collateral = p.collateral;
return (ctx.packageInfo, true);
}
function _populateProviderInfo(Types.PackageContext memory ctx)
internal
view
returns (Types.ClientPackageWithClaimStatus memory)
{
Storage.AllocationPackage storage p = Storage.s().allocationPackages[ctx.packageId];
uint256 spCount = p.storageProviders.length;
ctx.packageInfo.providers = new Types.StorageProviderInfo[](spCount);
bool allComplete = true;
for (uint256 i = 0; i < spCount; i++) {
if (!_fillProvider(ctx, i)) {
allComplete = false;
}
}
ctx.packageInfo.canBeClaimed = allComplete && !ctx.packageInfo.claimed;
return ctx.packageInfo;
}
function _fillProvider(Types.PackageContext memory ctx, uint256 idx) internal view returns (bool) {
Storage.AllocationPackage storage p = Storage.s().allocationPackages[ctx.packageId];
uint64 providerId = p.storageProviders[idx];
uint64[] memory allocs = p.spAllocationIds[providerId];
Types.StorageProviderInfo memory infoSlot = ctx.packageInfo.providers[idx];
infoSlot.providerId = providerId;
infoSlot.allocationIds = allocs;
bool complete = ctx.packageInfo.claimed ? true : _checkProviderClaimStatus(providerId, allocs);
infoSlot.claimStatusComplete = complete;
return complete;
}
function _checkProviderClaimStatus(uint64 provider, uint64[] memory allocationIds) internal view returns (bool) {
// // If package is already claimed, all SPs must have completed their claims
// if (packageClaimed) {
// return true;
// }
try this.checkProviderClaims(provider, allocationIds) returns (bool ok) {
return ok;
} catch {
return false;
}
}
/**
* @notice Check if a provider has completed all claims
* @dev This function is external so we can use try/catch on it
*/
function checkProviderClaims(uint64 provider, uint64[] calldata allocationIds) external view returns (bool) {
VerifRegTypes.GetClaimsParams memory params = VerifRegTypes.GetClaimsParams({
provider: CommonTypes.FilActorId.wrap(provider),
claim_ids: FilecoinConverter.allocationIdsToClaimIds(allocationIds)
});
(int256 exit_code, VerifRegTypes.GetClaimsReturn memory result) = VerifRegAPI.getClaims(params);
if (exit_code != 0) {
return false;
}
return result.batch_info.success_count == allocationIds.length;
}
function getStorageEntity(address entityOwner) external view returns (Types.StorageEntityView memory) {
if (Storage.s().storageEntities[entityOwner].owner == address(0)) {
revert ErrorLib.StorageEntityDoesNotExist();
}
return _storageEntityToView(Storage.s().storageEntities[entityOwner]);
}
function getStorageEntities() external view returns (Types.StorageEntityView[] memory) {
address[] storage entityAddresses = Storage.s().entityAddresses;
Types.StorageEntityView[] memory storageEntities = new Types.StorageEntityView[](entityAddresses.length);
for (uint256 i = 0; i < entityAddresses.length; i++) {
storageEntities[i] = _storageEntityToView(Storage.s().storageEntities[entityAddresses[i]]);
}
return storageEntities;
}
function _storageEntityToView(Storage.StorageEntity storage se)
internal
view
returns (Types.StorageEntityView memory)
{
Types.StorageEntityView memory entityView;
entityView.isActive = se.isActive;
entityView.owner = se.owner;
entityView.storageProviders = se.storageProviders;
entityView.providerDetails = new Types.ProviderDetailsView[](se.storageProviders.length);
for (uint256 i = 0; i < se.storageProviders.length; i++) {
uint64 providerId = se.storageProviders[i];
entityView.providerDetails[i] = Types.ProviderDetailsView({
providerId: providerId,
spaceLeft: se.providerDetails[providerId].spaceLeft,
isActive: se.providerDetails[providerId].isActive
});
}
return entityView;
}
}