Skip to content

Commit 2adb311

Browse files
committed
Improve Staking and Escrow contracts
1 parent 75ca1a7 commit 2adb311

File tree

29 files changed

+574
-505
lines changed

29 files changed

+574
-505
lines changed

packages/apps/dashboard/ui-2024/src/pages/SearchResults/RoleDetails/RoleDetails.tsx

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ const RoleDetails = ({
171171
reputation,
172172
amountJobsProcessed,
173173
amountStaked,
174-
amountAllocated,
175174
amountLocked,
176175
},
177176
}: {
@@ -351,36 +350,6 @@ const RoleDetails = ({
351350
</Stack>
352351
</Stack>
353352
) : null}
354-
{amountAllocated !== undefined && amountAllocated !== null ? (
355-
<Stack
356-
gap={{ xs: 1, md: 0 }}
357-
direction={{ sm: 'column', md: 'row' }}
358-
>
359-
<Typography
360-
sx={{
361-
width: 300,
362-
}}
363-
variant="subtitle2"
364-
>
365-
Tokens Allocated
366-
</Typography>
367-
<Stack sx={{ whiteSpace: 'nowrap', flexDirection: 'row' }}>
368-
<Typography variant="body2">
369-
<FormatNumberWithDecimals value={amountAllocated} />
370-
</Typography>
371-
<Typography
372-
sx={{
373-
marginLeft: 0.5,
374-
}}
375-
variant="body2"
376-
color={colorPalette.fog.main}
377-
component="span"
378-
>
379-
HMT
380-
</Typography>
381-
</Stack>
382-
</Stack>
383-
) : null}
384353
{amountLocked !== undefined && amountLocked !== null ? (
385354
<Stack
386355
gap={{ xs: 1, md: 0 }}

packages/apps/dashboard/ui-2024/src/services/api/use-address-details.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ const leaderSchema = z.object({
7777
balance: z.string().transform(transformOptionalTokenAmount),
7878
role: z.nativeEnum(Roles).nullable(),
7979
amountStaked: z.string().optional().transform(transformOptionalTokenAmount),
80-
amountAllocated: z
81-
.string()
82-
.optional()
83-
.transform(transformOptionalTokenAmount),
8480
amountLocked: z.string().optional().transform(transformOptionalTokenAmount),
8581
lockedUntilTimestamp: z.string().optional(),
8682
reputation: reputationSchema,

packages/apps/dashboard/ui/src/state/leader/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export type LeaderData = {
55
address: string;
66
role?: string;
77
amountStaked: number;
8-
amountAllocated: number;
98
amountLocked: number;
109
amountSlashed: number;
1110
amountWithdrawn: number;
@@ -17,7 +16,6 @@ export type LeaderData = {
1716

1817
export type LeaderEscrowData = {
1918
address: string;
20-
amountAllocated: number;
2119
amountPayout: number;
2220
status: string;
2321
};

packages/apps/reputation-oracle/server/src/modules/payout/payout.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class PayoutService {
6969
results.amounts,
7070
results.url,
7171
results.hash,
72+
true,
7273
{
7374
gasPrice: await this.web3Service.calculateGasPrice(chainId),
7475
},

packages/core/contracts/Escrow.sol

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ contract Escrow is IEscrow, ReentrancyGuard {
189189
trusted
190190
notBroke
191191
notComplete
192-
notPaid
193192
nonReentrant
194193
returns (bool)
195194
{
@@ -218,6 +217,8 @@ contract Escrow is IEscrow, ReentrancyGuard {
218217
return true;
219218
}
220219

220+
// For backward compatibility: this function can only be called on existing escrows,
221+
// as the "Paid" status is not set anywhere for new escrows.
221222
function complete() external override notExpired trustedOrReputationOracle {
222223
require(status == EscrowStatuses.Paid, 'Escrow not in Paid state');
223224
status = EscrowStatuses.Complete;
@@ -243,7 +244,7 @@ contract Escrow is IEscrow, ReentrancyGuard {
243244

244245
/**
245246
* @dev Performs bulk payout to multiple workers
246-
* Escrow needs to be complted / cancelled, so that it can be paid out.
247+
* Escrow needs to be completed / cancelled, so that it can be paid out.
247248
* Every recipient is paid with the amount after reputation and recording oracle fees taken out.
248249
* If the amount is less than the fee, the recipient is not paid.
249250
* If the fee is zero, reputation, and recording oracle are not paid.
@@ -257,20 +258,21 @@ contract Escrow is IEscrow, ReentrancyGuard {
257258
* @param _url URL storing results as transaction details
258259
* @param _hash Hash of the results
259260
* @param _txId Transaction ID
261+
* @param forceComplete Boolean parameter indicating if remaining balance should be transferred to the escrow creator
260262
*/
261263
function bulkPayOut(
262264
address[] memory _recipients,
263265
uint256[] memory _amounts,
264266
string memory _url,
265267
string memory _hash,
266-
uint256 _txId
268+
uint256 _txId,
269+
bool forceComplete
267270
)
268-
external
271+
public
269272
override
270273
trustedOrReputationOracle
271274
notBroke
272275
notLaunched
273-
notPaid
274276
notExpired
275277
nonReentrant
276278
{
@@ -343,15 +345,22 @@ contract Escrow is IEscrow, ReentrancyGuard {
343345

344346
remainingFunds = cachedRemainingFunds;
345347

348+
// Check the forceComplete flag and transfer remaining funds if true
349+
if (forceComplete && cachedRemainingFunds > 0) {
350+
_safeTransfer(token, launcher, cachedRemainingFunds);
351+
cachedRemainingFunds = 0;
352+
}
353+
346354
if (cachedRemainingFunds == 0) {
347-
status = EscrowStatuses.Paid;
355+
status = EscrowStatuses.Complete;
348356
emit BulkTransferV2(
349357
_txId,
350358
_recipients,
351359
_amounts,
352360
false,
353361
finalResultsUrl
354362
);
363+
emit Completed();
355364
} else {
356365
status = EscrowStatuses.Partial;
357366
emit BulkTransferV2(
@@ -364,6 +373,26 @@ contract Escrow is IEscrow, ReentrancyGuard {
364373
}
365374
}
366375

376+
/**
377+
* @dev Overloaded function to perform bulk payout with default forceComplete set to false.
378+
* Calls the main bulkPayout function with forceComplete as false.
379+
*
380+
* @param _recipients Array of recipients
381+
* @param _amounts Array of amounts to be paid to each recipient.
382+
* @param _url URL storing results as transaction details
383+
* @param _hash Hash of the results
384+
* @param _txId Transaction ID
385+
*/
386+
function bulkPayOut(
387+
address[] memory _recipients,
388+
uint256[] memory _amounts,
389+
string memory _url,
390+
string memory _hash,
391+
uint256 _txId
392+
) external {
393+
bulkPayOut(_recipients, _amounts, _url, _hash, _txId, false);
394+
}
395+
367396
function _safeTransfer(address _token, address to, uint256 value) internal {
368397
SafeERC20.safeTransfer(IERC20(_token), to, value);
369398
}

packages/core/contracts/Staking.sol

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,12 @@ contract Staking is
220220
* @param _tokens Amount of tokens to unstake
221221
*/
222222
function unstake(uint256 _tokens) external override {
223-
Stakes.Staker storage staker = stakes[msg.sender];
224223
require(_tokens > 0, 'Must be a positive number');
224+
Stakes.Staker storage staker = stakes[msg.sender];
225+
require(
226+
staker.tokensLocked == 0,
227+
'Unstake in progress, complete it first'
228+
);
225229
uint256 stakeAvailable = staker.tokensAvailable();
226230
require(stakeAvailable >= _tokens, 'Insufficient amount to unstake');
227231

packages/core/contracts/interfaces/IEscrow.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ interface IEscrow {
3535

3636
function storeResults(string memory _url, string memory _hash) external;
3737

38+
function bulkPayOut(
39+
address[] memory _recipients,
40+
uint256[] memory _amounts,
41+
string memory _url,
42+
string memory _hash,
43+
uint256 _txId,
44+
bool forceComplete
45+
) external;
46+
3847
function bulkPayOut(
3948
address[] memory _recipients,
4049
uint256[] memory _amounts,

packages/core/contracts/libs/Stakes.sol

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
pragma solidity ^0.8.0;
44

5-
import '../utils/Math.sol';
6-
75
/**
86
* @title Structures, methods and data are available to manage the staker state.
97
*/
@@ -43,19 +41,8 @@ library Stakes {
4341
uint256 _tokens,
4442
uint256 _period
4543
) internal {
46-
uint256 lockingPeriod = _period;
47-
48-
if (stake.tokensLocked > 0) {
49-
lockingPeriod = Math.weightedAverage(
50-
Math.diffOrZero(stake.tokensLockedUntil, block.number), // Remaining lock period
51-
stake.tokensLocked,
52-
_period,
53-
_tokens
54-
);
55-
}
56-
5744
stake.tokensLocked += _tokens;
58-
stake.tokensLockedUntil = block.number + lockingPeriod;
45+
stake.tokensLockedUntil = block.number + _period;
5946
}
6047

6148
/**

0 commit comments

Comments
 (0)