-
Notifications
You must be signed in to change notification settings - Fork 19
/
staking_account_stub.go
91 lines (79 loc) · 2.85 KB
/
staking_account_stub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package stubs
import (
"context"
"errors"
"fmt"
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
)
type StakingAccountStub struct {
partyToStake map[string]*num.Uint
partyToStakeForEpoch map[int64]map[string]*num.Uint
currentEpoch *types.Epoch
}
func (t *StakingAccountStub) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if t.currentEpoch == nil || t.currentEpoch.Seq != epoch.Seq {
t.currentEpoch = &epoch
emptyT := time.Time{}
if epoch.EndTime == emptyT {
t.partyToStakeForEpoch[epoch.StartTime.UnixNano()] = map[string]*num.Uint{}
for p, s := range t.partyToStake {
t.partyToStakeForEpoch[epoch.StartTime.UnixNano()][p] = s.Clone()
}
}
}
}
func (t *StakingAccountStub) OnEpochRestore(_ context.Context, _ types.Epoch) {}
func (t *StakingAccountStub) IncrementBalance(party string, amount *num.Uint) error {
if _, ok := t.partyToStake[party]; !ok {
t.partyToStake[party] = num.UintZero()
}
t.partyToStake[party].AddSum(amount)
return nil
}
func (t *StakingAccountStub) DecrementBalance(party string, amount *num.Uint) error {
if _, ok := t.partyToStake[party]; !ok {
return errors.New("party staking accoung is missing")
}
if t.partyToStake[party].LT(amount) {
return errors.New("incorrect balance for unstaking")
}
t.partyToStake[party] = t.partyToStake[party].Sub(t.partyToStake[party], amount)
t.partyToStakeForEpoch[t.currentEpoch.StartTime.UnixNano()][party] = t.partyToStake[party].Clone()
return nil
}
func NewStakingAccountStub() *StakingAccountStub {
return &StakingAccountStub{
partyToStake: make(map[string]*num.Uint),
partyToStakeForEpoch: make(map[int64]map[string]*num.Uint),
}
}
func (t *StakingAccountStub) GetAvailableBalance(party string) (*num.Uint, error) {
ret, ok := t.partyToStake[party]
if !ok {
return num.UintZero(), fmt.Errorf("party not found")
}
return ret, nil
}
func (t *StakingAccountStub) GetAvailableBalanceInRange(party string, from, _ time.Time) (*num.Uint, error) {
partyStake, ok := t.partyToStakeForEpoch[from.UnixNano()][party]
if !ok {
return num.UintZero(), fmt.Errorf("party not found")
}
return partyStake, nil
}