Skip to content

Commit a53a188

Browse files
authored
Merge pull request #30 from DAM-Protocol/issue10-fix
2 parents a10d99b + 2ceae50 commit a53a188

2 files changed

Lines changed: 54 additions & 40 deletions

File tree

smart-contracts/contracts/StrollManager.sol

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import "./interfaces/IStrollManager.sol";
1212
/// @notice StrollManager is a contract that manages top ups for the Stroll protocol.
1313
contract StrollManager is IStrollManager, Ownable {
1414
IConstantFlowAgreementV1 public immutable CFA_V1;
15-
15+
1616
/// @dev IStrollManager.minLower implementation.
1717
uint64 public override minLower;
1818

@@ -23,7 +23,7 @@ contract StrollManager is IStrollManager, Ownable {
2323
mapping(address => bool) public override approvedStrategies;
2424

2525
mapping(bytes32 => TopUp) private topUps; //id = sha3(user, superToken, liquidityToken)
26-
26+
2727
constructor(
2828
address _icfa,
2929
uint64 _minLower,
@@ -43,12 +43,6 @@ contract StrollManager is IStrollManager, Ownable {
4343
uint64 _lowerLimit,
4444
uint64 _upperLimit
4545
) external override {
46-
if (
47-
_superToken == address(0) ||
48-
_strategy == address(0) ||
49-
_liquidityToken == address(0)
50-
) revert ZeroAddress();
51-
5246
if (_expiry <= block.timestamp)
5347
revert InvalidExpirationTime(_expiry, block.timestamp);
5448

@@ -58,27 +52,42 @@ contract StrollManager is IStrollManager, Ownable {
5852
if (_upperLimit < minUpper)
5953
revert InsufficientLimits(_upperLimit, minUpper);
6054

55+
bytes32 index = getTopUpIndex(msg.sender, _superToken, _liquidityToken);
6156

62-
if (!approvedStrategies[_strategy]) revert InvalidStrategy(_strategy);
63-
if (
64-
!IStrategy(_strategy).isSupportedSuperToken(
65-
ISuperToken(_superToken)
66-
)
67-
) revert UnsupportedSuperToken(address(_superToken));
57+
// If index owner/user is address(0), we are creating a new top-up.
58+
if (topUps[index].user != msg.sender) {
59+
if (
60+
_superToken == address(0) ||
61+
_strategy == address(0) ||
62+
_liquidityToken == address(0)
63+
) revert ZeroAddress();
64+
65+
if (!approvedStrategies[_strategy])
66+
revert InvalidStrategy(_strategy);
67+
if (
68+
!IStrategy(_strategy).isSupportedSuperToken(
69+
ISuperToken(_superToken)
70+
)
71+
) revert UnsupportedSuperToken(address(_superToken));
72+
73+
TopUp memory topUp = TopUp( // create new TopUp or update topup
74+
msg.sender,
75+
ISuperToken(_superToken),
76+
IStrategy(_strategy),
77+
_liquidityToken,
78+
_expiry,
79+
_lowerLimit,
80+
_upperLimit
81+
);
6882

69-
// check if topUp already exists for given user and superToken
70-
bytes32 index = getTopUpIndex(msg.sender, _superToken, _liquidityToken);
83+
topUps[index] = topUp;
84+
} else {
85+
// Else just update the limits and expiry, save gas.
7186

72-
TopUp memory topUp = TopUp( // create new TopUp or update topup
73-
msg.sender,
74-
ISuperToken(_superToken),
75-
IStrategy(_strategy),
76-
_liquidityToken,
77-
_expiry,
78-
_lowerLimit,
79-
_upperLimit
80-
);
81-
topUps[index] = topUp;
87+
topUps[index].expiry = _expiry;
88+
topUps[index].lowerLimit = _lowerLimit;
89+
topUps[index].upperLimit = _upperLimit;
90+
}
8291

8392
emit TopUpCreated(
8493
index,
@@ -126,7 +135,7 @@ contract StrollManager is IStrollManager, Ownable {
126135
onlyOwner
127136
{
128137
if (_strategy == address(0)) revert InvalidStrategy(_strategy);
129-
if(!approvedStrategies[_strategy]) {
138+
if (!approvedStrategies[_strategy]) {
130139
approvedStrategies[_strategy] = true;
131140
emit AddedApprovedStrategy(_strategy);
132141
}
@@ -168,23 +177,26 @@ contract StrollManager is IStrollManager, Ownable {
168177

169178
if (topUpAmount == 0) revert TopUpNotRequired(_index);
170179

171-
TopUp memory topUp = topUps[_index];
172-
topUp.strategy.topUp(
173-
topUp.user,
174-
topUp.superToken,
175-
topUpAmount
176-
);
180+
TopUp storage topUp = topUps[_index];
181+
182+
ISuperToken superToken = topUp.superToken;
183+
IStrategy strategy = topUp.strategy;
184+
185+
if (!strategy.isSupportedSuperToken(superToken))
186+
revert UnsupportedSuperToken(address(superToken));
187+
188+
strategy.topUp(topUp.user, superToken, topUpAmount);
177189
emit PerformedTopUp(_index, topUpAmount);
178190
}
179191

180192
/// @dev IStrollManager.deleteTopUpByIndex implementation.
181193
function deleteTopUpByIndex(bytes32 _index) public {
182-
TopUp memory topUp = topUps[_index];
183-
184-
if (topUp.user != msg.sender && topUp.expiry >= block.timestamp)
185-
revert UnauthorizedCaller(msg.sender, topUp.user);
194+
TopUp storage topUp = topUps[_index];
186195

187-
delete topUps[_index];
196+
address user = topUp.user;
197+
198+
if (user != msg.sender && topUp.expiry >= block.timestamp)
199+
revert UnauthorizedCaller(msg.sender, user);
188200

189201
emit TopUpDeleted(
190202
_index,
@@ -193,6 +205,8 @@ contract StrollManager is IStrollManager, Ownable {
193205
address(topUp.strategy),
194206
topUp.liquidityToken
195207
);
208+
209+
delete topUps[_index];
196210
}
197211

198212
/// @dev IStrollManager.getTopUpByIndex implementation.
@@ -210,7 +224,7 @@ contract StrollManager is IStrollManager, Ownable {
210224
view
211225
returns (uint256 _amount)
212226
{
213-
TopUp memory topUp = topUps[_index];
227+
TopUp storage topUp = topUps[_index];
214228

215229
if (
216230
topUp.user == address(0) || // Task exists and has a valid user

smart-contracts/hardhat.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ module.exports = {
8383
mocha: {
8484
timeout: 0,
8585
},
86-
}
86+
},
8787
};

0 commit comments

Comments
 (0)