From b275b271a715fbc13d51ee8be269dc6b9d1d7140 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Tue, 21 May 2024 17:48:19 -0400 Subject: [PATCH 01/10] deploy subnet --- avalanche/app.go | 28 ++++++++++ avalanche/avalanche.go | 8 +++ avalanche/keychain.go | 18 +++++++ avalanche/network.go | 24 +++++++++ avalanche/vm.go | 29 ++++++++++ subnet/deploy_subnet.go | 116 ++++++++++++++++++++++++++++++++++++++++ subnet/subnet.go | 12 ++++- utils/common.go | 13 +++++ 8 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 avalanche/app.go create mode 100644 avalanche/avalanche.go create mode 100644 avalanche/keychain.go create mode 100644 avalanche/network.go create mode 100644 avalanche/vm.go create mode 100644 subnet/deploy_subnet.go create mode 100644 utils/common.go diff --git a/avalanche/app.go b/avalanche/app.go new file mode 100644 index 0000000..60fbb01 --- /dev/null +++ b/avalanche/app.go @@ -0,0 +1,28 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +import "github.com/ava-labs/avalanchego/utils/logging" + +type Avalanche struct { + Log logging.Logger + //baseDir string + //Conf *config.Config + //Prompt prompts.Prompter + //Apm *apm.APM + //ApmDir string + //Downloader Downloader +} + +func New() *Avalanche { + return &Avalanche{} +} + +func (app *Avalanche) Setup(log logging.Logger) { + //app.baseDir = baseDir + app.Log = log + //app.Conf = conf + //app.Prompt = prompt + //app.Downloader = downloader +} diff --git a/avalanche/avalanche.go b/avalanche/avalanche.go new file mode 100644 index 0000000..47770f3 --- /dev/null +++ b/avalanche/avalanche.go @@ -0,0 +1,8 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +const ( + SubnetEVMRepoName = "subnet-evm" +) diff --git a/avalanche/keychain.go b/avalanche/keychain.go new file mode 100644 index 0000000..fd1741c --- /dev/null +++ b/avalanche/keychain.go @@ -0,0 +1,18 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +import "github.com/ava-labs/avalanchego/utils/crypto/keychain" + +type Keychain struct { + Network Network + + Keychain keychain.Keychain + + Ledger keychain.Ledger + + UsesLedger bool + + LedgerIndices []uint32 +} diff --git a/avalanche/network.go b/avalanche/network.go new file mode 100644 index 0000000..fc18bea --- /dev/null +++ b/avalanche/network.go @@ -0,0 +1,24 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +type NetworkKind int64 + +const ( + Undefined NetworkKind = iota + Mainnet + Fuji + LocalNetwork + Devnet +) + +type Network struct { + Kind NetworkKind + + ID uint32 + + Endpoint string + + ClusterName string +} diff --git a/avalanche/vm.go b/avalanche/vm.go new file mode 100644 index 0000000..9d433b2 --- /dev/null +++ b/avalanche/vm.go @@ -0,0 +1,29 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +type VMType string + +const ( + SubnetEvm = "Subnet-EVM" + CustomVM = "Custom" +) + +func VMTypeFromString(s string) VMType { + switch s { + case SubnetEvm: + return SubnetEvm + default: + return CustomVM + } +} + +func (v VMType) RepoName() string { + switch v { + case SubnetEvm: + return SubnetEVMRepoName + default: + return "unknown" + } +} diff --git a/subnet/deploy_subnet.go b/subnet/deploy_subnet.go new file mode 100644 index 0000000..7bef7ef --- /dev/null +++ b/subnet/deploy_subnet.go @@ -0,0 +1,116 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package subnet + +import ( + "avalanche-tooling-sdk-go/avalanche" + "avalanche-tooling-sdk-go/utils" + "context" + "fmt" + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/utils/crypto/keychain" + "github.com/ava-labs/avalanchego/utils/formatting/address" + "github.com/ava-labs/avalanchego/utils/set" + "github.com/ava-labs/avalanchego/vms/platformvm/txs" + "github.com/ava-labs/avalanchego/vms/secp256k1fx" + "github.com/ava-labs/avalanchego/wallet/subnet/primary" + "github.com/ava-labs/avalanchego/wallet/subnet/primary/common" +) + +// createSubnetTx creates uncommitted createSubnet transaction +func createSubnetTx(subnet Subnet, wallet primary.Wallet) (*txs.Tx, error) { + addrs, err := address.ParseToIDs(subnet.ControlKeys) + if err != nil { + return nil, fmt.Errorf("failure parsing control keys: %w", err) + } + owners := &secp256k1fx.OutputOwners{ + Addrs: addrs, + Threshold: subnet.Threshold, + Locktime: 0, + } + unsignedTx, err := wallet.P().Builder().NewCreateSubnetTx( + owners, + ) + if err != nil { + return nil, fmt.Errorf("error building tx: %w", err) + } + tx := txs.Tx{Unsigned: unsignedTx} + if err := wallet.P().Signer().Sign(context.Background(), &tx); err != nil { + return nil, fmt.Errorf("error signing tx: %w", err) + } + return &tx, nil +} + +// createBlockchainTx creates uncommitted createBlockchain transaction +func createBlockchainTx(subnet Subnet, wallet primary.Wallet, network avalanche.Network, keyChain avalanche.Keychain) (*txs.Tx, error) { + wallet, err := loadCacheWallet(network, keyChain, wallet, subnet.SubnetID, subnet.TransferSubnetOwnershipTxID) + if err != nil { + return nil, err + } + fxIDs := make([]ids.ID, 0) + options := getMultisigTxOptions(keyChain.Keychain, subnet.SubnetAuthKeys) + // create tx + unsignedTx, err := wallet.P().Builder().NewCreateChainTx( + subnet.SubnetID, + subnet.Genesis, + subnet.VMID, + fxIDs, + subnet.Name, + options..., + ) + if err != nil { + return nil, fmt.Errorf("error building tx: %w", err) + } + tx := txs.Tx{Unsigned: unsignedTx} + // sign with current wallet + if err := wallet.P().Signer().Sign(context.Background(), &tx); err != nil { + return nil, fmt.Errorf("error signing tx: %w", err) + } + return &tx, nil +} + +func getMultisigTxOptions(keychain keychain.Keychain, subnetAuthKeys []ids.ShortID) []common.Option { + options := []common.Option{} + walletAddrs := keychain.Addresses().List() + changeAddr := walletAddrs[0] + // addrs to use for signing + customAddrsSet := set.Set[ids.ShortID]{} + customAddrsSet.Add(walletAddrs...) + customAddrsSet.Add(subnetAuthKeys...) + options = append(options, common.WithCustomAddresses(customAddrsSet)) + // set change to go to wallet addr (instead of any other subnet auth key) + changeOwner := &secp256k1fx.OutputOwners{ + Threshold: 1, + Addrs: []ids.ShortID{changeAddr}, + } + options = append(options, common.WithChangeOwner(changeOwner)) + return options +} + +func loadCacheWallet(network avalanche.Network, keyChain avalanche.Keychain, wallet primary.Wallet, preloadTxs ...ids.ID) (primary.Wallet, error) { + var err error + if wallet == nil { + wallet, err = loadWallet(network, keyChain, preloadTxs...) + } + return wallet, err +} + +func loadWallet(network avalanche.Network, keyChain avalanche.Keychain, preloadTxs ...ids.ID) (primary.Wallet, error) { + ctx := context.Background() + // filter out ids.Empty txs + filteredTxs := utils.Filter(preloadTxs, func(e ids.ID) bool { return e != ids.Empty }) + wallet, err := primary.MakeWallet( + ctx, + &primary.WalletConfig{ + URI: network.Endpoint, + AVAXKeychain: keyChain.Keychain, + EthKeychain: secp256k1fx.NewKeychain(), + PChainTxsToFetch: set.Of(filteredTxs...), + }, + ) + if err != nil { + return nil, err + } + return wallet, nil +} diff --git a/subnet/subnet.go b/subnet/subnet.go index bbdba05..271687d 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -21,6 +21,8 @@ type SubnetParams struct { // Custom VM parameters to use // Do not set CustomVM value if you are using Subnet-EVM CustomVM CustomVMParams + + Name string } type SubnetEVMParams struct { @@ -86,11 +88,13 @@ type CustomVMParams struct { } type Subnet struct { + Name string + Genesis []byte ControlKeys []string - SubnetAuthKeys []string + SubnetAuthKeys []ids.ShortID SubnetID ids.ID @@ -101,4 +105,10 @@ type Subnet struct { Threshold uint32 VMID ids.ID + + RPCVersion int + + TokenName string + + TokenSymbol string } diff --git a/utils/common.go b/utils/common.go new file mode 100644 index 0000000..fbe5340 --- /dev/null +++ b/utils/common.go @@ -0,0 +1,13 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. +package utils + +func Filter[T any](input []T, f func(T) bool) []T { + output := make([]T, 0, len(input)) + for _, e := range input { + if f(e) { + output = append(output, e) + } + } + return output +} From c75013d377cb80fc723b5c67ce1a5ef73168af77 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Tue, 21 May 2024 17:48:40 -0400 Subject: [PATCH 02/10] deploy subnet --- avalanche/network.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/avalanche/network.go b/avalanche/network.go index fc18bea..3c7a775 100644 --- a/avalanche/network.go +++ b/avalanche/network.go @@ -5,14 +5,6 @@ package avalanche type NetworkKind int64 -const ( - Undefined NetworkKind = iota - Mainnet - Fuji - LocalNetwork - Devnet -) - type Network struct { Kind NetworkKind From fb614e7d37b839badf24add287d552fd1dc8ff65 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Wed, 22 May 2024 11:57:10 -0400 Subject: [PATCH 03/10] remove app go --- avalanche/app.go | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 avalanche/app.go diff --git a/avalanche/app.go b/avalanche/app.go deleted file mode 100644 index 60fbb01..0000000 --- a/avalanche/app.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package avalanche - -import "github.com/ava-labs/avalanchego/utils/logging" - -type Avalanche struct { - Log logging.Logger - //baseDir string - //Conf *config.Config - //Prompt prompts.Prompter - //Apm *apm.APM - //ApmDir string - //Downloader Downloader -} - -func New() *Avalanche { - return &Avalanche{} -} - -func (app *Avalanche) Setup(log logging.Logger) { - //app.baseDir = baseDir - app.Log = log - //app.Conf = conf - //app.Prompt = prompt - //app.Downloader = downloader -} From 68b71703adac5c9877e10aa905bb75c249c3d267 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Wed, 22 May 2024 15:35:25 -0400 Subject: [PATCH 04/10] base app --- avalanche/avalanche.go | 13 ++++ avalanche/log.go | 145 +++++++++++++++++++++++++++++++++++++++++ subnet/subnet.go | 14 +++- subnet/subnet_test.go | 42 ++++++++++++ 4 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 avalanche/log.go create mode 100644 subnet/subnet_test.go diff --git a/avalanche/avalanche.go b/avalanche/avalanche.go index 47770f3..e90aee4 100644 --- a/avalanche/avalanche.go +++ b/avalanche/avalanche.go @@ -6,3 +6,16 @@ package avalanche const ( SubnetEVMRepoName = "subnet-evm" ) + +// Client provides client interface for Avalanche operations +type Client struct { + // The logger writer interface to write logging messages to. + Logger LeveledLoggerInterface +} + +func New() *Client { + client := &Client{ + Logger: DefaultLeveledLogger, + } + return client +} diff --git a/avalanche/log.go b/avalanche/log.go new file mode 100644 index 0000000..db44b8a --- /dev/null +++ b/avalanche/log.go @@ -0,0 +1,145 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package avalanche + +import ( + "fmt" + "io" + "os" +) + +// +// Public constants +// + +const ( + // LevelNull sets a logger to show no messages at all. + LevelNull Level = 0 + + // LevelError sets a logger to show error messages only. + LevelError Level = 1 + + // LevelWarn sets a logger to show warning messages or anything more + // severe. + LevelWarn Level = 2 + + // LevelInfo sets a logger to show informational messages or anything more + // severe. + LevelInfo Level = 3 + + // LevelDebug sets a logger to show informational messages or anything more + // severe. + LevelDebug Level = 4 +) + +// +// Public variables +// + +// DefaultLeveledLogger is the default logger that the library will use to log +// errors, warnings, and informational messages. +// +// LeveledLoggerInterface is implemented by LeveledLogger, and one can be +// initialized at the desired level of logging. LeveledLoggerInterface also +// provides out-of-the-box compatibility with a Logrus Logger, but may require +// a thin shim for use with other logging libraries that use less standard +// conventions like Zap. +// +// This Logger will be inherited by any backends created by default, but will +// be overridden if a backend is created with GetBackendWithConfig with a +// custom LeveledLogger set. +var DefaultLeveledLogger LeveledLoggerInterface = &LeveledLogger{ + Level: LevelError, +} + +// +// Public types +// + +// Level represents a logging level. +type Level uint32 + +// LeveledLogger is a leveled logger implementation. +// +// It prints warnings and errors to `os.Stderr` and other messages to +// `os.Stdout`. +type LeveledLogger struct { + // Level is the minimum logging level that will be emitted by this logger. + // + // For example, a Level set to LevelWarn will emit warnings and errors, but + // not informational or debug messages. + // + // Always set this with a constant like LevelWarn because the individual + // values are not guaranteed to be stable. + Level Level + + // Internal testing use only. + stderrOverride io.Writer + stdoutOverride io.Writer +} + +// Debugf logs a debug message using Printf conventions. +func (l *LeveledLogger) Debugf(format string, v ...interface{}) { + if l.Level >= LevelDebug { + fmt.Fprintf(l.stdout(), "[DEBUG] "+format+"\n", v...) + } +} + +// Errorf logs a warning message using Printf conventions. +func (l *LeveledLogger) Errorf(format string, v ...interface{}) { + // Infof logs a debug message using Printf conventions. + if l.Level >= LevelError { + fmt.Fprintf(l.stderr(), "[ERROR] "+format+"\n", v...) + } +} + +// Infof logs an informational message using Printf conventions. +func (l *LeveledLogger) Infof(format string, v ...interface{}) { + if l.Level >= LevelInfo { + fmt.Fprintf(l.stdout(), "[INFO] "+format+"\n", v...) + } +} + +// Warnf logs a warning message using Printf conventions. +func (l *LeveledLogger) Warnf(format string, v ...interface{}) { + if l.Level >= LevelWarn { + fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...) + } +} + +func (l *LeveledLogger) stderr() io.Writer { + if l.stderrOverride != nil { + return l.stderrOverride + } + + return os.Stderr +} + +func (l *LeveledLogger) stdout() io.Writer { + if l.stdoutOverride != nil { + return l.stdoutOverride + } + + return os.Stdout +} + +// LeveledLoggerInterface provides a basic leveled logging interface for +// printing debug, informational, warning, and error messages. +// +// It's implemented by LeveledLogger and also provides out-of-the-box +// compatibility with a Logrus Logger, but may require a thin shim for use with +// other logging libraries that you use less standard conventions like Zap. +type LeveledLoggerInterface interface { + // Debugf logs a debug message using Printf conventions. + Debugf(format string, v ...interface{}) + + // Errorf logs a warning message using Printf conventions. + Errorf(format string, v ...interface{}) + + // Infof logs an informational message using Printf conventions. + Infof(format string, v ...interface{}) + + // Warnf logs a warning message using Printf conventions. + Warnf(format string, v ...interface{}) +} diff --git a/subnet/subnet.go b/subnet/subnet.go index 271687d..e9813f8 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -3,7 +3,10 @@ package subnet -import "github.com/ava-labs/avalanchego/ids" +import ( + "avalanche-tooling-sdk-go/avalanche" + "github.com/ava-labs/avalanchego/ids" +) type SubnetParams struct { // File path of Genesis to use @@ -111,4 +114,13 @@ type Subnet struct { TokenName string TokenSymbol string + + Logger avalanche.LeveledLoggerInterface +} + +func New(client *avalanche.Client, subnetParams SubnetParams) Subnet { + subnet := Subnet{ + Logger: client.Logger, + } + return subnet } diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go new file mode 100644 index 0000000..9144f1f --- /dev/null +++ b/subnet/subnet_test.go @@ -0,0 +1,42 @@ +// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package subnet + +import ( + "avalanche-tooling-sdk-go/avalanche" + "context" + "fmt" + "github.com/ava-labs/avalanchego/vms/secp256k1fx" + "github.com/ava-labs/avalanchego/wallet/subnet/primary" + "testing" +) + +func TestSubnetDeploy(t *testing.T) { + client := avalanche.New() + subnetParams := SubnetParams{ + SubnetEVM: SubnetEVMParams{ + EvmChainID: 1234567, + EvmToken: "AVAX", + EvmDefaults: true, + UseLatestReleasedEvmVersion: true, + EnableWarp: true, + EnableTeleporter: true, + EnableRelayer: true, + }, + } + newSubnet := New(client, subnetParams) + ctx := context.Background() + wallet, _ := primary.MakeWallet( + ctx, + &primary.WalletConfig{ + URI: "", + AVAXKeychain: nil, + EthKeychain: secp256k1fx.NewKeychain(), + PChainTxsToFetch: nil, + }, + ) + //deploy Subnet returns tx ID and error + deploySubnetTx, _ := createSubnetTx(newSubnet, wallet) + fmt.Printf("deploySubnetTx %s", deploySubnetTx) +} From ad012d1a47405f0fb769a4fdef59e13e69af26f7 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Wed, 22 May 2024 15:39:30 -0400 Subject: [PATCH 05/10] use pointers --- subnet/deploy_subnet.go | 4 ++-- subnet/subnet.go | 4 ++-- subnet/subnet_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/subnet/deploy_subnet.go b/subnet/deploy_subnet.go index 7bef7ef..cef7818 100644 --- a/subnet/deploy_subnet.go +++ b/subnet/deploy_subnet.go @@ -19,7 +19,7 @@ import ( ) // createSubnetTx creates uncommitted createSubnet transaction -func createSubnetTx(subnet Subnet, wallet primary.Wallet) (*txs.Tx, error) { +func createSubnetTx(subnet *Subnet, wallet primary.Wallet) (*txs.Tx, error) { addrs, err := address.ParseToIDs(subnet.ControlKeys) if err != nil { return nil, fmt.Errorf("failure parsing control keys: %w", err) @@ -43,7 +43,7 @@ func createSubnetTx(subnet Subnet, wallet primary.Wallet) (*txs.Tx, error) { } // createBlockchainTx creates uncommitted createBlockchain transaction -func createBlockchainTx(subnet Subnet, wallet primary.Wallet, network avalanche.Network, keyChain avalanche.Keychain) (*txs.Tx, error) { +func createBlockchainTx(subnet *Subnet, wallet primary.Wallet, network avalanche.Network, keyChain avalanche.Keychain) (*txs.Tx, error) { wallet, err := loadCacheWallet(network, keyChain, wallet, subnet.SubnetID, subnet.TransferSubnetOwnershipTxID) if err != nil { return nil, err diff --git a/subnet/subnet.go b/subnet/subnet.go index e9813f8..94d0c3b 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -118,9 +118,9 @@ type Subnet struct { Logger avalanche.LeveledLoggerInterface } -func New(client *avalanche.Client, subnetParams SubnetParams) Subnet { +func New(client *avalanche.Client, subnetParams *SubnetParams) *Subnet { subnet := Subnet{ Logger: client.Logger, } - return subnet + return &subnet } diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go index 9144f1f..9d51a94 100644 --- a/subnet/subnet_test.go +++ b/subnet/subnet_test.go @@ -25,7 +25,7 @@ func TestSubnetDeploy(t *testing.T) { EnableRelayer: true, }, } - newSubnet := New(client, subnetParams) + newSubnet := New(client, &subnetParams) ctx := context.Background() wallet, _ := primary.MakeWallet( ctx, From 7ab201a6087e07e125a069abc68eb2474ae20012 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Wed, 22 May 2024 17:00:55 -0400 Subject: [PATCH 06/10] fix conflict --- subnet/deploy_subnet.go | 1 - utils/common.go | 13 ------------- 2 files changed, 14 deletions(-) delete mode 100644 utils/common.go diff --git a/subnet/deploy_subnet.go b/subnet/deploy_subnet.go index 456c949..bb4ce4e 100644 --- a/subnet/deploy_subnet.go +++ b/subnet/deploy_subnet.go @@ -9,7 +9,6 @@ import ( "avalanche-tooling-sdk-go/avalanche" - "avalanche-tooling-sdk-go/utils" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/utils/crypto/keychain" "github.com/ava-labs/avalanchego/utils/formatting/address" diff --git a/utils/common.go b/utils/common.go deleted file mode 100644 index fbe5340..0000000 --- a/utils/common.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. -package utils - -func Filter[T any](input []T, f func(T) bool) []T { - output := make([]T, 0, len(input)) - for _, e := range input { - if f(e) { - output = append(output, e) - } - } - return output -} From d54ae8a5a15434052809f720e72385bf28b2bdc3 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Thu, 23 May 2024 14:21:08 -0400 Subject: [PATCH 07/10] use receivers --- subnet/deploy_subnet.go | 22 +++++++++++----------- subnet/subnet_test.go | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/subnet/deploy_subnet.go b/subnet/deploy_subnet.go index bb4ce4e..66202b9 100644 --- a/subnet/deploy_subnet.go +++ b/subnet/deploy_subnet.go @@ -19,15 +19,15 @@ import ( "github.com/ava-labs/avalanchego/wallet/subnet/primary/common" ) -// createSubnetTx creates uncommitted createSubnet transaction -func createSubnetTx(subnet *Subnet, wallet primary.Wallet) (*txs.Tx, error) { - addrs, err := address.ParseToIDs(subnet.ControlKeys) +// CreateSubnetTx creates uncommitted createSubnet transaction +func (c *Subnet) CreateSubnetTx(wallet primary.Wallet) (*txs.Tx, error) { + addrs, err := address.ParseToIDs(c.ControlKeys) if err != nil { return nil, fmt.Errorf("failure parsing control keys: %w", err) } owners := &secp256k1fx.OutputOwners{ Addrs: addrs, - Threshold: subnet.Threshold, + Threshold: c.Threshold, Locktime: 0, } unsignedTx, err := wallet.P().Builder().NewCreateSubnetTx( @@ -43,17 +43,17 @@ func createSubnetTx(subnet *Subnet, wallet primary.Wallet) (*txs.Tx, error) { return &tx, nil } -// createBlockchainTx creates uncommitted createBlockchain transaction -func createBlockchainTx(subnet Subnet, wallet primary.Wallet, keyChain avalanche.Keychain) (*txs.Tx, error) { +// CreateBlockchainTx creates uncommitted createBlockchain transaction +func (c *Subnet) CreateBlockchainTx(wallet primary.Wallet, keyChain avalanche.Keychain) (*txs.Tx, error) { fxIDs := make([]ids.ID, 0) - options := getMultisigTxOptions(keyChain.Keychain, subnet.SubnetAuthKeys) + options := getMultisigTxOptions(keyChain.Keychain, c.SubnetAuthKeys) // create tx unsignedTx, err := wallet.P().Builder().NewCreateChainTx( - subnet.SubnetID, - subnet.Genesis, - subnet.VMID, + c.SubnetID, + c.Genesis, + c.VMID, fxIDs, - subnet.Name, + c.Name, options..., ) if err != nil { diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go index 9d51a94..6ebacac 100644 --- a/subnet/subnet_test.go +++ b/subnet/subnet_test.go @@ -37,6 +37,6 @@ func TestSubnetDeploy(t *testing.T) { }, ) //deploy Subnet returns tx ID and error - deploySubnetTx, _ := createSubnetTx(newSubnet, wallet) + deploySubnetTx, _ := newSubnet.CreateSubnetTx(wallet) fmt.Printf("deploySubnetTx %s", deploySubnetTx) } From f6eacf5faaaf26e09603b096f1c0fa6af8695bbf Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Thu, 23 May 2024 14:54:46 -0400 Subject: [PATCH 08/10] update logging in constructor --- avalanche/avalanche.go | 20 +++++++++++++------- subnet/subnet.go | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/avalanche/avalanche.go b/avalanche/avalanche.go index e6ed8bf..3f1d49b 100644 --- a/avalanche/avalanche.go +++ b/avalanche/avalanche.go @@ -7,15 +7,21 @@ const ( SubnetEVMRepoName = "subnet-evm" ) -// Client provides client interface for Avalanche operations -type Client struct { +// BaseApp provides a base type that implements the foundation of Avalanche SDK +// including Logger +type BaseApp struct { // The logger writer interface to write logging messages to. Logger LeveledLoggerInterface } -func New() *Client { - client := &Client{ - Logger: DefaultLeveledLogger, +// New creates a new base type of Avalanche SDK +// if logger is nil, default logger DefaultLeveledLogger will be used instead +func New(logger LeveledLoggerInterface) *BaseApp { + if logger == nil { + logger = DefaultLeveledLogger } - return client -} \ No newline at end of file + baseApp := &BaseApp{ + Logger: logger, + } + return baseApp +} diff --git a/subnet/subnet.go b/subnet/subnet.go index 94d0c3b..af08336 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -118,7 +118,7 @@ type Subnet struct { Logger avalanche.LeveledLoggerInterface } -func New(client *avalanche.Client, subnetParams *SubnetParams) *Subnet { +func New(client *avalanche.BaseApp, subnetParams *SubnetParams) *Subnet { subnet := Subnet{ Logger: client.Logger, } From 39587c24b6598c97195fcf7d45ae53d9bf9efc7c Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Thu, 23 May 2024 14:55:20 -0400 Subject: [PATCH 09/10] update logging in constructor --- subnet/subnet_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go index 6ebacac..198adbb 100644 --- a/subnet/subnet_test.go +++ b/subnet/subnet_test.go @@ -13,7 +13,7 @@ import ( ) func TestSubnetDeploy(t *testing.T) { - client := avalanche.New() + client := avalanche.New(avalanche.DefaultLeveledLogger) subnetParams := SubnetParams{ SubnetEVM: SubnetEVMParams{ EvmChainID: 1234567, From 90723d00948912a3ee91d0df75b2e970173ea145 Mon Sep 17 00:00:00 2001 From: Raymond Sukanto Date: Thu, 23 May 2024 15:02:20 -0400 Subject: [PATCH 10/10] update logging in constructor --- subnet/subnet_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go index 198adbb..56e70f0 100644 --- a/subnet/subnet_test.go +++ b/subnet/subnet_test.go @@ -13,7 +13,7 @@ import ( ) func TestSubnetDeploy(t *testing.T) { - client := avalanche.New(avalanche.DefaultLeveledLogger) + baseApp := avalanche.New(avalanche.DefaultLeveledLogger) subnetParams := SubnetParams{ SubnetEVM: SubnetEVMParams{ EvmChainID: 1234567, @@ -25,7 +25,7 @@ func TestSubnetDeploy(t *testing.T) { EnableRelayer: true, }, } - newSubnet := New(client, &subnetParams) + newSubnet := New(baseApp, &subnetParams) ctx := context.Background() wallet, _ := primary.MakeWallet( ctx,