-
Notifications
You must be signed in to change notification settings - Fork 179
/
fees.go
251 lines (221 loc) · 7.32 KB
/
fees.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
package blueprints
import (
_ "embed"
"encoding/hex"
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/flow-core-contracts/lib/go/templates"
"github.com/onflow/flow-go/model/flow"
)
var TransactionFeesExecutionEffortWeightsPath = cadence.Path{
Domain: common.PathDomainStorage,
Identifier: "executionEffortWeights",
}
var TransactionFeesExecutionMemoryWeightsPath = cadence.Path{
Domain: common.PathDomainStorage,
Identifier: "executionMemoryWeights",
}
var TransactionFeesExecutionMemoryLimitPath = cadence.Path{
Domain: common.PathDomainStorage,
Identifier: "executionMemoryLimit",
}
//go:embed scripts/deployTxFeesTransactionTemplate.cdc
var deployTxFeesTransactionTemplate string
//go:embed scripts/setupParametersTransactionTemplate.cdc
var setupParametersTransactionTemplate string
//go:embed scripts/setupStorageForServiceAccountsTemplate.cdc
var setupStorageForServiceAccountsTemplate string
//go:embed scripts/setupStorageForAccount.cdc
var setupStorageForAccountTemplate string
//go:embed scripts/setupFeesTransactionTemplate.cdc
var setupFeesTransactionTemplate string
//go:embed scripts/setExecutionMemoryLimit.cdc
var setExecutionMemoryLimit string
func DeployTxFeesContractTransaction(service, fungibleToken, flowToken, storageFees, flowFees flow.Address) *flow.TransactionBody {
contract := contracts.FlowFees(
fungibleToken.HexWithPrefix(),
flowToken.HexWithPrefix(),
storageFees.HexWithPrefix(),
)
return flow.NewTransactionBody().
SetScript([]byte(deployTxFeesTransactionTemplate)).
AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
AddAuthorizer(flowFees).
AddAuthorizer(service)
}
func DeployStorageFeesContractTransaction(service flow.Address, contract []byte) *flow.TransactionBody {
return DeployContractTransaction(service, contract, "FlowStorageFees")
}
func SetupParametersTransaction(
service flow.Address,
addressCreationFee,
minimumStorageReservation,
storagePerFlow cadence.UFix64,
restrictedAccountCreationEnabled cadence.Bool,
) *flow.TransactionBody {
addressCreationFeeArg, err := jsoncdc.Encode(addressCreationFee)
if err != nil {
panic(fmt.Sprintf("failed to encode address creation fee: %s", err.Error()))
}
minimumStorageReservationArg, err := jsoncdc.Encode(minimumStorageReservation)
if err != nil {
panic(fmt.Sprintf("failed to encode minimum storage reservation: %s", err.Error()))
}
storagePerFlowArg, err := jsoncdc.Encode(storagePerFlow)
if err != nil {
panic(fmt.Sprintf("failed to encode storage ratio: %s", err.Error()))
}
restrictedAccountCreationEnabledArg, err := jsoncdc.Encode(restrictedAccountCreationEnabled)
if err != nil {
panic(fmt.Sprintf("failed to encode restrictedAccountCreationEnabled: %s", err.Error()))
}
return flow.NewTransactionBody().
SetScript([]byte(templates.ReplaceAddresses(setupParametersTransactionTemplate,
templates.Environment{
StorageFeesAddress: service.Hex(),
ServiceAccountAddress: service.Hex(),
})),
).
AddArgument(addressCreationFeeArg).
AddArgument(minimumStorageReservationArg).
AddArgument(storagePerFlowArg).
AddArgument(restrictedAccountCreationEnabledArg).
AddAuthorizer(service)
}
func SetupStorageForServiceAccountsTransaction(
service, fungibleToken, flowToken, feeContract flow.Address,
) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(templates.ReplaceAddresses(setupStorageForServiceAccountsTemplate,
templates.Environment{
ServiceAccountAddress: service.Hex(),
StorageFeesAddress: service.Hex(),
FungibleTokenAddress: fungibleToken.Hex(),
FlowTokenAddress: flowToken.Hex(),
})),
).
AddAuthorizer(service).
AddAuthorizer(fungibleToken).
AddAuthorizer(flowToken).
AddAuthorizer(feeContract)
}
func SetupStorageForAccountTransaction(
account, service, fungibleToken, flowToken flow.Address,
) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(templates.ReplaceAddresses(setupStorageForAccountTemplate,
templates.Environment{
ServiceAccountAddress: service.Hex(),
StorageFeesAddress: service.Hex(),
FungibleTokenAddress: fungibleToken.Hex(),
FlowTokenAddress: flowToken.Hex(),
})),
).
AddAuthorizer(account).
AddAuthorizer(service)
}
func SetupFeesTransaction(
service flow.Address,
flowFees flow.Address,
surgeFactor,
inclusionEffortCost,
executionEffortCost cadence.UFix64,
) *flow.TransactionBody {
surgeFactorArg, err := jsoncdc.Encode(surgeFactor)
if err != nil {
panic(fmt.Sprintf("failed to encode surge factor: %s", err.Error()))
}
inclusionEffortCostArg, err := jsoncdc.Encode(inclusionEffortCost)
if err != nil {
panic(fmt.Sprintf("failed to encode inclusion effort cost: %s", err.Error()))
}
executionEffortCostArg, err := jsoncdc.Encode(executionEffortCost)
if err != nil {
panic(fmt.Sprintf("failed to encode execution effort cost: %s", err.Error()))
}
return flow.NewTransactionBody().
SetScript([]byte(templates.ReplaceAddresses(setupFeesTransactionTemplate,
templates.Environment{
FlowFeesAddress: flowFees.Hex(),
})),
).
AddArgument(surgeFactorArg).
AddArgument(inclusionEffortCostArg).
AddArgument(executionEffortCostArg).
AddAuthorizer(service)
}
// SetExecutionEffortWeightsTransaction creates a transaction that sets up weights for the weighted Meter.
func SetExecutionEffortWeightsTransaction(
service flow.Address,
weights map[uint]uint64,
) (*flow.TransactionBody, error) {
return setExecutionWeightsTransaction(
service,
weights,
TransactionFeesExecutionEffortWeightsPath,
)
}
// SetExecutionMemoryWeightsTransaction creates a transaction that sets up weights for the weighted Meter.
func SetExecutionMemoryWeightsTransaction(
service flow.Address,
weights map[uint]uint64,
) (*flow.TransactionBody, error) {
return setExecutionWeightsTransaction(
service,
weights,
TransactionFeesExecutionMemoryWeightsPath,
)
}
func setExecutionWeightsTransaction(
service flow.Address,
weights map[uint]uint64,
path cadence.Path,
) (*flow.TransactionBody, error) {
newWeightsKeyValuePairs := make([]cadence.KeyValuePair, len(weights))
i := 0
for k, w := range weights {
newWeightsKeyValuePairs[i] = cadence.KeyValuePair{
Key: cadence.UInt64(k),
Value: cadence.UInt64(w),
}
i += 1
}
newWeights, err := jsoncdc.Encode(cadence.NewDictionary(newWeightsKeyValuePairs))
if err != nil {
return nil, err
}
storagePath, err := jsoncdc.Encode(path)
if err != nil {
return nil, err
}
tx := flow.NewTransactionBody().
SetScript([]byte(setExecutionWeightsScript)).
AddArgument(newWeights).
AddArgument(storagePath).
AddAuthorizer(service)
return tx, nil
}
//go:embed scripts/setExecutionWeightsScript.cdc
var setExecutionWeightsScript string
func SetExecutionMemoryLimitTransaction(
service flow.Address,
limit uint64,
) (*flow.TransactionBody, error) {
newLimit, err := jsoncdc.Encode(cadence.UInt64(limit))
if err != nil {
return nil, err
}
storagePath, err := jsoncdc.Encode(TransactionFeesExecutionMemoryLimitPath)
if err != nil {
return nil, err
}
tx := flow.NewTransactionBody().
SetScript([]byte(setExecutionMemoryLimit)).
AddArgument(newLimit).
AddArgument(storagePath).
AddAuthorizer(service)
return tx, nil
}