@@ -124,15 +124,16 @@ contract Escrow is
124
124
require (token!= address (0 ),'Invalid token ' );
125
125
require (payee!= address (0 ),'Invalid payee ' );
126
126
uint256 i;
127
- for (i= 0 ;i< userAuths[msg .sender ][token].length ;i++ ){
127
+ uint256 length= userAuths[msg .sender ][token].length ;
128
+ for (i= 0 ;i< length;i++ ){
128
129
if (userAuths[msg .sender ][token][i].payee== payee){
129
130
userAuths[msg .sender ][token][i].maxLockedAmount= maxLockedAmount;
130
131
userAuths[msg .sender ][token][i].maxLockSeconds= maxLockSeconds;
131
132
userAuths[msg .sender ][token][i].maxLockCounts= maxLockCounts;
132
133
break ;
133
134
}
134
135
}
135
- if (i== userAuths[ msg . sender ][token]. length ){ //not found
136
+ if (i== length){ //not found
136
137
userAuths[msg .sender ][token].push (auth (payee,maxLockedAmount,0 ,maxLockSeconds,maxLockCounts,0 ));
137
138
}
138
139
emit Auth (msg .sender ,payee,maxLockedAmount,maxLockSeconds,maxLockCounts);
@@ -161,7 +162,8 @@ contract Escrow is
161
162
function getLocks (address token ,address payer ,address payee ) public view returns (lock[] memory ){
162
163
// since solidty does not supports dynamic memory arrays, we need to calculate the return size first
163
164
uint256 size= 0 ;
164
- for (uint256 i= 0 ;i< locks.length ;i++ ){
165
+ uint256 length= locks.length ;
166
+ for (uint256 i= 0 ;i< length;i++ ){
165
167
if (
166
168
(address (token)== address (0 ) || address (token)== locks[i].token) ||
167
169
(address (payee)== address (0 ) || address (payee)== locks[i].payee) ||
@@ -173,7 +175,7 @@ contract Escrow is
173
175
174
176
lock[] memory tempPendings= new lock [](size);
175
177
size= 0 ;
176
- for (uint256 i= 0 ;i< locks. length ;i++ ){
178
+ for (uint256 i= 0 ;i< length;i++ ){
177
179
if (
178
180
(address (token)== address (0 ) || address (token)== locks[i].token) ||
179
181
(address (payee)== address (0 ) || address (payee)== locks[i].payee) ||
@@ -200,12 +202,13 @@ contract Escrow is
200
202
require (token!= address (0 ),'Invalid token ' );
201
203
// since solidty does not supports dynamic memory arrays, we need to calculate the return size first
202
204
uint256 size= 0 ;
203
- for (uint256 i= 0 ;i< userAuths[payer][token].length ;i++ ){
205
+ uint256 length= userAuths[payer][token].length ;
206
+ for (uint256 i= 0 ;i< length;i++ ){
204
207
if (address (payee)== address (0 ) || address (payee)== userAuths[payer][token][i].payee) size++ ;
205
208
}
206
209
auth[] memory tempAuths= new auth [](size);
207
210
size= 0 ;
208
- for (uint256 i= 0 ;i< userAuths[payer][token]. length ;i++ ){
211
+ for (uint256 i= 0 ;i< length;i++ ){
209
212
if (address (payee)== address (0 ) || address (payee)== userAuths[payer][token][i].payee){
210
213
tempAuths[size]= userAuths[payer][token][i];
211
214
size++ ;
@@ -231,11 +234,12 @@ contract Escrow is
231
234
require (token!= address (0 ),'Invalid token ' );
232
235
require (amount> 0 ,"Invalid amount " );
233
236
require (jobId> 0 ,"Invalid jobId " );
234
- auth memory tempAuth;
237
+ auth memory tempAuth= auth ( address ( 0 ), 0 , 0 , 0 , 0 , 0 ) ;
235
238
uint256 index;
236
239
uint256 ts= block .timestamp ;
237
240
require (funds[payer][token].available>= amount,"Payer does not have enough funds " );
238
- for (index= 0 ;index< userAuths[payer][token].length ;index++ ){
241
+ uint256 length= userAuths[payer][token].length ;
242
+ for (index= 0 ;index< length;index++ ){
239
243
if (msg .sender == userAuths[payer][token][index].payee) {
240
244
tempAuth= userAuths[payer][token][index];
241
245
break ;
@@ -247,7 +251,8 @@ contract Escrow is
247
251
require (tempAuth.currentLockedAmount+ amount<= tempAuth.maxLockedAmount,"Exceeds maxLockedAmount " );
248
252
require (tempAuth.currentLocks< tempAuth.maxLockCounts,"Exceeds maxLockCounts " );
249
253
// check jobId
250
- for (uint256 i= 0 ;i< locks.length ;i++ ){
254
+ length= locks.length ;
255
+ for (uint256 i= 0 ;i< length;i++ ){
251
256
if (locks[i].payer== payer && locks[i].payee== msg .sender && locks[i].jobId== jobId){
252
257
revert ("JobId already exists " );
253
258
}
@@ -282,7 +287,8 @@ contract Escrow is
282
287
require (jobId> 0 ,'Invalid jobId ' );
283
288
lock memory tempLock;
284
289
uint256 index;
285
- for (index= 0 ;index< locks.length ;index++ ){
290
+ uint256 length= locks.length ;
291
+ for (index= 0 ;index< length;index++ ){
286
292
if (
287
293
msg .sender == locks[index].payee &&
288
294
payer== locks[index].payer &&
@@ -302,7 +308,8 @@ contract Escrow is
302
308
require (tempLock.amount>= amount,"Amount too high " );
303
309
304
310
//update auths
305
- for (uint256 i= 0 ;i< userAuths[payer][token].length ;i++ ){
311
+ length= userAuths[payer][token].length ;
312
+ for (uint256 i= 0 ;i< length;i++ ){
306
313
if (userAuths[payer][token][i].payee== msg .sender ){
307
314
userAuths[payer][token][i].currentLockedAmount-= tempLock.amount;
308
315
userAuths[payer][token][i].currentLocks-= 1 ;
@@ -335,7 +342,8 @@ contract Escrow is
335
342
uint256 index;
336
343
//since solidy does not supports dynamic arrays, we need to count first
337
344
uint256 found= 0 ;
338
- for (index= 0 ;index< locks.length ;index++ ){
345
+ uint256 length= locks.length ;
346
+ for (index= 0 ;index< length;index++ ){
339
347
if (locks[index].expiry< block .timestamp &&
340
348
(
341
349
(jobId== 0 || jobId== locks[index].jobId) &&
@@ -352,7 +360,7 @@ contract Escrow is
352
360
uint256 [] memory indexToDelete=new uint256 [](found);
353
361
uint256 currentIndex= 0 ;
354
362
// now actually do the work
355
- for (index= 0 ;index< locks. length ;index++ ){
363
+ for (index= 0 ;index< length;index++ ){
356
364
if (locks[index].expiry< block .timestamp &&
357
365
(
358
366
(jobId== 0 || jobId== locks[index].jobId) &&
@@ -363,7 +371,8 @@ contract Escrow is
363
371
){
364
372
//cancel each lock, one by one
365
373
//update auths
366
- for (uint256 i= 0 ;i< userAuths[locks[index].payer][locks[index].token].length ;i++ ){
374
+ uint256 authsLength= userAuths[locks[index].payer][locks[index].token].length ;
375
+ for (uint256 i= 0 ;i< authsLength;i++ ){
367
376
if (userAuths[payer][token][i].payee== locks[index].payee){
368
377
userAuths[payer][token][i].currentLockedAmount-= locks[index].amount;
369
378
userAuths[payer][token][i].currentLocks-= 1 ;
@@ -379,7 +388,8 @@ contract Escrow is
379
388
}
380
389
}
381
390
//delete the locks
382
- for (index= 0 ;index< indexToDelete.length ;index++ ){
391
+ uint256 delLength= indexToDelete.length ;
392
+ for (index= 0 ;index< delLength;index++ ){
383
393
locks[indexToDelete[index]]= locks[locks.length -1 ];
384
394
locks.pop ();
385
395
}
0 commit comments