1
- /** pragma type contract **/
2
-
3
- import {
4
- getEnvironment ,
5
- replaceImportAddresses ,
6
- reportMissingImports ,
7
- deployContract ,
8
- } from 'flow-cadut'
9
-
10
- export const CODE = `
11
- pub contract FlowManager {
12
- /// Account Manager
13
- pub event AccountAdded(address: Address)
14
- pub struct Mapper {
15
- pub let accounts: {String: Address}
16
- pub fun getAddress(_ name: String): Address? {
17
- return self.accounts[name]
18
- }
19
- pub fun setAddress(_ name: String, address: Address){
20
- self.accounts[name] = address
21
- emit FlowManager.AccountAdded(address: address)
22
- }
23
- init(){
24
- self.accounts = {}
25
- }
26
- }
27
- pub fun getAccountAddress(_ name: String): Address?{
28
- let accountManager = self.account
29
- .getCapability(self.accountManagerPath)
30
- .borrow<&FlowManager.Mapper>()!
31
- return accountManager.getAddress(name)
32
- }
33
- pub let defaultAccounts: {Address : String}
34
- pub fun resolveDefaultAccounts(_ address: Address): Address{
35
- let alias = self.defaultAccounts[address]!
36
- return self.getAccountAddress(alias)!
37
- }
38
- pub let accountManagerStorage: StoragePath
39
- pub let contractManagerStorage: StoragePath
40
- pub let accountManagerPath: PublicPath
41
- pub let contractManagerPath: PublicPath
42
- /// Environment Manager
43
- pub event BlockOffsetChanged(offset: UInt64)
44
- pub event TimestampOffsetChanged(offset: UFix64)
45
- pub struct MockBlock {
46
- pub let id: [UInt8; 32]
47
- pub let height: UInt64
48
- pub let view: UInt64
49
- pub let timestamp: UFix64
50
- init(_ id: [UInt8; 32], _ height: UInt64, _ view: UInt64, _ timestamp: UFix64){
51
- self.id = id
52
- self.height = height
53
- self.view = view
54
- self.timestamp = timestamp
55
- }
56
- }
57
- pub fun setBlockOffset(_ offset: UInt64){
58
- self.blockOffset = offset
59
- emit FlowManager.BlockOffsetChanged(offset: offset)
60
- }
61
- pub fun setTimestampOffset(_ offset: UFix64){
62
- self.timestampOffset = offset
63
- emit FlowManager.TimestampOffsetChanged(offset: offset)
64
- }
65
- pub fun getBlockHeight(): UInt64 {
66
- var block = getCurrentBlock()
67
- return block.height + self.blockOffset
68
- }
69
- pub fun getBlockTimestamp(): UFix64 {
70
- var block = getCurrentBlock()
71
- return block.timestamp + self.timestampOffset
72
- }
73
- pub fun getBlock(): MockBlock {
74
- var block = getCurrentBlock()
75
- let mockBlock = MockBlock(block.id, block.height, block.view, block.timestamp);
76
- return mockBlock
77
- }
78
- pub var blockOffset: UInt64;
79
- pub var timestampOffset: UFix64;
80
- // Initialize contract
81
- init(){
82
- // Environment defaults
83
- self.blockOffset = 0;
84
- self.timestampOffset = 0.0;
85
- // Account Manager initialization
86
- let accountManager = Mapper()
87
- let contractManager = Mapper()
88
- self.defaultAccounts = {
89
- 0x01: "Alice",
90
- 0x02: "Bob",
91
- 0x03: "Charlie",
92
- 0x04: "Dave",
93
- 0x05: "Eve"
94
- }
95
- self.accountManagerStorage = /storage/testSuitAccountManager
96
- self.contractManagerStorage = /storage/testSuitContractManager
97
- self.accountManagerPath = /public/testSuitAccountManager
98
- self.contractManagerPath = /public/testSuitContractManager
99
-
100
- // Destroy previously stored values
101
- self.account.load<Mapper>(from: self.accountManagerStorage)
102
- self.account.load<Mapper>(from: self.contractManagerStorage)
103
- self.account.save(accountManager, to: self.accountManagerStorage)
104
- self.account.save(contractManager, to: self.contractManagerStorage)
105
- self.account.link<&Mapper>(self.accountManagerPath, target: self.accountManagerStorage)
106
- self.account.link<&Mapper>(self.contractManagerPath, target: self.contractManagerStorage)
107
- }
108
- }
109
-
110
- ` ;
111
-
112
- /**
113
- * Method to generate cadence code for FlowManager contract
114
- * @param {Object.<string, string> } addressMap - contract name as a key and address where it's deployed as value
115
- */
116
- export const FlowManagerTemplate = async ( addressMap = { } ) => {
117
- const envMap = await getEnvironment ( ) ;
118
- const fullMap = {
119
- ...envMap ,
120
- ...addressMap ,
121
- } ;
122
-
123
- // If there are any missing imports in fullMap it will be reported via console
124
- reportMissingImports ( CODE , fullMap , `FlowManager =>` )
125
-
126
- return replaceImportAddresses ( CODE , fullMap ) ;
127
- } ;
128
-
129
-
130
- /**
131
- * Deploys FlowManager transaction to the network
132
- * @param {Object.<string, string> } addressMap - contract name as a key and address where it's deployed as value
133
- * @param Array<*> args - list of arguments
134
- * param Array<string> - list of signers
135
- */
136
- export const deployFlowManager = async ( props ) => {
137
- const { addressMap = { } } = props ;
138
- const code = await FlowManagerTemplate ( addressMap ) ;
139
- const name = "FlowManager"
140
-
141
- return deployContract ( { code, name, processed : true , ...props } )
142
- }
143
-
1
+ /** pragma type contract **/
2
+
3
+ import {
4
+ getEnvironment ,
5
+ replaceImportAddresses ,
6
+ reportMissingImports ,
7
+ deployContract ,
8
+ } from '@onflow/flow-cadut'
9
+
10
+ export const CODE = `
11
+ pub contract FlowManager {
12
+
13
+ /// Account Manager
14
+ pub event AccountAdded(address: Address)
15
+
16
+ pub struct Mapper {
17
+ pub let accounts: {String: Address}
18
+
19
+ pub fun getAddress(_ name: String): Address? {
20
+ return self.accounts[name]
21
+ }
22
+
23
+ pub fun setAddress(_ name: String, address: Address){
24
+ self.accounts[name] = address
25
+ emit FlowManager.AccountAdded(address: address)
26
+ }
27
+
28
+ init(){
29
+ self.accounts = {}
30
+ }
31
+ }
32
+
33
+ pub fun getAccountAddress(_ name: String): Address?{
34
+ let accountManager = self.account
35
+ .getCapability(self.accountManagerPath)
36
+ .borrow<&FlowManager.Mapper>()!
37
+
38
+ return accountManager.getAddress(name)
39
+ }
40
+
41
+ pub let defaultAccounts: {Address : String}
42
+
43
+ pub fun resolveDefaultAccounts(_ address: Address): Address{
44
+ let alias = self.defaultAccounts[address]!
45
+ return self.getAccountAddress(alias)!
46
+ }
47
+
48
+ pub let accountManagerStorage: StoragePath
49
+ pub let contractManagerStorage: StoragePath
50
+ pub let accountManagerPath: PublicPath
51
+ pub let contractManagerPath: PublicPath
52
+
53
+ /// Environment Manager
54
+ pub event BlockOffsetChanged(offset: UInt64)
55
+ pub event TimestampOffsetChanged(offset: UFix64)
56
+
57
+ pub struct MockBlock {
58
+ pub let id: [UInt8; 32]
59
+ pub let height: UInt64
60
+ pub let view: UInt64
61
+ pub let timestamp: UFix64
62
+
63
+ init(_ id: [UInt8; 32], _ height: UInt64, _ view: UInt64, _ timestamp: UFix64){
64
+ self.id = id
65
+ self.height = height
66
+ self.view = view
67
+ self.timestamp = timestamp
68
+ }
69
+ }
70
+
71
+ pub fun setBlockOffset(_ offset: UInt64){
72
+ self.blockOffset = offset
73
+ emit FlowManager.BlockOffsetChanged(offset: offset)
74
+ }
75
+
76
+ pub fun setTimestampOffset(_ offset: UFix64){
77
+ self.timestampOffset = offset
78
+ emit FlowManager.TimestampOffsetChanged(offset: offset)
79
+ }
80
+
81
+ pub fun getBlockHeight(): UInt64 {
82
+ var block = getCurrentBlock()
83
+ return block.height + self.blockOffset
84
+ }
85
+
86
+ pub fun getBlockTimestamp(): UFix64 {
87
+ var block = getCurrentBlock()
88
+ return block.timestamp + self.timestampOffset
89
+ }
90
+
91
+ pub fun getBlock(): MockBlock {
92
+ var block = getCurrentBlock()
93
+ let mockBlock = MockBlock(block.id, block.height, block.view, block.timestamp);
94
+ return mockBlock
95
+ }
96
+
97
+ pub var blockOffset: UInt64;
98
+ pub var timestampOffset: UFix64;
99
+
100
+
101
+ // Initialize contract
102
+ init(){
103
+ // Environment defaults
104
+ self.blockOffset = 0;
105
+ self.timestampOffset = 0.0;
106
+
107
+ // Account Manager initialization
108
+ let accountManager = Mapper()
109
+ let contractManager = Mapper()
110
+
111
+ self.defaultAccounts = {
112
+ 0x01: "Alice",
113
+ 0x02: "Bob",
114
+ 0x03: "Charlie",
115
+ 0x04: "Dave",
116
+ 0x05: "Eve"
117
+ }
118
+
119
+ self.accountManagerStorage = /storage/testSuiteAccountManager
120
+ self.contractManagerStorage = /storage/testSuiteContractManager
121
+
122
+ self.accountManagerPath = /public/testSuiteAccountManager
123
+ self.contractManagerPath = /public/testSuiteContractManager
124
+
125
+ // Destroy previously stored values
126
+ self.account.load<Mapper>(from: self.accountManagerStorage)
127
+ self.account.load<Mapper>(from: self.contractManagerStorage)
128
+
129
+ self.account.save(accountManager, to: self.accountManagerStorage)
130
+ self.account.save(contractManager, to: self.contractManagerStorage)
131
+
132
+ self.account.link<&Mapper>(self.accountManagerPath, target: self.accountManagerStorage)
133
+ self.account.link<&Mapper>(self.contractManagerPath, target: self.contractManagerStorage)
134
+ }
135
+ }
136
+
137
+ ` ;
138
+
139
+ /**
140
+ * Method to generate cadence code for FlowManager contract
141
+ * @param {Object.<string, string> } addressMap - contract name as a key and address where it's deployed as value
142
+ */
143
+ export const FlowManagerTemplate = async ( addressMap = { } ) => {
144
+ const envMap = await getEnvironment ( ) ;
145
+ const fullMap = {
146
+ ...envMap ,
147
+ ...addressMap ,
148
+ } ;
149
+
150
+ // If there are any missing imports in fullMap it will be reported via console
151
+ reportMissingImports ( CODE , fullMap , `FlowManager =>` )
152
+
153
+ return replaceImportAddresses ( CODE , fullMap ) ;
154
+ } ;
155
+
156
+
157
+ /**
158
+ * Deploys FlowManager transaction to the network
159
+ * @param {Object.<string, string> } addressMap - contract name as a key and address where it's deployed as value
160
+ * @param Array<*> args - list of arguments
161
+ * param Array<string> - list of signers
162
+ */
163
+ export const deployFlowManager = async ( props ) => {
164
+ const { addressMap = { } } = props ;
165
+ const code = await FlowManagerTemplate ( addressMap ) ;
166
+ const name = "FlowManager"
167
+
168
+ return deployContract ( { code, name, processed : true , ...props } )
169
+ }
0 commit comments