From a8a705b9039fcb9cb2800ee7dfd7b7f556545d20 Mon Sep 17 00:00:00 2001 From: Darren Kelly Date: Fri, 9 Aug 2024 09:17:42 +0100 Subject: [PATCH] fix client expanded block & add contract decode call --- README.md | 12 +--------- client/blocks.go | 4 ++-- client/blocks_test.go | 12 ++++++++++ thorgo/accounts/contract.go | 39 +++++++++++++++++++++++++++----- thorgo/accounts/contract_test.go | 10 ++++++++ 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5b6032f..faa5ffb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ To install the Thor GO SDK, run the following command: go get github.com/darrenvechain/thor-go-sdk ``` -## Examples +## Usage ```golang package main @@ -37,13 +37,3 @@ func main() { fmt.Println(acc.Balance) } ``` - -The following test files demonstrate how to interact with the VechainThor blockchain using the Thor GO SDK: - -- [Accounts](./examples/internal/accounts_test.go) - Fetch account details, code and storage. -- [Blocks](./examples/internal/blocks_test.go) - Fetch blocks by revisions. -- [Contracts](./examples/internal/contracts_test.go) - Read state and create transaction clauses. -- [Events](./examples/internal/log_events_test.go) - Query on chain events. -- [Transfers](./examples/internal/log_transfers_test.go) - Query on chain VET transfers. -- [Transactions](./examples/internal/transactions_test.go) - Send transactions and query transaction details. -- [Signers](./examples/internal/private_key_signer_test.go) - Simplify transaction building, signing, sending and retrieving. diff --git a/client/blocks.go b/client/blocks.go index 749451b..9a3197e 100644 --- a/client/blocks.go +++ b/client/blocks.go @@ -50,8 +50,8 @@ type BlockTransaction struct { Size uint64 `json:"size"` GasUsed uint64 `json:"gasUsed"` GasPayer common.Address `json:"gasPayer"` - Paid uint64 `json:"paid"` - Reward uint64 `json:"reward"` + Paid hex.Int `json:"paid"` + Reward hex.Int `json:"reward"` Reverted bool `json:"reverted"` Outputs []Output `json:"outputs"` } diff --git a/client/blocks_test.go b/client/blocks_test.go index 99d7fad..a1f9967 100644 --- a/client/blocks_test.go +++ b/client/blocks_test.go @@ -30,6 +30,18 @@ func TestClient_ExpandedBlock(t *testing.T) { assert.NotNil(t, block) } +func TestClient_ExpandedBlockWithTxs(t *testing.T) { + c, err := FromURL("https://mainnet.vechain.org") + assert.NoError(t, err) + + blk, err := c.ExpandedBlock("0x0125fb07988ff3c36b261b5f7227688c1c0473c4873825ac299bc256ea991b0f") + assert.NoError(t, err) + + assert.NotNil(t, blk) + assert.NotNil(t, blk.Transactions) + assert.Greater(t, len(blk.Transactions), 0) +} + func TestClient_ChainTag(t *testing.T) { chainTag := client.ChainTag() assert.Equal(t, solo.ChainTag(), chainTag) diff --git a/thorgo/accounts/contract.go b/thorgo/accounts/contract.go index e56b287..f53e882 100644 --- a/thorgo/accounts/contract.go +++ b/thorgo/accounts/contract.go @@ -1,6 +1,7 @@ package accounts import ( + "bytes" "errors" "fmt" "math/big" @@ -62,6 +63,30 @@ func (c *Contract) Call(method string, value interface{}, args ...interface{}) e return nil } +// DecodeCall decodes the result of a contract call. +// The data must include the method signature. +func (c *Contract) DecodeCall(data []byte, value interface{}) error { + var method string + for name, m := range c.abi.Methods { + if len(data) >= 4 && bytes.Equal(data[:4], m.ID) { + method = name + break + } + } + + if method == "" { + return errors.New("method signature not found") + } + + data = data[4:] + + err := c.abi.UnpackIntoInterface(value, method, data) + if err != nil { + return fmt.Errorf("failed to unpack method %s: %w", method, err) + } + return nil +} + // AsClause returns a transaction clause for the given method and arguments. func (c *Contract) AsClause(method string, args ...interface{}) (*transaction.Clause, error) { packed, err := c.abi.Pack(method, args...) @@ -145,16 +170,17 @@ func (c *Contract) EventCriteria(name string, matchers ...interface{}) (client.E type Event struct { Name string Args map[string]interface{} + Log client.EventLog } -func (c *Contract) DecodeEvents(events []client.EventLog) ([]Event, error) { +func (c *Contract) DecodeEvents(logs []client.EventLog) ([]Event, error) { var decoded []Event - for _, ev := range events { - if len(ev.Topics) < 2 { + for _, log := range logs { + if len(log.Topics) < 2 { continue } - eventABI, err := c.abi.EventByID(ev.Topics[0]) + eventABI, err := c.abi.EventByID(log.Topics[0]) if err != nil { continue } @@ -167,12 +193,12 @@ func (c *Contract) DecodeEvents(events []client.EventLog) ([]Event, error) { } values := make(map[string]interface{}) - err = abi.ParseTopicsIntoMap(values, indexed, ev.Topics[1:]) + err = abi.ParseTopicsIntoMap(values, indexed, log.Topics[1:]) if err != nil { return nil, err } - data, err := hexutil.Decode(ev.Data) + data, err := hexutil.Decode(log.Data) if err != nil { return nil, err } @@ -184,6 +210,7 @@ func (c *Contract) DecodeEvents(events []client.EventLog) ([]Event, error) { decoded = append(decoded, Event{ Name: eventABI.Name, Args: values, + Log: log, }) } return decoded, nil diff --git a/thorgo/accounts/contract_test.go b/thorgo/accounts/contract_test.go index 0efde50..2303af4 100644 --- a/thorgo/accounts/contract_test.go +++ b/thorgo/accounts/contract_test.go @@ -31,6 +31,16 @@ func TestContract_Call(t *testing.T) { assert.Equal(t, uint8(18), decimals) } +func TestContract_DecodeCall(t *testing.T) { + packed, err := vthoABI.Pack("balanceOf", account1.Address()) + assert.NoError(t, err) + + balance := new(big.Int) + err = vtho.DecodeCall(packed, &balance) + assert.NoError(t, err) + assert.Greater(t, balance.Uint64(), uint64(0)) +} + func TestContract_AsClause(t *testing.T) { receiver, err := txmanager.GeneratePK() assert.NoError(t, err)