Skip to content

refactor: return typed parameters [WIP] #1322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions contracts/src/arbitration/KlerosCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
uint256 repartition; // The index of the repartition to execute.
}

struct JurorBalance {
uint256 totalStaked; // The total amount of PNKs staked on the arbitrator.
uint256 totalLocked; // The total amount of PNKs locked on the arbitrator.
uint256 stakedInCourt; // The amount of PNKs staked in a particular court.
uint256 nbCourts; // The number of courts the juror has staked in.
}

struct CurrencyRate {
bool feePaymentAccepted;
uint64 rateInEth;
Expand Down Expand Up @@ -1000,15 +1007,12 @@ contract KlerosCore is IArbitratorV2, UUPSProxiable, Initializable {
return disputes[_disputeID].rounds.length;
}

function getJurorBalance(
address _juror,
uint96 _courtID
) external view returns (uint256 totalStaked, uint256 totalLocked, uint256 stakedInCourt, uint256 nbCourts) {
function getJurorBalance(address _juror, uint96 _courtID) external view returns (JurorBalance memory balance) {
Juror storage juror = jurors[_juror];
totalStaked = juror.stakedPnk;
totalLocked = juror.lockedPnk;
stakedInCourt = juror.stakedPnkByCourt[_courtID];
nbCourts = juror.courtIDs.length;
balance.totalStaked = juror.stakedPnk;
balance.totalLocked = juror.lockedPnk;
balance.stakedInCourt = juror.stakedPnkByCourt[_courtID];
balance.nbCourts = juror.courtIDs.length;
}

function isSupported(uint96 _courtID, uint256 _disputeKitID) external view returns (bool) {
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/arbitration/SortitionModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ contract SortitionModule is ISortitionModule, UUPSProxiable, Initializable {
uint96 _courtID,
uint256 _stake
) external override onlyByCore returns (preStakeHookResult) {
(, , uint256 currentStake, uint256 nbCourts) = core.getJurorBalance(_account, _courtID);
if (currentStake == 0 && nbCourts >= MAX_STAKE_PATHS) {
KlerosCore.JurorBalance memory jurorBalance = core.getJurorBalance(_account, _courtID);
if (jurorBalance.stakedInCourt == 0 && jurorBalance.nbCourts >= MAX_STAKE_PATHS) {
// Prevent staking beyond MAX_STAKE_PATHS but unstaking is always allowed.
return preStakeHookResult.failed;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ contract DisputeKitClassic is IDisputeKit, Initializable, UUPSProxiable {
uint256 lockedAmountPerJuror = core
.getRoundInfo(_coreDisputeID, core.getNumberOfRounds(_coreDisputeID) - 1)
.pnkAtStakePerJuror;
(uint256 totalStaked, uint256 totalLocked, , ) = core.getJurorBalance(_juror, courtID);
return totalStaked >= totalLocked + lockedAmountPerJuror;
KlerosCore.JurorBalance memory jurorBalance = core.getJurorBalance(_juror, courtID);
return jurorBalance.totalStaked >= jurorBalance.totalLocked + lockedAmountPerJuror;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ contract DisputeKitSybilResistant is IDisputeKit, Initializable, UUPSProxiable {
uint256 lockedAmountPerJuror = core
.getRoundInfo(_coreDisputeID, core.getNumberOfRounds(_coreDisputeID) - 1)
.pnkAtStakePerJuror;
(uint256 totalStaked, uint256 totalLocked, , ) = core.getJurorBalance(_juror, courtID);
if (totalStaked < totalLocked + lockedAmountPerJuror) {
KlerosCore.JurorBalance memory jurorBalance = core.getJurorBalance(_juror, courtID);
if (jurorBalance.totalStaked < jurorBalance.totalLocked + lockedAmountPerJuror) {
return false;
} else {
return _proofOfHumanity(_juror);
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/Popup/Description/StakeWithdraw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const StakeWithdraw: React.FC<IStakeWithdraw> = ({ pnkStaked, courtName, isStake

<TotalStakeContainer>
<StyledKlerosLogo /> <MyStakeContainer>My Stake:</MyStakeContainer>{" "}
<AmountContainer>{`${formatUnits(jurorBalance?.[2] ?? BigInt(0), 18)} PNK`} </AmountContainer>
<AmountContainer>{`${formatUnits(jurorBalance?.stakedInCourt ?? BigInt(0), 18)} PNK`} </AmountContainer>
</TotalStakeContainer>
</Container>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({
args: [address, id],
watch: true,
});
const parsedStake = formatPNK(jurorBalance?.[2] || 0n, 0, true);
const parsedStake = formatPNK(jurorBalance?.stakedInCourt || 0n, 0, true);
const isStaking = action === ActionType.stake;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ const bigIntRatioToPercentage = (numerator: bigint, denominator: bigint): string
};

const useCalculateJurorOdds = (
jurorBalance: readonly [bigint, bigint, bigint] | undefined,
jurorBalance:
| {
totalStaked: bigint;
totalLocked: bigint;
stakedInCourt: bigint;
nbCourts: bigint;
}
| undefined,
stakedByAllJurors: string | undefined,
loading: boolean
): string => {
Expand All @@ -58,7 +65,7 @@ const useCalculateJurorOdds = (
return "0.00%";
}

return bigIntRatioToPercentage(jurorBalance[2], BigInt(stakedByAllJurors));
return bigIntRatioToPercentage(jurorBalance.stakedInCourt, BigInt(stakedByAllJurors));
}, [jurorBalance, stakedByAllJurors, loading]);
};

Expand All @@ -78,10 +85,10 @@ const JurorBalanceDisplay = () => {
const [previousStakedByAllJurors, setPreviousStakedByAllJurors] = useState<bigint | undefined>(undefined);

useEffect(() => {
if (previousJurorBalance !== undefined && jurorBalance?.[2] !== previousJurorBalance) {
if (previousJurorBalance !== undefined && jurorBalance?.stakedInCourt !== previousJurorBalance) {
setLoading(true);
}
setPreviousJurorBalance(jurorBalance?.[2]);
setPreviousJurorBalance(jurorBalance?.stakedInCourt);
}, [jurorBalance, previousJurorBalance]);

useEffect(() => {
Expand All @@ -99,12 +106,12 @@ const JurorBalanceDisplay = () => {
{
icon: PNKIcon,
name: "My Stake",
value: `${format(jurorBalance?.[2])} PNK`,
value: `${format(jurorBalance?.stakedInCourt)} PNK`,
},
{
icon: LockerIcon,
name: "Locked Stake",
value: `${format(jurorBalance?.[1])} PNK`,
value: `${format(jurorBalance?.totalLocked)} PNK`,
},
{
icon: DiceIcon,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
if (isAllowance) {
return parsedAmount;
} else if (isStaking) {
return jurorBalance[2] + parsedAmount;
return jurorBalance.stakedInCourt + parsedAmount;
} else {
return jurorBalance[2] - parsedAmount;
return jurorBalance.stakedInCourt - parsedAmount;
}
}
return 0n;
Expand Down Expand Up @@ -121,7 +121,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
},
[ActionType.withdraw]: {
text: "Withdraw",
checkDisabled: () => !jurorBalance || parsedAmount > jurorBalance[2],
checkDisabled: () => !jurorBalance || parsedAmount > jurorBalance.stakedInCourt,
onClick: handleStake,
},
};
Expand Down