-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
154 lines (126 loc) · 3.41 KB
/
main.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
var mux sync.Mutex
func getPercentageEpoch() (float64, error) {
startHeight, err := GetEpochStartHeight()
if err != nil {
return 0, err
}
latestHeight, err := getLatestBlockHeight()
if err != nil {
return 0, err
}
return float64(100) * float64(latestHeight-startHeight) / float64(EPOCH_LENGTH), nil
}
func AdaptStake(totalStake, seatPrice int) (error) {
toPropose := int(float64(seatPrice) * (1 + AGGRESSIVENESS))
if totalStake < toPropose {
// low current locked stake. Stake some near token
accountUnstaked, err := getAccountUnStakedBalance()
if err != nil {
log.Println("get account unstaked balance error", err)
return err
}
log.Println("account unstaked balance:", accountUnstaked)
toStake := min(toPropose-totalStake, accountUnstaked)
if toStake > 0 {
log.Println("try to stake:", toStake)
err = runStake(POOL_ID, "stake", stringFromStake(toStake), DELEGATOR_ID)
if err != nil {
log.Println("stake error:", err)
}
log.Println("stake successfully")
return err
}
return nil
} else if totalStake > toPropose {
accountStaked, err := getAccountStakedBalance()
if err != nil {
log.Println("get account staked balance error", err)
return err
}
log.Println("account staked balance:", accountStaked)
toUnstake := min(totalStake-toPropose, accountStaked)
if toUnstake > 0 {
log.Println("try to unstake:", toUnstake)
err = runStake(POOL_ID, "unstake", stringFromStake(toUnstake), DELEGATOR_ID)
if err != nil {
log.Println("stake error:", err)
}
log.Println("unstake successfully")
return err
}
return nil
} else {
log.Println("Nothing to do now.")
return nil
}
}
func main() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
ticker := time.NewTicker(time.Duration(REAPT_TIME) * time.Second)
for {
select {
case <-ticker.C:
log.Println("--------------------")
// Get set Price
currentSeatPrice, err := getSeatPrice("current")
if err != nil {
log.Println("get current set price error", err)
continue
}
log.Println("current seat price:", currentSeatPrice)
nextSeatPrice, err := getSeatPrice("next")
if err != nil {
log.Println("get next set price error", err)
continue
}
log.Println("next seat price:", nextSeatPrice)
proposalSeatPrice, err := getSeatPrice("proposal")
if err != nil {
log.Println("get proposal set price error", err)
continue
}
log.Println("proposal seat price:", proposalSeatPrice)
// get total stake
contractTotalStake, err := getTotalStakedBalance()
if err != nil {
log.Println("get total staked balance error", err)
continue
}
log.Println("contract total stake", contractTotalStake)
// Get Percentage Epoch
percentage, err := getPercentageEpoch()
if err != nil {
log.Println("get epoch percentage error", err)
continue
}
log.Println("current epoch percentage:", percentage)
if percentage <= 95 {
log.Println("nothing to do now, epoch percentage:", percentage)
continue
}
// Ping contract
_, err = pingContract()
if err != nil {
log.Println("ping contract error", err)
continue
}
log.Println("ping contract success.")
mux.Lock()
AdaptStake(contractTotalStake, proposalSeatPrice)
mux.Unlock()
case <-sigc:
log.Println("System kill")
os.Exit(0)
}
}
}