diff --git a/avalanche/network.go b/avalanche/network.go index 76f09df..c33fdf7 100644 --- a/avalanche/network.go +++ b/avalanche/network.go @@ -5,6 +5,28 @@ package avalanche type NetworkKind int64 +const ( + Undefined NetworkKind = iota + Mainnet + Fuji + Local + Devnet +) + +func (nk NetworkKind) String() string { + switch nk { + case Mainnet: + return "Mainnet" + case Fuji: + return "Fuji" + case Local: + return "Local Network" + case Devnet: + return "Devnet" + } + return "invalid network" +} + type Network struct { Kind NetworkKind @@ -12,3 +34,5 @@ type Network struct { Endpoint string } + +var UndefinedNetwork = Network{} diff --git a/go.mod b/go.mod index 05bc006..a6686a5 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,11 @@ go 1.21.9 toolchain go1.21.10 -require github.com/ava-labs/avalanchego v1.11.5 +require ( + github.com/ava-labs/avalanchego v1.11.5 + github.com/ava-labs/subnet-evm v0.6.4 + github.com/ava-labs/teleporter v1.0.0 +) require ( github.com/DataDog/zstd v1.5.2 // indirect diff --git a/multisig/multisig.go b/multisig/multisig.go index a7316b5..c7a9467 100644 --- a/multisig/multisig.go +++ b/multisig/multisig.go @@ -3,7 +3,7 @@ package multisig import ( - "github.com/ava-labs/avalanche-tooling-sdk-go/network" + "avalanche-tooling-sdk-go/avalanche" "github.com/ava-labs/avalanchego/ids" "github.com/ava-labs/avalanchego/vms/platformvm/txs" "github.com/ava-labs/avalanchego/wallet/subnet/primary" @@ -69,8 +69,8 @@ func (*PChainMultisig) GetKind() PChainTxKind { return Invalid } -func (*PChainMultisig) GetNetwork() (network.Network, error) { - return nil, nil +func (*PChainMultisig) GetNetwork() (avalanche.Network, error) { + return avalanche.UndefinedNetwork, nil } func (*PChainMultisig) GetSubnetID() (ids.ID, error) { diff --git a/network/multisig.go b/network/multisig.go new file mode 100644 index 0000000..a7316b5 --- /dev/null +++ b/network/multisig.go @@ -0,0 +1,82 @@ +// Copyright (C) 2022, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. +package multisig + +import ( + "github.com/ava-labs/avalanche-tooling-sdk-go/network" + "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/avalanchego/vms/platformvm/txs" + "github.com/ava-labs/avalanchego/wallet/subnet/primary" +) + +type PChainTxKind int + +const ( + Invalid = iota + CreateBlockchain + TransferSubnetOwnership +) + +type PChainMultisig struct { + _ *txs.Tx +} + +func New(_ *txs.Tx) *PChainMultisig { + return nil +} + +func (*PChainMultisig) ToBytes() ([]byte, error) { + return nil, nil +} + +func (*PChainMultisig) FromBytes(_ []byte) error { + return nil +} + +func (*PChainMultisig) ToFile(_ string) error { + return nil +} + +func (*PChainMultisig) FromFile(_ string) error { + return nil +} + +func (*PChainMultisig) Sign(_ *primary.Wallet) error { + return nil +} + +func (*PChainMultisig) Commit() error { + return nil +} + +func (*PChainMultisig) IsReadyToCommit() error { + return nil +} + +func (*PChainMultisig) GetRemainingSigners() ([]ids.ID, error) { + return nil, nil +} + +func (*PChainMultisig) GetAuthSigners() ([]ids.ID, error) { + return nil, nil +} + +func (*PChainMultisig) GetFeeSigners() ([]ids.ID, error) { + return nil, nil +} + +func (*PChainMultisig) GetKind() PChainTxKind { + return Invalid +} + +func (*PChainMultisig) GetNetwork() (network.Network, error) { + return nil, nil +} + +func (*PChainMultisig) GetSubnetID() (ids.ID, error) { + return ids.Empty, nil +} + +func (*PChainMultisig) GetSubnetOwners() ([]ids.ID, int, error) { + return nil, 0, nil +} diff --git a/subnet/subnet.go b/subnet/subnet.go index af08336..5abcea5 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -5,7 +5,15 @@ package subnet import ( "avalanche-tooling-sdk-go/avalanche" + "bytes" + "encoding/json" + "fmt" "github.com/ava-labs/avalanchego/ids" + "github.com/ava-labs/coreth/core" + "github.com/ava-labs/coreth/utils" + "github.com/ava-labs/subnet-evm/params" + "math/big" + "time" ) type SubnetParams struct { @@ -124,3 +132,133 @@ func New(client *avalanche.BaseApp, subnetParams *SubnetParams) *Subnet { } return &subnet } + +func createEvmGenesis( + subnetName string, + subnetEVMVersion string, + rpcVersion int, + subnetEVMChainID uint64, + subnetEVMTokenSymbol string, + useSubnetEVMDefaults bool, + useWarp bool, + teleporterInfo *teleporter.Info, +) ([]byte, error) { + genesis := core.Genesis{} + genesis.Timestamp = *utils.TimeToNewUint64(time.Now()) + + conf := params.SubnetEVMDefaultChainConfig + conf.NetworkUpgrades = params.NetworkUpgrades{} + + const ( + descriptorsState = "descriptors" + feeState = "fee" + airdropState = "airdrop" + precompilesState = "precompiles" + ) + + var ( + chainID *big.Int + tokenSymbol string + allocation core.GenesisAlloc + //direction statemachine.StateDirection + err error + ) + + //subnetEvmState, err := statemachine.NewStateMachine( + // []string{descriptorsState, feeState, airdropState, precompilesState}, + //) + //if err != nil { + // return nil, err + //} + //for subnetEvmState.Running() { + // switch subnetEvmState.CurrentState() { + // case descriptorsState: + // chainID, tokenSymbol, direction, err = getDescriptors( + // app, + // subnetEVMChainID, + // subnetEVMTokenSymbol, + // ) + // case feeState: + // *conf, direction, err = GetFeeConfig(*conf, app, useSubnetEVMDefaults) + // case airdropState: + // allocation, direction, err = getAllocation( + // app, + // subnetName, + // defaultEvmAirdropAmount, + // oneAvax, + // fmt.Sprintf("Amount to airdrop (in %s units)", tokenSymbol), + // useSubnetEVMDefaults, + // ) + // if teleporterInfo != nil { + // allocation = addTeleporterAddressToAllocations( + // allocation, + // teleporterInfo.FundedAddress, + // teleporterInfo.FundedBalance, + // ) + // } + // case precompilesState: + // *conf, direction, err = getPrecompiles(*conf, app, &genesis.Timestamp, useSubnetEVMDefaults, useWarp) + // if teleporterInfo != nil { + // *conf = addTeleporterAddressesToAllowLists( + // *conf, + // teleporterInfo.FundedAddress, + // teleporterInfo.MessengerDeployerAddress, + // teleporterInfo.RelayerAddress, + // ) + // } + // default: + // err = errors.New("invalid creation stage") + // } + // if err != nil { + // return nil, nil, err + // } + // subnetEvmState.NextState(direction) + //} + + if conf != nil && conf.GenesisPrecompiles[txallowlist.ConfigKey] != nil { + allowListCfg, ok := conf.GenesisPrecompiles[txallowlist.ConfigKey].(*txallowlist.Config) + if !ok { + return nil, fmt.Errorf( + "expected config of type txallowlist.AllowListConfig, but got %T", + allowListCfg, + ) + } + + if err := ensureAdminsHaveBalance( + allowListCfg.AdminAddresses, + allocation); err != nil { + return nil, nil, err + } + } + + conf.ChainID = chainID + + genesis.Alloc = allocation + genesis.Config = conf + genesis.Difficulty = Difficulty + genesis.GasLimit = conf.FeeConfig.GasLimit.Uint64() + + jsonBytes, err := genesis.MarshalJSON() + if err != nil { + return nil, err + } + + var prettyJSON bytes.Buffer + err = json.Indent(&prettyJSON, jsonBytes, "", " ") + if err != nil { + return nil, err + } + + //sc := &models.Sidecar{ + // Name: subnetName, + // VM: models.SubnetEvm, + // VMVersion: subnetEVMVersion, + // RPCVersion: rpcVersion, + // Subnet: subnetName, + // TokenSymbol: tokenSymbol, + // TokenName: tokenSymbol + " Token", + //} + + //return prettyJSON.Bytes(), sc, nil + return prettyJSON.Bytes(), nil +}