-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathprice.go
More file actions
238 lines (205 loc) · 6.84 KB
/
price.go
File metadata and controls
238 lines (205 loc) · 6.84 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package substrate
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
// PricingPolicy struct represents a PricingPolicy
type PricingPolicy struct {
Versioned
ID types.U32 `json:"id"`
Name string `json:"name"`
SU Policy `json:"su"`
CU Policy `json:"cu"`
NU Policy `json:"nu"`
IPU Policy `json:"ipu"`
UniqueName Policy `json:"unique_name"`
DomainName Policy `json:"domain_name"`
FoundationAccount AccountID `json:"foundation_name"`
CertifiedSalesAccount AccountID `json:"certified_sales_account"`
DedicatedNodesDiscount types.U8 `json:"dedication_nodes_discount"`
}
// GetPricingPolicies gets pricing policy from tfgrid module
func (s *Substrate) GetPricingPolicy(id uint32) (pricingPolicy PricingPolicy, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return
}
bytes, err := Encode(id)
if err != nil {
return pricingPolicy, errors.Wrap(err, "substrate: encoding error building query arguments")
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "PricingPolicies", bytes)
if err != nil {
return pricingPolicy, errors.Wrap(err, "failed to create substrate query key")
}
ok, err := cl.RPC.State.GetStorageLatest(key, &pricingPolicy)
if err != nil {
return pricingPolicy, errors.Wrap(err, "failed to lookup entity")
}
if !ok {
return pricingPolicy, errors.Wrap(ErrNotFound, "pricing policy not found")
}
return
}
// GetTFTPrice gets the TFT price
func (s *Substrate) GetTFTPrice() (price types.U32, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return
}
key, err := types.CreateStorageKey(meta, "TFTPriceModule", "TftPrice")
if err != nil {
return price, errors.Wrap(err, "failed to create substrate query key")
}
ok, err := cl.RPC.State.GetStorageLatest(key, &price)
if err != nil {
return price, errors.Wrap(err, "failed to lookup entity")
}
if !ok {
return price, errors.Wrap(ErrNotFound, "price not found")
}
return
}
// GetAverageTFTPrice gets the average TFT price
func (s *Substrate) GetAverageTFTPrice() (price types.U32, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return
}
key, err := types.CreateStorageKey(meta, "TFTPriceModule", "AverageTftPrice")
if err != nil {
return price, errors.Wrap(err, "failed to create substrate query key")
}
ok, err := cl.RPC.State.GetStorageLatest(key, &price)
if err != nil {
return price, errors.Wrap(err, "failed to lookup entity")
}
if !ok {
return price, errors.Wrap(ErrNotFound, "average price not found")
}
return
}
// GetTFTBillingRate returns the current billing rate (in mUSD) used on-chain,
// computed as AverageTftPrice clamped between MinTftPrice and MaxTftPrice.
func (s *Substrate) GetTFTBillingRate() (rate types.U32, err error) {
cl, meta, err := s.GetClient()
if err != nil {
return
}
// Build keys
keyAvg, err := types.CreateStorageKey(meta, "TFTPriceModule", "AverageTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (AverageTftPrice)")
}
keyMin, err := types.CreateStorageKey(meta, "TFTPriceModule", "MinTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (MinTftPrice)")
}
keyMax, err := types.CreateStorageKey(meta, "TFTPriceModule", "MaxTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (MaxTftPrice)")
}
// Read latest values
var avg, min, max types.U32
ok, err := cl.RPC.State.GetStorageLatest(keyAvg, &avg)
if err != nil {
return rate, errors.Wrap(err, "failed to read AverageTftPrice")
}
if !ok {
return rate, errors.Wrap(ErrNotFound, "AverageTftPrice not found")
}
ok, err = cl.RPC.State.GetStorageLatest(keyMin, &min)
if err != nil {
return rate, errors.Wrap(err, "failed to read MinTftPrice")
}
if !ok {
return rate, errors.Wrap(ErrNotFound, "MinTftPrice not found")
}
ok, err = cl.RPC.State.GetStorageLatest(keyMax, &max)
if err != nil {
return rate, errors.Wrap(err, "failed to read MaxTftPrice")
}
if !ok {
return rate, errors.Wrap(ErrNotFound, "MaxTftPrice not found")
}
rate = clampTFTPrice(avg, min, max)
return
}
// GetTFTBillingRateAt returns the billing rate (in mUSD) at a specific block number,
// computed as AverageTftPrice clamped between MinTftPrice and MaxTftPrice at that block.
func (s *Substrate) GetTFTBillingRateAt(block uint64) (rate types.U32, err error) {
cl, _, err := s.GetClient()
if err != nil {
return
}
// Resolve block hash
bh, err := cl.RPC.Chain.GetBlockHash(block)
if err != nil {
return rate, errors.Wrap(err, "failed to resolve block hash")
}
// Metadata at block
metaAtBlock, err := cl.RPC.State.GetMetadata(bh)
if err != nil {
return rate, errors.Wrap(err, "failed to get metadata at block")
}
// Keys at block
keyAvg, err := types.CreateStorageKey(metaAtBlock, "TFTPriceModule", "AverageTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (AverageTftPrice)")
}
keyMin, err := types.CreateStorageKey(metaAtBlock, "TFTPriceModule", "MinTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (MinTftPrice)")
}
keyMax, err := types.CreateStorageKey(metaAtBlock, "TFTPriceModule", "MaxTftPrice")
if err != nil {
return rate, errors.Wrap(err, "failed to create storage key (MaxTftPrice)")
}
// Read at block
var avg, min, max types.U32
raw, err := cl.RPC.State.GetStorageRaw(keyAvg, bh)
if err != nil {
return rate, errors.Wrap(err, "failed to get AverageTftPrice at block")
}
if len(*raw) == 0 {
return rate, errors.Wrap(ErrNotFound, "AverageTftPrice not found at block")
}
if err := Decode(*raw, &avg); err != nil {
return rate, errors.Wrap(err, "failed to decode AverageTftPrice")
}
raw, err = cl.RPC.State.GetStorageRaw(keyMin, bh)
if err != nil {
return rate, errors.Wrap(err, "failed to get MinTftPrice at block")
}
if len(*raw) == 0 {
return rate, errors.Wrap(ErrNotFound, "MinTftPrice not found at block")
}
if err := Decode(*raw, &min); err != nil {
return rate, errors.Wrap(err, "failed to decode MinTftPrice")
}
raw, err = cl.RPC.State.GetStorageRaw(keyMax, bh)
if err != nil {
return rate, errors.Wrap(err, "failed to get MaxTftPrice at block")
}
if len(*raw) == 0 {
return rate, errors.Wrap(ErrNotFound, "MaxTftPrice not found at block")
}
if err := Decode(*raw, &max); err != nil {
return rate, errors.Wrap(err, "failed to decode MaxTftPrice")
}
// Clamp
rate = clampTFTPrice(avg, min, max)
return
}
// clampTFTPrice mirrors the on-chain clamping logic used by pallet-smart-contract
// tft_price = max(AverageTftPrice, MinTftPrice) then min(_, MaxTftPrice)
func clampTFTPrice(avg, min, max types.U32) types.U32 {
rate := avg
if rate < min {
rate = min
}
if rate > max {
rate = max
}
return rate
}