-
Notifications
You must be signed in to change notification settings - Fork 45
Add BatcherConfidential
#293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 36 commits
Commits
Show all changes
72 commits
Select commit
Hold shift + click to select a range
2348d95
progress
arr00 c642cda
progress
arr00 4dde8eb
up
arr00 c87e19f
add mocks
arr00 33c573f
mv file
arr00 7f91019
up and quit
arr00 80a1228
Merge branch 'master' into feat/batcher
arr00 2c670f8
Add docs
arr00 1790102
Fix import order
arr00 6e0684e
fix mock
arr00 f6cd2ec
Remove .only
arr00 dce9c87
fix pragma
arr00 84315d6
fix test
arr00 fc11a75
fix codespell
arr00 5d01683
add tests
arr00 59c1128
fix lint
arr00 cd3735e
rename `exit` to `claim` and `quit` to `cancel`
arr00 97d4739
add testing for quit
arr00 c355860
modify exchange rate type to uint64
arr00 d0311e6
add docs
arr00 3d6522d
nit
arr00 f16b837
Merge branch 'master' into feat/batcher
arr00 ab44326
Merge branch 'master' into feat/batcher
arr00 fe407cd
reorder ops in join
arr00 94d5b5d
update mantissa and ensure that exchange rate is valid
arr00 46be773
update tests
arr00 8244ba5
add warning
arr00 9e5544c
add events
arr00 58af428
rename cancel to quit
arr00 3e4c4a1
return amount from functions
arr00 5a5f09d
don't assume transfer succeeded on batcher
arr00 d5e0fb4
Batcher: store cleartext batch total (#301)
arr00 db2d1c0
review feedback
arr00 617afde
Update contracts/mocks/utils/BatcherConfidentialSwapMock.sol
arr00 c11bd1a
review feedback
arr00 3dcf817
correct exchange rate precision renaming
arr00 3dff1dc
use getters when possible
arr00 4a12cde
exchange rate of 0 is invalid
arr00 a5b1d0d
state machine
arr00 6994796
add cancelled state
arr00 2aea578
allow quitting if state is cancelled
arr00 bce33a4
move join to internal function. Add callback support. return unproces…
arr00 15cfd92
spelling nit
arr00 b0d5d06
review feedback
arr00 b24d9b1
bug fixes
arr00 7a8d425
test callback usage
arr00 1ab0541
`_executeRoute` swaps between ERC20s only (no wrapping)
arr00 ac81367
Update BatcherConfidential.sol
Amxx 94a5fef
review feedback
arr00 302af5a
inline set exchange rate
arr00 aa57944
stop storing total deposits cleartext
arr00 4cbb77b
remove join function
arr00 2a174c9
use abi.encodeWithSignature
Amxx b374eb4
add test showcasing the cancel + quit bug
Amxx 161cb81
Fix bug in the cancel workflow (#309)
Amxx 2ec257e
add tests
arr00 49fd716
add test
arr00 b209463
add changeset
arr00 b9aca9f
update docs
arr00 9ceb3f0
update docs
arr00 683010e
add reentrancy protection to `dispatchBatchCallback`
arr00 eb23ca3
cancel if batch amount is 0
arr00 11793fe
lint fix
arr00 5d6c8f5
fix tests
arr00 f5a6ecd
L-03 add docs
arr00 417c7dd
update docs
arr00 5d1ed4a
L-01 warning
arr00 2783026
create internal function `_getAndIncreaseBatchId`
arr00 f58da88
Move batcher to finance
arr00 85b57f5
update files and docs for move
arr00 10b624d
move test file
arr00 9be290f
change logic to check transfer success on claim
arr00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.24; | ||
|
|
||
| import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol"; | ||
|
|
||
| contract ExchangeMock { | ||
| IERC20 public tokenA; | ||
| IERC20 public tokenB; | ||
| uint256 public exchangeRate; | ||
|
|
||
| event ExchangeRateSet(uint256 oldExchangeRate, uint256 newExchangeRate); | ||
|
|
||
| constructor(IERC20 tokenA_, IERC20 tokenB_, uint256 initialExchangeRate) { | ||
| tokenA = tokenA_; | ||
| tokenB = tokenB_; | ||
| exchangeRate = initialExchangeRate; | ||
| } | ||
|
|
||
| function swapAToB(uint256 amount) public returns (uint256) { | ||
| uint256 amountOut = (amount * exchangeRate) / 1e18; | ||
| require(tokenA.transferFrom(msg.sender, address(this), amount), "Transfer of token A failed"); | ||
| require(tokenB.transfer(msg.sender, amountOut), "Transfer of token B failed"); | ||
| return amountOut; | ||
| } | ||
|
|
||
| function swapBToA(uint256 amount) public returns (uint256) { | ||
| uint256 amountOut = (amount * 1e18) / exchangeRate; | ||
| require(tokenB.transferFrom(msg.sender, address(this), amount), "Transfer of token B failed"); | ||
| require(tokenA.transfer(msg.sender, amountOut), "Transfer of token A failed"); | ||
| return amountOut; | ||
| } | ||
|
|
||
| function setExchangeRate(uint256 newExchangeRate) public { | ||
| emit ExchangeRateSet(exchangeRate, newExchangeRate); | ||
|
|
||
| exchangeRate = newExchangeRate; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.27; | ||
|
|
||
| import {ZamaEthereumConfig} from "@fhevm/solidity/config/ZamaConfig.sol"; | ||
| import {FHE, externalEuint64, euint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
| import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol"; | ||
| import {BatcherConfidential} from "./../../utils/BatcherConfidential.sol"; | ||
| import {ExchangeMock} from "./../finance/ExchangeMock.sol"; | ||
|
|
||
| abstract contract BatcherConfidentialSwapMock is ZamaEthereumConfig, BatcherConfidential { | ||
| ExchangeMock public exchange; | ||
| address public admin; | ||
| bool public setExchangeRate = true; | ||
|
|
||
| constructor(ExchangeMock exchange_, address admin_) { | ||
| exchange = exchange_; | ||
| admin = admin_; | ||
| } | ||
|
|
||
| function routeDescription() public pure override returns (string memory) { | ||
| return "Exchange fromToken for toToken by swapping through the mock exchange."; | ||
| } | ||
|
|
||
| function shouldSetExchangeRate(bool setExchangeRate_) public { | ||
| setExchangeRate = setExchangeRate_; | ||
| } | ||
|
|
||
| function join(uint64 amount) public { | ||
| euint64 ciphertext = FHE.asEuint64(amount); | ||
| FHE.allowTransient(ciphertext, msg.sender); | ||
|
|
||
| bytes memory callData = abi.encodePacked( | ||
| BatcherConfidential.join.selector, | ||
| abi.encode(externalEuint64.wrap(euint64.unwrap(ciphertext)), hex"") | ||
| ); | ||
|
|
||
| (bool success, bytes memory returnVal) = address(this).delegatecall(callData); | ||
|
|
||
| if (!success) { | ||
| assembly ("memory-safe") { | ||
| revert(add(0x20, returnVal), mload(returnVal)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function join(externalEuint64 externalAmount, bytes calldata inputProof) public virtual override returns (euint64) { | ||
| euint64 amount = super.join(externalAmount, inputProof); | ||
| FHE.allow(totalDeposits(currentBatchId()), admin); | ||
| return amount; | ||
| } | ||
|
|
||
| function quit(uint256 batchId) public virtual override returns (euint64) { | ||
| euint64 amount = super.quit(batchId); | ||
| FHE.allow(totalDeposits(currentBatchId()), admin); | ||
| return amount; | ||
| } | ||
|
|
||
| function _executeRoute(uint256 batchId, uint256 unwrapAmount) internal override { | ||
| // Approve exchange to spend unwrapped tokens | ||
| uint256 rawAmount = unwrapAmount * fromToken().rate(); | ||
| IERC20(fromToken().underlying()).approve(address(exchange), rawAmount); | ||
|
|
||
| // Swap unwrapped tokens via exchange | ||
| uint256 swappedAmount = exchange.swapAToB(rawAmount); | ||
|
james-toussaint marked this conversation as resolved.
Outdated
|
||
|
|
||
| // excess over rate is essentially burned. Should be considered a fee that goes to the owner. | ||
| IERC20(toToken().underlying()).approve(address(toToken()), swappedAmount); | ||
| toToken().wrap(address(this), swappedAmount); | ||
|
arr00 marked this conversation as resolved.
Outdated
|
||
|
|
||
| uint256 amountOut = swappedAmount / toToken().rate(); | ||
|
|
||
| // Set the exchange rate for the batch based on swapped amount | ||
| uint256 exchangeRate = (amountOut * (uint256(10) ** exchangeRateDecimals())) / unwrapAmount; | ||
|
|
||
| if (setExchangeRate) _setExchangeRate(batchId, uint64(exchangeRate)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.