Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TransformSubnetTx wallet example #2206

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions wallet/subnet/primary/examples/transform-subnet/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
"github.com/ava-labs/avalanchego/wallet/subnet/primary"
)

func main() {
key := genesis.EWOQKey
uri := primary.FujiAPIURI
kc := secp256k1fx.NewKeychain(key)
subnetIDStr := "2TkAkG8Tn522nMsKDec3CTHfvaSb4kGrgs2Ho39VvmaLysjayE"
assetIDStr := "tWt78T4XYdCSfqXoyhf9WGgbjf9i4GzqTwB9stje2bd6G5kSC"

ctx := context.Background()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather just pass context.Background() below but w/e


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

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

// MakeWallet fetches the available UTXOs owned by [kc] on the network that
// [uri] is hosting.
walletSyncStartTime := time.Now()
wallet, err := primary.MakeWallet(ctx, &primary.WalletConfig{
URI: uri,
AVAXKeychain: kc,
EthKeychain: kc,
PChainTxsToFetch: set.Of(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()

transformSubnetStartTime := time.Now()
transformSubnetTx, err := pWallet.IssueTransformSubnetTx(
subnetID,
assetID,
360*units.MegaAvax,
720*units.MegaAvax,
.10*reward.PercentDenominator,
.12*reward.PercentDenominator,
2*units.KiloAvax,
3*units.MegaAvax,
2*7*24*time.Hour,
365*24*time.Hour,
.02*reward.PercentDenominator,
25*units.Avax,
5,
.8*reward.PercentDenominator,
)
if err != nil {
log.Fatalf("failed to issue transform subnet transaction: %s\n", err)
}
log.Printf("issued transform subnet %s in %s\n", transformSubnetTx.ID(), time.Since(transformSubnetStartTime))
}
95 changes: 95 additions & 0 deletions wallet/subnet/primary/examples/x-chain-export/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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/genesis"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/vms/components/avax"
"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)
assetIDStr := "tWt78T4XYdCSfqXoyhf9WGgbjf9i4GzqTwB9stje2bd6G5kSC"
addr := key.Address()

ctx := context.Background()

customAssetID, err := ids.FromString(assetIDStr)
if err != nil {
log.Fatalf("failed to parse assetID: %s\n", err)
}

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

xWallet := wallet.X()
pWallet := wallet.P()

// Pull out useful constants to use when issuing transactions.
avaxAssetID := xWallet.AVAXAssetID()
xChainID := xWallet.BlockchainID()
owner := secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{
addr,
},
}

exportStartTime := time.Now()
exportTx, err := xWallet.IssueExportTx(
constants.PlatformChainID,
[]*avax.TransferableOutput{
{
Asset: avax.Asset{
ID: avaxAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: units.Avax,
OutputOwners: owner,
},
},
{
Asset: avax.Asset{
ID: customAssetID,
},
Out: &secp256k1fx.TransferOutput{
Amt: 360 * units.MegaAvax,
OutputOwners: owner,
},
},
},
)
if err != nil {
log.Fatalf("failed to issue export transaction: %s\n", err)
}
log.Printf("issued export %s in %s\n", exportTx.ID(), time.Since(exportStartTime))

importStartTime := time.Now()
importTx, err := pWallet.IssueImportTx(xChainID, &owner)
if err != nil {
log.Fatalf("failed to issue import transaction: %s\n", err)
}
log.Printf("issued import %s in %s\n", importTx.ID(), time.Since(importStartTime))
}