-
Notifications
You must be signed in to change notification settings - Fork 125
Amm v2 - AssetManager fix #2445
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,8 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright 2017 Loopring Technology Limited. | ||
| pragma solidity ^0.7.0; | ||
|
|
||
|
|
||
| /// @author Brecht Devos - <[email protected]> | ||
| interface IAssetManager | ||
| {} |
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
52 changes: 52 additions & 0 deletions
52
packages/loopring_v3/contracts/amm/libamm/AmmAssetManagement.sol
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,52 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright 2017 Loopring Technology Limited. | ||
| pragma solidity ^0.7.0; | ||
| pragma experimental ABIEncoderV2; | ||
|
|
||
| import "../../lib/ERC20.sol"; | ||
| import "../../lib/MathUint.sol"; | ||
| import "../../lib/TransferUtil.sol"; | ||
| import "./AmmData.sol"; | ||
|
|
||
|
|
||
| /// @title AmmAssetManagement | ||
| library AmmAssetManagement | ||
| { | ||
| using TransferUtil for address; | ||
|
|
||
| function deposit( | ||
| AmmData.State storage S, | ||
| address token, | ||
| uint96 amount | ||
| ) | ||
| public | ||
| { | ||
| if (amount == 0) { | ||
| return; | ||
| } | ||
| uint ethValue = 0; | ||
| if (token == address(0)) { | ||
| ethValue = amount; | ||
| } else { | ||
| ERC20(token).approve(address(S.exchange.getDepositContract()), amount); | ||
| } | ||
| S.exchange.deposit{value: ethValue}( | ||
| address(this), | ||
| address(this), | ||
| token, | ||
| amount, | ||
| new bytes(0) | ||
| ); | ||
| } | ||
|
|
||
| function transferOut( | ||
| AmmData.State storage /*S*/, | ||
| address to, | ||
| address token, | ||
| uint amount | ||
| ) | ||
| public | ||
| { | ||
| token.transferOut(to, amount); | ||
| } | ||
| } | ||
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
76 changes: 76 additions & 0 deletions
76
packages/loopring_v3/contracts/amm/libamm/AmmDepositProcess.sol
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,76 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright 2017 Loopring Technology Limited. | ||
| pragma solidity ^0.7.0; | ||
| pragma experimental ABIEncoderV2; | ||
|
|
||
| import "../../lib/ERC20.sol"; | ||
| import "../../lib/MathUint96.sol"; | ||
| import "../../lib/TransferUtil.sol"; | ||
| import "./AmmAssetManagement.sol"; | ||
| import "./AmmData.sol"; | ||
| import "./AmmPoolToken.sol"; | ||
| import "./AmmStatus.sol"; | ||
| import "./AmmUtil.sol"; | ||
| import "./AmmWithdrawal.sol"; | ||
|
|
||
|
|
||
| /// @title AmmDepositProcess | ||
| library AmmDepositProcess | ||
| { | ||
| using AmmAssetManagement for AmmData.State; | ||
| using MathUint96 for uint96; | ||
| using TransferUtil for address; | ||
|
|
||
| function processDeposit( | ||
| AmmData.State storage S, | ||
| AmmData.Context memory ctx, | ||
| AmmData.PoolDeposit memory poolDeposit | ||
| ) | ||
| internal | ||
| { | ||
| require(poolDeposit.amounts.length == ctx.tokens.length, "INVALID_DEPOSIT_DATA"); | ||
| for (uint i = 0; i < ctx.tokens.length; i++) { | ||
| address token = ctx.tokens[i].addr; | ||
| uint96 amount = poolDeposit.amounts[i]; | ||
dong77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| verifyDepositTx(ctx, ctx.tokens[i].tokenID, amount); | ||
| S.deposit(token, amount); | ||
| S.balancesL1[token] = S.balancesL1[token].sub(amount); | ||
| } | ||
| } | ||
|
|
||
| function verifyDepositTx( | ||
| AmmData.Context memory ctx, | ||
| uint tokenID, | ||
| uint96 amount | ||
| ) | ||
| internal | ||
| view | ||
| { | ||
| if (amount == 0) { | ||
| return; | ||
| } | ||
|
|
||
| // Verify deposit data | ||
| // Start by reading the first 27 bytes into packedData | ||
| uint txsDataPtr = ctx.txsDataPtr + 27; | ||
| // packedData: txType (1) | owner (20) | accountID (4) | tokenID (2) | ||
| uint packedData; | ||
| uint96 txAmount; | ||
| assembly { | ||
| packedData := calldataload(txsDataPtr) | ||
| txAmount := calldataload(add(txsDataPtr, 12)) | ||
| } | ||
|
|
||
| require( | ||
| // txType == ExchangeData.TransactionType.DEPOSIT && | ||
| // owner == address(this) && | ||
| // accountID == ctx.accountID && | ||
| // tokenID == tokenID && | ||
| packedData & 0xffffffffffffffffffffffffffffffffffffffffffffffffffffff == (uint(ExchangeData.TransactionType.DEPOSIT) << 208) | (uint(address(this)) << 48) | (uint(ctx.accountID) << 16) | uint(tokenID) && | ||
| amount == txAmount, | ||
| "INVALID_DEPOSIT_TX_DATA" | ||
dong77 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| ctx.txsDataPtr += ExchangeData.TX_DATA_AVAILABILITY_SIZE; | ||
| } | ||
| } | ||
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.