Skip to content

Commit a96675f

Browse files
committed
Move deployment of common contracts to Blockchain type
We also introduce a new option, called WithContracts, in order to programmatically specify when a Blockchain instance should deploy common contracts.
1 parent 1bb2689 commit a96675f

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

Diff for: blockchain.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ type config struct {
158158
TransactionValidationEnabled bool
159159
ChainID flowgo.ChainID
160160
CoverageReportingEnabled bool
161+
WithContracts bool
161162
}
162163

163164
func (conf config) GetStore() storage.Store {
@@ -215,6 +216,7 @@ var defaultConfig = func() config {
215216
TransactionValidationEnabled: true,
216217
ChainID: flowgo.Emulator,
217218
CoverageReportingEnabled: false,
219+
WithContracts: false,
218220
}
219221
}()
220222

@@ -403,6 +405,15 @@ func WithCoverageReportingEnabled(enabled bool) Option {
403405
}
404406
}
405407

408+
// WithContracts enables/disables deployment of common contracts, such as:
409+
// NFT, FUSD, MetadataViews, NFTStorefront/NFTStorefrontV2, ExampleNFT
410+
// The default value is falue.
411+
func WithContracts(enabled bool) Option {
412+
return func(c *config) {
413+
c.WithContracts = enabled
414+
}
415+
}
416+
406417
func (b *Blockchain) ReloadBlockchain() error {
407418
var err error
408419

@@ -430,8 +441,7 @@ func (b *Blockchain) ReloadBlockchain() error {
430441

431442
// NewBlockchain instantiates a new emulated blockchain with the provided options.
432443
func NewBlockchain(opts ...Option) (*Blockchain, error) {
433-
434-
// apply options to the default config
444+
// Apply options to the default config
435445
conf := defaultConfig
436446
for _, opt := range opts {
437447
opt(&conf)
@@ -449,8 +459,18 @@ func NewBlockchain(opts ...Option) (*Blockchain, error) {
449459
if err != nil {
450460
return nil, err
451461
}
452-
return b, nil
462+
if conf.WithContracts {
463+
deployments, err := DeployContracts(b)
464+
if err != nil {
465+
conf.ServerLogger.Error().Err(err).Msg("❗ Failed to deploy contracts")
466+
}
453467

468+
for _, contract := range deployments {
469+
conf.ServerLogger.Info().Fields(map[string]any{
470+
contract.Name: fmt.Sprintf("0x%s", contract.Address.Hex())}).Msg(contract.Description)
471+
}
472+
}
473+
return b, nil
454474
}
455475

456476
func (b *Blockchain) rollbackProvider() (storage.RollbackProvider, error) {

Diff for: server/contracts.go renamed to contracts.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
package server
1+
package emulator
22

33
import (
44
"fmt"
55

6-
emulator "github.com/onflow/flow-emulator"
76
flowsdk "github.com/onflow/flow-go-sdk"
87
"github.com/onflow/flow-go-sdk/templates"
98
"github.com/onflow/flow-go/fvm"
@@ -14,12 +13,12 @@ import (
1413
)
1514

1615
type DeployDescription struct {
17-
name string
18-
address flowsdk.Address
19-
description string
16+
Name string
17+
Address flowsdk.Address
18+
Description string
2019
}
2120

22-
func deployContracts(b *emulator.Blockchain) ([]DeployDescription, error) {
21+
func DeployContracts(b *Blockchain) ([]DeployDescription, error) {
2322
ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(b.GetChain()).Hex())
2423
serviceAddress := b.ServiceKey().Address
2524

@@ -50,18 +49,18 @@ func deployContracts(b *emulator.Blockchain) ([]DeployDescription, error) {
5049
},
5150
{
5251
name: "NFTStorefrontV2",
53-
description: "✨ NFT Storefront contract v2",
52+
description: "✨ NFT Storefront contract v2",
5453
source: nftstorefront.NFTStorefront(2, ftAddress.String(), serviceAddress.String()),
5554
},
5655
{
5756
name: "NFTStorefront",
58-
description: "✨ NFT Storefront contract",
57+
description: "✨ NFT Storefront contract",
5958
source: nftstorefront.NFTStorefront(1, ftAddress.String(), serviceAddress.String()),
6059
},
6160
}
6261

6362
for _, c := range toDeploy {
64-
err := deployContract(b, c.name, c.source)
63+
err := DeployContract(b, c.name, c.source)
6564
if err != nil {
6665
return nil, err
6766
}
@@ -81,18 +80,17 @@ func deployContracts(b *emulator.Blockchain) ([]DeployDescription, error) {
8180
deployDescriptions = append(
8281
deployDescriptions,
8382
DeployDescription{
84-
name: c.name,
85-
address: serviceAddress,
86-
description: c.description,
83+
Name: c.name,
84+
Address: serviceAddress,
85+
Description: c.description,
8786
},
8887
)
8988
}
9089

9190
return deployDescriptions, nil
9291
}
9392

94-
func deployContract(b *emulator.Blockchain, name string, contract []byte) error {
95-
93+
func DeployContract(b *Blockchain, name string, contract []byte) error {
9694
serviceKey := b.ServiceKey()
9795
serviceAddress := serviceKey.Address
9896

Diff for: server/server.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,6 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer {
166166
logger.Info().Fields(map[string]any{contract: address}).Msg("📜 Flow contract")
167167
}
168168

169-
if conf.WithContracts {
170-
deployments, err := deployContracts(blockchain)
171-
if err != nil {
172-
logger.Error().Err(err).Msg("❗ Failed to deploy contracts")
173-
}
174-
175-
for _, contract := range deployments {
176-
logger.Info().Fields(map[string]any{
177-
contract.name: fmt.Sprintf("0x%s", contract.address.Hex())}).Msg(contract.description)
178-
}
179-
}
180-
181169
be := configureBackend(logger, conf, blockchain)
182170

183171
livenessTicker := NewLivenessTicker(conf.LivenessCheckTolerance)
@@ -350,6 +338,7 @@ func configureBlockchain(logger *zerolog.Logger, conf *Config, store storage.Sto
350338
emulator.WithChainID(conf.ChainID),
351339
emulator.WithContractRemovalEnabled(conf.ContractRemovalEnabled),
352340
emulator.WithCoverageReportingEnabled(conf.CoverageReportingEnabled),
341+
emulator.WithContracts(conf.WithContracts),
353342
}
354343

355344
if conf.SkipTransactionValidation {

0 commit comments

Comments
 (0)