From d910fac1b19f00e59786b0c0ea4be01ffd63f41b Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Wed, 19 Jan 2022 11:41:04 -0500 Subject: [PATCH] Fixed `get_producers` call. * Fixed [`Producer`](https://github.com/eoscanada/eos-go/blob/c56c2f318d421cbc9d49d9094ea3732ddb94969a/responses.go#L451) to use more standard `eos-go` types. * Fixed [ProducersResp](https://github.com/eoscanada/eos-go/blob/c56c2f318d421cbc9d49d9094ea3732ddb94969a/responses.go#L462) to fit with actual EOSIO API. --- CHANGELOG.md | 27 ++++++++++++++++++++++++ api.go | 2 +- example_api_get_producers_test.go | 34 +++++++++++++++++++++++++++++++ responses.go | 25 +++++++++++++++-------- 4 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 example_api_get_producers_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index c3b0ebf7..2d18e613 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,14 +7,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Breaking Changes +* Fixed [`Producer`](https://github.com/eoscanada/eos-go/blob/c56c2f318d421cbc9d49d9094ea3732ddb94969a/responses.go#L451) to use more standard `eos-go` types. +* Fixed [ProducersResp](https://github.com/eoscanada/eos-go/blob/c56c2f318d421cbc9d49d9094ea3732ddb94969a/responses.go#L462) to fit with actual EOSIO API. + +#### Added + +* Add EOSIO Testnet symbol (#165) +* Update ActionResp (#166) + +#### Changed + +#### Fixed + +* Bugfix StringToSymbol (44b6fbd) +* Fixed built-in examples (pointing by default to EOS Nation API nodes) (36114bd) + +#### Deprecated + +### [**0.10.1**](https://github.com/eoscanada/eos-go/releases/tag/v0.10.1) (January 19th, 2022) + +#### Breaking Changes + * Renamed [`BlockTimestampFormat`](https://github.com/eoscanada/eos-go/blob/a1623cc5a2223005a4dc7d4dec972d6119de42ff/types.go#L844) to `blockTimestampFormat` making it private. #### Added +* Add EOSIO Testnet symbol (#165) +* Update ActionResp (#166) + #### Changed #### Fixed +* Bugfix StringToSymbol (44b6fbd) +* Fixed built-in examples (pointing by default to EOS Nation API nodes) (36114bd) + #### Deprecated ### [**0.10**](https://github.com/eoscanada/eos-go/releases/tag/v0.10.0) (October 16th, 2021) diff --git a/api.go b/api.go index dd425eb5..1e3766d5 100644 --- a/api.go +++ b/api.go @@ -549,7 +549,7 @@ func (api *API) GetProducers(ctx context.Context) (out *ProducersResp, err error /* +FC_REFLECT( eosio::chain_apis::read_only::get_producers_params, (json)(lower_bound)(limit) ) +FC_REFLECT( eosio::chain_apis::read_only::get_producers_result, (rows)(total_producer_vote_weight)(more) ); */ - err = api.call(ctx, "chain", "get_producers", nil, &out) + err = api.call(ctx, "chain", "get_producers", M{"json": true}, &out) return } diff --git a/example_api_get_producers_test.go b/example_api_get_producers_test.go new file mode 100644 index 00000000..e82ef94e --- /dev/null +++ b/example_api_get_producers_test.go @@ -0,0 +1,34 @@ +package eos_test + +import ( + "context" + "fmt" + "sort" + + eos "github.com/eoscanada/eos-go" +) + +func ExampleAPI_GetProducers() { + api := eos.New(getAPIURL()) + + resp, err := api.GetProducers(context.Background()) + if err != nil { + panic(fmt.Errorf("get account: %w", err)) + } + + sort.Slice(resp.Producers, func(i, j int) bool { + if resp.Producers[i].IsActive && !resp.Producers[j].IsActive { + return true + } + + return resp.Producers[i].TotalVotes < resp.Producers[j].TotalVotes + }) + + for _, producer := range resp.Producers { + fmt.Printf("Producer %s (%s) with %.5f votes (last claimed %s)\n", producer.Owner, producer.URL, producer.TotalVotes, producer.LastClaimTime) + } + + fmt.Printf("Total Vote Weight: %.4f\n", resp.TotalProducerVoteWeight) + fmt.Printf("More: %s\n", resp.More) + // Output: any +} diff --git a/responses.go b/responses.go index 92c81282..27ae0a15 100644 --- a/responses.go +++ b/responses.go @@ -449,23 +449,28 @@ type Global struct { } type Producer struct { - Owner string `json:"owner"` - TotalVotes float64 `json:"total_votes,string"` - ProducerKey string `json:"producer_key"` - IsActive int `json:"is_active"` - URL string `json:"url"` - UnpaidBlocks int `json:"unpaid_blocks"` - LastClaimTime Float64 `json:"last_claim_time"` - Location int `json:"location"` + Owner Name `json:"owner"` + TotalVotes Float64 `json:"total_votes,string"` + ProducerKey ecc.PublicKey `json:"producer_key"` + IsActive Bool `json:"is_active"` + URL string `json:"url"` + UnpaidBlocks int `json:"unpaid_blocks"` + LastClaimTime JSONTime `json:"last_claim_time"` + Location int `json:"location"` } + type ProducersResp struct { - Producers []Producer `json:"producers"` + Producers []Producer `json:"rows"` + TotalProducerVoteWeight Float64 `json:"total_producer_vote_weight"` + More Name `json:"more"` } + type GetActionsRequest struct { AccountName AccountName `json:"account_name"` Pos Int64 `json:"pos"` Offset Int64 `json:"offset"` } + type ActionResp struct { GlobalSeq JSONInt64 `json:"global_action_seq"` AccountSeq JSONInt64 `json:"account_action_seq"` @@ -474,10 +479,12 @@ type ActionResp struct { Trace ActionTrace `json:"action_trace"` Irreversible bool `json:"irreversible"` } + type ActionsResp struct { Actions []ActionResp `json:"actions"` LastIrreversibleBlock uint32 `json:"last_irreversible_block"` } + type KeyAccountsResp struct { AccountNames []string `json:"account_names"` }