Skip to content

Commit

Permalink
Merge pull request #1458 from onflow/supun/fix-imports
Browse files Browse the repository at this point in the history
Replace imports of the staged contracts
  • Loading branch information
SupunS authored Mar 15, 2024
2 parents 8aeddf9 + f85ee14 commit e93bdd1
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions internal/migrate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,25 @@ import (
"fmt"
"os"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"

"github.com/onflow/flow-emulator/storage/migration"
emulatorMigrate "github.com/onflow/flow-emulator/storage/migration"
"github.com/onflow/flow-emulator/storage/sqlite"
"github.com/onflow/flow-go-sdk"

"github.com/onflow/flow-go/cmd/util/ledger/migrations"
"github.com/onflow/flow-go/cmd/util/ledger/reporters"

"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/output"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/onflow/flowkit/v2/project"

"github.com/onflow/flow-go-sdk"

"github.com/onflow/flow-cli/internal/command"
)
Expand Down Expand Up @@ -102,7 +109,19 @@ func migrateState(
}

func resolveStagedContracts(state *flowkit.State, contractNames []string) ([]migrations.StagedContract, error) {
contracts := make([]migrations.StagedContract, len(contractNames))
stagedContracts := make([]migrations.StagedContract, len(contractNames))

network := config.EmulatorNetwork

contracts, err := state.DeploymentContractsByNetwork(network)
if err != nil {
return nil, err
}

importReplacer := project.NewImportReplacer(
contracts,
state.AliasesForNetwork(network),
)

for i, contractName := range contractNames {
// First try to get contract address from aliases
Expand All @@ -112,7 +131,7 @@ func resolveStagedContracts(state *flowkit.State, contractNames []string) ([]mig
}

var address flow.Address
alias := contract.Aliases.ByNetwork(config.EmulatorNetwork.Name)
alias := contract.Aliases.ByNetwork(network.Name)
if alias != nil {
address = alias.Address
}
Expand All @@ -124,20 +143,34 @@ func resolveStagedContracts(state *flowkit.State, contractNames []string) ([]mig

// If contract is not aliased, try to get address by deployment account
if address == flow.EmptyAddress {
address, err = getAddressByContractName(state, contractName, config.EmulatorNetwork)
address, err = getAddressByContractName(state, contractName, network)
if err != nil {
return nil, fmt.Errorf("failed to get address by contract name: %w", err)
}
}

contracts[i] = migrations.StagedContract{
program, err := project.NewProgram(code, []cadence.Value{}, contract.Location)
if err != nil {
return nil, err
}

if program.HasImports() {
program, err = importReplacer.Replace(program)
if err != nil {
return nil, err
}
}

updatedCode := program.Code()

stagedContracts[i] = migrations.StagedContract{
Contract: migrations.Contract{
Name: contractName,
Code: code,
Code: updatedCode,
},
Address: common.Address(address),
}
}

return contracts, nil
return stagedContracts, nil
}

0 comments on commit e93bdd1

Please sign in to comment.