From 7d450f8188f4ffe908e20687aaf56697cd75bf9a Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Tue, 20 Jun 2023 16:43:12 +0200 Subject: [PATCH 1/6] contracts monotonic support --- emulator/contracts.go | 5 ++--- emulator/contracts_test.go | 3 ++- server/server.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/emulator/contracts.go b/emulator/contracts.go index fc022037..004b0bb0 100644 --- a/emulator/contracts.go +++ b/emulator/contracts.go @@ -14,8 +14,7 @@ import ( nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts" ) -var CommonContracts = func() []ContractDescription { - chain := flowgo.Emulator.Chain() +var CommonContracts = func(chain flowgo.Chain) []ContractDescription { ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix()) serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix()) @@ -57,7 +56,7 @@ var CommonContracts = func() []ContractDescription { Source: nftstorefront.NFTStorefront(1, ftAddress.String(), serviceAddress.String()), }, } -}() +} type ContractDescription struct { Name string diff --git a/emulator/contracts_test.go b/emulator/contracts_test.go index 27cea3f3..3a45f9bc 100644 --- a/emulator/contracts_test.go +++ b/emulator/contracts_test.go @@ -2,6 +2,7 @@ package emulator_test import ( "fmt" + flowgo "github.com/onflow/flow-go/model/flow" "testing" "github.com/onflow/cadence" @@ -15,7 +16,7 @@ func TestCommonContractsDeployment(t *testing.T) { t.Parallel() b, err := emulator.New( - emulator.Contracts(emulator.CommonContracts), + emulator.Contracts(emulator.CommonContracts(flowgo.Emulator.Chain())), ) require.NoError(t, err) diff --git a/server/server.go b/server/server.go index 800355dd..b66e17b6 100644 --- a/server/server.go +++ b/server/server.go @@ -174,7 +174,7 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer { } if conf.WithContracts { - commonContracts := emulator.CommonContracts + commonContracts := emulator.CommonContracts(chain) err := emulator.DeployContracts(emulatedBlockchain, commonContracts) if err != nil { logger.Error().Err(err).Msg("❗ Failed to deploy contracts") From b3da7909dc7a6c148f4cfd5013ddafe37b6416bd Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Tue, 20 Jun 2023 16:47:19 +0200 Subject: [PATCH 2/6] contracts monotonic support / keep backwards compatible --- emulator/contracts.go | 4 +++- emulator/contracts_test.go | 3 +-- server/server.go | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/emulator/contracts.go b/emulator/contracts.go index 004b0bb0..27485475 100644 --- a/emulator/contracts.go +++ b/emulator/contracts.go @@ -14,7 +14,7 @@ import ( nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts" ) -var CommonContracts = func(chain flowgo.Chain) []ContractDescription { +var CommonContractsGenerator = func(chain flowgo.Chain) []ContractDescription { ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix()) serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix()) @@ -58,6 +58,8 @@ var CommonContracts = func(chain flowgo.Chain) []ContractDescription { } } +var CommonContracts = CommonContractsGenerator(flowgo.Emulator.Chain()) + type ContractDescription struct { Name string Address flowsdk.Address diff --git a/emulator/contracts_test.go b/emulator/contracts_test.go index 3a45f9bc..27cea3f3 100644 --- a/emulator/contracts_test.go +++ b/emulator/contracts_test.go @@ -2,7 +2,6 @@ package emulator_test import ( "fmt" - flowgo "github.com/onflow/flow-go/model/flow" "testing" "github.com/onflow/cadence" @@ -16,7 +15,7 @@ func TestCommonContractsDeployment(t *testing.T) { t.Parallel() b, err := emulator.New( - emulator.Contracts(emulator.CommonContracts(flowgo.Emulator.Chain())), + emulator.Contracts(emulator.CommonContracts), ) require.NoError(t, err) diff --git a/server/server.go b/server/server.go index b66e17b6..1b44af06 100644 --- a/server/server.go +++ b/server/server.go @@ -174,7 +174,7 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer { } if conf.WithContracts { - commonContracts := emulator.CommonContracts(chain) + commonContracts := emulator.CommonContractsGenerator(chain) err := emulator.DeployContracts(emulatedBlockchain, commonContracts) if err != nil { logger.Error().Err(err).Msg("❗ Failed to deploy contracts") From 8cb3b871e8f9ddccca19b9fb0a627e9dd744be7e Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Wed, 21 Jun 2023 17:12:19 +0200 Subject: [PATCH 3/6] rename func --- emulator/contracts.go | 4 ++-- server/server.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/emulator/contracts.go b/emulator/contracts.go index 27485475..38246fc6 100644 --- a/emulator/contracts.go +++ b/emulator/contracts.go @@ -14,7 +14,7 @@ import ( nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts" ) -var CommonContractsGenerator = func(chain flowgo.Chain) []ContractDescription { +func NewCommonContracts(chain flowgo.Chain) []ContractDescription { ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix()) serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix()) @@ -58,7 +58,7 @@ var CommonContractsGenerator = func(chain flowgo.Chain) []ContractDescription { } } -var CommonContracts = CommonContractsGenerator(flowgo.Emulator.Chain()) +var CommonContracts = NewCommonContracts(flowgo.Emulator.Chain()) type ContractDescription struct { Name string diff --git a/server/server.go b/server/server.go index 1b44af06..6c32b36e 100644 --- a/server/server.go +++ b/server/server.go @@ -174,7 +174,7 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer { } if conf.WithContracts { - commonContracts := emulator.CommonContractsGenerator(chain) + commonContracts := emulator.NewCommonContracts(chain) err := emulator.DeployContracts(emulatedBlockchain, commonContracts) if err != nil { logger.Error().Err(err).Msg("❗ Failed to deploy contracts") From 5c6494b3e938544bee1a60e5e15f1d8b45eb3ded Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Wed, 21 Jun 2023 17:31:20 +0200 Subject: [PATCH 4/6] update test for monotonic --- emulator/contracts.go | 1 - emulator/contracts_test.go | 56 ++++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/emulator/contracts.go b/emulator/contracts.go index 38246fc6..e06ae6dd 100644 --- a/emulator/contracts.go +++ b/emulator/contracts.go @@ -17,7 +17,6 @@ import ( func NewCommonContracts(chain flowgo.Chain) []ContractDescription { ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix()) serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix()) - return []ContractDescription{ { Name: "FUSD", diff --git a/emulator/contracts_test.go b/emulator/contracts_test.go index 27cea3f3..fac8b6c1 100644 --- a/emulator/contracts_test.go +++ b/emulator/contracts_test.go @@ -2,6 +2,7 @@ package emulator_test import ( "fmt" + flowgo "github.com/onflow/flow-go/model/flow" "testing" "github.com/onflow/cadence" @@ -14,13 +15,22 @@ func TestCommonContractsDeployment(t *testing.T) { t.Parallel() - b, err := emulator.New( - emulator.Contracts(emulator.CommonContracts), - ) - require.NoError(t, err) + //only test monotonic and emulator ( mainnet / testnet is used for remote debugging ) + chains := []flowgo.Chain{ + flowgo.MonotonicEmulator.Chain(), + flowgo.Emulator.Chain(), + } - serviceAccount := b.ServiceKey().Address.Hex() - scriptCode := fmt.Sprintf(` + for _, chain := range chains { + + b, err := emulator.New( + emulator.Contracts(emulator.NewCommonContracts(chain)), + emulator.WithChainID(chain.ChainID()), + ) + require.NoError(t, err) + + serviceAccount := b.ServiceKey().Address.Hex() + scriptCode := fmt.Sprintf(` import FUSD, NonFungibleToken, @@ -33,28 +43,28 @@ func TestCommonContractsDeployment(t *testing.T) { pub fun main(): Bool { log(Type().identifier) log(Type().identifier) - log(Type().identifier) + log(Type().identifier) log(Type().identifier) log(Type().identifier) log(Type().identifier) - return true } `, serviceAccount) - scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{}) - require.NoError(t, err) - assert.ElementsMatch( - t, - []string{ - "\"A.f8d6e0586b0a20c7.FUSD\"", - "\"A.f8d6e0586b0a20c7.NonFungibleToken\"", - "\"A.f8d6e0586b0a20c7.MetadataViews\"", - "\"A.f8d6e0586b0a20c7.ExampleNFT\"", - "\"A.f8d6e0586b0a20c7.NFTStorefrontV2\"", - "\"A.f8d6e0586b0a20c7.NFTStorefront\"", - }, - scriptResult.Logs, - ) - assert.Equal(t, cadence.NewBool(true), scriptResult.Value) + scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{}) + require.NoError(t, err) + assert.ElementsMatch( + t, + []string{ + fmt.Sprintf("\"A.%s.FUSD\"", serviceAccount), + fmt.Sprintf("\"A.%s.NonFungibleToken\"", serviceAccount), + fmt.Sprintf("\"A.%s.MetadataViews\"", serviceAccount), + fmt.Sprintf("\"A.%s.ExampleNFT\"", serviceAccount), + fmt.Sprintf("\"A.%s.NFTStorefrontV2\"", serviceAccount), + fmt.Sprintf("\"A.%s.NFTStorefront\"", serviceAccount), + }, + scriptResult.Logs, + ) + assert.Equal(t, cadence.NewBool(true), scriptResult.Value) + } } From 45f68788e08f9a643ccf974f0f660ec341f0a593 Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Wed, 21 Jun 2023 18:01:54 +0200 Subject: [PATCH 5/6] better test --- emulator/contracts_test.go | 57 +++++++++++++------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) diff --git a/emulator/contracts_test.go b/emulator/contracts_test.go index fac8b6c1..bdb0cd83 100644 --- a/emulator/contracts_test.go +++ b/emulator/contracts_test.go @@ -5,9 +5,7 @@ import ( flowgo "github.com/onflow/flow-go/model/flow" "testing" - "github.com/onflow/cadence" "github.com/onflow/flow-emulator/emulator" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -17,10 +15,18 @@ func TestCommonContractsDeployment(t *testing.T) { //only test monotonic and emulator ( mainnet / testnet is used for remote debugging ) chains := []flowgo.Chain{ - flowgo.MonotonicEmulator.Chain(), flowgo.Emulator.Chain(), + flowgo.MonotonicEmulator.Chain(), } + contracts := []string{ + "FUSD", + "MetadataViews", + "ExampleNFT", + "NFTStorefrontV2", + "NFTStorefront", + "NonFungibleToken", + } for _, chain := range chains { b, err := emulator.New( @@ -30,41 +36,18 @@ func TestCommonContractsDeployment(t *testing.T) { require.NoError(t, err) serviceAccount := b.ServiceKey().Address.Hex() - scriptCode := fmt.Sprintf(` - import - FUSD, - NonFungibleToken, - MetadataViews, - ExampleNFT, - NFTStorefrontV2, - NFTStorefront - from 0x%s - pub fun main(): Bool { - log(Type().identifier) - log(Type().identifier) - log(Type().identifier) - log(Type().identifier) - log(Type().identifier) - log(Type().identifier) - return true - } - `, serviceAccount) + for _, contract := range contracts { - scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{}) - require.NoError(t, err) - assert.ElementsMatch( - t, - []string{ - fmt.Sprintf("\"A.%s.FUSD\"", serviceAccount), - fmt.Sprintf("\"A.%s.NonFungibleToken\"", serviceAccount), - fmt.Sprintf("\"A.%s.MetadataViews\"", serviceAccount), - fmt.Sprintf("\"A.%s.ExampleNFT\"", serviceAccount), - fmt.Sprintf("\"A.%s.NFTStorefrontV2\"", serviceAccount), - fmt.Sprintf("\"A.%s.NFTStorefront\"", serviceAccount), - }, - scriptResult.Logs, - ) - assert.Equal(t, cadence.NewBool(true), scriptResult.Value) + scriptCode := fmt.Sprintf(` + pub fun main() { + getAccount(0x%s).contracts.get(name: "%s") ?? panic("contract is not deployed") + }`, serviceAccount, contract) + + scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{}) + require.NoError(t, err) + require.NoError(t, scriptResult.Error) + + } } } From ff07bcfe4e698741ac881eeeb63aa71e893be52c Mon Sep 17 00:00:00 2001 From: Deniz Mert Edincik Date: Wed, 21 Jun 2023 18:22:23 +0200 Subject: [PATCH 6/6] test update --- emulator/contracts_test.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/emulator/contracts_test.go b/emulator/contracts_test.go index bdb0cd83..6e0f262d 100644 --- a/emulator/contracts_test.go +++ b/emulator/contracts_test.go @@ -19,30 +19,23 @@ func TestCommonContractsDeployment(t *testing.T) { flowgo.MonotonicEmulator.Chain(), } - contracts := []string{ - "FUSD", - "MetadataViews", - "ExampleNFT", - "NFTStorefrontV2", - "NFTStorefront", - "NonFungibleToken", - } for _, chain := range chains { + contracts := emulator.NewCommonContracts(chain) b, err := emulator.New( - emulator.Contracts(emulator.NewCommonContracts(chain)), + emulator.Contracts(contracts), emulator.WithChainID(chain.ChainID()), ) require.NoError(t, err) - serviceAccount := b.ServiceKey().Address.Hex() - for _, contract := range contracts { + require.Equal(t, contract.Address.Hex(), chain.ServiceAddress().Hex()) + scriptCode := fmt.Sprintf(` pub fun main() { getAccount(0x%s).contracts.get(name: "%s") ?? panic("contract is not deployed") - }`, serviceAccount, contract) + }`, contract.Address, contract.Name) scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{}) require.NoError(t, err)