@@ -74,6 +74,8 @@ contract Escrow is IEscrow, ReentrancyGuard {
7474
7575 uint256 public remainingFunds;
7676
77+ uint256 public reservedFunds;
78+
7779 constructor (
7880 address _token ,
7981 address _launcher ,
@@ -96,10 +98,7 @@ contract Escrow is IEscrow, ReentrancyGuard {
9698 }
9799
98100 function getBalance () public view returns (uint256 ) {
99- (bool success , bytes memory returnData ) = token.staticcall (
100- abi.encodeWithSelector (FUNC_SELECTOR_BALANCE_OF, address (this ))
101- );
102- return success ? abi.decode (returnData, (uint256 )) : 0 ;
101+ return getTokenBalance (token);
103102 }
104103
105104 function getTokenBalance (address _token ) public view returns (uint256 ) {
@@ -192,10 +191,7 @@ contract Escrow is IEscrow, ReentrancyGuard {
192191 nonReentrant
193192 returns (bool )
194193 {
195- _safeTransfer (token, canceler, remainingFunds);
196- status = EscrowStatuses.Cancelled;
197- remainingFunds = 0 ;
198- emit Cancelled ();
194+ status = EscrowStatuses.ToCancel;
199195 return true ;
200196 }
201197
@@ -229,26 +225,49 @@ contract Escrow is IEscrow, ReentrancyGuard {
229225 if (remainingFunds > 0 ) {
230226 _safeTransfer (token, launcher, remainingFunds);
231227 remainingFunds = 0 ;
228+ reservedFunds = 0 ;
229+ }
230+ if (status == EscrowStatuses.ToCancel) {
231+ status = EscrowStatuses.Cancelled;
232+ emit Cancelled ();
233+ } else {
234+ status = EscrowStatuses.Complete;
235+ emit Completed ();
232236 }
233- status = EscrowStatuses.Complete;
234- emit Completed ();
235237 }
236238
237239 function storeResults (
238240 string memory _url ,
239- string memory _hash
241+ string memory _hash ,
242+ uint256 _amount
240243 ) external override trustedOrRecordingOracle notExpired {
241244 require (
242245 status == EscrowStatuses.Pending ||
243- status == EscrowStatuses.Partial,
244- 'Escrow not in Pending or Partial status state '
246+ status == EscrowStatuses.Partial ||
247+ status == EscrowStatuses.ToCancel,
248+ 'Escrow not in Pending, Partial or ToCancel status state '
245249 );
246250 require (bytes (_url).length != 0 , "URL can't be empty " );
247251 require (bytes (_hash).length != 0 , "Hash can't be empty " );
252+ require (_amount > 0 , 'Amount must be greater than zero ' );
253+ require (
254+ _amount <= remainingFunds - reservedFunds,
255+ 'Not enough unreserved funds '
256+ );
248257
249258 intermediateResultsUrl = _url;
259+ reservedFunds += _amount;
250260
251261 emit IntermediateStorage (_url, _hash);
262+
263+ // If the escrow is ToCancel, transfer unreserved funds to launcher
264+ if (status == EscrowStatuses.ToCancel) {
265+ uint256 unreservedFunds = remainingFunds - reservedFunds;
266+ if (unreservedFunds > 0 ) {
267+ _safeTransfer (token, launcher, unreservedFunds);
268+ remainingFunds = reservedFunds;
269+ }
270+ }
252271 }
253272
254273 /**
@@ -298,20 +317,19 @@ contract Escrow is IEscrow, ReentrancyGuard {
298317 );
299318
300319 uint256 aggregatedBulkAmount = 0 ;
301- uint256 cachedRemainingFunds = remainingFunds;
302-
303320 for (uint256 i = 0 ; i < _amounts.length ; i++ ) {
304321 uint256 amount = _amounts[i];
305322 require (amount > 0 , 'Amount should be greater than zero ' );
306323 aggregatedBulkAmount += amount;
307324 }
308325 require (aggregatedBulkAmount < BULK_MAX_VALUE, 'Bulk value too high ' );
309326 require (
310- aggregatedBulkAmount <= cachedRemainingFunds ,
311- 'Not enough balance '
327+ aggregatedBulkAmount <= reservedFunds ,
328+ 'Not enough reserved funds '
312329 );
313330
314- cachedRemainingFunds -= aggregatedBulkAmount;
331+ reservedFunds -= aggregatedBulkAmount;
332+ remainingFunds -= aggregatedBulkAmount;
315333
316334 require (bytes (_url).length != 0 , "URL can't be empty " );
317335 require (bytes (_hash).length != 0 , "Hash can't be empty " );
@@ -352,9 +370,7 @@ contract Escrow is IEscrow, ReentrancyGuard {
352370 );
353371 }
354372
355- remainingFunds = cachedRemainingFunds;
356-
357- if (cachedRemainingFunds == 0 || forceComplete) {
373+ if (remainingFunds == 0 || forceComplete) {
358374 emit BulkTransferV2 (
359375 _txId,
360376 _recipients,
@@ -364,7 +380,9 @@ contract Escrow is IEscrow, ReentrancyGuard {
364380 );
365381 _complete ();
366382 } else {
367- status = EscrowStatuses.Partial;
383+ if (status != EscrowStatuses.ToCancel) {
384+ status = EscrowStatuses.Partial;
385+ }
368386 emit BulkTransferV2 (
369387 _txId,
370388 _recipients,
@@ -433,11 +451,6 @@ contract Escrow is IEscrow, ReentrancyGuard {
433451 _;
434452 }
435453
436- modifier notPaid () {
437- require (status != EscrowStatuses.Paid, 'Escrow in Paid status state ' );
438- _;
439- }
440-
441454 modifier notLaunched () {
442455 require (
443456 status != EscrowStatuses.Launched,
0 commit comments