-
Notifications
You must be signed in to change notification settings - Fork 253
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
Thor client #818
Merged
Merged
Thor client #818
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
80c7a62
feat: add thorclient
paologalligit 2638d21
refactor: remove roundTripper
paologalligit 09de0ec
refactor: change null check
paologalligit 71db025
clean: remove commented code
paologalligit 83524a1
feat: add account revision and pending tx
paologalligit dc9e53c
fix: add licence headers and fix linter issue
paologalligit 9892bad
refactor: rename package
paologalligit 81824ae
refactor: change revision type to string
paologalligit 8788f8c
refactor: rename GetLogs and GetTransfers to FilterEvents and FilterT…
paologalligit 06a58de
refactor: change FilterEvents and FilterTransactions request type to …
paologalligit 5764771
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview e829f16
Adding common.EventWrapper to handle channel errors
otherview a521ce5
tweak
otherview 88cadba
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview aa94aa1
update rawclient + update account tests
otherview a9d37ef
tidy up names
otherview 72858c8
update tests
otherview 6ab2443
pr comments
otherview d2f6c8b
adding raw tx
otherview c667542
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview e3ebf93
Tidy up method names and calls
otherview 7a9e102
options client
otherview 44b61b9
tweaks
otherview ba08cff
pr comments
otherview c721582
Update thorclient/common/common.go
otherview c3bc7a3
pr comments
otherview 2119256
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview 875b728
Adding Subscriptions
otherview 39e3237
Pr comments
otherview 0ec320d
adjust func orders
libotony 8d78663
pr comments
otherview 279a674
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview b2153fa
changing subscribe to use the channel close vs multiple channels
otherview d86f0f2
adding go-doc
otherview a2569e0
no error after unsubscribe
libotony b753aea
pr comments
otherview 6fa1b1b
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview d2604d2
checking status code is 2xx
paologalligit c1f1159
Merge conflicts
paologalligit a1e92c3
fix: change FilterTransfers argument
paologalligit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/vechain/thor/v2/thor" | ||
) | ||
|
||
var ( | ||
NotFoundErr = fmt.Errorf("not found") | ||
Not200StatusErr = fmt.Errorf("not 200 status code") | ||
) | ||
|
||
type TxSendResult struct { | ||
ID *thor.Bytes32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package httpclient | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/vechain/thor/v2/api/accounts" | ||
"github.com/vechain/thor/v2/api/blocks" | ||
"github.com/vechain/thor/v2/api/events" | ||
"github.com/vechain/thor/v2/api/node" | ||
"github.com/vechain/thor/v2/api/transactions" | ||
"github.com/vechain/thor/v2/api/transfers" | ||
"github.com/vechain/thor/v2/thor" | ||
"github.com/vechain/thor/v2/thorclient/common" | ||
) | ||
|
||
type Client struct { | ||
url string | ||
c *http.Client | ||
} | ||
|
||
func NewClient(url string) *Client { | ||
otherview marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &Client{ | ||
url: url, | ||
c: &http.Client{}, | ||
} | ||
} | ||
|
||
func (c *Client) GetTransactionReceipt(txID *thor.Bytes32) (*transactions.Receipt, error) { | ||
body, err := c.httpGET(c.url + "/transactions/" + txID.String() + "/receipt") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to fetch receipt - %w", err) | ||
} | ||
|
||
if len(body) == 0 || bytes.Equal(bytes.TrimSpace(body), []byte("null")) { | ||
return nil, common.NotFoundErr | ||
} | ||
|
||
var receipt transactions.Receipt | ||
if err = json.Unmarshal(body, &receipt); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall receipt - %w", err) | ||
} | ||
|
||
return &receipt, nil | ||
} | ||
|
||
func (c *Client) InspectClauses(calldata *accounts.BatchCallData) ([]*accounts.CallResult, error) { | ||
body, err := c.httpPOST(c.url+"/accounts/*", calldata) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to request inspect clauses - %w", err) | ||
} | ||
|
||
var inspectionRes []*accounts.CallResult | ||
if err = json.Unmarshal(body, &inspectionRes); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall inspection - %w", err) | ||
} | ||
|
||
return inspectionRes, nil | ||
} | ||
|
||
func (c *Client) SendTransaction(obj *transactions.RawTx) (*common.TxSendResult, error) { | ||
body, err := c.httpPOST(c.url+"/transactions", obj) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to send raw transaction - %w", err) | ||
} | ||
|
||
var txID common.TxSendResult | ||
if err = json.Unmarshal(body, &txID); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall inspection - %w", err) | ||
} | ||
|
||
return &txID, nil | ||
} | ||
|
||
func (c *Client) FilterEvents(req *events.EventFilter) ([]events.FilteredEvent, error) { | ||
body, err := c.httpPOST(c.url+"/logs/event", req) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to send raw transaction - %w", err) | ||
} | ||
|
||
var filteredEvents []events.FilteredEvent | ||
if err = json.Unmarshal(body, &filteredEvents); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return filteredEvents, nil | ||
} | ||
|
||
func (c *Client) FilterTransfers(req *events.EventFilter) ([]*transfers.FilteredTransfer, error) { | ||
body, err := c.httpPOST(c.url+"/logs/transfer", req) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to send retrieve transfer logs - %w", err) | ||
} | ||
|
||
var filteredEvents []*transfers.FilteredTransfer | ||
if err = json.Unmarshal(body, &filteredEvents); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return filteredEvents, nil | ||
} | ||
|
||
func (c *Client) GetAccount(addr *thor.Address, revision string) (*accounts.Account, error) { | ||
url := c.url + "/accounts/" + addr.String() | ||
if revision != "" { | ||
url += "?revision=" + revision | ||
} | ||
|
||
body, err := c.httpGET(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to retrieve account - %w", err) | ||
} | ||
|
||
var account accounts.Account | ||
if err = json.Unmarshal(body, &account); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return &account, nil | ||
} | ||
|
||
func (c *Client) GetAccountCode(addr *thor.Address, revision string) ([]byte, error) { | ||
url := c.url + "/accounts/" + addr.String() + "/code" | ||
if revision != "" { | ||
url += "?revision=" + revision | ||
} | ||
|
||
return c.httpGET(url) | ||
} | ||
|
||
func (c *Client) GetStorage(addr *thor.Address, key *thor.Bytes32) ([]byte, error) { | ||
return c.httpGET(c.url + "/accounts/" + addr.String() + "/key/" + key.String()) | ||
} | ||
|
||
func (c *Client) GetExpandedBlock(blockID string) (*blocks.JSONExpandedBlock, error) { | ||
body, err := c.httpGET(c.url + "/blocks/" + blockID + "?expanded=true") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to retrieve block - %w", err) | ||
} | ||
|
||
var block blocks.JSONExpandedBlock | ||
if err = json.Unmarshal(body, &block); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return &block, nil | ||
} | ||
|
||
func (c *Client) GetBlock(blockID string) (*blocks.JSONBlockSummary, error) { | ||
otherview marked this conversation as resolved.
Show resolved
Hide resolved
|
||
body, err := c.httpGET(c.url + "/blocks/" + blockID) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to retrieve block - %w", err) | ||
} | ||
|
||
var block blocks.JSONBlockSummary | ||
if err = json.Unmarshal(body, &block); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return &block, nil | ||
} | ||
|
||
func (c *Client) GetTransaction(txID *thor.Bytes32, isPending bool) (*transactions.Transaction, error) { | ||
url := c.url + "/transactions/" + txID.String() | ||
if isPending { | ||
url += "?pending=true" | ||
} | ||
|
||
body, err := c.httpGET(url) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to retrieve transaction - %w", err) | ||
} | ||
|
||
var tx transactions.Transaction | ||
if err = json.Unmarshal(body, &tx); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return &tx, nil | ||
} | ||
|
||
func (c *Client) RawHTTPPost(url string, calldata interface{}) ([]byte, error) { | ||
return c.httpPOST(c.url+url, calldata) | ||
} | ||
|
||
func (c *Client) RawHTTPGet(url string) ([]byte, error) { | ||
return c.httpGET(c.url + url) | ||
} | ||
|
||
func (c *Client) GetPeers() ([]*node.PeerStats, error) { | ||
body, err := c.httpGET(c.url + "/node/network/peers") | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to retrieve peers - %w", err) | ||
} | ||
|
||
var peers []*node.PeerStats | ||
if err = json.Unmarshal(body, &peers); err != nil { | ||
return nil, fmt.Errorf("unable to unmarshall events - %w", err) | ||
} | ||
|
||
return peers, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need its own package? Seems tiny and could easily be placed inline with httpclient or thorclient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's imported by multiple packages. Errors for error testing,
TxSendResult
for tx results and the EventWrapper for other uses.But it's a good point, what if I leave this comment open and review it again once the PR is closer to done ?