Skip to content

Commit

Permalink
fix client expanded block & add contract decode call
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Aug 9, 2024
1 parent 54710b9 commit a8a705b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
4 changes: 2 additions & 2 deletions client/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
12 changes: 12 additions & 0 deletions client/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 33 additions & 6 deletions thorgo/accounts/contract.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package accounts

import (
"bytes"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions thorgo/accounts/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a8a705b

Please sign in to comment.