github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/cmd/burrow/commands/natives.go (about)

     1  // Copyright Monax Industries Limited
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package commands
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/hyperledger/burrow/execution/native"
    10  	cli "github.com/jawher/mow.cli"
    11  
    12  	"github.com/hyperledger/burrow/util/natives/templates"
    13  )
    14  
    15  // Dump native contracts
    16  func Natives(output Output) func(cmd *cli.Cmd) {
    17  	return func(cmd *cli.Cmd) {
    18  		contractsOpt := cmd.StringsOpt("c contracts", nil, "Contracts to generate")
    19  		cmd.Action = func() {
    20  			callables := native.MustDefaultNatives().Callables()
    21  			// Index of next contract
    22  			i := 1
    23  			for _, callable := range callables {
    24  				contract, ok := callable.(*native.Contract)
    25  				if !ok {
    26  					// For now we will omit loose functions (i.e. precompiles)
    27  					// They can't be called via Solidity interface contract anyway.
    28  					continue
    29  				}
    30  				if len(*contractsOpt) > 0 {
    31  					found := false
    32  					for _, c := range *contractsOpt {
    33  						if c == contract.Name {
    34  							found = true
    35  							break
    36  						}
    37  					}
    38  					if !found {
    39  						continue
    40  					}
    41  				}
    42  				solidity, err := templates.NewSolidityContract(contract).Solidity()
    43  				if err != nil {
    44  					fmt.Printf("Error generating solidity for contract %s: %s\n",
    45  						contract.Name, err)
    46  				}
    47  				fmt.Println(solidity)
    48  				if i < len(callables) {
    49  					// Two new lines between contracts as per Solidity style guide
    50  					// (the template gives us 1 trailing new line)
    51  					fmt.Println()
    52  				}
    53  				i++
    54  			}
    55  		}
    56  	}
    57  }