@inco/lightning 0.8.0-devnet → 0.8.0-devnet-2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inco/lightning",
3
- "version": "0.8.0-devnet",
3
+ "version": "0.8.0-devnet-2",
4
4
  "repository": "https://github.com/Inco-fhevm/inco-monorepo",
5
5
  "files": [
6
6
  "src/",
@@ -41,6 +41,10 @@ contract IncoLightning is
41
41
  __Ownable_init(owner);
42
42
  }
43
43
 
44
+ function withdrawFees() external onlyOwner {
45
+ _withdrawFeesTo(owner());
46
+ }
47
+
44
48
  fallback() external {} // must be included for createX deploy
45
49
 
46
50
  }
@@ -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
  }
@@ -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
  }
@@ -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
  }
package/src/Lib.sol CHANGED
@@ -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
  }
@@ -16,6 +16,8 @@ function typeOf(bytes32 handle) pure returns (ETypes) {
16
16
 
17
17
  library e {
18
18
 
19
+ error CallFailedAfterFeeRefresh();
20
+
19
21
  function sanitize(euint256 a) internal returns (euint256) {
20
22
  if (euint256.unwrap(a) == bytes32(0)) {
21
23
  return asEuint256(0);
@@ -355,20 +357,24 @@ library e {
355
357
 
356
358
  /// @dev costs the inco fee
357
359
  function rand() internal returns (euint256) {
358
- return euint256.wrap(inco.eRand{value: inco.getFee()}(ETypes.Uint256));
360
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.eRand.selector, ETypes.Uint256));
361
+ return euint256.wrap(result);
359
362
  }
360
363
 
361
364
  /// @dev costs the inco fee
362
365
  function randBounded(uint256 upperBound) internal returns (euint256) {
363
- return
364
- euint256.wrap(
365
- inco.eRandBounded{value: inco.getFee()}(euint256.unwrap(asEuint256(upperBound)), ETypes.Uint256)
366
- );
366
+ bytes32 boundHandle = euint256.unwrap(asEuint256(upperBound));
367
+ bytes32 result =
368
+ _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
369
+ return euint256.wrap(result);
367
370
  }
368
371
 
369
372
  /// @dev costs the inco fee
370
373
  function randBounded(euint256 upperBound) internal returns (euint256) {
371
- return euint256.wrap(inco.eRandBounded{value: inco.getFee()}(euint256.unwrap(s(upperBound)), ETypes.Uint256));
374
+ bytes32 boundHandle = euint256.unwrap(s(upperBound));
375
+ bytes32 result =
376
+ _callWithFeeRetry(abi.encodeWithSelector(inco.eRandBounded.selector, boundHandle, ETypes.Uint256));
377
+ return euint256.wrap(result);
372
378
  }
373
379
 
374
380
  function asEuint256(uint256 a) internal returns (euint256) {
@@ -400,7 +406,8 @@ library e {
400
406
  /// @notice Creates a new encrypted uint256 for the given user.
401
407
  /// @dev costs the inco fee
402
408
  function newEuint256(bytes memory ciphertext, address user) internal returns (euint256) {
403
- return inco.newEuint256{value: inco.getFee()}(ciphertext, user);
409
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEuint256.selector, ciphertext, user));
410
+ return euint256.wrap(result);
404
411
  }
405
412
 
406
413
  /// @notice Creates a new encrypted bool assuming msg.sender is the user
@@ -412,7 +419,8 @@ library e {
412
419
  /// @notice Creates a new encrypted bool for the given user.
413
420
  /// @dev costs the inco fee
414
421
  function newEbool(bytes memory ciphertext, address user) internal returns (ebool) {
415
- return inco.newEbool{value: inco.getFee()}(ciphertext, user);
422
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEbool.selector, ciphertext, user));
423
+ return ebool.wrap(result);
416
424
  }
417
425
 
418
426
  /// @notice Creates a new encrypted address assuming msg.sender is the user
@@ -424,7 +432,8 @@ library e {
424
432
  /// @notice Creates a new encrypted address for the given user.
425
433
  /// @dev costs the inco fee
426
434
  function newEaddress(bytes memory ciphertext, address user) internal returns (eaddress) {
427
- return inco.newEaddress{value: inco.getFee()}(ciphertext, user);
435
+ bytes32 result = _callWithFeeRetry(abi.encodeWithSelector(inco.newEaddress.selector, ciphertext, user));
436
+ return eaddress.wrap(result);
428
437
  }
429
438
 
430
439
  function allow(euint256 a, address to) internal {
@@ -479,4 +488,51 @@ library e {
479
488
  return eaddress.wrap(inco.eIfThenElse(s(control), eaddress.unwrap(s(ifTrue)), eaddress.unwrap(s(ifFalse))));
480
489
  }
481
490
 
491
+ /// @dev slot to store the fee for inco operations
492
+ bytes32 private constant FEE_SLOT = keccak256("inco.fee");
493
+
494
+ /// @dev Store fee in the custom slot
495
+ /// @param _fee The fee to store
496
+ function _setFee(uint256 _fee) private {
497
+ bytes32 slot = FEE_SLOT;
498
+ assembly {
499
+ sstore(slot, _fee)
500
+ }
501
+ }
502
+
503
+ /// @dev Retrieve fee from the custom slot
504
+ /// @return fee The stored fee
505
+ function _getFee() private view returns (uint256 fee) {
506
+ bytes32 slot = FEE_SLOT;
507
+ assembly {
508
+ fee := sload(slot)
509
+ }
510
+ }
511
+
512
+ /// @dev Get current fee with fallback to inco.getFee() if not cached
513
+ function getCurrentFee() private returns (uint256) {
514
+ uint256 cachedFee = _getFee();
515
+ if (cachedFee == 0) {
516
+ cachedFee = inco.getFee();
517
+ _setFee(cachedFee);
518
+ }
519
+ return cachedFee;
520
+ }
521
+
522
+ /// @dev Execute a call to inco with fee, retrying with fresh fee if it fails
523
+ /// @param callData The encoded function call (use abi.encodeWithSelector)
524
+ /// @return result The bytes32 result from the call
525
+ function _callWithFeeRetry(bytes memory callData) private returns (bytes32) {
526
+ uint256 fee = getCurrentFee();
527
+ (bool success, bytes memory result) = address(inco).call{value: fee}(callData);
528
+ if (!success) {
529
+ // Fee might be outdated, get fresh fee and retry
530
+ fee = inco.getFee();
531
+ _setFee(fee);
532
+ (success, result) = address(inco).call{value: fee}(callData);
533
+ require(success, CallFailedAfterFeeRefresh());
534
+ }
535
+ return abi.decode(result, (bytes32));
536
+ }
537
+
482
538
  }