-
Notifications
You must be signed in to change notification settings - Fork 86
/
driver.go
93 lines (81 loc) · 2.41 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
// 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"
"sync"
"decred.org/dcrdex/dex"
)
var (
driversMtx sync.RWMutex
drivers = make(map[uint32]Driver)
)
// Driver is the interface required of all exchange wallets.
type Driver interface {
Setup(*WalletConfig, dex.Logger, dex.Network) (Wallet, error)
DecodeCoinID(coinID []byte) (string, error)
Info() *WalletInfo
}
// Register should be called by the init function of an asset's package.
func Register(assetID uint32, driver Driver) {
driversMtx.Lock()
defer driversMtx.Unlock()
if driver == nil {
panic("asset: Register driver is nil")
}
if _, dup := drivers[assetID]; dup {
panic(fmt.Sprint("asset: Register called twice for asset driver ", assetID))
}
drivers[assetID] = driver
}
// Setup sets up the asset, returning the exchange wallet.
func Setup(assetID uint32, cfg *WalletConfig, logger dex.Logger, network dex.Network) (Wallet, error) {
driversMtx.Lock()
drv, ok := drivers[assetID]
driversMtx.Unlock()
if !ok {
return nil, fmt.Errorf("asset: unknown asset driver %d", assetID)
}
return drv.Setup(cfg, logger, network)
}
// 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) {
driversMtx.Lock()
drv, ok := drivers[assetID]
driversMtx.Unlock()
if !ok {
return "", fmt.Errorf("asset: unknown asset driver %d", assetID)
}
return drv.DecodeCoinID(coinID)
}
// A registered asset is information about a supported asset.
type RegisteredAsset struct {
ID uint32
Symbol string
Info *WalletInfo
}
// Assets returns a list of information about supported assets.
func Assets() map[uint32]RegisteredAsset {
driversMtx.RLock()
defer driversMtx.RUnlock()
assets := make(map[uint32]RegisteredAsset, len(drivers))
for assetID, driver := range drivers {
assets[assetID] = RegisteredAsset{
ID: assetID,
Symbol: dex.BipIDSymbol(assetID),
Info: driver.Info(),
}
}
return assets
}
// Info returns the WalletInfo for the specified asset, if supported.
func Info(assetID uint32) (*WalletInfo, error) {
driversMtx.RLock()
drv, ok := drivers[assetID]
driversMtx.RUnlock()
if !ok {
return nil, fmt.Errorf("asset: unsupported asset %d", assetID)
}
return drv.Info(), nil
}