Skip to content

Commit 67586c8

Browse files
author
0xTopaz
committed
GSW-2083 refactor: change setter access permission to private in gov staker
1 parent 6dcc8b5 commit 67586c8

File tree

7 files changed

+30
-31
lines changed

7 files changed

+30
-31
lines changed

contract/r/gnoswap/gov/staker/api_staker_test.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestGetLockedInfoByAddress_EmptyLocks(t *testing.T) {
7373
func TestGetClaimableRewardByAddress(t *testing.T) {
7474
addr := testutils.TestAddress("claimable_test")
7575

76-
rewardState.AddStake(uint64(std.GetHeight()), addr, 100, 0, nil)
76+
rewardState.addStake(uint64(std.GetHeight()), addr, 100, 0, nil)
7777

7878
currentGNSBalance = 1000
7979
// userEmissionReward.Set(addr.String(), uint64(1000))

contract/r/gnoswap/gov/staker/reward_calculation.gno

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (self *RewardState) deductReward(staker std.Address, currentBalance uint64)
174174
return reward64, protocolFeeRewards
175175
}
176176

177-
// This function MUST be called as a part of AddStake or RemoveStake
177+
// This function MUST be called as a part of addStake or removeStake
178178
// CurrentBalance / StakeChange / IsRemoveStake will be updated in those functions
179179
func (self *RewardState) finalize(currentBalance uint64, currentProtocolFeeBalances map[string]uint64) {
180180
if self.TotalStake == uint64(0) {
@@ -207,7 +207,7 @@ func (self *RewardState) finalize(currentBalance uint64, currentProtocolFeeBalan
207207
}
208208
}
209209

210-
func (self *RewardState) AddStake(currentHeight uint64, staker std.Address, amount uint64, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) {
210+
func (self *RewardState) addStake(currentHeight uint64, staker std.Address, amount uint64, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) {
211211
self.finalize(currentBalance, currentProtocolFeeBalances)
212212

213213
self.TotalStake += amount
@@ -249,7 +249,7 @@ func (self *RewardState) AddStake(currentHeight uint64, staker std.Address, amou
249249
self.info.Set(staker.String(), info)
250250
}
251251

252-
func (self *RewardState) Claim(staker std.Address, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) (uint64, map[string]uint64) {
252+
func (self *RewardState) claim(staker std.Address, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) (uint64, map[string]uint64) {
253253
if !self.info.Has(staker.String()) {
254254
return 0, make(map[string]uint64)
255255
}
@@ -266,7 +266,7 @@ func (self *RewardState) Claim(staker std.Address, currentBalance uint64, curren
266266
return reward, protocolFeeRewards
267267
}
268268

269-
func (self *RewardState) RemoveStake(staker std.Address, amount uint64, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) (uint64, map[string]uint64) {
269+
func (self *RewardState) removeStake(staker std.Address, amount uint64, currentBalance uint64, currentProtocolFeeBalances map[string]uint64) (uint64, map[string]uint64) {
270270
self.finalize(currentBalance, currentProtocolFeeBalances)
271271

272272
reward, protocolFeeRewards := self.deductReward(staker, currentBalance)
@@ -316,10 +316,10 @@ func SetAmountByProjectWallet(addr std.Address, amount uint64, add bool) {
316316
currentAmount := getAmountByProjectWallet(addr)
317317
if add {
318318
amountByProjectWallet.Set(addr.String(), currentAmount+amount)
319-
rewardState.AddStake(uint64(std.GetHeight()), caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
319+
rewardState.addStake(uint64(std.GetHeight()), caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
320320
} else {
321321
amountByProjectWallet.Set(addr.String(), currentAmount-amount)
322-
rewardState.RemoveStake(caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
322+
rewardState.removeStake(caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
323323
}
324324
}
325325

contract/r/gnoswap/gov/staker/reward_calculation_test.gno

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ func TestRewardCalculation_1_1(t *testing.T) {
1010
state := NewRewardState()
1111

1212
current := 100
13-
state.AddStake(10, testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
13+
state.addStake(10, testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
1414

1515
current += 100
16-
reward, _ := state.RemoveStake(testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
16+
reward, _ := state.removeStake(testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
1717

1818
if reward != 100+100 {
1919
t.Errorf("expected reward %d, got %d", 100+100, reward)
@@ -24,21 +24,21 @@ func TestRewardCalculation_1_2(t *testing.T) {
2424
state := NewRewardState()
2525

2626
current := 100
27-
state.AddStake(10, testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
27+
state.addStake(10, testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
2828

2929
current += 100
30-
reward, _ := state.RemoveStake(testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
30+
reward, _ := state.removeStake(testutils.TestAddress("alice"), 100, uint64(current), make(map[string]uint64))
3131
current -= int(reward)
3232

3333
if reward != 100+100 {
3434
t.Errorf("expected reward %d, got %d", 100+100, reward)
3535
}
3636

3737
current += 100
38-
state.AddStake(12, testutils.TestAddress("bob"), 100, uint64(current), make(map[string]uint64))
38+
state.addStake(12, testutils.TestAddress("bob"), 100, uint64(current), make(map[string]uint64))
3939

4040
current += 100
41-
reward, _ = state.RemoveStake(testutils.TestAddress("bob"), 100, uint64(current), make(map[string]uint64))
41+
reward, _ = state.removeStake(testutils.TestAddress("bob"), 100, uint64(current), make(map[string]uint64))
4242
current -= int(reward)
4343
if reward != 100+100 {
4444
t.Errorf("expected reward %d, got %d", 100+100, reward)
@@ -50,56 +50,55 @@ func TestRewardCalculation_1_3(t *testing.T) {
5050

5151
// Alice takes 100 GNS
5252
current := 100
53-
state.AddStake(10, testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
53+
state.addStake(10, testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
5454

5555
// Alice takes 100 GNS
5656
current += 100
57-
state.AddStake(11, testutils.TestAddress("bob"), 10, uint64(current), make(map[string]uint64))
57+
state.addStake(11, testutils.TestAddress("bob"), 10, uint64(current), make(map[string]uint64))
5858

5959
// Alice takes 50 GNS, Bob takes 50 GNS
6060
current += 100
61-
reward, _ := state.RemoveStake(testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
61+
reward, _ := state.removeStake(testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
6262
current -= int(reward)
6363
if reward != 100+100+50 {
6464
t.Errorf("expected reward %d, got %d", 100+100+50, reward)
6565
}
6666

6767
// Bob takes 100 GNS
6868
current += 100
69-
reward, _ = state.RemoveStake(testutils.TestAddress("bob"), 10, uint64(current), make(map[string]uint64))
69+
reward, _ = state.removeStake(testutils.TestAddress("bob"), 10, uint64(current), make(map[string]uint64))
7070
current -= int(reward)
7171
if reward != 100+50 {
7272
t.Errorf("expected reward %d, got %d", 100+50, reward)
7373
}
7474
}
7575

76-
7776
func TestRewardCalculation_1_4(t *testing.T) {
7877
state := NewRewardState()
7978

8079
// Alice takes 100 GNS
8180
current := 100
82-
state.AddStake(10, testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
81+
state.addStake(10, testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
8382

8483
// Alice takes 200GNS
8584
current += 200
86-
state.AddStake(11, testutils.TestAddress("bob"), 30, uint64(current), make(map[string]uint64))
85+
state.addStake(11, testutils.TestAddress("bob"), 30, uint64(current), make(map[string]uint64))
8786

8887
// Alice 25, Bob 75
8988
current += 100
90-
state.AddStake(12, testutils.TestAddress("charlie"), 10, uint64(current), make(map[string]uint64))
89+
state.addStake(12, testutils.TestAddress("charlie"), 10, uint64(current), make(map[string]uint64))
9190

9291
// Alice 20, Bob 60, Charlie 20
9392
current += 100
94-
reward, _ := state.RemoveStake(testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
93+
reward, _ := state.removeStake(testutils.TestAddress("alice"), 10, uint64(current), make(map[string]uint64))
9594
current -= int(reward)
9695
if reward != 100+200+25+20 {
9796
t.Errorf("expected reward %d, got %d", 100+200+25+20, reward)
9897
}
9998

10099
// Bob 75, Charlie 25
101100
current += 100
102-
reward, _ = state.RemoveStake(testutils.TestAddress("bob"), 30, uint64(current), make(map[string]uint64))
101+
reward, _ = state.removeStake(testutils.TestAddress("bob"), 30, uint64(current), make(map[string]uint64))
103102
current -= int(reward)
104103
if reward != 75+60+75 {
105104
t.Errorf("expected reward %d, got %d", 75+60+75, reward)

contract/r/gnoswap/gov/staker/staker.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func Delegate(to std.Address, amount uint64) {
8080
))
8181
}
8282

83-
rewardState.AddStake(uint64(std.GetHeight()), caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
83+
rewardState.addStake(uint64(std.GetHeight()), caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
8484

8585
// GNS // caller -> GovStaker
8686
gns.TransferFrom(caller, std.CurrentRealm().Addr(), amount)
@@ -201,7 +201,7 @@ func Undelegate(from std.Address, amount uint64) {
201201
))
202202
}
203203

204-
reward, protocolFeeRewards := rewardState.RemoveStake(caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
204+
reward, protocolFeeRewards := rewardState.removeStake(caller, amount, getCurrentBalance(), getCurrentProtocolFeeBalance())
205205

206206
// burn equivalent amount of xGNS
207207
xgns.Burn(caller, amount)
@@ -306,7 +306,7 @@ func CollectReward() {
306306
prevAddr, prevPkgPath := getPrev()
307307
caller := std.PrevRealm().Addr()
308308

309-
reward, protocolFeeRewards := rewardState.Claim(caller, getCurrentBalance(), getCurrentProtocolFeeBalance())
309+
reward, protocolFeeRewards := rewardState.claim(caller, getCurrentBalance(), getCurrentProtocolFeeBalance())
310310

311311
// XXX (@notJoon): There could be cases where the reward pool is empty, In such case,
312312
// it seems appropriate to return 0 and continue processing.
@@ -372,7 +372,7 @@ func CollectRewardFromLaunchPad(to std.Address) {
372372

373373
prevAddr, prevPkgPath := getPrev()
374374

375-
emissionReward, protocolFeeRewards := rewardState.Claim(to, getCurrentBalance(), getCurrentProtocolFeeBalance())
375+
emissionReward, protocolFeeRewards := rewardState.claim(to, getCurrentBalance(), getCurrentProtocolFeeBalance())
376376
if emissionReward > 0 {
377377
gns.Transfer(to, emissionReward)
378378
std.Emit(

contract/r/gnoswap/gov/staker/staker_test.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func TestCollectReward(t *testing.T) {
483483
std.TestSetRealm(user2Realm)
484484
user := user2Realm.Addr().String()
485485

486-
rewardState.AddStake(uint64(std.GetHeight()), std.Address(user), 10, 0, make(map[string]uint64))
486+
rewardState.addStake(uint64(std.GetHeight()), std.Address(user), 10, 0, make(map[string]uint64))
487487

488488
// set a fake emission reward
489489
//userEmissionReward.Set(user, uint64(50_000))

contract/r/gnoswap/launchpad/reward_calculation_test.gno

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ func TestRewardState_ClaimAtEndHeight(t *testing.T) {
185185

186186
state.AddStake(150, "deposit1", 100)
187187

188-
// Claim exactly at endHeight
188+
// claim exactly at endHeight
189189
reward1 := state.Claim("deposit1", endHeight)
190190

191-
// Claim after endHeight
191+
// claim after endHeight
192192
reward2 := state.Claim("deposit1", endHeight+50)
193193

194194
uassert.NotEqual(t, reward1, uint64(0),

contract/r/gnoswap/staker/staker.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ func UnStakeToken(tokenId uint64, unwrapResult bool) (string, string, string) {
539539
deposit := deposits.Get(tokenId)
540540
poolPath := deposit.targetPoolPath
541541

542-
// Claim All Rewards
542+
// claim All Rewards
543543
CollectReward(tokenId, unwrapResult)
544544
token0Amount, token1Amount := getTokenPairBalanceFromPosition(poolPath, tokenId)
545545

0 commit comments

Comments
 (0)