Skip to content

Commit

Permalink
Merge pull request #427 from bluesign/contractsMonotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jun 22, 2023
2 parents d5568e9 + df80cc9 commit 49545d6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
8 changes: 4 additions & 4 deletions emulator/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts"
)

var CommonContracts = func() []ContractDescription {
chain := flowgo.Emulator.Chain()
func NewCommonContracts(chain flowgo.Chain) []ContractDescription {
ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix())
serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix())

return []ContractDescription{
{
Name: "FUSD",
Expand Down Expand Up @@ -57,7 +55,9 @@ var CommonContracts = func() []ContractDescription {
Source: nftstorefront.NFTStorefront(1, ftAddress.String(), serviceAddress.String()),
},
}
}()
}

var CommonContracts = NewCommonContracts(flowgo.Emulator.Chain())

type ContractDescription struct {
Name string
Expand Down
76 changes: 31 additions & 45 deletions emulator/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,45 @@ package emulator_test

import (
"fmt"
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"
)

func TestCommonContractsDeployment(t *testing.T) {

t.Parallel()

b, err := emulator.New(
emulator.Contracts(emulator.CommonContracts),
)
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<FUSD>().identifier)
log(Type<NonFungibleToken>().identifier)
log(Type<MetadataViews>().identifier)
log(Type<ExampleNFT>().identifier)
log(Type<NFTStorefrontV2>().identifier)
log(Type<NFTStorefront>().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)
//only test monotonic and emulator ( mainnet / testnet is used for remote debugging )
chains := []flowgo.Chain{
flowgo.Emulator.Chain(),
flowgo.MonotonicEmulator.Chain(),
}

for _, chain := range chains {
contracts := emulator.NewCommonContracts(chain)

b, err := emulator.New(
emulator.Contracts(contracts),
emulator.WithChainID(chain.ChainID()),
)
require.NoError(t, err)

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")
}`, contract.Address, contract.Name)

scriptResult, err := b.ExecuteScript([]byte(scriptCode), [][]byte{})
require.NoError(t, err)
require.NoError(t, scriptResult.Error)

}
}
}
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func NewEmulatorServer(logger *zerolog.Logger, conf *Config) *EmulatorServer {
}

if conf.WithContracts {
commonContracts := emulator.CommonContracts
commonContracts := emulator.NewCommonContracts(chain)
err := emulator.DeployContracts(emulatedBlockchain, commonContracts)
if err != nil {
logger.Error().Err(err).Msg("❗ Failed to deploy contracts")
Expand Down

0 comments on commit 49545d6

Please sign in to comment.