Add getInfo websocket method
This commit is contained in:
parent
a04b2b67b5
commit
f332c0aa9e
@ -583,10 +583,7 @@ type resultGetInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) {
|
func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) {
|
||||||
height, _, err := s.db.GetBestBlock()
|
_, height, _ := s.is.GetSyncState()
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
res.Result.Blocks = int(height)
|
res.Result.Blocks = int(height)
|
||||||
res.Result.Testnet = s.chain.IsTestnet()
|
res.Result.Testnet = s.chain.IsTestnet()
|
||||||
res.Result.Network = s.chain.GetNetworkName()
|
res.Result.Network = s.chain.GetNetworkName()
|
||||||
|
|||||||
@ -60,6 +60,7 @@ type WebsocketServer struct {
|
|||||||
metrics *common.Metrics
|
metrics *common.Metrics
|
||||||
is *common.InternalState
|
is *common.InternalState
|
||||||
api *api.Worker
|
api *api.Worker
|
||||||
|
block0hash string
|
||||||
newBlockSubscriptions map[*websocketChannel]string
|
newBlockSubscriptions map[*websocketChannel]string
|
||||||
newBlockSubscriptionsLock sync.Mutex
|
newBlockSubscriptionsLock sync.Mutex
|
||||||
addressSubscriptions map[string]map[*websocketChannel]string
|
addressSubscriptions map[string]map[*websocketChannel]string
|
||||||
@ -72,6 +73,10 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxC
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
b0, err := db.GetBlockHash(0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
s := &WebsocketServer{
|
s := &WebsocketServer{
|
||||||
upgrader: &websocket.Upgrader{
|
upgrader: &websocket.Upgrader{
|
||||||
ReadBufferSize: 1024 * 32,
|
ReadBufferSize: 1024 * 32,
|
||||||
@ -85,6 +90,7 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxC
|
|||||||
metrics: metrics,
|
metrics: metrics,
|
||||||
is: is,
|
is: is,
|
||||||
api: api,
|
api: api,
|
||||||
|
block0hash: b0,
|
||||||
newBlockSubscriptions: make(map[*websocketChannel]string),
|
newBlockSubscriptions: make(map[*websocketChannel]string),
|
||||||
addressSubscriptions: make(map[string]map[*websocketChannel]string),
|
addressSubscriptions: make(map[string]map[*websocketChannel]string),
|
||||||
}
|
}
|
||||||
@ -216,6 +222,9 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
|
"getInfo": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
|
||||||
|
return s.getInfo()
|
||||||
|
},
|
||||||
"sendTransaction": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
|
"sendTransaction": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
|
||||||
r := struct {
|
r := struct {
|
||||||
Hex string `json:"hex"`
|
Hex string `json:"hex"`
|
||||||
@ -303,27 +312,52 @@ func unmarshalGetAccountInfoRequest(params []byte) (*accountInfoReq, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *WebsocketServer) getAccountInfo(req *accountInfoReq) (res *api.Address, err error) {
|
func (s *WebsocketServer) getAccountInfo(req *accountInfoReq) (res *api.Address, err error) {
|
||||||
if s.chainParser.GetChainType() == bchain.ChainEthereumType {
|
var opt api.GetAddressOption
|
||||||
var opt api.GetAddressOption
|
switch req.Details {
|
||||||
switch req.Details {
|
case "balance":
|
||||||
case "balance":
|
opt = api.Balance
|
||||||
opt = api.Balance
|
case "txids":
|
||||||
case "txids":
|
opt = api.TxidHistory
|
||||||
opt = api.TxidHistory
|
case "txs":
|
||||||
case "txs":
|
opt = api.TxHistory
|
||||||
opt = api.TxHistory
|
default:
|
||||||
default:
|
opt = api.Basic
|
||||||
opt = api.Basic
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{
|
|
||||||
FromHeight: uint32(req.FromHeight),
|
|
||||||
ToHeight: uint32(req.ToHeight),
|
|
||||||
Contract: req.ContractFilter,
|
|
||||||
Vout: api.AddressFilterVoutOff,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return nil, errors.New("Not implemented")
|
|
||||||
|
return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{
|
||||||
|
FromHeight: uint32(req.FromHeight),
|
||||||
|
ToHeight: uint32(req.ToHeight),
|
||||||
|
Contract: req.ContractFilter,
|
||||||
|
Vout: api.AddressFilterVoutOff,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *WebsocketServer) getInfo() (interface{}, error) {
|
||||||
|
vi := common.GetVersionInfo()
|
||||||
|
height, hash, err := s.db.GetBestBlock()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
type info struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Shortcut string `json:"shortcut"`
|
||||||
|
Decimals int `json:"decimals"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
BestHeight int `json:"bestheight"`
|
||||||
|
BestHash string `json:"besthash"`
|
||||||
|
Block0Hash string `json:"block0hash"`
|
||||||
|
Testnet bool `json:"testnet"`
|
||||||
|
}
|
||||||
|
return &info{
|
||||||
|
Name: s.is.Coin,
|
||||||
|
Shortcut: s.is.CoinShortcut,
|
||||||
|
Decimals: s.chainParser.AmountDecimals(),
|
||||||
|
BestHeight: int(height),
|
||||||
|
BestHash: hash,
|
||||||
|
Version: vi.Version,
|
||||||
|
Block0Hash: s.block0hash,
|
||||||
|
Testnet: s.chain.IsTestnet(),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WebsocketServer) sendTransaction(tx string) (res resultSendTransaction, err error) {
|
func (s *WebsocketServer) sendTransaction(tx string) (res resultSendTransaction, err error) {
|
||||||
|
|||||||
@ -121,6 +121,15 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getInfo() {
|
||||||
|
const method = 'getInfo';
|
||||||
|
const params = {
|
||||||
|
};
|
||||||
|
send(method, params, function (result) {
|
||||||
|
document.getElementById('getInfoResult').innerText = JSON.stringify(result).replace(/,/g, ", ");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function sendTransaction() {
|
function sendTransaction() {
|
||||||
var hex = document.getElementById('sendTransactionHex').value.trim();
|
var hex = document.getElementById('sendTransactionHex').value.trim();
|
||||||
const method = 'sendTransaction';
|
const method = 'sendTransaction';
|
||||||
@ -224,20 +233,6 @@
|
|||||||
return socket.send({ method, params }, f);
|
return socket.send({ method, params }, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getInfo() {
|
|
||||||
lookupSyncStatus(function (result) {
|
|
||||||
console.log('getInfo sent successfully');
|
|
||||||
console.log(result);
|
|
||||||
document.getElementById('getInfoResult').innerText = JSON.stringify(result).replace(/,/g, ", ");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function lookupSyncStatus(f) {
|
|
||||||
const method = 'getInfo';
|
|
||||||
const params = [];
|
|
||||||
return socket.send({ method, params }, f);
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
@ -311,15 +306,14 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col" id="estimateFeeResult">
|
<div class="col" id="estimateFeeResult">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>-->
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<input class="btn btn-secondary" type="button" value="getInfo" onclick="getInfo()">
|
<input class="btn btn-secondary" type="button" value="getInfo" onclick="getInfo()">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-10" id="getInfoResult">
|
<div class="col-10" id="getInfoResult">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
-->
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col">
|
<div class="col">
|
||||||
<input class="btn btn-secondary" type="button" value="sendTransaction" onclick="sendTransaction()">
|
<input class="btn btn-secondary" type="button" value="sendTransaction" onclick="sendTransaction()">
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user