github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/txs/txstest/wallet.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package txstest
     5  
     6  import (
     7  	"context"
     8  	"math"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/ava-labs/avalanchego/ids"
    14  	"github.com/ava-labs/avalanchego/snow"
    15  	"github.com/ava-labs/avalanchego/utils/constants"
    16  	"github.com/ava-labs/avalanchego/vms/components/avax"
    17  	"github.com/ava-labs/avalanchego/vms/platformvm/config"
    18  	"github.com/ava-labs/avalanchego/vms/platformvm/fx"
    19  	"github.com/ava-labs/avalanchego/vms/platformvm/state"
    20  	"github.com/ava-labs/avalanchego/vms/platformvm/txs"
    21  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    22  	"github.com/ava-labs/avalanchego/wallet/chain/p/builder"
    23  	"github.com/ava-labs/avalanchego/wallet/chain/p/signer"
    24  	"github.com/ava-labs/avalanchego/wallet/chain/p/wallet"
    25  	"github.com/ava-labs/avalanchego/wallet/subnet/primary/common"
    26  )
    27  
    28  func NewWallet(
    29  	t testing.TB,
    30  	ctx *snow.Context,
    31  	config *config.Config,
    32  	state state.State,
    33  	kc *secp256k1fx.Keychain,
    34  	subnetIDs []ids.ID,
    35  	chainIDs []ids.ID,
    36  ) wallet.Wallet {
    37  	var (
    38  		require = require.New(t)
    39  		addrs   = kc.Addresses()
    40  		utxos   = common.NewUTXOs()
    41  	)
    42  
    43  	pChainUTXOs, err := avax.GetAllUTXOs(state, addrs)
    44  	require.NoError(err)
    45  
    46  	for _, utxo := range pChainUTXOs {
    47  		require.NoError(utxos.AddUTXO(
    48  			context.Background(),
    49  			constants.PlatformChainID,
    50  			constants.PlatformChainID,
    51  			utxo,
    52  		))
    53  	}
    54  
    55  	for _, chainID := range chainIDs {
    56  		remoteChainUTXOs, _, _, err := avax.GetAtomicUTXOs(
    57  			ctx.SharedMemory,
    58  			txs.Codec,
    59  			chainID,
    60  			addrs,
    61  			ids.ShortEmpty,
    62  			ids.Empty,
    63  			math.MaxInt,
    64  		)
    65  		require.NoError(err)
    66  
    67  		for _, utxo := range remoteChainUTXOs {
    68  			require.NoError(utxos.AddUTXO(
    69  				context.Background(),
    70  				chainID,
    71  				constants.PlatformChainID,
    72  				utxo,
    73  			))
    74  		}
    75  	}
    76  
    77  	owners := make(map[ids.ID]fx.Owner, len(subnetIDs))
    78  	for _, subnetID := range subnetIDs {
    79  		owner, err := state.GetSubnetOwner(subnetID)
    80  		require.NoError(err)
    81  		owners[subnetID] = owner
    82  	}
    83  
    84  	builderContext := newContext(ctx, config, state)
    85  	backend := wallet.NewBackend(
    86  		builderContext,
    87  		common.NewChainUTXOs(constants.PlatformChainID, utxos),
    88  		owners,
    89  	)
    90  	return wallet.New(
    91  		&client{
    92  			backend: backend,
    93  		},
    94  		builder.New(
    95  			addrs,
    96  			builderContext,
    97  			backend,
    98  		),
    99  		signer.New(
   100  			kc,
   101  			backend,
   102  		),
   103  	)
   104  }
   105  
   106  type client struct {
   107  	backend wallet.Backend
   108  }
   109  
   110  func (c *client) IssueTx(
   111  	tx *txs.Tx,
   112  	options ...common.Option,
   113  ) error {
   114  	ops := common.NewOptions(options)
   115  	if f := ops.PostIssuanceFunc(); f != nil {
   116  		txID := tx.ID()
   117  		f(txID)
   118  	}
   119  	ctx := ops.Context()
   120  	return c.backend.AcceptTx(ctx, tx)
   121  }