commit inicial do projeto
This commit is contained in:
19
internal/staking/rewards.go
Normal file
19
internal/staking/rewards.go
Normal file
@ -0,0 +1,19 @@
|
||||
package staking
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// CalculateRewards calcula as recompensas baseadas em tempo e APR.
|
||||
// Exemplo: APR 12% ao ano = 0.01 ao mês, 0.00033 ao dia.
|
||||
func CalculateRewards(amount uint64, startTime int64, apr float64) uint64 {
|
||||
daysStaked := float64(time.Now().Unix()-startTime) / (60 * 60 * 24)
|
||||
if daysStaked <= 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Fórmula de juros simples: R = P * i * t
|
||||
interest := float64(amount) * (apr / 365.0) * daysStaked
|
||||
return uint64(math.Floor(interest))
|
||||
}
|
||||
82
internal/staking/store.go
Normal file
82
internal/staking/store.go
Normal file
@ -0,0 +1,82 @@
|
||||
package staking
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type StakeInfo struct {
|
||||
Amount uint64
|
||||
Duration uint64
|
||||
}
|
||||
|
||||
type StakingStore struct {
|
||||
data map[string]StakeInfo
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func NewStakingStore() *StakingStore {
|
||||
return &StakingStore{
|
||||
data: make(map[string]StakeInfo),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StakingStore) Stake(address string, amount uint64, duration uint64) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.data[address] = StakeInfo{Amount: amount, Duration: duration}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StakingStore) Unstake(address string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
delete(s.data, address)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *StakingStore) GetStakeInfo(address string) (StakeInfo, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
info, ok := s.data[address]
|
||||
return info, ok
|
||||
}
|
||||
|
||||
func (s *StakingStore) UpdateStakeInfo(address string, info StakeInfo) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.data[address] = info
|
||||
}
|
||||
|
||||
func (s *StakingStore) Snapshot() map[string]StakeInfo {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
clone := make(map[string]StakeInfo)
|
||||
for k, v := range s.data {
|
||||
clone[k] = v
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
||||
func (s *StakingStore) SaveToDisk(filename string) error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
file, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
encoder := gob.NewEncoder(file)
|
||||
return encoder.Encode(s.data)
|
||||
}
|
||||
|
||||
func (s *StakingStore) LoadFromDisk(filename string) error {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
decoder := gob.NewDecoder(file)
|
||||
return decoder.Decode(&s.data)
|
||||
}
|
||||
Reference in New Issue
Block a user