diff --git a/internal/v1_1/generator.go b/internal/v1_1/generator.go index d8e8dee..86dbfd8 100644 --- a/internal/v1_1/generator.go +++ b/internal/v1_1/generator.go @@ -100,27 +100,25 @@ func (g Generator) CreateTemplate(ctx context.Context, code string, preFill stri } func (g Generator) calculateNetworkPins() error { - networksOfInterest := []string{} + networkPins := make([]NetworkPin, 0) // only interested in the client networks for _, c := range g.clients { params, err := c.GetNetworkParameters(context.Background()) if err != nil { continue } - networksOfInterest = append(networksOfInterest, params.ChainID.String()) - } - - networkPins := make([]NetworkPin, 0) - for _, netName := range networksOfInterest { - cad, err := g.template.ReplaceCadenceImports(netName) + // remove flow- prefix + networkName := strings.TrimPrefix(params.ChainID.String(), "flow-") + cad, err := g.template.ReplaceCadenceImports(networkName) if err != nil { continue } networkPins = append(networkPins, NetworkPin{ - Network: netName, + Network: networkName, PinSelf: ShaHex(cad, ""), }) } + g.template.Data.Cadence.NetworkPins = networkPins return nil } diff --git a/internal/v1_1/generator_test.go b/internal/v1_1/generator_test.go index b8a895c..acce360 100644 --- a/internal/v1_1/generator_test.go +++ b/internal/v1_1/generator_test.go @@ -104,7 +104,6 @@ func TestValidImports(t *testing.T) { code := ` import "Alice" import Bob from 0x0000000000000002 - import Joe access(all) fun main(): Void {} @@ -114,6 +113,62 @@ func TestValidImports(t *testing.T) { assert.NoError(err, "Generate should not return an error") autogold.ExpectFile(t, template) +} +func TestInValidImports(t *testing.T) { + contracts := []Contract{ + { + Contract: "Alice", + Networks: []Network{ + { + Network: "testnet", + Address: "0x0000000000000001", + }, + { + Network: "mainnet", + Address: "0x0000000000000001", + }, + { + Network: "emulator", + Address: "0x0000000000000001", + }, + }, + }, + { + Contract: "Bob", + Networks: []Network{ + { + Network: "testnet", + Address: "0x0000000000000002", + }, + { + Network: "mainnet", + Address: "0x0000000000000002", + }, + { + Network: "emulator", + Address: "0x0000000000000002", + }, + }, + }, + } + + generator := Generator{ + deployedContracts: contracts, + } + + assert := assert.New(t) + code := ` + import "Alice" + import Bob from 0x0000000000000002 + import "Joe" + + access(all) + fun main(): Void {} +` + ctx := context.Background() + _, err := generator.CreateTemplate(ctx, code, "") + assert.Error(err, "Generate should not return an error") + } func TestTransactionValue(t *testing.T) { diff --git a/internal/v1_1/testdata/TestValidImports.golden b/internal/v1_1/testdata/TestValidImports.golden index c148246..69e176c 100644 --- a/internal/v1_1/testdata/TestValidImports.golden +++ b/internal/v1_1/testdata/TestValidImports.golden @@ -1,13 +1,13 @@ `{ "f_type": "InteractionTemplate", "f_version": "1.1.0", - "id": "585d2dd4fc3523ba140fbe0f798c07f1d4c3792aaed72a3b0d7c32c23a6457e7", + "id": "057b6191eaec9c20040b8be92758de58f01c74f7f50767e552e60b7bccf4865d", "data": { "type": "script", "interface": "", "messages": null, "cadence": { - "body": "\n\timport \"Alice\"\n\timport \"Bob\"\n\timport Joe\n\n\taccess(all)\n\tfun main(): Void {}\n", + "body": "\n\timport \"Alice\"\n\timport \"Bob\"\n\n\taccess(all)\n\tfun main(): Void {}\n", "network_pins": [] }, "dependencies": [