github.com/MetalBlockchain/metalgo@v1.11.9/wallet/chain/p/builder/context.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package builder
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/api/info"
    10  	"github.com/MetalBlockchain/metalgo/ids"
    11  	"github.com/MetalBlockchain/metalgo/snow"
    12  	"github.com/MetalBlockchain/metalgo/utils/constants"
    13  	"github.com/MetalBlockchain/metalgo/utils/logging"
    14  	"github.com/MetalBlockchain/metalgo/vms/avm"
    15  )
    16  
    17  const Alias = "P"
    18  
    19  type Context struct {
    20  	NetworkID                     uint32
    21  	AVAXAssetID                   ids.ID
    22  	BaseTxFee                     uint64
    23  	CreateSubnetTxFee             uint64
    24  	TransformSubnetTxFee          uint64
    25  	CreateBlockchainTxFee         uint64
    26  	AddPrimaryNetworkValidatorFee uint64
    27  	AddPrimaryNetworkDelegatorFee uint64
    28  	AddSubnetValidatorFee         uint64
    29  	AddSubnetDelegatorFee         uint64
    30  }
    31  
    32  func NewContextFromURI(ctx context.Context, uri string) (*Context, error) {
    33  	infoClient := info.NewClient(uri)
    34  	xChainClient := avm.NewClient(uri, "X")
    35  	return NewContextFromClients(ctx, infoClient, xChainClient)
    36  }
    37  
    38  func NewContextFromClients(
    39  	ctx context.Context,
    40  	infoClient info.Client,
    41  	xChainClient avm.Client,
    42  ) (*Context, error) {
    43  	networkID, err := infoClient.GetNetworkID(ctx)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	asset, err := xChainClient.GetAssetDescription(ctx, "METAL")
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	txFees, err := infoClient.GetTxFee(ctx)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	return &Context{
    59  		NetworkID:                     networkID,
    60  		AVAXAssetID:                   asset.AssetID,
    61  		BaseTxFee:                     uint64(txFees.TxFee),
    62  		CreateSubnetTxFee:             uint64(txFees.CreateSubnetTxFee),
    63  		TransformSubnetTxFee:          uint64(txFees.TransformSubnetTxFee),
    64  		CreateBlockchainTxFee:         uint64(txFees.CreateBlockchainTxFee),
    65  		AddPrimaryNetworkValidatorFee: uint64(txFees.AddPrimaryNetworkValidatorFee),
    66  		AddPrimaryNetworkDelegatorFee: uint64(txFees.AddPrimaryNetworkDelegatorFee),
    67  		AddSubnetValidatorFee:         uint64(txFees.AddSubnetValidatorFee),
    68  		AddSubnetDelegatorFee:         uint64(txFees.AddSubnetDelegatorFee),
    69  	}, nil
    70  }
    71  
    72  func NewSnowContext(networkID uint32, avaxAssetID ids.ID) (*snow.Context, error) {
    73  	lookup := ids.NewAliaser()
    74  	return &snow.Context{
    75  		NetworkID:   networkID,
    76  		SubnetID:    constants.PrimaryNetworkID,
    77  		ChainID:     constants.PlatformChainID,
    78  		AVAXAssetID: avaxAssetID,
    79  		Log:         logging.NoLog{},
    80  		BCLookup:    lookup,
    81  	}, lookup.Alias(constants.PlatformChainID, Alias)
    82  }