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

Thor client #818

Merged
merged 40 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
80c7a62
feat: add thorclient
paologalligit Aug 12, 2024
2638d21
refactor: remove roundTripper
paologalligit Aug 12, 2024
09de0ec
refactor: change null check
paologalligit Aug 12, 2024
71db025
clean: remove commented code
paologalligit Aug 12, 2024
83524a1
feat: add account revision and pending tx
paologalligit Aug 13, 2024
dc9e53c
fix: add licence headers and fix linter issue
paologalligit Aug 13, 2024
9892bad
refactor: rename package
paologalligit Aug 13, 2024
81824ae
refactor: change revision type to string
paologalligit Aug 13, 2024
8788f8c
refactor: rename GetLogs and GetTransfers to FilterEvents and FilterT…
paologalligit Aug 13, 2024
06a58de
refactor: change FilterEvents and FilterTransactions request type to …
paologalligit Aug 13, 2024
5764771
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 14, 2024
e829f16
Adding common.EventWrapper to handle channel errors
otherview Aug 14, 2024
a521ce5
tweak
otherview Aug 16, 2024
88cadba
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 19, 2024
aa94aa1
update rawclient + update account tests
otherview Aug 19, 2024
a9d37ef
tidy up names
otherview Aug 19, 2024
72858c8
update tests
otherview Aug 19, 2024
6ab2443
pr comments
otherview Aug 20, 2024
d2f6c8b
adding raw tx
otherview Aug 20, 2024
c667542
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 23, 2024
e3ebf93
Tidy up method names and calls
otherview Aug 26, 2024
7a9e102
options client
otherview Aug 26, 2024
44b61b9
tweaks
otherview Aug 29, 2024
ba08cff
pr comments
otherview Aug 29, 2024
c721582
Update thorclient/common/common.go
otherview Aug 30, 2024
c3bc7a3
pr comments
otherview Aug 30, 2024
2119256
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview Aug 30, 2024
875b728
Adding Subscriptions
otherview Sep 2, 2024
39e3237
Pr comments
otherview Sep 2, 2024
0ec320d
adjust func orders
libotony Sep 3, 2024
8d78663
pr comments
otherview Sep 3, 2024
279a674
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview Sep 3, 2024
b2153fa
changing subscribe to use the channel close vs multiple channels
otherview Sep 5, 2024
d86f0f2
adding go-doc
otherview Sep 5, 2024
a2569e0
no error after unsubscribe
libotony Sep 12, 2024
b753aea
pr comments
otherview Sep 13, 2024
6fa1b1b
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Sep 13, 2024
d2604d2
checking status code is 2xx
paologalligit Oct 11, 2024
c1f1159
Merge conflicts
paologalligit Oct 11, 2024
a1e92c3
fix: change FilterTransfers argument
paologalligit Oct 23, 2024
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
21 changes: 21 additions & 0 deletions thorclient/common/common.go
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
Copy link
Member

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

Copy link
Member

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 ?


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
}
209 changes: 209 additions & 0 deletions thorclient/httpclient/client.go
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
}
Loading
Loading