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
18 changes: 18 additions & 0 deletions packages/loopring_v3/contracts/amm/AmplifiedAmmController.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2017 Loopring Technology Limited.
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;

import "./IAmmController.sol";
import "../amm/LoopringAmmPool.sol";
Expand Down Expand Up @@ -77,4 +78,21 @@ contract AmplifiedAmmController is IAmmController, Claimable
amplificationFactor = AMPLIFICATION_FACTOR_BASE;
}
}

function setupPool(
LoopringAmmPool pool,
AmmData.PoolConfig calldata config
)
external
onlyOwner
{
pool.setupPool(config);
}

function enableExitMode(LoopringAmmPool pool)
external
onlyOwner
{
pool.enableExitMode();
}
}
9 changes: 8 additions & 1 deletion packages/loopring_v3/contracts/amm/LoopringAmmPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,17 @@ contract LoopringAmmPool is
external
nonReentrant
{
require(state.accountID == 0 || msg.sender == address(controller), "UNAUTHORIZED");
state.setupPool(config);
}

function enableExitMode()
external
onlyFromController
{
state.exitMode = true;
}

// Anyone is able to shut down the pool when requests aren't being processed any more.
function shutdown(address exitOwner)
external
Expand All @@ -113,7 +121,6 @@ contract LoopringAmmPool is

function shutdownByController()
external
payable
onlyWhenOnline
nonReentrant
onlyFromController
Expand Down
1 change: 1 addition & 0 deletions packages/loopring_v3/contracts/amm/libamm/AmmData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,6 @@ library AmmData
mapping (bytes32 => bool) approvedTx;

mapping (address => uint96) balancesL1;
bool exitMode;
}
}
18 changes: 11 additions & 7 deletions packages/loopring_v3/contracts/amm/libamm/AmmExitProcess.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ library AmmExitProcess
bool isForcedExit = false;

if (signature.length == 0) {
bytes32 forcedExitHash = AmmExitRequest.hash(ctx.domainSeparator, S.forcedExit[exit.owner]);
if (txHash == forcedExitHash) {
delete S.forcedExit[exit.owner];
S.forcedExitCount--;
isForcedExit = true;
if (S.exitMode) {
require(exit.fee == 0, "INVALID_FEE");
} else {
require(S.approvedTx[txHash], "INVALID_ONCHAIN_APPROVAL");
delete S.approvedTx[txHash];
bytes32 forcedExitHash = AmmExitRequest.hash(ctx.domainSeparator, S.forcedExit[exit.owner]);
if (txHash == forcedExitHash) {
delete S.forcedExit[exit.owner];
S.forcedExitCount--;
isForcedExit = true;
} else {
require(S.approvedTx[txHash], "INVALID_ONCHAIN_APPROVAL");
delete S.approvedTx[txHash];
}
}
} else if (signature.length == 1) {
ctx.verifySignatureL2(exit.owner, txHash, signature);
Expand Down
2 changes: 1 addition & 1 deletion packages/loopring_v3/contracts/amm/libamm/AmmPoolToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ library AmmPoolToken
uint256 deadline,
bytes calldata signature
)
internal
public
{
require(deadline >= block.timestamp, 'EXPIRED');

Expand Down
46 changes: 28 additions & 18 deletions packages/loopring_v3/contracts/amm/libamm/AmmStatus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,26 @@ library AmmStatus
bytes(config.poolName).length > 0 && bytes(config.tokenSymbol).length > 0,
"INVALID_NAME_OR_SYMBOL"
);

require(config.sharedConfig != address(0), "INVALID_SHARED_CONFIG");
require(config.tokens.length == config.weights.length, "INVALID_DATA");
require(config.tokens.length >= 2, "INVALID_DATA");
require(config.exchange != address(0), "INVALID_EXCHANGE");
require(config.accountID != 0, "INVALID_ACCOUNT_ID");
require(S.tokens.length == 0, "ALREADY_INITIALIZED");

S.sharedConfig = IAmmSharedConfig(config.sharedConfig);
IExchangeV3 exchange = IExchangeV3(config.exchange);
S.exchange = exchange;
S.exchangeOwner = exchange.owner();
S.exchangeDomainSeparator = exchange.getDomainSeparator();
S.accountID = config.accountID;
S.poolTokenID = exchange.getTokenID(address(this));
require(S._totalSupply == 0, "POOL_IN_USE");

S.feeBips = config.feeBips;
S.domainSeparator = EIP712.hash(EIP712.Domain(config.poolName, "1.0.0", address(this)));

S.poolName = config.poolName;
S.symbol = config.tokenSymbol;
S.shutdownTimestamp = 0;
S.exitMode = false;

IExchangeV3 exchange = IExchangeV3(config.exchange);

delete S.tokens;
for (uint i = 0; i < config.tokens.length; i++) {
address token = config.tokens[i];
S.tokens.push(AmmData.Token({
Expand All @@ -71,16 +71,26 @@ library AmmStatus
}));
}

// Mint all liquidity tokens to the pool account on L2
S.balanceOf[address(this)] = AmmData.POOL_TOKEN_MINTED_SUPPLY;
S.allowance[address(this)][address(exchange.getDepositContract())] = type(uint256).max;
exchange.deposit(
address(this), // from
address(this), // to
address(this), // token
uint96(AmmData.POOL_TOKEN_MINTED_SUPPLY),
new bytes(0)
);
bool newPool = (S.accountID == 0);
if (newPool) {
S.sharedConfig = IAmmSharedConfig(config.sharedConfig);
S.exchange = exchange;
S.exchangeOwner = exchange.owner();
S.exchangeDomainSeparator = exchange.getDomainSeparator();
S.accountID = config.accountID;
S.poolTokenID = exchange.getTokenID(address(this));

// Mint all liquidity tokens to the pool account on L2
S.balanceOf[address(this)] = AmmData.POOL_TOKEN_MINTED_SUPPLY;
S.allowance[address(this)][address(exchange.getDepositContract())] = type(uint256).max;
exchange.deposit(
address(this), // from
address(this), // to
address(this), // token
uint96(AmmData.POOL_TOKEN_MINTED_SUPPLY),
new bytes(0)
);
}
}

// Anyone is able to shut down the pool when requests aren't being processed any more.
Expand Down
4 changes: 4 additions & 0 deletions packages/loopring_v3/migrations/7_deploy_amm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ const AmmExitRequest = artifacts.require("AmmExitRequest");
const AmmStatus = artifacts.require("AmmStatus");
const AmmWithdrawal = artifacts.require("AmmWithdrawal");
const AmmAssetManagement = artifacts.require("AmmAssetManagement");
const AmmPoolToken = artifacts.require("AmmPoolToken");
const AmplifiedAmmController = artifacts.require("AmplifiedAmmController");

module.exports = function(deployer, network, accounts) {
if (network != "live" && network != "live-fork") {
deployer.then(async () => {
await deployer.deploy(AmplifiedAmmController);

await deployer.deploy(AmmPoolToken);
await deployer.deploy(AmmAssetManagement);
await deployer.deploy(AmmJoinRequest);
await deployer.deploy(AmmExitRequest);
await deployer.deploy(AmmStatus);
await deployer.deploy(AmmWithdrawal);
await deployer.link(AmmPoolToken, LoopringAmmPool);
await deployer.link(AmmAssetManagement, LoopringAmmPool);
await deployer.link(AmmJoinRequest, LoopringAmmPool);
await deployer.link(AmmExitRequest, LoopringAmmPool);
Expand All @@ -32,6 +35,7 @@ module.exports = function(deployer, network, accounts) {
false
);

await deployer.link(AmmPoolToken, LoopringAmmPoolCopy);
await deployer.link(AmmAssetManagement, LoopringAmmPoolCopy);
await deployer.link(AmmJoinRequest, LoopringAmmPoolCopy);
await deployer.link(AmmExitRequest, LoopringAmmPoolCopy);
Expand Down