-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathcontracts.go
377 lines (299 loc) · 12.4 KB
/
contracts.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package contracts
//go:generate go run github.com/kevinburke/go-bindata/go-bindata -ignore=.*_test\.cdc -ignore=.*.json -prefix ../../../contracts -o internal/assets/assets.go -pkg assets -nometadata -nomemcopy ../../../contracts/...
import (
"fmt"
"strings"
_ "github.com/kevinburke/go-bindata"
ftcontracts "github.com/onflow/flow-ft/lib/go/contracts"
"github.com/onflow/flow-go-sdk"
nftcontracts "github.com/onflow/flow-nft/lib/go/contracts"
"github.com/onflow/flow-core-contracts/lib/go/templates"
"github.com/onflow/flow-core-contracts/lib/go/contracts/internal/assets"
)
/// This package contains utility functions to get contract code for the contracts in this repo
/// To use this package, import the `flow-core-contracts/lib/go/contracts` package,
/// then use the contracts package to call one of these functions.
/// They will return the byte array version of the contract.
///
/// Example
///
/// flowTokenCode := contracts.FlowToken(env)
///
const (
flowFeesFilename = "FlowFees.cdc"
storageFeesFilename = "FlowStorageFees.cdc"
executionParametersFilename = "FlowExecutionParameters.cdc"
flowServiceAccountFilename = "FlowServiceAccount.cdc"
flowTokenFilename = "FlowToken.cdc"
flowIdentityTableFilename = "FlowIDTableStaking.cdc"
flowQCFilename = "epochs/FlowClusterQC.cdc"
flowDKGFilename = "epochs/FlowDKG.cdc"
flowEpochFilename = "epochs/FlowEpoch.cdc"
flowLockedTokensFilename = "LockedTokens.cdc"
flowStakingProxyFilename = "StakingProxy.cdc"
flowStakingCollectionFilename = "FlowStakingCollection.cdc"
flowContractAuditsFilename = "FlowContractAudits.cdc"
flowNodeVersionBeaconFilename = "NodeVersionBeacon.cdc"
flowRandomBeaconHistoryFilename = "RandomBeaconHistory.cdc"
cryptoFilename = "Crypto.cdc"
linearCodeAddressGeneratorFilename = "LinearCodeAddressGenerator.cdc"
// Test contracts
// only used for testing
TESTFlowIdentityTableFilename = "testContracts/TestFlowIDTableStaking.cdc"
// Each contract has placeholder addresses that need to be replaced
// depending on which network they are being used with
placeholderFungibleTokenAddress = "\"FungibleToken\""
placeholderFungibleTokenMVAddress = "\"FungibleTokenMetadataViews\""
placeholderMetadataViewsAddress = "\"MetadataViews\""
placeholderFlowTokenAddress = "\"FlowToken\""
placeholderIDTableAddress = "\"FlowIDTableStaking\""
placeholderBurnerAddress = "\"Burner\""
placeholderStakingProxyAddress = "\"StakingProxy\""
placeholderQCAddr = "\"FlowClusterQC\""
placeholderDKGAddr = "\"FlowDKG\""
placeholderEpochAddr = "\"FlowEpoch\""
placeholderFlowFeesAddress = "\"FlowFees\""
placeholderStorageFeesAddress = "\"FlowStorageFees\""
placeholderLockedTokensAddress = "\"LockedTokens\""
placeholderStakingCollectionAddress = "\"FlowStakingCollection\""
placeholderNodeVersionBeaconAddress = "\"NodeVersionBeacon\""
)
// Adds a `0x` prefix to the provided address string
func withHexPrefix(address string) string {
if address == "" {
return ""
}
if address[0:2] == "0x" {
return address
}
return fmt.Sprintf("0x%s", address)
}
// FungibleToken returns the FungibleToken contract interface.
func FungibleToken(env templates.Environment) []byte {
return ftcontracts.FungibleToken(env.ViewResolverAddress, env.BurnerAddress)
}
// FungibleTokenMetadataViews returns the FungibleTokenMetadataViews contract interface.
func FungibleTokenMetadataViews(env templates.Environment) []byte {
return ftcontracts.FungibleTokenMetadataViews(env.FungibleTokenAddress, env.MetadataViewsAddress, env.ViewResolverAddress)
}
// FungibleTokenSwitchboard returns the FungibleTokenSwitchboard contract interface.
func FungibleTokenSwitchboard(env templates.Environment) []byte {
return ftcontracts.FungibleTokenSwitchboard(env.FungibleTokenAddress)
}
func NonFungibleToken(env templates.Environment) []byte {
return nftcontracts.NonFungibleToken(env.ViewResolverAddress)
}
func ViewResolver() []byte {
return nftcontracts.ViewResolver()
}
func Burner() []byte {
return ftcontracts.Burner()
}
// MetadataViews returns the MetadataViews contract interface.
func MetadataViews(env templates.Environment) []byte {
return nftcontracts.MetadataViews(env.FungibleTokenAddress, env.NonFungibleTokenAddress, env.ViewResolverAddress)
}
func CrossVMMetadataViews(env templates.Environment) []byte {
return nftcontracts.CrossVMMetadataViews(env.ViewResolverAddress, env.EVMAddress)
}
// FlowToken returns the FlowToken contract.
//
// The returned contract will import the FungibleToken contract from the specified address.
func FlowToken(env templates.Environment) []byte {
code := assets.MustAssetString(flowTokenFilename)
code = templates.ReplaceAddresses(code, env)
// Replace the init method storage operations
code = strings.ReplaceAll(
code,
"self.account.",
"adminAccount.",
)
// Replace the init method admin account parameter
code = strings.ReplaceAll(
code,
"init()",
"init(adminAccount: auth(Storage, Capabilities) &Account)",
)
return []byte(code)
}
// FlowFees returns the FlowFees contract.
//
// The returned contract will import the FungibleToken and FlowToken
// contracts from the specified addresses.
func FlowFees(env templates.Environment) []byte {
code := assets.MustAssetString(flowFeesFilename)
code = templates.ReplaceAddresses(code, env)
// Replace the init method storage operations
code = strings.ReplaceAll(
code,
"self.account.storage.save(<-admin, to: /storage/flowFeesAdmin)",
"adminAccount.storage.save(<-admin, to: /storage/flowFeesAdmin)",
)
// Replace the init method admin account parameter
code = strings.ReplaceAll(
code,
"init()",
"init(adminAccount: auth(SaveValue) &Account)",
)
return []byte(code)
}
// FlowStorageFees returns the FlowStorageFees contract
// which imports the fungible token and flow token contracts
func FlowStorageFees(env templates.Environment) []byte {
code := assets.MustAssetString(storageFeesFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowExecutionParameters returns the FlowExecutionParameters contract
func FlowExecutionParameters(env templates.Environment) []byte {
code := assets.MustAssetString(executionParametersFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowServiceAccount returns the FlowServiceAccount contract.
//
// The returned contract will import the FungibleToken, FlowToken, FlowFees, and FlowStorageFees
// contracts from the specified addresses.
func FlowServiceAccount(env templates.Environment) []byte {
code := assets.MustAssetString(flowServiceAccountFilename)
if env.FlowExecutionParametersAddress == "" {
// Remove the import of FlowExecutionParameters
code = strings.ReplaceAll(
code,
"import \"FlowExecutionParameters\"",
"//import \"FlowExecutionParameters\"",
)
// Replace the metering getter functions
code = strings.ReplaceAll(
code,
"return FlowExecutionParameters.getExecutionEffortWeights()",
"return self.account.storage.copy<{UInt64: UInt64}>(from: /storage/executionEffortWeights) ?? panic(\"execution effort weights not set yet\")",
)
code = strings.ReplaceAll(
code,
"return FlowExecutionParameters.getExecutionMemoryWeights()",
"return self.account.storage.copy<{UInt64: UInt64}>(from: /storage/executionMemoryWeights) ?? panic(\"execution memory weights not set yet\")",
)
code = strings.ReplaceAll(
code,
"return FlowExecutionParameters.getExecutionMemoryLimit()",
"return self.account.storage.copy<UInt64>(from: /storage/executionMemoryLimit) ?? panic(\"execution memory limit not set yet\")",
)
}
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowIDTableStaking returns the FlowIDTableStaking contract
func FlowIDTableStaking(env templates.Environment) []byte {
code := assets.MustAssetString(flowIdentityTableFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowStakingProxy returns the StakingProxy contract.
func FlowStakingProxy() []byte {
return assets.MustAsset(flowStakingProxyFilename)
}
// FlowStakingCollection returns the StakingCollection contract.
func FlowStakingCollection(
env templates.Environment,
) []byte {
code := assets.MustAssetString(flowStakingCollectionFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowLockedTokens return the LockedTokens contract
//
// Locked Tokens imports FungibleToken, FlowToken, FlowIDTableStaking, StakingProxy, and FlowStorageFees
func FlowLockedTokens(
env templates.Environment,
) []byte {
code := assets.MustAssetString(flowLockedTokensFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// FlowQC returns the FlowClusterQCs contract.
func FlowQC() []byte {
return assets.MustAsset(flowQCFilename)
}
// FlowDKG returns the FlowDKG contract.
func FlowDKG() []byte {
return assets.MustAsset(flowDKGFilename)
}
// FlowEpoch returns the FlowEpoch contract.
func FlowEpoch(env templates.Environment) []byte {
code := assets.MustAssetString(flowEpochFilename)
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
// NodeVersionBeacon returns the NodeVersionBeacon contract content.
func NodeVersionBeacon() []byte {
return assets.MustAsset(flowNodeVersionBeaconFilename)
}
func RandomBeaconHistory() []byte {
return assets.MustAsset(flowRandomBeaconHistoryFilename)
}
// FlowContractAudits returns the deprecated FlowContractAudits contract.
// This contract is no longer used on any network
func FlowContractAudits() []byte {
return assets.MustAsset(flowContractAuditsFilename)
}
func Crypto() []byte {
return assets.MustAsset(cryptoFilename)
}
func LinearCodeAddressGenerator() []byte {
return assets.MustAsset(linearCodeAddressGeneratorFilename)
}
/******************** Test contracts *********************/
// TESTFlowIDTableStaking returns the TestFlowIDTableStaking contract
func TESTFlowIDTableStaking(fungibleTokenAddress, flowTokenAddress string) []byte {
code := assets.MustAssetString(TESTFlowIdentityTableFilename)
code = strings.ReplaceAll(code, placeholderFungibleTokenAddress, withHexPrefix(fungibleTokenAddress))
code = strings.ReplaceAll(code, placeholderFlowTokenAddress, withHexPrefix(flowTokenAddress))
return []byte(code)
}
// TESTFlowStakingCollection returns the StakingCollection contract with all public fields and functions.
func TESTFlowStakingCollection(
fungibleTokenAddress,
flowTokenAddress,
idTableAddress,
stakingProxyAddress,
lockedTokensAddress,
storageFeesAddress,
qcAddress,
dkgAddress,
epochAddress string,
) []byte {
code := assets.MustAssetString(flowStakingCollectionFilename)
env := templates.Environment{
FungibleTokenAddress: fungibleTokenAddress,
FlowTokenAddress: flowTokenAddress,
IDTableAddress: idTableAddress,
StakingProxyAddress: stakingProxyAddress,
LockedTokensAddress: lockedTokensAddress,
StorageFeesAddress: storageFeesAddress,
QuorumCertificateAddress: qcAddress,
DkgAddress: dkgAddress,
EpochAddress: epochAddress,
BurnerAddress: storageFeesAddress,
}
code = templates.ReplaceAddresses(code, env)
code = strings.ReplaceAll(code, "access(self) fun getTokens", "access(all) fun getTokens")
code = strings.ReplaceAll(code, "access(self) fun depositTokens", "access(all) fun depositTokens")
return []byte(code)
}
func TestFlowFees(fungibleTokenAddress, flowTokenAddress, storageFeesAddress string) []byte {
code := assets.MustAssetString(flowFeesFilename)
env := templates.Environment{
FungibleTokenAddress: fungibleTokenAddress,
FlowTokenAddress: flowTokenAddress,
StorageFeesAddress: storageFeesAddress,
}
code = templates.ReplaceAddresses(code, env)
return []byte(code)
}
func ExampleToken(env templates.Environment) []byte {
return ftcontracts.ExampleToken(env.FungibleTokenAddress, env.MetadataViewsAddress, env.FungibleTokenMetadataViewsAddress)
}
func ExampleNFT(env templates.Environment) []byte {
return nftcontracts.ExampleNFTWithCrossVMPointers(flow.HexToAddress(env.NonFungibleTokenAddress), flow.HexToAddress(env.MetadataViewsAddress), flow.HexToAddress(env.ViewResolverAddress), flow.HexToAddress(env.EVMAddress), flow.HexToAddress(env.CrossVMMetadataViewsAddress))
}