From d8d6e7a6250651e3f63798312e0a9949b3850bd7 Mon Sep 17 00:00:00 2001 From: Saeed Dadkhah Date: Thu, 8 Feb 2024 17:15:36 +0330 Subject: [PATCH] Add missing json-rpc calls. --- README.md | 6 ++ core/types.go | 17 ++++++ provider/provider.go | 120 ++++++++++++++++++++++++++++++++++++-- provider/provider_test.go | 45 ++++++++++++++ 4 files changed, 184 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4fb6266..9ec7825 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ go test ./... Blockchain-related methods - [x] getNetworkId +- [x] getVersion +- [x] getNodeType +- [x] getNumPeers - [x] getBlockchainInfo - [x] getShardingStructure - [x] getDsBlock @@ -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 diff --git a/core/types.go b/core/types.go index 943af7a..47a7a98 100644 --- a/core/types.go +++ b/core/types.go @@ -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 +} diff --git a/provider/provider.go b/provider/provider.go index e4b6f9a..10142d5 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -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 { @@ -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") @@ -96,7 +147,6 @@ func (provider *Provider) GetShardingStructure() (*core.ShardingStructure, error } return &shardingStructure, nil - } // Returns the details of a specified Directory Service block. @@ -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. @@ -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. @@ -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 { @@ -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) { diff --git a/provider/provider_test.go b/provider/provider_test.go index 69f7b23..e803d85 100644 --- a/provider/provider_test.go +++ b/provider/provider_test.go @@ -21,6 +21,8 @@ import ( "fmt" "os" "testing" + + "github.com/stretchr/testify/assert" ) func SkipIfCI(t *testing.T) { @@ -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/") @@ -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/") @@ -240,6 +271,13 @@ 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/") @@ -247,6 +285,13 @@ func TestProvider_GetTxnBodiesForTxBlock(t *testing.T) { 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/")