Search Apps Documentation Source Content File Folder Download Copy Actions Download

delegation.gno

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