@inco/lightning 0.7.10 → 0.8.0-devnet-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/manifest.yaml +22 -0
  2. package/package.json +1 -1
  3. package/src/IncoLightning.sol +4 -0
  4. package/src/Lib.alphanet.sol +62 -6
  5. package/src/Lib.demonet.sol +62 -6
  6. package/src/Lib.devnet.sol +63 -7
  7. package/src/Lib.sol +63 -7
  8. package/src/Lib.template.sol +65 -9
  9. package/src/Lib.testnet.sol +62 -6
  10. package/src/interfaces/IIncoLightning.sol +2 -0
  11. package/src/libs/incoLightning_alphanet_v0_297966649.sol +62 -6
  12. package/src/libs/incoLightning_alphanet_v1_725458969.sol +62 -6
  13. package/src/libs/incoLightning_alphanet_v2_976644394.sol +62 -6
  14. package/src/libs/incoLightning_demonet_v0_863421733.sol +62 -6
  15. package/src/libs/incoLightning_demonet_v2_467437523.sol +62 -6
  16. package/src/libs/incoLightning_devnet_v0_340846814.sol +62 -6
  17. package/src/libs/incoLightning_devnet_v1_904635675.sol +62 -6
  18. package/src/libs/incoLightning_devnet_v2_295237520.sol +62 -6
  19. package/src/libs/incoLightning_devnet_v3_976859633.sol +534 -0
  20. package/src/libs/incoLightning_testnet_v0_183408998.sol +62 -6
  21. package/src/libs/incoLightning_testnet_v2_889158349.sol +62 -6
  22. package/src/lightning-parts/Fee.sol +10 -14
  23. package/src/lightning-parts/TEELifecycle.sol +11 -12
  24. package/src/lightning-parts/TEELifecycle.types.sol +1 -4
  25. package/src/periphery/IncoUtils.sol +42 -0
  26. package/src/test/AddTwo.sol +3 -3
  27. package/src/test/FakeIncoInfra/MockRemoteAttestation.sol +2 -3
  28. package/src/test/IncoTest.sol +2 -2
  29. package/src/test/TEELifecycle/TEELifecycleMockTest.t.sol +1 -1
  30. package/src/test/TestFakeInfra.t.sol +263 -0
  31. package/src/test/TestFeeWithdrawal.t.sol +60 -0
  32. package/src/test/TestIncoUtils.t.sol +242 -0
  33. package/src/version/IncoLightningConfig.sol +2 -2
  34. package/src/IIncoLightning.sol +0 -22
@@ -0,0 +1,534 @@
1
+ // AUTOGENERATED FILE. DO NOT EDIT.
2
+ // This file was generated by the IncoLightning library generator.
3
+ // The original template is located at Lib.template.sol
4
+
5
+ /// SPDX-License-Identifier: No License
6
+ pragma solidity ^0.8;
7
+
8
+ import { IncoLightning } from "../IncoLightning.sol";
9
+ import { ebool, euint256, eaddress, ETypes } from "../Types.sol";
10
+
11
+ IncoLightning constant inco = IncoLightning(0x4732520194584a04Cac0224e067658619F4086bD);
12
+ address constant deployedBy = 0x8202D2D747784Cb7D48868E44C42C4bf162a70BC;
13
+
14
+ function typeOf(bytes32 handle) pure returns (ETypes) {
15
+ return ETypes(uint8(uint256(handle) >> 8));
16
+ }
17
+
18
+ library e {
19
+ error CallFailedAfterFeeRefresh();
20
+
21
+ /// @dev slot to store the fee for inco operations
22
+ bytes32 private constant FEE_SLOT = keccak256("inco.fee");
23
+
24
+ function sanitize(euint256 a) internal returns (euint256) {
25
+ if (euint256.unwrap(a) == bytes32(0)) {
26
+ return asEuint256(0);
27
+ }
28
+ return a;
29
+ }
30
+
31
+ function sanitize(ebool a) internal returns (ebool) {
32
+ if (ebool.unwrap(a) == bytes32(0)) {
33
+ return asEbool(false);
34
+ }
35
+ return a;
36
+ }
37
+
38
+ function sanitize(eaddress a) internal returns (eaddress) {
39
+ if (eaddress.unwrap(a) == bytes32(0)) {
40
+ return asEaddress(address(0));
41
+ }
42
+ return a;
43
+ }
44
+
45
+ function s(euint256 a) internal returns (euint256) {
46
+ return sanitize(a);
47
+ }
48
+
49
+ function s(ebool a) internal returns (ebool) {
50
+ return sanitize(a);
51
+ }
52
+
53
+ function s(eaddress a) internal returns (eaddress) {
54
+ return sanitize(a);
55
+ }
56
+
57
+ function add(euint256 a, euint256 b) internal returns (euint256) {
58
+ return inco.eAdd(s(a), s(b));
59
+ }
60
+
61
+ function add(euint256 a, uint256 b) internal returns (euint256) {
62
+ return inco.eAdd(s(a), asEuint256(b));
63
+ }
64
+
65
+ function add(uint256 a, euint256 b) internal returns (euint256) {
66
+ return inco.eAdd(asEuint256(a), s(b));
67
+ }
68
+
69
+ function sub(euint256 a, euint256 b) internal returns (euint256) {
70
+ return inco.eSub(s(a), s(b));
71
+ }
72
+
73
+ function sub(euint256 a, uint256 b) internal returns (euint256) {
74
+ return inco.eSub(s(a), asEuint256(b));
75
+ }
76
+
77
+ function sub(uint256 a, euint256 b) internal returns (euint256) {
78
+ return inco.eSub(asEuint256(a), s(b));
79
+ }
80
+
81
+ function mul(euint256 a, euint256 b) internal returns (euint256) {
82
+ return inco.eMul(s(a), s(b));
83
+ }
84
+
85
+ function mul(euint256 a, uint256 b) internal returns (euint256) {
86
+ return inco.eMul(s(a), asEuint256(b));
87
+ }
88
+
89
+ function mul(uint256 a, euint256 b) internal returns (euint256) {
90
+ return inco.eMul(asEuint256(a), s(b));
91
+ }
92
+
93
+ function div(euint256 a, euint256 b) internal returns (euint256) {
94
+ return inco.eDiv(s(a), s(b));
95
+ }
96
+
97
+ function div(euint256 a, uint256 b) internal returns (euint256) {
98
+ return inco.eDiv(s(a), asEuint256(b));
99
+ }
100
+
101
+ function div(uint256 a, euint256 b) internal returns (euint256) {
102
+ return inco.eDiv(asEuint256(a), s(b));
103
+ }
104
+
105
+ function rem(euint256 a, euint256 b) internal returns (euint256) {
106
+ return inco.eRem(s(a), s(b));
107
+ }
108
+
109
+ function rem(euint256 a, uint256 b) internal returns (euint256) {
110
+ return inco.eRem(s(a), asEuint256(b));
111
+ }
112
+
113
+ function rem(uint256 a, euint256 b) internal returns (euint256) {
114
+ return inco.eRem(asEuint256(a), s(b));
115
+ }
116
+
117
+ function and(euint256 a, euint256 b) internal returns (euint256) {
118
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
119
+ }
120
+
121
+ function and(euint256 a, uint256 b) internal returns (euint256) {
122
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
123
+ }
124
+
125
+ function and(uint256 a, euint256 b) internal returns (euint256) {
126
+ return euint256.wrap(inco.eBitAnd(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
127
+ }
128
+
129
+ function and(ebool a, ebool b) internal returns (ebool) {
130
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
131
+ }
132
+
133
+ function and(ebool a, bool b) internal returns (ebool) {
134
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
135
+ }
136
+
137
+ function and(bool a, ebool b) internal returns (ebool) {
138
+ return ebool.wrap(inco.eBitAnd(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
139
+ }
140
+
141
+ function or(euint256 a, euint256 b) internal returns (euint256) {
142
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
143
+ }
144
+
145
+ function or(euint256 a, uint256 b) internal returns (euint256) {
146
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
147
+ }
148
+
149
+ function or(uint256 a, euint256 b) internal returns (euint256) {
150
+ return euint256.wrap(inco.eBitOr(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
151
+ }
152
+
153
+ function or(ebool a, ebool b) internal returns (ebool) {
154
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
155
+ }
156
+
157
+ function or(ebool a, bool b) internal returns (ebool) {
158
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
159
+ }
160
+
161
+ function or(bool a, ebool b) internal returns (ebool) {
162
+ return ebool.wrap(inco.eBitOr(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
163
+ }
164
+
165
+ function xor(euint256 a, euint256 b) internal returns (euint256) {
166
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(s(b))));
167
+ }
168
+
169
+ function xor(euint256 a, uint256 b) internal returns (euint256) {
170
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b))));
171
+ }
172
+
173
+ function xor(uint256 a, euint256 b) internal returns (euint256) {
174
+ return euint256.wrap(inco.eBitXor(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b))));
175
+ }
176
+
177
+ function xor(ebool a, ebool b) internal returns (ebool) {
178
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(s(b))));
179
+ }
180
+
181
+ function xor(ebool a, bool b) internal returns (ebool) {
182
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(s(a)), ebool.unwrap(asEbool(b))));
183
+ }
184
+
185
+ function xor(bool a, ebool b) internal returns (ebool) {
186
+ return ebool.wrap(inco.eBitXor(ebool.unwrap(asEbool(a)), ebool.unwrap(s(b))));
187
+ }
188
+
189
+ function shl(euint256 a, euint256 b) internal returns (euint256) {
190
+ return inco.eShl(s(a), s(b));
191
+ }
192
+
193
+ function shl(euint256 a, uint256 b) internal returns (euint256) {
194
+ return inco.eShl(s(a), asEuint256(b));
195
+ }
196
+
197
+ function shl(uint256 a, euint256 b) internal returns (euint256) {
198
+ return inco.eShl(asEuint256(a), s(b));
199
+ }
200
+
201
+ function shr(euint256 a, euint256 b) internal returns (euint256) {
202
+ return inco.eShr(s(a), s(b));
203
+ }
204
+
205
+ function shr(euint256 a, uint256 b) internal returns (euint256) {
206
+ return inco.eShr(s(a), asEuint256(b));
207
+ }
208
+
209
+ function shr(uint256 a, euint256 b) internal returns (euint256) {
210
+ return inco.eShr(asEuint256(a), s(b));
211
+ }
212
+
213
+ function rotl(euint256 a, euint256 b) internal returns (euint256) {
214
+ return inco.eRotl(s(a), s(b));
215
+ }
216
+
217
+ function rotl(euint256 a, uint256 b) internal returns (euint256) {
218
+ return inco.eRotl(s(a), asEuint256(b));
219
+ }
220
+
221
+ function rotl(uint256 a, euint256 b) internal returns (euint256) {
222
+ return inco.eRotl(asEuint256(a), s(b));
223
+ }
224
+
225
+ function rotr(euint256 a, euint256 b) internal returns (euint256) {
226
+ return inco.eRotr(s(a), s(b));
227
+ }
228
+
229
+ function rotr(euint256 a, uint256 b) internal returns (euint256) {
230
+ return inco.eRotr(s(a), asEuint256(b));
231
+ }
232
+
233
+ function rotr(uint256 a, euint256 b) internal returns (euint256) {
234
+ return inco.eRotr(asEuint256(a), s(b));
235
+ }
236
+
237
+ function eq(euint256 a, euint256 b) internal returns (ebool) {
238
+ return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(s(b)));
239
+ }
240
+
241
+ function eq(euint256 a, uint256 b) internal returns (ebool) {
242
+ return inco.eEq(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)));
243
+ }
244
+
245
+ function eq(uint256 a, euint256 b) internal returns (ebool) {
246
+ return inco.eEq(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)));
247
+ }
248
+
249
+ function eq(eaddress a, address b) internal returns (ebool) {
250
+ return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b)));
251
+ }
252
+
253
+ function eq(eaddress a, eaddress b) internal returns (ebool) {
254
+ return inco.eEq(eaddress.unwrap(s(a)), eaddress.unwrap(s(b)));
255
+ }
256
+
257
+ function eq(address a, eaddress b) internal returns (ebool) {
258
+ return inco.eEq(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b)));
259
+ }
260
+
261
+ function ne(euint256 a, euint256 b) internal returns (ebool) {
262
+ return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(s(b)));
263
+ }
264
+
265
+ function ne(euint256 a, uint256 b) internal returns (ebool) {
266
+ return inco.eNe(euint256.unwrap(s(a)), euint256.unwrap(asEuint256(b)));
267
+ }
268
+
269
+ function ne(uint256 a, euint256 b) internal returns (ebool) {
270
+ return inco.eNe(euint256.unwrap(asEuint256(a)), euint256.unwrap(s(b)));
271
+ }
272
+
273
+ function ne(eaddress a, eaddress b) internal returns (ebool) {
274
+ return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(s(b)));
275
+ }
276
+
277
+ function ne(eaddress a, address b) internal returns (ebool) {
278
+ return inco.eNe(eaddress.unwrap(s(a)), eaddress.unwrap(asEaddress(b)));
279
+ }
280
+
281
+ function ne(address a, eaddress b) internal returns (ebool) {
282
+ return inco.eNe(eaddress.unwrap(asEaddress(a)), eaddress.unwrap(s(b)));
283
+ }
284
+
285
+ function ge(euint256 a, euint256 b) internal returns (ebool) {
286
+ return inco.eGe(s(a), s(b));
287
+ }
288
+
289
+ function ge(euint256 a, uint256 b) internal returns (ebool) {
290
+ return inco.eGe(s(a), asEuint256(b));
291
+ }
292
+
293
+ function ge(uint256 a, euint256 b) internal returns (ebool) {
294
+ return inco.eGe(asEuint256(a), s(b));
295
+ }
296
+
297
+ function gt(euint256 a, euint256 b) internal returns (ebool) {
298
+ return inco.eGt(s(a), s(b));
299
+ }
300
+
301
+ function gt(euint256 a, uint256 b) internal returns (ebool) {
302
+ return inco.eGt(s(a), asEuint256(b));
303
+ }
304
+
305
+ function gt(uint256 a, euint256 b) internal returns (ebool) {
306
+ return inco.eGt(asEuint256(a), s(b));
307
+ }
308
+
309
+ function le(euint256 a, euint256 b) internal returns (ebool) {
310
+ return inco.eLe(s(a), s(b));
311
+ }
312
+
313
+ function le(euint256 a, uint256 b) internal returns (ebool) {
314
+ return inco.eLe(s(a), asEuint256(b));
315
+ }
316
+
317
+ function le(uint256 a, euint256 b) internal returns (ebool) {
318
+ return inco.eLe(asEuint256(a), s(b));
319
+ }
320
+
321
+ function lt(euint256 a, euint256 b) internal returns (ebool) {
322
+ return inco.eLt(s(a), s(b));
323
+ }
324
+
325
+ function lt(euint256 a, uint256 b) internal returns (ebool) {
326
+ return inco.eLt(s(a), asEuint256(b));
327
+ }
328
+
329
+ function lt(uint256 a, euint256 b) internal returns (ebool) {
330
+ return inco.eLt(asEuint256(a), s(b));
331
+ }
332
+
333
+ function min(euint256 a, euint256 b) internal returns (euint256) {
334
+ return inco.eMin(s(a), s(b));
335
+ }
336
+
337
+ function min(euint256 a, uint256 b) internal returns (euint256) {
338
+ return inco.eMin(s(a), asEuint256(b));
339
+ }
340
+
341
+ function min(uint256 a, euint256 b) internal returns (euint256) {
342
+ return inco.eMin(asEuint256(a), s(b));
343
+ }
344
+
345
+ function max(euint256 a, euint256 b) internal returns (euint256) {
346
+ return inco.eMax(s(a), s(b));
347
+ }
348
+
349
+ function max(euint256 a, uint256 b) internal returns (euint256) {
350
+ return inco.eMax(s(a), asEuint256(b));
351
+ }
352
+
353
+ function max(uint256 a, euint256 b) internal returns (euint256) {
354
+ return inco.eMax(asEuint256(a), s(b));
355
+ }
356
+
357
+ function not(ebool a) internal returns (ebool) {
358
+ return inco.eNot(s(a));
359
+ }
360
+
361
+ /// @dev costs the inco fee
362
+ function rand() internal returns (euint256) {
363
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRand.selector, ETypes.Uint256));
364
+ return euint256.wrap(result);
365
+ }
366
+
367
+ /// @dev costs the inco fee
368
+ function randBounded(uint256 upperBound) internal returns (euint256) {
369
+ bytes32 boundHandle = euint256.unwrap(asEuint256(upperBound));
370
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
371
+ return euint256.wrap(result);
372
+ }
373
+
374
+ /// @dev costs the inco fee
375
+ function randBounded(euint256 upperBound) internal returns (euint256) {
376
+ bytes32 boundHandle = euint256.unwrap(s(upperBound));
377
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
378
+ return euint256.wrap(result);
379
+ }
380
+
381
+ function asEuint256(uint256 a) internal returns (euint256) {
382
+ return inco.asEuint256(a);
383
+ }
384
+
385
+ function asEbool(bool a) internal returns (ebool) {
386
+ return inco.asEbool(a);
387
+ }
388
+
389
+ function asEaddress(address a) internal returns (eaddress) {
390
+ return inco.asEaddress(a);
391
+ }
392
+
393
+ function asEbool(euint256 a) internal returns (ebool) {
394
+ return ebool.wrap(inco.eCast(euint256.unwrap(a), ETypes.Bool));
395
+ }
396
+
397
+ function asEuint256(ebool a) internal returns (euint256) {
398
+ return euint256.wrap(inco.eCast(ebool.unwrap(a), ETypes.Uint256));
399
+ }
400
+
401
+ /// @notice Creates a new encrypted uint256 assuming msg.sender is the user
402
+ /// @dev costs the inco fee
403
+ function newEuint256(bytes memory ciphertext) internal returns (euint256) {
404
+ return newEuint256(ciphertext, msg.sender);
405
+ }
406
+
407
+ /// @notice Creates a new encrypted uint256 for the given user.
408
+ /// @dev costs the inco fee
409
+ function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
410
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEuint256.selector, ciphertext, user));
411
+ return euint256.wrap(result);
412
+ }
413
+
414
+ /// @notice Creates a new encrypted bool assuming msg.sender is the user
415
+ /// @dev costs the inco fee
416
+ function newEbool(bytes memory ciphertext) internal returns (ebool) {
417
+ return newEbool(ciphertext, msg.sender);
418
+ }
419
+
420
+ /// @notice Creates a new encrypted bool for the given user.
421
+ /// @dev costs the inco fee
422
+ function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
423
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEbool.selector, ciphertext, user));
424
+ return ebool.wrap(result);
425
+ }
426
+
427
+ /// @notice Creates a new encrypted address assuming msg.sender is the user
428
+ /// @dev costs the inco fee
429
+ function newEaddress(bytes memory ciphertext) internal returns (eaddress) {
430
+ return newEaddress(ciphertext, msg.sender);
431
+ }
432
+
433
+ /// @notice Creates a new encrypted address for the given user.
434
+ /// @dev costs the inco fee
435
+ function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
436
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEaddress.selector, ciphertext, user));
437
+ return eaddress.wrap(result);
438
+ }
439
+
440
+ function allow(euint256 a, address to) internal {
441
+ inco.allow(euint256.unwrap(a), to);
442
+ }
443
+
444
+ function allow(ebool a, address to) internal {
445
+ inco.allow(ebool.unwrap(a), to);
446
+ }
447
+
448
+ function allow(eaddress a, address to) internal {
449
+ inco.allow(eaddress.unwrap(a), to);
450
+ }
451
+
452
+ function reveal(euint256 a) internal {
453
+ inco.reveal(euint256.unwrap(a));
454
+ }
455
+
456
+ function reveal(ebool a) internal {
457
+ inco.reveal(ebool.unwrap(a));
458
+ }
459
+
460
+ function reveal(eaddress a) internal {
461
+ inco.reveal(eaddress.unwrap(a));
462
+ }
463
+
464
+ function allowThis(euint256 a) internal {
465
+ allow(a, address(this));
466
+ }
467
+
468
+ function allowThis(ebool a) internal {
469
+ allow(a, address(this));
470
+ }
471
+
472
+ function allowThis(eaddress a) internal {
473
+ allow(a, address(this));
474
+ }
475
+
476
+ function isAllowed(address user, euint256 a) internal view returns (bool) {
477
+ return inco.isAllowed(euint256.unwrap(a), user);
478
+ }
479
+
480
+ function select(ebool control, euint256 ifTrue, euint256 ifFalse) internal returns (euint256) {
481
+ return euint256.wrap(inco.eIfThenElse(s(control), euint256.unwrap(s(ifTrue)), euint256.unwrap(s(ifFalse))));
482
+ }
483
+
484
+ function select(ebool control, ebool ifTrue, ebool ifFalse) internal returns (ebool) {
485
+ return ebool.wrap(inco.eIfThenElse(s(control), ebool.unwrap(s(ifTrue)), ebool.unwrap(s(ifFalse))));
486
+ }
487
+
488
+ function select(ebool control, eaddress ifTrue, eaddress ifFalse) internal returns (eaddress) {
489
+ return eaddress.wrap(inco.eIfThenElse(s(control), eaddress.unwrap(s(ifTrue)), eaddress.unwrap(s(ifFalse))));
490
+ }
491
+
492
+ /// @dev Store fee in the custom slot
493
+ /// @param _fee The fee to store
494
+ function _setFee(uint256 _fee) private {
495
+ bytes32 slot = FEE_SLOT;
496
+ assembly {
497
+ sstore(slot, _fee)
498
+ }
499
+ }
500
+
501
+ /// @dev Retrieve fee from the custom slot
502
+ /// @return fee The stored fee
503
+ function _getFee() private view returns (uint256 fee) {
504
+ bytes32 slot = FEE_SLOT;
505
+ assembly {
506
+ fee := sload(slot)
507
+ }
508
+ }
509
+
510
+ /// @dev Get current fee with fallback to inco.getFee() if not cached
511
+ function getCurrentFee() private returns (uint256) {
512
+ uint256 cachedFee = _getFee();
513
+ if (cachedFee == 0) {
514
+ cachedFee = inco.getFee();
515
+ _setFee(cachedFee);
516
+ }
517
+ return cachedFee;
518
+ }
519
+
520
+ /// @dev Execute a call to inco with fee, retrying with fresh fee if it fails
521
+ /// @param callData The encoded function call (use abi.encodeWithSelector)
522
+ /// @return result The bytes32 result from the call
523
+ function _callWithFeeRetry(bytes memory callData) private returns (bytes32) {
524
+ uint256 fee = getCurrentFee();
525
+ (bool success, bytes memory result) = address(inco).call{value: fee}(callData);
526
+ if (!success) {
527
+ fee = inco.getFee();
528
+ _setFee(fee);
529
+ (success, result) = address(inco).call{value: fee}(callData);
530
+ require(success, CallFailedAfterFeeRefresh());
531
+ }
532
+ return abi.decode(result, (bytes32));
533
+ }
534
+ }
@@ -16,6 +16,11 @@ function typeOf(bytes32 handle) pure returns (ETypes) {
16
16
  }
17
17
 
18
18
  library e {
19
+ error CallFailedAfterFeeRefresh();
20
+
21
+ /// @dev slot to store the fee for inco operations
22
+ bytes32 private constant FEE_SLOT = keccak256("inco.fee");
23
+
19
24
  function sanitize(euint256 a) internal returns (euint256) {
20
25
  if (euint256.unwrap(a) == bytes32(0)) {
21
26
  return asEuint256(0);
@@ -355,17 +360,22 @@ library e {
355
360
 
356
361
  /// @dev costs the inco fee
357
362
  function rand() internal returns (euint256) {
358
- return euint256.wrap(inco.eRand{value: inco.getFee()}(ETypes.Uint256));
363
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRand.selector, ETypes.Uint256));
364
+ return euint256.wrap(result);
359
365
  }
360
366
 
361
367
  /// @dev costs the inco fee
362
368
  function randBounded(uint256 upperBound) internal returns (euint256) {
363
- return euint256.wrap(inco.eRandBounded{value: inco.getFee()}(euint256.unwrap(asEuint256(upperBound)), ETypes.Uint256));
369
+ bytes32 boundHandle = euint256.unwrap(asEuint256(upperBound));
370
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
371
+ return euint256.wrap(result);
364
372
  }
365
373
 
366
374
  /// @dev costs the inco fee
367
375
  function randBounded(euint256 upperBound) internal returns (euint256) {
368
- return euint256.wrap(inco.eRandBounded{value: inco.getFee()}(euint256.unwrap(s(upperBound)), ETypes.Uint256));
376
+ bytes32 boundHandle = euint256.unwrap(s(upperBound));
377
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
378
+ return euint256.wrap(result);
369
379
  }
370
380
 
371
381
  function asEuint256(uint256 a) internal returns (euint256) {
@@ -397,7 +407,8 @@ library e {
397
407
  /// @notice Creates a new encrypted uint256 for the given user.
398
408
  /// @dev costs the inco fee
399
409
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
400
- return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
410
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEuint256.selector, ciphertext, user));
411
+ return euint256.wrap(result);
401
412
  }
402
413
 
403
414
  /// @notice Creates a new encrypted bool assuming msg.sender is the user
@@ -409,7 +420,8 @@ library e {
409
420
  /// @notice Creates a new encrypted bool for the given user.
410
421
  /// @dev costs the inco fee
411
422
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
412
- return inco.newEbool{value: inco.getFee()}(ciphertext, user);
423
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEbool.selector, ciphertext, user));
424
+ return ebool.wrap(result);
413
425
  }
414
426
 
415
427
  /// @notice Creates a new encrypted address assuming msg.sender is the user
@@ -421,7 +433,8 @@ library e {
421
433
  /// @notice Creates a new encrypted address for the given user.
422
434
  /// @dev costs the inco fee
423
435
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
424
- return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
436
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEaddress.selector, ciphertext, user));
437
+ return eaddress.wrap(result);
425
438
  }
426
439
 
427
440
  function allow(euint256 a, address to) internal {
@@ -475,4 +488,47 @@ library e {
475
488
  function select(ebool control, eaddress ifTrue, eaddress ifFalse) internal returns (eaddress) {
476
489
  return eaddress.wrap(inco.eIfThenElse(s(control), eaddress.unwrap(s(ifTrue)), eaddress.unwrap(s(ifFalse))));
477
490
  }
491
+
492
+ /// @dev Store fee in the custom slot
493
+ /// @param _fee The fee to store
494
+ function _setFee(uint256 _fee) private {
495
+ bytes32 slot = FEE_SLOT;
496
+ assembly {
497
+ sstore(slot, _fee)
498
+ }
499
+ }
500
+
501
+ /// @dev Retrieve fee from the custom slot
502
+ /// @return fee The stored fee
503
+ function _getFee() private view returns (uint256 fee) {
504
+ bytes32 slot = FEE_SLOT;
505
+ assembly {
506
+ fee := sload(slot)
507
+ }
508
+ }
509
+
510
+ /// @dev Get current fee with fallback to inco.getFee() if not cached
511
+ function getCurrentFee() private returns (uint256) {
512
+ uint256 cachedFee = _getFee();
513
+ if (cachedFee == 0) {
514
+ cachedFee = inco.getFee();
515
+ _setFee(cachedFee);
516
+ }
517
+ return cachedFee;
518
+ }
519
+
520
+ /// @dev Execute a call to inco with fee, retrying with fresh fee if it fails
521
+ /// @param callData The encoded function call (use abi.encodeWithSelector)
522
+ /// @return result The bytes32 result from the call
523
+ function _callWithFeeRetry(bytes memory callData) private returns (bytes32) {
524
+ uint256 fee = getCurrentFee();
525
+ (bool success, bytes memory result) = address(inco).call{value: fee}(callData);
526
+ if (!success) {
527
+ fee = inco.getFee();
528
+ _setFee(fee);
529
+ (success, result) = address(inco).call{value: fee}(callData);
530
+ require(success, CallFailedAfterFeeRefresh());
531
+ }
532
+ return abi.decode(result, (bytes32));
533
+ }
478
534
  }