forked from blocktree/openwallet
/
assets_manager.go
117 lines (101 loc) · 3.42 KB
/
assets_manager.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
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package openw
import (
"fmt"
"github.com/astaxie/beego/config"
"github.com/blocktree/openwallet/v2/log"
"github.com/blocktree/openwallet/v2/openwallet"
"strings"
)
//type AssetsManager interface {
// openwallet.AssetsAdapter
//
// //ImportWatchOnlyAddress 导入观测地址
// ImportWatchOnlyAddress(address ...*openwallet.Address) error
//
// //GetAddressWithBalance 获取多个地址余额,使用查账户和单地址
// GetAddressWithBalance(address ...*openwallet.Address) error
//}
//注册钱包管理工具
func initAssetAdapter() {
//注册钱包管理工具
log.Notice("Wallet Manager Load Successfully.")
//RegAssets(qtum.Symbol, qtum.NewWalletManager())
//RegAssets(nebulasio.Symbol, nebulasio.NewWalletManager())
//RegAssets(ontology.Symbol, ontology.NewWalletManager())
//RegAssets(virtualeconomy.Symbol, virtualeconomy.NewWalletManager())
//RegAssets(eosio.Symbol, eosio.NewWalletManager())
}
//区块链适配器注册组
var assetsAdapterManagers = make(map[string]interface{})
// RegAssets 注册资产
// @param name 资产别名
// @param manager 资产适配器或管理器
// @param config 加载配置
// 资产适配器实现了openwallet.AssetsConfig,可以传入配置接口完成预加载配置
// usage:
// RegAssets(cardano.Symbol, &cardano.WalletManager{}, c)
// RegAssets(bytom.Symbol, &bytom.WalletManager{}, c)
func RegAssets(name string, manager interface{}, config ...config.Configer) {
name = strings.ToUpper(name)
if manager == nil {
panic("assets: Register adapter is nil")
}
if _, ok := assetsAdapterManagers[name]; ok {
log.Error("assets: Register called twice for adapter ", name)
return
}
assetsAdapterManagers[name] = manager
//如果有配置则加载所有配置
if ac, ok := manager.(openwallet.AssetsConfig); ok && config != nil {
for _, c := range config {
ac.LoadAssetsConfig(c)
}
}
}
// GetAssets 根据币种类型获取已注册的管理者
func GetAssets(symbol string) interface{} {
symbol = strings.ToUpper(symbol)
manager, ok := assetsAdapterManagers[symbol]
if !ok {
return nil
}
return manager
}
// GetSymbolInfo 获取资产的币种信息
func GetSymbolInfo(symbol string) (openwallet.SymbolInfo, error) {
adapter := GetAssets(symbol)
if adapter == nil {
return nil, fmt.Errorf("assets: %s is not support", symbol)
}
manager, ok := adapter.(openwallet.SymbolInfo)
if !ok {
return nil, fmt.Errorf("assets: %s is not support", symbol)
}
return manager, nil
}
// GetAssetsAdapter 获取资产控制器
func GetAssetsAdapter(symbol string) (openwallet.AssetsAdapter, error) {
adapter := GetAssets(symbol)
if adapter == nil {
return nil, fmt.Errorf("assets: %s is not support", symbol)
}
manager, ok := adapter.(openwallet.AssetsAdapter)
if !ok {
return nil, fmt.Errorf("assets: %s is not support", symbol)
}
return manager, nil
}