Skip to content

Commit

Permalink
Add missing json-rpc calls. (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
its-saeed authored Feb 9, 2024
1 parent 369a05d commit ca85f4f
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ go test ./...
Blockchain-related methods

- [x] getNetworkId
- [x] getVersion
- [x] getNodeType
- [x] getNumPeers
- [x] getBlockchainInfo
- [x] getShardingStructure
- [x] getDsBlock
Expand All @@ -128,6 +131,9 @@ Transaction-related methods
- [x] getTransaction
- [x] getRecentTransactions
- [x] getTransactionsForTxBlock
- [x] getTransactionsForTxBlockEx
- [x] getTxnBodiesForTxBlock
- [x] getTxnBodiesForTxBlockEx
- [x] getNumTxnsTxEpoch
- [x] getNumTxnsDSEpoch
- [x] getMinimumGasPrice
Expand Down
17 changes: 17 additions & 0 deletions core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,20 @@ type DSComm struct {
NumOfDSGuard int
DSComm []string `json:"dscomm"`
}

type Version struct {
Commit string
Version string
}

type TransactionsForTxBlockEx struct {
CurrPage uint32
NumPages uint32
Transactions [][]string
}

type TxnBodiesForTxBlockEx struct {
CurrPage uint32
NumPages uint32
Transactions []Transaction
}
120 changes: 116 additions & 4 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"bytes"
"encoding/json"
"errors"
"github.com/Zilliqa/gozilliqa-sdk/v3/core"
"github.com/ybbus/jsonrpc"
"io/ioutil"
"net/http"
"strconv"

"github.com/Zilliqa/gozilliqa-sdk/v3/core"
"github.com/ybbus/jsonrpc"
)

type Provider struct {
Expand All @@ -49,6 +51,55 @@ func (provider *Provider) GetNetworkId() (string, error) {
return result.Result.(string), nil
}

// Returns the software version of the specified network. This is represented as a String.
func (provider *Provider) GetVersion() (*core.Version, error) {
result, err := provider.call("GetVersion")
if err != nil {
return nil, err
}

if result.Error != nil {
return nil, result.Error
}

var version core.Version
jsonString, err2 := json.Marshal(result.Result)
if err2 != nil {
return nil, err2
}

err3 := json.Unmarshal(jsonString, &version)
if err3 != nil {
return nil, err3
}

return &version, nil
}

// Returns tye node type. This is represented as a String.
func (provider *Provider) GetNodeType() (string, error) {
result, err := provider.call("GetNodeType")
if err != nil {
return "", err
}
if result.Error != nil {
return "", result.Error
}
return result.Result.(string), nil
}

// Returns tye node type. This is represented as a String.
func (provider *Provider) GetNumPeers() (int64, error) {
result, err := provider.call("GetNumPeers")
if err != nil {
return -1, err
}
if result.Error != nil {
return -1, result.Error
}
return result.Result.(json.Number).Int64()
}

// Returns the current network statistics for the specified network.
func (provider *Provider) GetBlockchainInfo() (*core.BlockchainInfo, error) {
result, err := provider.call("GetBlockchainInfo")
Expand Down Expand Up @@ -96,7 +147,6 @@ func (provider *Provider) GetShardingStructure() (*core.ShardingStructure, error
}

return &shardingStructure, nil

}

// Returns the details of a specified Directory Service block.
Expand Down Expand Up @@ -516,6 +566,20 @@ func (provider *Provider) GetTotalCoinSupply() (string, error) {
return result.Result.(string), nil
}

// Returns the total supply (ZIL) of coins in the network. This is represented as a 64-bit integer.
func (provider *Provider) GetTotalCoinSupplyAsInt() (int64, error) {
result, err := provider.call("GetTotalCoinSupplyAsInt")
if err != nil {
return -1, err
}

if result.Error != nil {
return -1, result.Error
}

return result.Result.(json.Number).Int64()
}

// Returns the mining nodes (i.e., the members of the DS committee and shards) at the specified DS block.
// Notes: 1. Nodes owned by Zilliqa Research are omitted. 2. dscommittee has no size field since the DS committee size
// is fixed for a given chain. 3. For the Zilliqa Mainnet, this API is only available from DS block 5500 onwards.
Expand Down Expand Up @@ -730,7 +794,7 @@ func (provider *Provider) GetRecentTransactions() (*core.Transactions, error) {
return &transactions, nil
}

// Returns the validated transactions included within a specfied final transaction block as an array of length i,
// Returns the validated transactions included within a specified final transaction block as an array of length i,
// where i is the number of shards plus the DS committee. The transactions are grouped based on the group that processed
// the transaction. The first element of the array refers to the first shard. The last element of the array at index, i,
// refers to the transactions processed by the DS Committee.
Expand Down Expand Up @@ -758,6 +822,30 @@ func (provider *Provider) GetTransactionsForTxBlock(tx_block_number string) ([][
return transactions, nil
}

func (provider *Provider) GetTransactionsForTxBlockEx(tx_block_number string, page_number uint32) (*core.TransactionsForTxBlockEx, error) {
result, err := provider.call("GetTransactionsForTxBlockEx", tx_block_number, strconv.Itoa(int(page_number)))
if err != nil {
return nil, err
}

if result.Error != nil {
return nil, result.Error
}

var tx_block_ex core.TransactionsForTxBlockEx
jsonString, err2 := json.Marshal(result.Result)
if err2 != nil {
return nil, err2
}

err3 := json.Unmarshal(jsonString, &tx_block_ex)
if err3 != nil {
return nil, err3
}

return &tx_block_ex, nil
}

func (provider *Provider) GetTxnBodiesForTxBlock(tx_block_number string) ([]core.Transaction, error) {
result, err := provider.call("GetTxnBodiesForTxBlock", tx_block_number)
if err != nil {
Expand All @@ -782,6 +870,30 @@ func (provider *Provider) GetTxnBodiesForTxBlock(tx_block_number string) ([]core
return transactions, nil
}

func (provider *Provider) GetTxnBodiesForTxBlockEx(tx_block_number string, page_number uint32) (*core.TxnBodiesForTxBlockEx, error) {
result, err := provider.call("GetTxnBodiesForTxBlockEx", tx_block_number)
if err != nil {
return nil, err
}

if result.Error != nil {
return nil, result.Error
}

var transactions core.TxnBodiesForTxBlockEx
jsonString, err2 := json.Marshal(result.Result)
if err2 != nil {
return nil, err2
}

err3 := json.Unmarshal(jsonString, &transactions)
if err3 != nil {
return nil, err3
}

return &transactions, nil
}

// Returns the number of validated transactions included in this Transaction epoch.
// This is represented as String.
func (provider *Provider) GetNumTxnsTxEpoch() (string, error) {
Expand Down
45 changes: 45 additions & 0 deletions provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func SkipIfCI(t *testing.T) {
Expand All @@ -36,6 +38,28 @@ func TestGetNetworkId(t *testing.T) {
fmt.Println(id)
}

func TestGetVersion(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
id, _ := provider.GetVersion()
fmt.Println(id)
}

func TestGetNodeType(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
nt, _ := provider.GetNodeType()
fmt.Println(nt)
}

func TestGetNumPeers(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
peers, _ := provider.GetNumPeers()
assert.Positive(t, peers)
fmt.Println(peers)
}

func TestGetBlockchainInfo(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
Expand Down Expand Up @@ -189,6 +213,13 @@ func TestProvider_GetTotalCoinSupply(t *testing.T) {
fmt.Println(result)
}

func TestProvider_GetTotalCoinSupplyAsInt(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
result, _ := provider.GetTotalCoinSupplyAsInt()
fmt.Println(result)
}

func TestProvider_GetMinerInfo(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://api.zilliqa.com/")
Expand Down Expand Up @@ -240,13 +271,27 @@ func TestGetTransactionsForTxBlock(t *testing.T) {
fmt.Println(result)
}

func TestGetTransactionsForTxBlockEx(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
result, _ := provider.GetTransactionsForTxBlockEx("1442201", 0)
fmt.Println(result)
}

func TestProvider_GetTxnBodiesForTxBlock(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
result, _ := provider.GetTxnBodiesForTxBlock("1364221")
fmt.Println(result)
}

func TestProvider_GetTxnBodiesForTxBlockEx(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
result, _ := provider.GetTxnBodiesForTxBlockEx("1364221", 0)
fmt.Println(result)
}

func TestGetNumTxnsTxEpoch(t *testing.T) {
SkipIfCI(t)
provider := NewProvider("https://dev-api.zilliqa.com/")
Expand Down

0 comments on commit ca85f4f

Please sign in to comment.