Skip to content

Commit

Permalink
Add more wallet examples (#2382)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Dec 22, 2022
1 parent 91c2dbe commit b226716
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"log"
"time"

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/validator"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"
startTime := time.Now().Add(time.Minute)
duration := 2 * 7 * 24 * time.Hour // 2 weeks
weight := units.Schmeckle

subnetID, err := ids.FromString(subnetIDStr)
if err != nil {
log.Fatalf("failed to parse subnet ID: %s\n", err)
}

ctx := context.Background()
infoClient := info.NewClient(uri)

nodeInfoStartTime := time.Now()
nodeID, _, err := infoClient.GetNodeID(ctx)
if err != nil {
log.Fatalf("failed to fetch node IDs: %s\n", err)
}
log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime))

// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
// that [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.NewWalletWithTxs(ctx, uri, kc, subnetID)
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()

addValidatorStartTime := time.Now()
addValidatorTxID, err := pWallet.IssueAddSubnetValidatorTx(&validator.SubnetValidator{
Validator: validator.Validator{
NodeID: nodeID,
Start: uint64(startTime.Unix()),
End: uint64(startTime.Add(duration).Unix()),
Wght: weight,
},
Subnet: subnetID,
})
if err != nil {
log.Fatalf("failed to issue add subnet validator transaction: %s\n", err)
}
log.Printf("added new subnet validator %s to %s with %s in %s\n", nodeID, subnetID, addValidatorTxID, time.Since(addValidatorStartTime))
}
79 changes: 79 additions & 0 deletions wallet/subnet/primary/examples/add-primary-validator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"log"
"time"

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/validator"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
startTime := time.Now().Add(time.Minute)
duration := 3 * 7 * 24 * time.Hour // 3 weeks
weight := 2_000 * units.Avax
validatorRewardAddr := key.Address()
delegatorRewardAddr := key.Address()
delegationFee := uint32(reward.PercentDenominator / 2) // 50%

ctx := context.Background()
infoClient := info.NewClient(uri)

nodeInfoStartTime := time.Now()
nodeID, nodePOP, err := infoClient.GetNodeID(ctx)
if err != nil {
log.Fatalf("failed to fetch node IDs: %s\n", err)
}
log.Printf("fetched node ID %s in %s\n", nodeID, time.Since(nodeInfoStartTime))

// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
// that [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.NewWalletFromURI(ctx, uri, kc)
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()
avaxAssetID := pWallet.AVAXAssetID()

addValidatorStartTime := time.Now()
addValidatorTxID, err := pWallet.IssueAddPermissionlessValidatorTx(
&validator.SubnetValidator{Validator: validator.Validator{
NodeID: nodeID,
Start: uint64(startTime.Unix()),
End: uint64(startTime.Add(duration).Unix()),
Wght: weight,
}},
nodePOP,
avaxAssetID,
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{validatorRewardAddr},
},
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{delegatorRewardAddr},
},
delegationFee,
)
if err != nil {
log.Fatalf("failed to issue add permissionless validator transaction: %s\n", err)
}
log.Printf("added new primary network validator %s with %s in %s\n", nodeID, addValidatorTxID, time.Since(addValidatorStartTime))
}
63 changes: 63 additions & 0 deletions wallet/subnet/primary/examples/create-chain/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"encoding/hex"
"log"
"time"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
subnetIDStr := "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"
genesisHex := "00000000000000000000000000017b5490493f8a2fff444ac8b54e27b3339d7c60dcffffffffffffffff"
vmID := ids.ID{'x', 's', 'v', 'm'}
name := "let there"

subnetID, err := ids.FromString(subnetIDStr)
if err != nil {
log.Fatalf("failed to parse subnet ID: %s\n", err)
}

genesisBytes, err := hex.DecodeString(genesisHex)
if err != nil {
log.Fatalf("failed to parse genesis bytes: %s\n", err)
}

ctx := context.Background()

// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
// that [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.NewWalletWithTxs(ctx, uri, kc, subnetID)
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()

createChainStartTime := time.Now()
createChainTxID, err := pWallet.IssueCreateChainTx(
subnetID,
genesisBytes,
vmID,
nil,
name,
)
if err != nil {
log.Fatalf("failed to issue create chain transaction: %s\n", err)
}
log.Printf("created new chain %s in %s\n", createChainTxID, time.Since(createChainStartTime))
}
51 changes: 51 additions & 0 deletions wallet/subnet/primary/examples/create-subnet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package main

import (
"context"
"log"
"time"

"github.com/ava-labs/avalanchego/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.LocalAPIURI
kc := secp256k1fx.NewKeychain(key)
subnetOwner := key.Address()

ctx := context.Background()

// NewWalletFromURI fetches the available UTXOs owned by [kc] on the network
// that [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.NewWalletFromURI(ctx, uri, kc)
if err != nil {
log.Fatalf("failed to initialize wallet: %s\n", err)
}
log.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain wallet
pWallet := wallet.P()

// Pull out useful constants to use when issuing transactions.
owner := &secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
subnetOwner,
},
}

createSubnetStartTime := time.Now()
createSubnetTxID, err := pWallet.IssueCreateSubnetTx(owner)
if err != nil {
log.Fatalf("failed to issue create subnet transaction: %s\n", err)
}
log.Printf("created new subnet %s in %s\n", createSubnetTxID, time.Since(createSubnetStartTime))
}

0 comments on commit b226716

Please sign in to comment.