-
Notifications
You must be signed in to change notification settings - Fork 646
/
context.go
83 lines (68 loc) · 1.55 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package c
import (
stdcontext "context"
"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/avm"
)
var _ Context = (*context)(nil)
type Context interface {
NetworkID() uint32
BlockchainID() ids.ID
AVAXAssetID() ids.ID
}
type context struct {
networkID uint32
blockchainID ids.ID
avaxAssetID ids.ID
}
func NewContextFromURI(ctx stdcontext.Context, uri string) (Context, error) {
infoClient := info.NewClient(uri)
xChainClient := avm.NewClient(uri, "X")
return NewContextFromClients(ctx, infoClient, xChainClient)
}
func NewContextFromClients(
ctx stdcontext.Context,
infoClient info.Client,
xChainClient avm.Client,
) (Context, error) {
networkID, err := infoClient.GetNetworkID(ctx)
if err != nil {
return nil, err
}
chainID, err := infoClient.GetBlockchainID(ctx, "C")
if err != nil {
return nil, err
}
asset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
if err != nil {
return nil, err
}
return NewContext(
networkID,
chainID,
asset.AssetID,
), nil
}
func NewContext(
networkID uint32,
blockchainID ids.ID,
avaxAssetID ids.ID,
) Context {
return &context{
networkID: networkID,
blockchainID: blockchainID,
avaxAssetID: avaxAssetID,
}
}
func (c *context) NetworkID() uint32 {
return c.networkID
}
func (c *context) BlockchainID() ids.ID {
return c.blockchainID
}
func (c *context) AVAXAssetID() ids.ID {
return c.avaxAssetID
}