primary

package
v1.0.0-test Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2024 License: BSD-3-Clause Imports: 23 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	MainnetAPIURI = "https://api.dione.network"
	TestnetAPIURI = "https://api.dione-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.

func NewChainUTXOs

func NewChainUTXOs(chainID ids.ID, utxos UTXOs) common.ChainUTXOs

Types

type DIONEState

type DIONEState struct {
	OClient omegavm.Client
	OCTX    o.Context
	AClient alpha.Client
	ACTX    a.Context
	DClient delta.Client
	DCTX    d.Context
	UTXOs   UTXOs
}

func FetchState

func FetchState(
	ctx context.Context,
	uri string,
	addrs set.Set[ids.ShortID],
) (
	*DIONEState,
	error,
)

type EthState

type EthState struct {
	Client   ethclient.Client
	Accounts map[common.Address]*d.Account
}

func FetchEthState

func FetchEthState(
	ctx context.Context,
	uri string,
	addrs set.Set[common.Address],
) (*EthState, error)

type UTXOClient

type UTXOClient interface {
	GetAtomicUTXOs(
		ctx context.Context,
		addrs []ids.ShortID,
		sourceChain string,
		limit uint32,
		startAddress ids.ShortID,
		startUTXOID ids.ID,
		options ...rpc.Option,
	) ([][]byte, ids.ShortID, ids.ID, error)
}

type UTXOs

type UTXOs interface {
	AddUTXO(ctx context.Context, sourceChainID, destinationChainID ids.ID, utxo *dione.UTXO) error
	RemoveUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error

	UTXOs(ctx context.Context, sourceChainID, destinationChainID ids.ID) ([]*dione.UTXO, error)
	GetUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*dione.UTXO, error)
}

func NewUTXOs

func NewUTXOs() UTXOs

type Wallet

type Wallet interface {
	O() o.Wallet
	A() a.Wallet
	D() d.Wallet
}

Wallet provides chain wallets for the primary network.

Example
ctx := context.Background()
kc := secp256k1fx.NewKeychain(genesis.EWOQKey)

// MakeWallet fetches the available UTXOs owned by [kc] on the network that
// [LocalAPIURI] is hosting.
walletSyncStartTime := time.Now()
wallet, err := MakeWallet(ctx, &WalletConfig{
	URI:           LocalAPIURI,
	DIONEKeychain: kc,
	EthKeychain:   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 O-chain and the A-chain wallets
oWallet := wallet.O()
aWallet := wallet.A()

// Pull out useful constants to use when issuing transactions.
aChainID := aWallet.BlockchainID()
owner := &secp256k1fx.OutputOwners{
	Threshold: 1,
	Addrs: []ids.ShortID{
		genesis.EWOQKey.PublicKey().Address(),
	},
}

// Create a custom asset to send to the O-chain.
createAssetStartTime := time.Now()
createAssetTx, err := aWallet.IssueCreateAssetTx(
	"RnM",
	"RNM",
	9,
	map[uint32][]verify.State{
		0: {
			&secp256k1fx.TransferOutput{
				Amt:          100 * units.MegaDione,
				OutputOwners: *owner,
			},
		},
	},
)
if err != nil {
	log.Fatalf("failed to create new A-chain asset with: %s\n", err)
	return
}
createAssetTxID := createAssetTx.ID()
log.Printf("created A-chain asset %s in %s\n", createAssetTxID, time.Since(createAssetStartTime))

// Send 100 MegaDione to the O-chain.
exportStartTime := time.Now()
exportTx, err := aWallet.IssueExportTx(
	constants.OmegaChainID,
	[]*dione.TransferableOutput{
		{
			Asset: dione.Asset{
				ID: createAssetTxID,
			},
			Out: &secp256k1fx.TransferOutput{
				Amt:          100 * units.MegaDione,
				OutputOwners: *owner,
			},
		},
	},
)
if err != nil {
	log.Fatalf("failed to issue A->O export transaction with: %s\n", err)
	return
}
exportTxID := exportTx.ID()
log.Printf("issued A->O export %s in %s\n", exportTxID, time.Since(exportStartTime))

// Import the 100 MegaDione from the A-chain into the P-chain.
importStartTime := time.Now()
importTx, err := oWallet.IssueImportTx(aChainID, owner)
if err != nil {
	log.Fatalf("failed to issue A->O import transaction with: %s\n", err)
	return
}
importTxID := importTx.ID()
log.Printf("issued A->O import %s in %s\n", importTxID, time.Since(importStartTime))

createSubnetStartTime := time.Now()
createSubnetTx, err := oWallet.IssueCreateSubnetTx(owner)
if err != nil {
	log.Fatalf("failed to issue create subnet transaction with: %s\n", err)
	return
}
createSubnetTxID := createSubnetTx.ID()
log.Printf("issued create subnet transaction %s in %s\n", createSubnetTxID, time.Since(createSubnetStartTime))

transformSubnetStartTime := time.Now()
transformSubnetTx, err := oWallet.IssueTransformSubnetTx(
	createSubnetTxID,
	createAssetTxID,
	50*units.MegaDione,
	100*units.MegaDione,
	reward.PercentDenominator,
	reward.PercentDenominator,
	1,
	100*units.MegaDione,
	time.Second,
	365*24*time.Hour,
	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
}
transformSubnetTxID := transformSubnetTx.ID()
log.Printf("issued transform subnet transaction %s in %s\n", transformSubnetTxID, time.Since(transformSubnetStartTime))

addPermissionlessValidatorStartTime := time.Now()
startTime := time.Now().Add(time.Minute)
addSubnetValidatorTx, err := oWallet.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.MegaDione,
		},
		Subnet: createSubnetTxID,
	},
	&signer.Empty{},
	createAssetTx.ID(),
	&secp256k1fx.OutputOwners{},
	&secp256k1fx.OutputOwners{},
	reward.PercentDenominator,
)
if err != nil {
	log.Fatalf("failed to issue add subnet validator with: %s\n", err)
	return
}
addSubnetValidatorTxID := addSubnetValidatorTx.ID()
log.Printf("issued add subnet validator transaction %s in %s\n", addSubnetValidatorTxID, time.Since(addPermissionlessValidatorStartTime))

addPermissionlessDelegatorStartTime := time.Now()
addSubnetDelegatorTx, err := oWallet.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.MegaDione,
		},
		Subnet: createSubnetTxID,
	},
	createAssetTxID,
	&secp256k1fx.OutputOwners{},
)
if err != nil {
	log.Fatalf("failed to issue add subnet delegator with: %s\n", err)
	return
}
addSubnetDelegatorTxID := addSubnetDelegatorTx.ID()
log.Printf("issued add subnet validator delegator %s in %s\n", addSubnetDelegatorTxID, time.Since(addPermissionlessDelegatorStartTime))
Output:

func MakeWallet

func MakeWallet(ctx context.Context, config *WalletConfig) (Wallet, error)

MakeWallet returns a wallet that supports issuing transactions to the chains living in the primary network.

On creation, the wallet attaches to the provided uri and fetches all UTXOs that reference any of the provided keys. 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 will also fetch all requested O-chain transactions.

The wallet manages all state locally, and performs all tx signing locally.

func NewWallet

func NewWallet(o o.Wallet, a a.Wallet, d d.Wallet) Wallet

Creates a new default wallet

func NewWalletWithOptions

func NewWalletWithOptions(w Wallet, options ...common.Option) Wallet

Creates a Wallet with the given set of options

type WalletConfig

type WalletConfig struct {
	// Base URI to use for all node requests.
	URI string // required
	// Keys to use for signing all transactions.
	DIONEKeychain keychain.Keychain // required
	EthKeychain   d.EthKeychain     // required
	// Set of O-chain transactions that the wallet should know about to be able
	// to generate transactions.
	OChainTxs map[ids.ID]*txs.Tx // optional
	// Set of O-chain transactions that the wallet should fetch to be able to
	// generate transactions.
	OChainTxsToFetch set.Set[ids.ID] // optional
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL