delegation.gno
2.83 Kb ยท 102 lines
1package staker
2
3// DelegationType represents the type of delegation operation
4type DelegationType string
5
6const (
7 DelegateType DelegationType = "DELEGATE"
8 UnDelegateType DelegationType = "UNDELEGATE"
9)
10
11func (d DelegationType) String() string { return string(d) }
12func (d DelegationType) IsDelegate() bool { return d == DelegateType }
13func (d DelegationType) IsUnDelegate() bool { return d == UnDelegateType }
14
15// Delegation represents a delegation between two addresses
16type Delegation struct {
17 id int64
18 delegateAmount int64
19 unDelegateAmount int64
20 collectedAmount int64
21 delegateFrom address
22 delegateTo address
23 createdHeight int64
24 createdAt int64
25 withdraws []*DelegationWithdraw
26}
27
28// NewDelegation creates a new delegation
29func NewDelegation(
30 id int64,
31 delegateFrom, delegateTo address,
32 delegateAmount, createdHeight, createdAt int64,
33) *Delegation {
34 return &Delegation{
35 id: id,
36 delegateFrom: delegateFrom,
37 delegateTo: delegateTo,
38 delegateAmount: delegateAmount,
39 createdHeight: createdHeight,
40 createdAt: createdAt,
41 unDelegateAmount: 0,
42 collectedAmount: 0,
43 withdraws: make([]*DelegationWithdraw, 0),
44 }
45}
46
47// Basic getters
48func (d *Delegation) ID() int64 { return d.id }
49func (d *Delegation) DelegateFrom() address { return d.delegateFrom }
50func (d *Delegation) DelegateTo() address { return d.delegateTo }
51func (d *Delegation) CreatedAt() int64 { return d.createdAt }
52
53// Amount getters
54func (d *Delegation) TotalDelegatedAmount() int64 { return d.delegateAmount }
55func (d *Delegation) UnDelegatedAmount() int64 { return d.unDelegateAmount }
56func (d *Delegation) CollectedAmount() int64 { return d.collectedAmount }
57
58// Withdraws getters
59func (d *Delegation) Withdraws() []*DelegationWithdraw { return d.withdraws }
60
61// Setters
62func (d *Delegation) SetUnDelegateAmount(amount int64) {
63 d.unDelegateAmount = amount
64}
65
66func (d *Delegation) SetCollectedAmount(amount int64) {
67 d.collectedAmount = amount
68}
69
70func (d *Delegation) AddWithdraw(withdraw *DelegationWithdraw) {
71 d.withdraws = append(d.withdraws, withdraw)
72}
73
74func (d *Delegation) SetWithdraws(withdraws []*DelegationWithdraw) {
75 d.withdraws = withdraws
76}
77
78// Clone creates a deep copy of the delegation.
79func (d *Delegation) Clone() *Delegation {
80 if d == nil {
81 return nil
82 }
83
84 clonedWithdraws := make([]*DelegationWithdraw, len(d.withdraws))
85 for i, w := range d.withdraws {
86 if w != nil {
87 clonedWithdraws[i] = w.Clone()
88 }
89 }
90
91 return &Delegation{
92 id: d.id,
93 delegateAmount: d.delegateAmount,
94 unDelegateAmount: d.unDelegateAmount,
95 collectedAmount: d.collectedAmount,
96 delegateFrom: d.delegateFrom,
97 delegateTo: d.delegateTo,
98 createdHeight: d.createdHeight,
99 createdAt: d.createdAt,
100 withdraws: clonedWithdraws,
101 }
102}