Documentation ¶
Index ¶
- Constants
- func AddAllUTXOs(ctx context.Context, utxos UTXOs, client UTXOClient, codec codec.Manager, ...) error
- type ChainUTXOs
- type UTXOClient
- type UTXOs
- type Wallet
- func NewWallet(p p.Wallet, x x.Wallet) Wallet
- func NewWalletFromURI(ctx context.Context, uri string, kc keychain.Keychain) (Wallet, error)
- func NewWalletWithOptions(w Wallet, options ...common.Option) Wallet
- func NewWalletWithState(uri string, pCTX p.Context, xCTX x.Context, utxos UTXOs, kc keychain.Keychain) Wallet
- func NewWalletWithTxs(ctx context.Context, uri string, kc keychain.Keychain, preloadTXs ...ids.ID) (Wallet, error)
- func NewWalletWithTxsAndState(uri string, pCTX p.Context, xCTX x.Context, utxos UTXOs, kc keychain.Keychain, ...) Wallet
Examples ¶
Constants ¶
const ( MainnetAPIURI = "https://api.avax.network" FujiAPIURI = "https://api.avax-test.network" LocalAPIURI = "http://localhost:9650" )
Variables ¶
This section is empty.
Functions ¶
func AddAllUTXOs ¶
func AddAllUTXOs( ctx context.Context, utxos UTXOs, client UTXOClient, codec codec.Manager, sourceChainID ids.ID, destinationChainID ids.ID, addrs []ids.ShortID, ) error
AddAllUTXOs fetches all the UTXOs referenced by [addresses] that were sent from [sourceChainID] to [destinationChainID] from the [client]. It then uses codec to parse the returned UTXOs and it adds them into [utxos]. If [ctx] expires, then the returned error will be immediately reported.
Types ¶
type ChainUTXOs ¶
type ChainUTXOs interface { AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *avax.UTXO) error RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*avax.UTXO, error) GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*avax.UTXO, error) }
func NewChainUTXOs ¶
func NewChainUTXOs(chainID ids.ID, utxos UTXOs) ChainUTXOs
type UTXOClient ¶
type UTXOs ¶
type UTXOs interface { AddUTXO(ctx context.Context, sourceChainID, destinationChainID ids.ID, utxo *avax.UTXO) error RemoveUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error UTXOs(ctx context.Context, sourceChainID, destinationChainID ids.ID) ([]*avax.UTXO, error) GetUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*avax.UTXO, error) }
func FetchState ¶
type Wallet ¶
Wallet provides chain wallets for the primary network.
Example ¶
ctx := context.Background() kc := secp256k1fx.NewKeychain(genesis.EWOQKey) // NewWalletFromURI fetches the available UTXOs owned by [kc] on the network // that [LocalAPIURI] is hosting. walletSyncStartTime := time.Now() wallet, err := NewWalletFromURI(ctx, LocalAPIURI, kc) if err != nil { log.Fatalf("failed to initialize wallet with: %s\n", err) return } log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime)) // Get the P-chain and the X-chain wallets pWallet := wallet.P() xWallet := wallet.X() // Pull out useful constants to use when issuing transactions. xChainID := xWallet.BlockchainID() owner := &secp256k1fx.OutputOwners{ Threshold: 1, Addrs: []ids.ShortID{ genesis.EWOQKey.PublicKey().Address(), }, } // Create a custom asset to send to the P-chain. createAssetStartTime := time.Now() createAssetTxID, err := xWallet.IssueCreateAssetTx( "RnM", "RNM", 9, map[uint32][]verify.State{ 0: { &secp256k1fx.TransferOutput{ Amt: 100 * units.MegaAvax, OutputOwners: *owner, }, }, }, ) if err != nil { log.Fatalf("failed to create new X-chain asset with: %s\n", err) return } log.Printf("created X-chain asset %s in %s\n", createAssetTxID, time.Since(createAssetStartTime)) // Send 100 MegaAvax to the P-chain. exportStartTime := time.Now() exportTxID, err := xWallet.IssueExportTx( constants.PlatformChainID, []*avax.TransferableOutput{ { Asset: avax.Asset{ ID: createAssetTxID, }, Out: &secp256k1fx.TransferOutput{ Amt: 100 * units.MegaAvax, OutputOwners: *owner, }, }, }, ) if err != nil { log.Fatalf("failed to issue X->P export transaction with: %s\n", err) return } log.Printf("issued X->P export %s in %s\n", exportTxID, time.Since(exportStartTime)) // Import the 100 MegaAvax from the X-chain into the P-chain. importStartTime := time.Now() importTxID, err := pWallet.IssueImportTx(xChainID, owner) if err != nil { log.Fatalf("failed to issue X->P import transaction with: %s\n", err) return } log.Printf("issued X->P import %s in %s\n", importTxID, time.Since(importStartTime)) createSubnetStartTime := time.Now() createSubnetTxID, err := pWallet.IssueCreateSubnetTx(owner) if err != nil { log.Fatalf("failed to issue create subnet transaction with: %s\n", err) return } log.Printf("issued create subnet transaction %s in %s\n", createSubnetTxID, time.Since(createSubnetStartTime)) transformSubnetStartTime := time.Now() transformSubnetTxID, err := pWallet.IssueTransformSubnetTx( createSubnetTxID, createAssetTxID, 50*units.MegaAvax, 100*units.MegaAvax, reward.PercentDenominator, reward.PercentDenominator, 1, 100*units.MegaAvax, time.Second, 365*24*time.Hour, 0, 1, 5, .80*reward.PercentDenominator, ) if err != nil { log.Fatalf("failed to issue transform subnet transaction with: %s\n", err) return } log.Printf("issued transform subnet transaction %s in %s\n", transformSubnetTxID, time.Since(transformSubnetStartTime)) addPermissionlessValidatorStartTime := time.Now() startTime := time.Now().Add(time.Minute) addSubnetValidatorTxID, err := pWallet.IssueAddPermissionlessValidatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: genesis.LocalConfig.InitialStakers[0].NodeID, Start: uint64(startTime.Unix()), End: uint64(startTime.Add(5 * time.Second).Unix()), Wght: 25 * units.MegaAvax, }, Subnet: createSubnetTxID, }, &signer.Empty{}, createAssetTxID, &secp256k1fx.OutputOwners{}, &secp256k1fx.OutputOwners{}, reward.PercentDenominator, ) if err != nil { log.Fatalf("failed to issue add subnet validator with: %s\n", err) return } log.Printf("issued add subnet validator transaction %s in %s\n", addSubnetValidatorTxID, time.Since(addPermissionlessValidatorStartTime)) addPermissionlessDelegatorStartTime := time.Now() addSubnetDelegatorTxID, err := pWallet.IssueAddPermissionlessDelegatorTx( &txs.SubnetValidator{ Validator: txs.Validator{ NodeID: genesis.LocalConfig.InitialStakers[0].NodeID, Start: uint64(startTime.Unix()), End: uint64(startTime.Add(5 * time.Second).Unix()), Wght: 25 * units.MegaAvax, }, Subnet: createSubnetTxID, }, createAssetTxID, &secp256k1fx.OutputOwners{}, ) if err != nil { log.Fatalf("failed to issue add subnet delegator with: %s\n", err) return } log.Printf("issued add subnet validator delegator %s in %s\n", addSubnetDelegatorTxID, time.Since(addPermissionlessDelegatorStartTime))
Output:
func NewWalletFromURI ¶
NewWalletFromURI returns a wallet that supports issuing transactions to the chains living in the primary network to a provided [uri].
On creation, the wallet attaches to the provided [uri] and fetches all UTXOs that reference any of the keys contained in [kc]. If the UTXOs are modified through an external issuance process, such as another instance of the wallet, the UTXOs may become out of sync.
The wallet manages all UTXOs locally, and performs all tx signing locally.
func NewWalletWithOptions ¶
Creates a Wallet with the given set of options
func NewWalletWithState ¶
func NewWalletWithState( uri string, pCTX p.Context, xCTX x.Context, utxos UTXOs, kc keychain.Keychain, ) Wallet
Creates a wallet with pre-fetched state.