Skip to content

Commit

Permalink
fix contract name resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Mar 29, 2024
1 parent 59e881b commit f30c506
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions internal/migrate/staging_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func (v *stagingValidator) loadSystemContracts() {
}

v.contracts[location] = stagedSystemContract.Code
v.accountContractNames[stagedSystemContract.Address] = append(v.accountContractNames[stagedSystemContract.Address], stagedSystemContract.Name)
}
}

Expand Down Expand Up @@ -385,22 +386,40 @@ func (v *stagingValidator) resolveAddressContractNames(address common.Address) (
return names, nil
}

// Get the account for the contract
account, err := v.flow.GetAccount(context.Background(), flowsdk.Address(address))
cAddr := cadence.BytesToAddress(address.Bytes())
value, err := v.flow.ExecuteScript(
context.Background(),
flowkit.Script{
Code: templates.GenerateGetStagedContractNamesForAddressScript(MigrationContractStagingAddress(v.flow.Network().Name)),
Args: []cadence.Value{cAddr},
},
flowkit.LatestScriptQuery,
)

if err != nil {
return nil, fmt.Errorf("failed to get account: %w", err)
return nil, err
}

// Get the contract names
contractNames := make([]string, 0, len(account.Contracts))
for name := range account.Contracts {
contractNames = append(contractNames, name)
optValue, ok := value.(cadence.Optional)
if !ok {
return nil, fmt.Errorf("invalid script return value type: %T", value)
}

arrValue, ok := optValue.Value.(cadence.Array)
if !ok {
return nil, fmt.Errorf("invalid script return value type: %T", value)
}

// Cache the contract names
v.accountContractNames[address] = contractNames
for _, name := range arrValue.Values {
strName, ok := name.(cadence.String)
if !ok {
return nil, fmt.Errorf("invalid array value type: %T", name)
}
v.accountContractNames[address] = append(v.accountContractNames[address], string(strName))
}

return contractNames, nil
return v.accountContractNames[address], nil
}

// Helper for pretty printing errors
Expand Down

0 comments on commit f30c506

Please sign in to comment.