-
Notifications
You must be signed in to change notification settings - Fork 647
/
context.go
106 lines (88 loc) · 2.05 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package x
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
BaseTxFee() uint64
CreateAssetTxFee() uint64
}
type context struct {
networkID uint32
blockchainID ids.ID
avaxAssetID ids.ID
baseTxFee uint64
createAssetTxFee uint64
}
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, "X")
if err != nil {
return nil, err
}
asset, err := xChainClient.GetAssetDescription(ctx, "AVAX")
if err != nil {
return nil, err
}
txFees, err := infoClient.GetTxFee(ctx)
if err != nil {
return nil, err
}
return NewContext(
networkID,
chainID,
asset.AssetID,
uint64(txFees.TxFee),
uint64(txFees.CreateAssetTxFee),
), nil
}
func NewContext(
networkID uint32,
blockchainID ids.ID,
avaxAssetID ids.ID,
baseTxFee uint64,
createAssetTxFee uint64,
) Context {
return &context{
networkID: networkID,
blockchainID: blockchainID,
avaxAssetID: avaxAssetID,
baseTxFee: baseTxFee,
createAssetTxFee: createAssetTxFee,
}
}
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
}
func (c *context) BaseTxFee() uint64 {
return c.baseTxFee
}
func (c *context) CreateAssetTxFee() uint64 {
return c.createAssetTxFee
}