forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GetBalance examples for the P-chain and X-chain wallets (ava-labs…
- Loading branch information
1 parent
4debc0e
commit 277d223
Showing
4 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
wallet/subnet/primary/examples/get-p-chain-balance/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
"github.com/ava-labs/avalanchego/utils/formatting/address" | ||
"github.com/ava-labs/avalanchego/utils/set" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/txs" | ||
"github.com/ava-labs/avalanchego/wallet/chain/p" | ||
"github.com/ava-labs/avalanchego/wallet/subnet/primary" | ||
) | ||
|
||
func main() { | ||
uri := primary.LocalAPIURI | ||
addrStr := "P-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u" | ||
|
||
addr, err := address.ParseToID(addrStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse address: %s\n", err) | ||
} | ||
|
||
addresses := set.Set[ids.ShortID]{} | ||
addresses.Add(addr) | ||
|
||
ctx := context.Background() | ||
|
||
fetchStartTime := time.Now() | ||
pCtx, _, utxos, err := primary.FetchState(ctx, uri, addresses) | ||
if err != nil { | ||
log.Fatalf("failed to fetch state: %s\n", err) | ||
} | ||
log.Printf("fetched state of %s in %s\n", addrStr, time.Since(fetchStartTime)) | ||
|
||
pUTXOs := primary.NewChainUTXOs(constants.PlatformChainID, utxos) | ||
pBackend := p.NewBackend(pCtx, pUTXOs, make(map[ids.ID]*txs.Tx)) | ||
pBuilder := p.NewBuilder(addresses, pBackend) | ||
|
||
currentBalances, err := pBuilder.GetBalance() | ||
if err != nil { | ||
log.Fatalf("failed to get the balance: %s\n", err) | ||
} | ||
|
||
avaxID := pCtx.AVAXAssetID() | ||
avaxBalance := currentBalances[avaxID] | ||
log.Printf("current AVAX balance of %s is %d nAVAX\n", addrStr, avaxBalance) | ||
} |
53 changes: 53 additions & 0 deletions
53
wallet/subnet/primary/examples/get-x-chain-balance/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/utils/formatting/address" | ||
"github.com/ava-labs/avalanchego/utils/set" | ||
"github.com/ava-labs/avalanchego/wallet/chain/x" | ||
"github.com/ava-labs/avalanchego/wallet/subnet/primary" | ||
) | ||
|
||
func main() { | ||
uri := primary.LocalAPIURI | ||
addrStr := "X-local18jma8ppw3nhx5r4ap8clazz0dps7rv5u00z96u" | ||
|
||
addr, err := address.ParseToID(addrStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse address: %s\n", err) | ||
} | ||
|
||
addresses := set.Set[ids.ShortID]{} | ||
addresses.Add(addr) | ||
|
||
ctx := context.Background() | ||
|
||
fetchStartTime := time.Now() | ||
_, xCtx, utxos, err := primary.FetchState(ctx, uri, addresses) | ||
if err != nil { | ||
log.Fatalf("failed to fetch state: %s\n", err) | ||
} | ||
log.Printf("fetched state of %s in %s\n", addrStr, time.Since(fetchStartTime)) | ||
|
||
xChainID := xCtx.BlockchainID() | ||
|
||
xUTXOs := primary.NewChainUTXOs(xChainID, utxos) | ||
xBackend := x.NewBackend(xCtx, xUTXOs) | ||
xBuilder := x.NewBuilder(addresses, xBackend) | ||
|
||
currentBalances, err := xBuilder.GetFTBalance() | ||
if err != nil { | ||
log.Fatalf("failed to get the balance: %s\n", err) | ||
} | ||
|
||
avaxID := xCtx.AVAXAssetID() | ||
avaxBalance := currentBalances[avaxID] | ||
log.Printf("current AVAX balance of %s is %d nAVAX\n", addrStr, avaxBalance) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters