Skip to content

Commit

Permalink
Merge pull request #89 from onflow/fix-network-pins
Browse files Browse the repository at this point in the history
fix network names and setting network pins
  • Loading branch information
bthaile authored Dec 3, 2024
2 parents e8c6f14 + 6bc17b5 commit b7f5bb7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
14 changes: 6 additions & 8 deletions internal/v1_1/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
57 changes: 56 additions & 1 deletion internal/v1_1/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func TestValidImports(t *testing.T) {
code := `
import "Alice"
import Bob from 0x0000000000000002
import Joe
access(all)
fun main(): Void {}
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions internal/v1_1/testdata/TestValidImports.golden
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit b7f5bb7

Please sign in to comment.