-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathdriver.go
217 lines (195 loc) · 6.22 KB
/
driver.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package asset
import (
"fmt"
"decred.org/dcrdex/dex"
)
var (
drivers = make(map[uint32]Driver)
tokens = make(map[uint32]TokenDriver)
childTokens = make(map[uint32]map[uint32]*dex.Token)
)
// driverBase defines a base set of driver methods common to both base-chain and
// degenerate assets.
type driverBase interface {
DecodeCoinID(coinID []byte) (string, error)
// Version returns the Backend's version number, which is used to signal
// when major changes are made to internal details such as coin ID encoding
// and contract structure that must be common to a client's.
Version() uint32
// UnitInfo returns the dex.UnitInfo for the asset.
UnitInfo() dex.UnitInfo
// Name is the name for the asset.
Name() string
}
// minValuer can be implemented by assets with minimum dust sizes that vary
// with fee rate. If an asset driver does not implement minValuer, the min
// value is assumed to be 1 atom for both bond and lot sizes.
type minValuer interface {
MinBondSize(maxFeeRate uint64) uint64
MinLotSize(maxFeeRate uint64) uint64
}
// Driver is the interface required of all base chain assets.
type Driver interface {
driverBase
// Setup should create a Backend, but not start the backend connection.
Setup(*BackendConfig) (Backend, error)
}
// TokenDriver is the interface required of all token assets.
type TokenDriver interface {
driverBase
TokenInfo() *dex.Token
}
func baseDriver(assetID uint32) (driverBase, bool) {
if drv, found := drivers[assetID]; found {
return drv, true
}
if drv, found := tokens[assetID]; found {
return drv, true
}
return nil, false
}
// DecodeCoinID creates a human-readable representation of a coin ID for a named
// asset with a corresponding driver registered with this package.
func DecodeCoinID(assetID uint32, coinID []byte) (string, error) {
drv, ok := baseDriver(assetID)
if !ok {
return "", fmt.Errorf("unknown asset driver %d", assetID)
}
return drv.DecodeCoinID(coinID)
}
// asset with a corresponding driver registered with this package.
func UnitInfo(assetID uint32) (dex.UnitInfo, error) {
drv, ok := baseDriver(assetID)
if !ok {
return dex.UnitInfo{}, fmt.Errorf("unknown asset %d (%q) in UnitInfo", assetID, dex.BipIDSymbol(assetID))
}
return drv.UnitInfo(), nil
}
// Register should be called by the init function of an asset's package.
func Register(assetID uint32, drv Driver) {
if drv == nil {
panic("asset: Register driver is nil")
}
if _, ok := baseDriver(assetID); ok {
panic(fmt.Sprintf("asset: Register called twice for asset driver %d", assetID))
}
if drv.UnitInfo().Conventional.ConversionFactor == 0 {
panic(fmt.Sprintf("asset: Driver registered with unit conversion factor = 0: %q", assetID))
}
drivers[assetID] = drv
}
// RegisterToken is called to register a token. The parent asset should be
// registered first.
func RegisterToken(assetID uint32, drv TokenDriver) {
if drv == nil {
panic("asset: Register driver is nil")
}
if _, ok := tokens[assetID]; ok {
panic(fmt.Sprintf("asset: RegisterToken called twice for asset driver %d", assetID))
}
token := drv.TokenInfo()
if token == nil {
panic(fmt.Sprintf("nil *Token for asset %d", assetID))
}
if _, exists := drivers[token.ParentID]; !exists {
panic(fmt.Sprintf("no parent (%d) registered for %s", token.ParentID, token.Name))
}
if drv.UnitInfo().Conventional.ConversionFactor == 0 {
panic(fmt.Sprintf("asset: TokenDriver registered with unit conversion factor = 0: %q", assetID))
}
children := childTokens[token.ParentID]
if children == nil {
children = make(map[uint32]*dex.Token, 1)
childTokens[token.ParentID] = children
}
children[assetID] = token
tokens[assetID] = drv
}
type BackendConfig struct {
AssetID uint32
ConfigPath string
Logger dex.Logger
Net dex.Network
RelayAddr string
}
// Setup sets up the named asset. The RPC connection parameters are obtained
// from the asset's configuration file located at configPath. Setup is only
// called for base chain assets, not tokens.
func Setup(cfg *BackendConfig) (Backend, error) {
drv, ok := drivers[cfg.AssetID]
if !ok {
return nil, fmt.Errorf("asset: unknown asset driver %d", cfg.AssetID)
}
return drv.Setup(cfg)
}
// Version retrieves the version of the named asset's Backend implementation.
func Version(assetID uint32) (uint32, error) {
drv, ok := baseDriver(assetID)
if !ok {
return 0, fmt.Errorf("asset: unknown asset driver %d", assetID)
}
return drv.Version(), nil
}
// IsToken checks if the asset ID is for a token and returns the token's parent
// ID.
func IsToken(assetID uint32) (is bool, parentID uint32) {
token, is := tokens[assetID]
if !is {
return
}
return true, token.TokenInfo().ParentID
}
// Tokens returns the child tokens registered for a base chain asset.
func Tokens(assetID uint32) map[uint32]*dex.Token {
m := make(map[uint32]*dex.Token, len(childTokens[assetID]))
for k, v := range childTokens[assetID] {
m[k] = v
}
return m
}
// Minimums returns the minimimum lot size and bond size for a registered asset.
func Minimums(assetID uint32, maxFeeRate uint64) (minLotSize, minBondSize uint64, found bool) {
baseChainID := assetID
if token, is := tokens[assetID]; is {
baseChainID = token.TokenInfo().ParentID
}
drv, found := drivers[baseChainID]
if !found {
return 0, 0, false
}
m, is := drv.(minValuer)
if !is {
return 1, 1, true
}
return m.MinLotSize(maxFeeRate), m.MinBondSize(maxFeeRate), true
}
// RegisteredAsset is information about a registered asset.
type RegisteredAsset struct {
AssetID uint32
Symbol string
Name string
UnitInfo dex.UnitInfo
}
// Assets returns a information about registered assets.
func Assets() []*RegisteredAsset {
assets := make([]*RegisteredAsset, 0, len(drivers)+len(tokens))
for assetID, drv := range drivers {
assets = append(assets, &RegisteredAsset{
AssetID: assetID,
Symbol: dex.BipIDSymbol(assetID),
Name: drv.Name(),
UnitInfo: drv.UnitInfo(),
})
}
for assetID, drv := range tokens {
assets = append(assets, &RegisteredAsset{
AssetID: assetID,
Symbol: dex.BipIDSymbol(assetID),
Name: drv.Name(),
UnitInfo: drv.UnitInfo(),
})
}
return assets
}