Skip to content

Commit cdf01b8

Browse files
authored
Merge pull request #306 from api3dao/add-totalstake-to-logs
Add totalStake to Staked and Unstaked events
2 parents e05d4a3 + a90659e commit cdf01b8

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

packages/pool/contracts/StakeUtils.sol

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ abstract contract StakeUtils is TransferUtils, IStakeUtils {
1818
user.unstaked >= amount,
1919
"Pool: Amount exceeds unstaked"
2020
);
21-
user.unstaked -= amount;
21+
uint256 userUnstakedUpdate = user.unstaked - amount;
22+
user.unstaked = userUnstakedUpdate;
2223
uint256 totalSharesNow = totalShares();
2324
uint256 sharesToMint = amount * totalSharesNow / totalStake;
2425
uint256 userSharesUpdate = userShares(msg.sender) + sharesToMint;
@@ -37,8 +38,10 @@ abstract contract StakeUtils is TransferUtils, IStakeUtils {
3738
msg.sender,
3839
amount,
3940
sharesToMint,
41+
userUnstakedUpdate,
4042
userSharesUpdate,
41-
totalSharesUpdate
43+
totalSharesUpdate,
44+
totalStake
4245
);
4346
}
4447

@@ -139,7 +142,8 @@ abstract contract StakeUtils is TransferUtils, IStakeUtils {
139142
{
140143
unstakeAmount = unstakeAmountByShares;
141144
}
142-
user.unstaked += unstakeAmount;
145+
uint256 userUnstakedUpdate = user.unstaked + unstakeAmount;
146+
user.unstaked = userUnstakedUpdate;
143147

144148
uint256 totalSharesUpdate = totalShares - user.unstakeShares;
145149
updateCheckpointArray(
@@ -154,7 +158,9 @@ abstract contract StakeUtils is TransferUtils, IStakeUtils {
154158
emit Unstaked(
155159
userAddress,
156160
unstakeAmount,
157-
totalSharesUpdate
161+
userUnstakedUpdate,
162+
totalSharesUpdate,
163+
totalStake
158164
);
159165
return unstakeAmount;
160166
}

packages/pool/contracts/interfaces/IStakeUtils.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ interface IStakeUtils is ITransferUtils{
88
address indexed user,
99
uint256 amount,
1010
uint256 mintedShares,
11+
uint256 userUnstaked,
1112
uint256 userShares,
12-
uint256 totalShares
13+
uint256 totalShares,
14+
uint256 totalStake
1315
);
1416

1517
event ScheduledUnstake(
@@ -23,7 +25,9 @@ interface IStakeUtils is ITransferUtils{
2325
event Unstaked(
2426
address indexed user,
2527
uint256 amount,
26-
uint256 totalShares
28+
uint256 userUnstaked,
29+
uint256 totalShares,
30+
uint256 totalStake
2731
);
2832

2933
function stake(uint256 amount)

packages/pool/test/StakeUtils.sol.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ describe("stake", function () {
7676
roles.user1.address,
7777
user1Stake.div(2),
7878
user1Stake.div(2),
79+
0,
7980
user1Stake,
81+
user1Stake.add(1),
8082
user1Stake.add(1)
8183
);
8284
expect(await api3Pool.userStake(roles.user1.address)).to.equal(
@@ -111,7 +113,9 @@ describe("stake", function () {
111113
roles.user1.address,
112114
user1Stake,
113115
user1Stake,
116+
0,
114117
user1Stake,
118+
user1Stake.add(1),
115119
user1Stake.add(1)
116120
);
117121
expect(await api3Pool.userStake(roles.user1.address)).to.equal(
@@ -147,7 +151,9 @@ describe("depositAndStake", function () {
147151
roles.user1.address,
148152
user1Stake,
149153
user1Stake,
154+
0,
150155
user1Stake,
156+
user1Stake.add(1),
151157
user1Stake.add(1)
152158
);
153159
});
@@ -334,11 +340,18 @@ describe("unstake", function () {
334340
]);
335341
// Unstake
336342
await api3Pool.mintReward();
343+
const totalStake = await api3Pool.totalStake();
337344
await expect(
338345
api3Pool.connect(roles.randomPerson).unstake(roles.user1.address)
339346
)
340347
.to.emit(api3Pool, "Unstaked")
341-
.withArgs(roles.user1.address, user1Stake, 1);
348+
.withArgs(
349+
roles.user1.address,
350+
user1Stake,
351+
user1Stake,
352+
1,
353+
totalStake.sub(user1Stake)
354+
);
342355
const user = await api3Pool.users(roles.user1.address);
343356
expect(user.unstaked).to.equal(user1Stake);
344357
});
@@ -395,14 +408,17 @@ describe("unstake", function () {
395408
const actualUnstakeAmount = unstakeShares
396409
.mul(await api3Pool.totalStake())
397410
.div(await api3Pool.totalShares());
411+
const totalStake = await api3Pool.totalStake();
398412
await expect(
399413
api3Pool.connect(roles.randomPerson).unstake(roles.user1.address)
400414
)
401415
.to.emit(api3Pool, "Unstaked")
402416
.withArgs(
403417
roles.user1.address,
404418
actualUnstakeAmount,
405-
user1Stake.div(2).add(1)
419+
actualUnstakeAmount,
420+
user1Stake.div(2).add(1),
421+
totalStake.sub(actualUnstakeAmount)
406422
);
407423
});
408424
});

0 commit comments

Comments
 (0)