Skip to content

Commit 00b5613

Browse files
authored
[Loopring AMM] Add more tests and fix a few issues (#1835)
1 parent e9a4174 commit 00b5613

File tree

11 files changed

+661
-197
lines changed

11 files changed

+661
-197
lines changed

packages/loopring_v3/contracts/amm/libamm/AmmBlockReceiver.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ library AmmBlockReceiver
4242
S.approveAmmUpdates(ctx, false);
4343

4444
// Update state
45-
S.poolTokenBurnedSupply = ctx.poolTokenBurnedSupply;
45+
S._totalSupply = ctx.totalSupply;
4646

4747
return ctx.txIdx - txIdx;
4848
}
@@ -65,7 +65,7 @@ library AmmBlockReceiver
6565
domainSeparator: S.domainSeparator,
6666
accountID: S.accountID,
6767
poolTokenID: S.poolTokenID,
68-
poolTokenBurnedSupply: S.poolTokenBurnedSupply,
68+
totalSupply: S._totalSupply,
6969
size: size,
7070
tokens: S.tokens,
7171
tokenBalancesL2: new uint96[](size)

packages/loopring_v3/contracts/amm/libamm/AmmData.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ library AmmData
8181
uint32 accountID;
8282

8383
uint16 poolTokenID;
84-
uint poolTokenBurnedSupply;
84+
uint totalSupply;
8585

8686
uint size; // == token.length;
8787
Token[] tokens;
@@ -92,7 +92,7 @@ library AmmData
9292
// Pool token state variables
9393
string poolName;
9494
string symbol;
95-
uint poolTokenBurnedSupply;
95+
uint _totalSupply;
9696

9797
mapping(address => uint) balanceOf;
9898
mapping(address => mapping(address => uint)) allowance;

packages/loopring_v3/contracts/amm/libamm/AmmExitProcess.sol

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ library AmmExitProcess
6565
return;
6666
}
6767

68-
ctx.poolTokenBurnedSupply = ctx.poolTokenBurnedSupply.add(exit.burnAmount);
68+
ctx.totalSupply = ctx.totalSupply.sub(exit.burnAmount);
6969
} else {
7070
require(slippageOK, "EXIT_SLIPPAGE_INVALID");
7171
_burnL2(ctx, exit.burnAmount, exit.owner, exit.burnStorageID);
@@ -124,29 +124,24 @@ library AmmExitProcess
124124
ctx.approveTransfer(transfer);
125125

126126
// Update pool balance
127-
ctx.poolTokenBurnedSupply = ctx.poolTokenBurnedSupply.add(transfer.amount);
127+
ctx.totalSupply = ctx.totalSupply.sub(transfer.amount);
128128
}
129129

130130
function _calculateExitAmounts(
131131
AmmData.Context memory ctx,
132132
AmmData.PoolExit memory exit
133133
)
134134
private
135-
view
135+
pure
136136
returns(
137137
bool /* slippageOK */,
138138
uint96[] memory amounts
139139
)
140140
{
141141
amounts = new uint96[](ctx.size);
142142

143-
// Check if we can still use this exit
144-
if (block.timestamp > exit.validUntil) {
145-
return (false, amounts);
146-
}
147-
148143
// Calculate how much will be withdrawn
149-
uint ratio = uint(AmmData.POOL_TOKEN_BASE()).mul(exit.burnAmount) / ctx.totalSupply();
144+
uint ratio = uint(AmmData.POOL_TOKEN_BASE()).mul(exit.burnAmount) / ctx.totalSupply;
150145

151146
for (uint i = 0; i < ctx.size; i++) {
152147
amounts[i] = (ratio.mul(ctx.tokenBalancesL2[i]) / AmmData.POOL_TOKEN_BASE()).toUint96();

packages/loopring_v3/contracts/amm/libamm/AmmJoinProcess.sol

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ library AmmJoinProcess
9999
ctx.approveTransfer(transfer);
100100

101101
// Update pool balance
102-
ctx.poolTokenBurnedSupply = ctx.poolTokenBurnedSupply.sub(transfer.amount);
102+
ctx.totalSupply = ctx.totalSupply.add(transfer.amount);
103103
}
104104

105105
function _calculateJoinAmounts(
106106
AmmData.Context memory ctx,
107107
AmmData.PoolJoin memory join
108108
)
109109
private
110-
view
110+
pure
111111
returns(
112112
bool slippageOK,
113113
uint96 mintAmount,
@@ -117,11 +117,7 @@ library AmmJoinProcess
117117
// Check if we can still use this join
118118
amounts = new uint96[](ctx.size);
119119

120-
if (block.timestamp > join.validUntil) {
121-
return (false, 0, amounts);
122-
}
123-
124-
if (ctx.totalSupply() == 0) {
120+
if (ctx.totalSupply == 0) {
125121
return(true, AmmData.POOL_TOKEN_BASE().toUint96(), join.joinAmounts);
126122
}
127123

@@ -130,7 +126,7 @@ library AmmJoinProcess
130126
for (uint i = 0; i < ctx.size; i++) {
131127
if (ctx.tokenBalancesL2[i] > 0) {
132128
uint amountOut = uint(join.joinAmounts[i])
133-
.mul(ctx.totalSupply()) / uint(ctx.tokenBalancesL2[i]);
129+
.mul(ctx.totalSupply) / uint(ctx.tokenBalancesL2[i]);
134130

135131
if (!initialized) {
136132
initialized = true;
@@ -146,10 +142,10 @@ library AmmJoinProcess
146142
}
147143

148144
// Calculate the amounts to deposit
149-
uint ratio = uint(AmmData.POOL_TOKEN_BASE()).mul(mintAmount) / ctx.totalSupply();
145+
uint ratio = uint(AmmData.POOL_TOKEN_BASE()).mul(mintAmount) / ctx.totalSupply;
150146

151147
for (uint i = 0; i < ctx.size; i++) {
152-
amounts[i] = ratio.mul(ctx.tokenBalancesL2[i] / AmmData.POOL_TOKEN_BASE()).toUint96();
148+
amounts[i] = (ratio.mul(ctx.tokenBalancesL2[i]) / AmmData.POOL_TOKEN_BASE()).toUint96();
153149
}
154150

155151
slippageOK = (mintAmount >= join.mintMinAmount);

packages/loopring_v3/contracts/amm/libamm/AmmPoolToken.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ library AmmPoolToken
2828
view
2929
returns (uint)
3030
{
31-
return AmmData.POOL_TOKEN_MINTED_SUPPLY().sub(S.poolTokenBurnedSupply);
31+
return S._totalSupply;
3232
}
3333

3434
function approve(

packages/loopring_v3/contracts/amm/libamm/AmmStatus.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ library AmmStatus
7373

7474
// Mint all liquidity tokens to the pool account on L2
7575
S.balanceOf[address(this)] = AmmData.POOL_TOKEN_MINTED_SUPPLY();
76-
S.poolTokenBurnedSupply = AmmData.POOL_TOKEN_MINTED_SUPPLY();
7776
S.allowance[address(this)][address(exchange.getDepositContract())] = uint(-1);
7877
exchange.deposit(
7978
address(this), // from
@@ -91,11 +90,13 @@ library AmmStatus
9190
)
9291
internal
9392
{
94-
uint64 validUntil = S.forcedExit[exitOwner].validUntil;
95-
require(validUntil > 0 && validUntil < block.timestamp, "INVALID_CHALLENGE");
96-
97-
uint size = S.tokens.length;
93+
// If the exchange is in withdrawal mode allow the pool to be shutdown immediately
9894
if (!S.exchange.isInWithdrawalMode()) {
95+
uint64 validUntil = S.forcedExit[exitOwner].validUntil;
96+
require(validUntil > 0 && validUntil < block.timestamp, "INVALID_CHALLENGE");
97+
98+
uint size = S.tokens.length;
99+
99100
for (uint i = 0; i < size; i++) {
100101
S.exchange.forceWithdraw{value: msg.value / size}(
101102
address(this),

packages/loopring_v3/contracts/amm/libamm/AmmUtil.sol

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ library AmmUtil
2828
ctx.exchange.approveTransaction(transfer.from, hash);
2929
}
3030

31-
function totalSupply(
32-
AmmData.Context memory ctx
33-
)
34-
internal
35-
pure
36-
returns (uint)
37-
{
38-
return AmmData.POOL_TOKEN_MINTED_SUPPLY().sub(ctx.poolTokenBurnedSupply);
39-
}
40-
4131
function isAlmostEqualAmount(
4232
uint96 amount,
4333
uint96 targetAmount

packages/loopring_v3/contracts/amm/libamm/AmmWithdrawal.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ library AmmWithdrawal
4848
// Burn any additional pool tokens stuck in forced exits
4949
AmmData.PoolExit storage exit = S.forcedExit[msg.sender];
5050
if (exit.burnAmount > 0) {
51-
delete S.forcedExit[msg.sender];
5251
poolAmount = poolAmount.add(exit.burnAmount);
52+
delete S.forcedExit[msg.sender];
5353
}
5454

5555
require(poolAmount > 0, "ZERO_POOL_AMOUNT");
@@ -66,7 +66,7 @@ library AmmWithdrawal
6666
AmmUtil.transferOut(token, amount, msg.sender);
6767
}
6868

69-
S.poolTokenBurnedSupply = S.poolTokenBurnedSupply.add(poolAmount);
69+
S._totalSupply = S._totalSupply.sub(poolAmount);
7070
}
7171

7272
function _checkWithdrawalConditionInShutdown(
@@ -90,7 +90,7 @@ library AmmWithdrawal
9090
// Check that nothing is withdrawable anymore.
9191
require(
9292
exchange.getAmountWithdrawable(address(this), token) == 0,
93-
"MORE_TO_WITHDRAWAL"
93+
"MORE_TO_WITHDRAW"
9494
);
9595
}
9696
}

0 commit comments

Comments
 (0)