-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.js
177 lines (140 loc) · 5.67 KB
/
add.js
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
const {getConfig, writeConfig, getProjectConfig, getDefaultConfigPath} = require("./utils");
const {getImports} = require("./dependency-tree");
const specialContractsHandlers = {
"FungibleToken": (contract, userConfig, account) => {
return handleFungibleTokenAccountContract(contract, userConfig, account, "FungibleToken")
},
"FungibleTokenMetadataViews": (contract, userConfig, account) => {
return handleFungibleTokenAccountContract(contract, userConfig, account, "FungibleTokenMetadataViews")
},
"FlowToken": (contract, userConfig, account) => {
console.log("FlowToken requires some special setup. The account `emulator-flowtoken` " +
"will be created and the contract will be deployed to it on the emulator. \nGoing forward, any deployments to the " +
"flow emulator will require the --update flag to work correctly.")
const name = "FlowToken"
const serverPK = userConfig.accounts[account].key
const ftAccount = {
address: "0ae53cb6e3f42a79", // this is the FungibleToken address on the flow emulator
key: serverPK
}
const emulatorAcct = "emulator-flowtoken"
// ensure emulator-ft is an account
userConfig.accounts[emulatorAcct] = ftAccount
if (!userConfig.deployments) {
userConfig.deployments = {}
}
// ensure that emulator-ft is a deployment account
if (!userConfig.deployments.emulator) {
userConfig.deployments.emulator = {}
}
if (!userConfig.deployments.emulator[emulatorAcct]) {
userConfig.deployments.emulator[emulatorAcct] = []
}
userConfig.contracts[name] = contract
if (!userConfig.deployments.emulator[emulatorAcct].includes(name)) {
userConfig.deployments.emulator[emulatorAcct].push(name)
}
}
}
const importContract = (contractName, source, config, account) => {
let newConfig = {...config}
const contract = source.contracts[contractName]
if (!contract) {
console.error(`Contract "${contractName}" could not be found`)
return
}
contract.source = contract.source.replace("./contracts/", "./node_modules/@flowtyio/flow-contracts/contracts/")
if (specialContractsHandlers[contractName]) {
specialContractsHandlers[contractName](contract, newConfig, account)
} else {
// only add the contract if it doesn't already exist
if (!newConfig.contracts[contractName]) {
newConfig.contracts[contractName] = contract
}
// add this contract to the deployment list of the specified account
if (!newConfig.deployments.emulator[account].includes(contractName)) {
newConfig.deployments.emulator[account].push(contractName)
}
}
return newConfig
}
const add = ({name, config, account}) => {
let userConfig = getConfig(config)
const exampleConfigLocation = `${__dirname}/flow.json`
const exampleConfig = getConfig(exampleConfigLocation)
let contract = exampleConfig.contracts[name]
if (!contract) {
console.error(`Contract "${name}" could not be found`)
return
}
contract.source = contract.source.replace("./contracts/", "./node_modules/@flowtyio/flow-contracts/contracts/")
if (!userConfig.contracts) {
userConfig.contracts = {}
}
// validate the specified account exists
if (!userConfig.accounts[account]) {
console.error(`Account "${account}" could not be found`)
return
}
if (!userConfig.deployments) {
userConfig.deployments = {}
}
if (!userConfig.deployments.emulator) {
userConfig.deployments.emulator = {}
}
if (!userConfig.deployments.emulator[account]) {
userConfig.deployments.emulator[account] = []
}
// get all imports, add them first, then add the one being requested.
const imports = getImports(name)
if (imports) {
console.log("The following contracts will also be added to the config: ", imports.join(", "))
imports.forEach(contractName => {
userConfig = importContract(contractName, exampleConfig, userConfig, account)
})
}
console.log("finished adding dependencies, adding requested contract")
userConfig = importContract(name, exampleConfig, userConfig, account)
writeConfig(config, userConfig)
console.log(`Contract "${name}" added to ${config}`)
}
const addAll = (path, account) => {
const configPath = path ?? getDefaultConfigPath()
console.log(`Adding all contracts to config found at ${configPath}`)
const projectConfig = getProjectConfig()
const userConfig = getConfig(configPath)
Object.keys(projectConfig.contracts).forEach(name => {
add({name, config: configPath, account})
})
}
const handleFungibleTokenAccountContract = (contract, userConfig, account, contractName) => {
console.log(`FungibleToken requires some special setup. The account "emulator-ft"\n
will be created and the contract will be deployed to it on the emulator. \nGoing forward, any deployments to the\n
flow emulator will require the --update flag to work correctly.`)
const serverPK = userConfig.accounts[account].key
const ftAccount = {
address: "ee82856bf20e2aa6", // this is the FungibleToken address on the flow emulator
key: serverPK
}
const emulatorAcct = "emulator-ft"
// ensure emulator-ft is an account
userConfig.accounts[emulatorAcct] = ftAccount
if (!userConfig.deployments) {
userConfig.deployments = {}
}
// ensure that emulator-ft is a deployment account
if (!userConfig.deployments.emulator) {
userConfig.deployments.emulator = {}
}
if (!userConfig.deployments.emulator[emulatorAcct]) {
userConfig.deployments.emulator[emulatorAcct] = []
}
userConfig.contracts[contractName] = contract
if (!userConfig.deployments.emulator[emulatorAcct].includes(contractName)) {
userConfig.deployments.emulator[emulatorAcct].push(contractName)
}
}
module.exports = {
add,
addAll
}