Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/loopring_v3.js/src/exchange_v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ export class ExchangeV3 {

// Get the block data from the transaction data
//const submitBlocksFunctionSignature = "0x8dadd3af"; // submitBlocks
const submitBlocksFunctionSignature = "0xc39ce618"; // submitBlocksWithCallbacks
const submitBlocksFunctionSignature = "0xfbb404ab"; // submitBlocksWithCallbacks

const transaction = await this.web3.eth.getTransaction(
event.transactionHash
Expand Down
8 changes: 8 additions & 0 deletions packages/loopring_v3/contracts/amm/IAssetManager.sol
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
{}
43 changes: 26 additions & 17 deletions packages/loopring_v3/contracts/amm/LoopringAmmPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import "../aux/access/ITransactionReceiver.sol";
import "../core/iface/IAgentRegistry.sol";
import "../lib/ReentrancyGuard.sol";
import "../lib/TransferUtil.sol";
import "./libamm/AmmTransactionReceiver.sol";
import "./libamm/AmmAssetManagement.sol";
import "./libamm/AmmData.sol";
import "./libamm/AmmExitRequest.sol";
import "./libamm/AmmJoinRequest.sol";
import "./libamm/AmmPoolToken.sol";
import "./libamm/AmmStatus.sol";
import "./libamm/AmmTransactionReceiver.sol";
import "./libamm/AmmWithdrawal.sol";
import "./PoolToken.sol";

Expand All @@ -24,11 +25,12 @@ contract LoopringAmmPool is
ITransactionReceiver,
ReentrancyGuard
{
using AmmTransactionReceiver for AmmData.State;
using AmmAssetManagement for AmmData.State;
using AmmJoinRequest for AmmData.State;
using AmmExitRequest for AmmData.State;
using AmmPoolToken for AmmData.State;
using AmmStatus for AmmData.State;
using AmmTransactionReceiver for AmmData.State;
using AmmWithdrawal for AmmData.State;
using TransferUtil for address;

Expand All @@ -38,7 +40,7 @@ contract LoopringAmmPool is
event Shutdown(uint timestamp);

IAmmController public immutable controller;
address public immutable assetManager;
IAssetManager public immutable assetManager;
bool public immutable joinsDisabled;

modifier onlyFromExchangeOwner()
Expand All @@ -49,7 +51,7 @@ contract LoopringAmmPool is

modifier onlyFromAssetManager()
{
require(msg.sender == assetManager, "UNAUTHORIZED");
require(msg.sender == address(assetManager), "UNAUTHORIZED");
_;
}

Expand All @@ -73,7 +75,7 @@ contract LoopringAmmPool is

constructor(
IAmmController _controller,
address _assetManager,
IAssetManager _assetManager,
bool _joinsDisabled
)
{
Expand Down Expand Up @@ -183,29 +185,36 @@ contract LoopringAmmPool is
state.withdrawWhenOffline();
}

function depositAssetsToL2(
uint96[] memory amounts
function transferOut(
address to,
address token,
uint amount
)
external
nonReentrant
onlyWhenOnline
onlyFromExchangeOwner
onlyFromAssetManager
{
for(uint i = 0; i < state.tokens.length; i++) {
require(amounts[i] != 0, "INVALID_DEPOSIT_AMOUNT");
state.deposit(state.tokens[i].addr, amounts[i]);
}
state.transferOut(to, token, amount);
}

function transferOut(
address to,
function setBalanceL1(
address token,
uint amount
uint96 balance
)
external
nonReentrant
onlyFromAssetManager
{
token.transferOut(to, amount);
state.balancesL1[token] = balance;
}

function getBalanceL1(
address token
)
public
view
returns (uint96)
{
return state.balancesL1[token];
}
}
52 changes: 52 additions & 0 deletions packages/loopring_v3/contracts/amm/libamm/AmmAssetManagement.sol
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);
}
}
5 changes: 4 additions & 1 deletion packages/loopring_v3/contracts/amm/libamm/AmmData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma experimental ABIEncoderV2;
import "../../core/iface/ExchangeData.sol";
import "../../core/iface/IExchangeV3.sol";
import "../IAmmController.sol";
import "../IAssetManager.sol";
import "./IAmmSharedConfig.sol";


Expand Down Expand Up @@ -83,7 +84,7 @@ library AmmData

struct Settings {
IAmmController controller;
address assetManager;
IAssetManager assetManager;
bool joinsDisabled;
}

Expand Down Expand Up @@ -139,5 +140,7 @@ library AmmData
// A map from a user to the forced exit.
mapping (address => PoolExit) forcedExit;
mapping (bytes32 => bool) approvedTx;

mapping (address => uint96) balancesL1;
}
}
76 changes: 76 additions & 0 deletions packages/loopring_v3/contracts/amm/libamm/AmmDepositProcess.sol
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];
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"
);

ctx.txsDataPtr += ExchangeData.TX_DATA_AVAILABILITY_SIZE;
}
}
Loading