-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (149 loc) · 4.5 KB
/
main.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
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/onflow/flow-emulator/storage/memstore"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/deps"
"github.com/onflow/flowkit/v2/output"
jsFlow "github.com/onflowser/flow-cli-wasm/js"
"github.com/onflowser/flow-cli-wasm/logging"
"syscall/js"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flowkit/v2/gateway"
)
type Config struct {
Verbose bool
LogFormat string // "text" or "json". Defaults to "json" if "logs" writer is used.
FileSystem flowkit.ReaderWriter
Prompter deps.Prompter
}
type FlowWasm struct {
config Config
state *flowkit.State
gateway *gateway.EmulatorGateway
logger *logging.Logger
kit *flowkit.Flowkit
installer *deps.DependencyInstaller
}
func main() {
w := New(Config{
Verbose: true,
LogFormat: "text",
Prompter: jsFlow.NewPrompter(js.Global().Get("prompter")),
FileSystem: jsFlow.NewFileSystem(js.Global().Get("flowFileSystem")),
})
// Register APIs
internalGateway := jsFlow.NewInternalGateway(w.gateway)
js.Global().Set("gateway", internalGateway.JsValue())
js.Global().Set("getLogs", js.FuncOf(w.getLogs))
js.Global().Set("install", js.FuncOf(w.install))
js.Global().Set("deploy", js.FuncOf(w.deploy))
// Indicate the emulator started and APIs were initialized
js.Global().Call("onStarted")
// Prevent the function from returning, which is required in a wasm module
select {}
}
func New(config Config) *FlowWasm {
logger := logging.NewLogger(logging.Config{
Verbose: config.Verbose,
LogFormat: config.LogFormat,
})
store := memstore.New()
emulatorGateway := gateway.NewEmulatorGatewayWithOpts(
&gateway.EmulatorKey{
PublicKey: emulator.DefaultServiceKey().AccountKey().PublicKey,
SigAlgo: emulator.DefaultServiceKeySigAlgo,
HashAlgo: emulator.DefaultServiceKeyHashAlgo,
},
gateway.WithEmulatorOptions(
emulator.WithLogger(*logger.Zerolog()),
emulator.WithStore(store),
emulator.WithTransactionValidationEnabled(false),
emulator.WithStorageLimitEnabled(false),
emulator.WithTransactionFeesEnabled(false),
),
)
configFilePaths := []string{
"flow.json",
}
state, err := flowkit.Load(configFilePaths, config.FileSystem)
if err != nil {
panic(err)
}
network, err := state.Networks().ByName("emulator")
if err != nil {
panic(err)
}
kit := flowkit.NewFlowkit(state, *network, emulatorGateway, logger)
installer, err := deps.NewDependencyInstaller(
state,
config.Prompter,
deps.WithGateways(jsGateways(emulatorGateway)),
deps.WithLogger(logger),
deps.WithSaveState(),
)
if err != nil {
panic(err)
}
return &FlowWasm{
config: config,
gateway: emulatorGateway,
logger: logger,
kit: kit,
installer: installer,
}
}
func jsGateways(emulatorGateway gateway.Gateway) map[string]gateway.Gateway {
testnetGateway := jsFlow.NewExternalGateway(js.Global().Get("testnetGateway"))
mainnetGateway := jsFlow.NewExternalGateway(js.Global().Get("mainnetGateway"))
previewnetGateway := jsFlow.NewExternalGateway(js.Global().Get("previewnetGateway"))
return map[string]gateway.Gateway{
config.EmulatorNetwork.Name: emulatorGateway,
config.TestnetNetwork.Name: testnetGateway,
config.MainnetNetwork.Name: mainnetGateway,
config.PreviewnetNetwork.Name: previewnetGateway,
}
}
func (w *FlowWasm) install(this js.Value, args []js.Value) any {
executor := func() (js.Value, error) {
err := w.installer.Install()
return js.Null(), err
}
return jsFlow.AsyncWork(executor)
}
func (w *FlowWasm) getLogs(this js.Value, args []js.Value) interface{} {
res, err := json.Marshal(w.logger.LogsHistory())
if err != nil {
panic(err)
}
return string(res)
}
func (w *FlowWasm) deploy(this js.Value, args []js.Value) interface{} {
executor := func() (js.Value, error) {
contracts, err := w.kit.DeployProject(context.Background(), flowkit.UpdateExistingContract(true))
if err != nil {
var projectErr *flowkit.ProjectDeploymentError
if errors.As(err, &projectErr) {
for name, err := range projectErr.Contracts() {
w.logger.Info(fmt.Sprintf(
"%s Failed to deploy contract %s: %s",
output.ErrorEmoji(),
name,
err.Error(),
))
}
return js.Null(), fmt.Errorf("failed deploying all contracts")
}
return js.Null(), err
}
for _, contract := range contracts {
w.logger.Info(fmt.Sprintf("deployed %s contract", contract.Name))
}
return js.Null(), err
}
return jsFlow.AsyncWork(executor)
}