Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getBlock websocket endpoint #2

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions server/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
}
return
},
"getBlock": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
r := struct {
Id string `json:"id"`
}{}
err = json.Unmarshal(req.Params, &r)
if err == nil {
rv, err = s.getBlock(r.Id)
}
return
},
"getAccountUtxo": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
r := struct {
Descriptor string `json:"descriptor"`
Expand Down Expand Up @@ -465,6 +475,14 @@ func (s *WebsocketServer) getBlockHash(height int) (interface{}, error) {
}, nil
}

func (s *WebsocketServer) getBlock(id string) (interface{}, error) {
block, err := s.api.GetBlock(id, 0, 100000)
if err != nil {
return nil, err
}
return block, nil
}

func (s *WebsocketServer) estimateFee(c *websocketChannel, params []byte) (interface{}, error) {
type estimateFeeReq struct {
Blocks []int `json:"blocks"`
Expand Down
24 changes: 24 additions & 0 deletions static/test-websocket.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@
});
}

function getBlock() {
const method = "getBlock";
const id = document.getElementById("getBlockId").value;
const params = {
id,
};
send(method, params, function (result) {
document.getElementById("getBlockResult").innerText = JSON.stringify(result).replace(/,/g, ", ");
});
}

function getAccountInfo() {
const descriptor = document.getElementById('getAccountInfoDescriptor').value.trim();
const selectDetails = document.getElementById('getAccountInfoDetails');
Expand Down Expand Up @@ -308,6 +319,19 @@ <h1>Blockbook Websocket Test Page</h1>
<div class="row">
<div class="col" id="getBlockHashResult"></div>
</div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="getBlock" onclick="getBlock()">
</div>
<div class="col-8">
<input type="text" class="form-control" placeholder="height/hash" id="getBlockId" value="">
</div>
<div class="col">
</div>
</div>
<div class="row">
<div class="col" id="getBlockResult"></div>
</div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="getAccountInfo" onclick="getAccountInfo()">
Expand Down