github.com/ava-labs/avalanchego@v1.11.11/wallet/chain/p/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 p
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/ava-labs/avalanchego/api/info"
    10  	"github.com/ava-labs/avalanchego/vms/avm"
    11  	"github.com/ava-labs/avalanchego/vms/platformvm"
    12  	"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
    13  	"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
    14  )
    15  
    16  // gasPriceMultiplier increases the gas price to support multiple transactions
    17  // to be issued.
    18  //
    19  // TODO: Handle this better. Either here or in the mempool.
    20  const gasPriceMultiplier = 2
    21  
    22  func NewContextFromURI(ctx context.Context, uri string) (*builder.Context, error) {
    23  	infoClient := info.NewClient(uri)
    24  	xChainClient := avm.NewClient(uri, "X")
    25  	pChainClient := platformvm.NewClient(uri)
    26  	return NewContextFromClients(ctx, infoClient, xChainClient, pChainClient)
    27  }
    28  
    29  func NewContextFromClients(
    30  	ctx context.Context,
    31  	infoClient info.Client,
    32  	xChainClient avm.Client,
    33  	pChainClient platformvm.Client,
    34  ) (*builder.Context, error) {
    35  	networkID, err := infoClient.GetNetworkID(ctx)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	asset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	dynamicFeeConfig, err := pChainClient.GetFeeConfig(ctx)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// TODO: After Etna is activated, assume the gas price is always non-zero.
    51  	if dynamicFeeConfig.MinPrice != 0 {
    52  		_, gasPrice, _, err := pChainClient.GetFeeState(ctx)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  
    57  		return &builder.Context{
    58  			NetworkID:         networkID,
    59  			AVAXAssetID:       asset.AssetID,
    60  			ComplexityWeights: dynamicFeeConfig.Weights,
    61  			GasPrice:          gasPriceMultiplier * gasPrice,
    62  		}, nil
    63  	}
    64  
    65  	staticFeeConfig, err := infoClient.GetTxFee(ctx)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return &builder.Context{
    71  		NetworkID:   networkID,
    72  		AVAXAssetID: asset.AssetID,
    73  		StaticFeeConfig: fee.StaticConfig{
    74  			TxFee:                         uint64(staticFeeConfig.TxFee),
    75  			CreateSubnetTxFee:             uint64(staticFeeConfig.CreateSubnetTxFee),
    76  			TransformSubnetTxFee:          uint64(staticFeeConfig.TransformSubnetTxFee),
    77  			CreateBlockchainTxFee:         uint64(staticFeeConfig.CreateBlockchainTxFee),
    78  			AddPrimaryNetworkValidatorFee: uint64(staticFeeConfig.AddPrimaryNetworkValidatorFee),
    79  			AddPrimaryNetworkDelegatorFee: uint64(staticFeeConfig.AddPrimaryNetworkDelegatorFee),
    80  			AddSubnetValidatorFee:         uint64(staticFeeConfig.AddSubnetValidatorFee),
    81  			AddSubnetDelegatorFee:         uint64(staticFeeConfig.AddSubnetDelegatorFee),
    82  		},
    83  	}, nil
    84  }