-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6339 from onflow/bastian/add-util-system-addresse…
…s-command [Util] Add a command to print all system addresses
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package addresses | ||
|
||
import ( | ||
"bytes" | ||
"sort" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/onflow/flow-go/fvm/systemcontracts" | ||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
var ( | ||
flagChain string | ||
flagSeparator string | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "system-addresses", | ||
Short: "print addresses of system contracts", | ||
Run: run, | ||
} | ||
|
||
func init() { | ||
Cmd.Flags().StringVar(&flagChain, "chain", "", "Chain name") | ||
_ = Cmd.MarkFlagRequired("chain") | ||
|
||
Cmd.Flags().StringVar(&flagSeparator, "separator", ",", "Separator to use between addresses") | ||
} | ||
|
||
func run(*cobra.Command, []string) { | ||
chainID := flow.ChainID(flagChain) | ||
// validate | ||
_ = chainID.Chain() | ||
|
||
systemContracts := systemcontracts.SystemContractsForChain(chainID) | ||
|
||
addressSet := map[flow.Address]struct{}{} | ||
for _, contract := range systemContracts.All() { | ||
addressSet[contract.Address] = struct{}{} | ||
} | ||
|
||
addresses := make([]flow.Address, 0, len(addressSet)) | ||
for address := range addressSet { | ||
addresses = append(addresses, address) | ||
} | ||
|
||
sort.Slice(addresses, func(i, j int) bool { | ||
a := addresses[i] | ||
b := addresses[j] | ||
return bytes.Compare(a[:], b[:]) < 0 | ||
}) | ||
|
||
for i, address := range addresses { | ||
str := address.Hex() | ||
|
||
if i > 0 { | ||
print(flagSeparator) | ||
} | ||
print(str) | ||
} | ||
} |