Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix issues #51

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ type ClientInformational interface {
// GetDeployFinalizedApproval returns Deploy with the finalized approvals substituted.
GetDeployFinalizedApproval(ctx context.Context, hash string) (InfoGetDeployResult, error)
// GetDictionaryItem returns an item from a Dictionary.
// Every dictionary has a seed URef, findable by using a dictionary_identifier.
// The address of a stored value is the blake2b hash of the seed URef and the byte representation of the dictionary key.
// If the param stateRootHash is nil, the client will make an additional RPC call to retrieve the latest stateRootHash.
GetDictionaryItem(ctx context.Context, stateRootHash *string, uref, key string) (StateGetDictionaryResult, error)
// GetDictionaryItemByIdentifier returns an item from a Dictionary.
// Every dictionary has a seed URef, findable by using a dictionary_identifier.
GetDictionaryItemByIdentifier(ctx context.Context, stateRootHash *string, identifier ParamDictionaryIdentifier) (StateGetDictionaryResult, error)
// GetStateItem allows to get item from the global state
// If the param stateRootHash is nil, the client will make an additional RPC call to retrieve the latest stateRootHash.
// Deprecated: use QueryGlobalStateByStateHash instead
Expand Down
7 changes: 4 additions & 3 deletions rpc/error.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package rpc

import (
"encoding/json"
"fmt"
"net/http"
)

type RpcError struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data,omitempty"`
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
}

func (h *RpcError) Error() string {
Expand Down
32 changes: 22 additions & 10 deletions rpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,28 @@ func NewParamBlockByHash(hash string) ParamBlockIdentifier {
return ParamBlockIdentifier{BlockIdentifier: BlockIdentifier{Hash: hash}}
}

func NewParamStateDictionaryItem(stateRootHash, uref, key string) map[string]interface{} {
return map[string]interface{}{
"state_root_hash": stateRootHash,
"dictionary_identifier": map[string]interface{}{
"URef": map[string]string{
"dictionary_item_key": key,
"seed_uref": uref,
},
},
}
type ParamDictionaryIdentifier struct {
AccountNamedKey *AccountNamedKey `json:"AccountNamedKey,omitempty"`
ContractNamedKey *ParamDictionaryIdentifierContractNamedKey `json:"ContractNamedKey,omitempty"`
URef *ParamDictionaryIdentifierURef `json:"URef,omitempty"`
Dictionary *string `json:"Dictionary,omitempty"`
}

type AccountNamedKey struct {
Key string `json:"key"`
DictionaryName string `json:"dictionary_name"`
DictionaryItemKey string `json:"dictionary_item_key"`
}

type ParamDictionaryIdentifierContractNamedKey struct {
Key string `json:"key"`
DictionaryName string `json:"dictionary_name"`
DictionaryItemKey string `json:"dictionary_item_key"`
}

type ParamDictionaryIdentifierURef struct {
DictionaryItemKey string `json:"dictionary_item_key"`
SeedUref string `json:"seed_uref"`
}

type SpeculativeExecParams struct {
Expand Down
4 changes: 2 additions & 2 deletions rpc/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type StateGetAuctionInfoResult struct {
}

type StateGetBalanceResult struct {
ApiVersion string `json:"api_version"`
BalanceValue uint64 `json:"balance_value,string"`
ApiVersion string `json:"api_version"`
BalanceValue clvalue.UInt512 `json:"balance_value"`
}

type StateGetAccountInfo struct {
Expand Down
14 changes: 13 additions & 1 deletion rpc/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ func (c *client) GetAccountInfoByBlochHeight(ctx context.Context, blockHeight ui
}

func (c *client) GetDictionaryItem(ctx context.Context, stateRootHash *string, uref, key string) (StateGetDictionaryResult, error) {
return c.GetDictionaryItemByIdentifier(ctx, stateRootHash, ParamDictionaryIdentifier{
URef: &ParamDictionaryIdentifierURef{
DictionaryItemKey: key,
SeedUref: uref,
},
})
}

func (c *client) GetDictionaryItemByIdentifier(ctx context.Context, stateRootHash *string, identifier ParamDictionaryIdentifier) (StateGetDictionaryResult, error) {
if stateRootHash == nil {
latestHashResult, err := c.GetStateRootHashLatest(ctx)
if err != nil {
Expand All @@ -107,7 +116,10 @@ func (c *client) GetDictionaryItem(ctx context.Context, stateRootHash *string, u
stateRootHash = &latestHashString
}
var result StateGetDictionaryResult
return result, c.processRequest(ctx, MethodGetDictionaryItem, NewParamStateDictionaryItem(*stateRootHash, uref, key), &result)
return result, c.processRequest(ctx, MethodGetDictionaryItem, map[string]interface{}{
"state_root_hash": *stateRootHash,
"dictionary_identifier": identifier,
}, &result)
}

func (c *client) GetAccountBalance(ctx context.Context, stateRootHash *string, purseURef string) (StateGetBalanceResult, error) {
Expand Down
2 changes: 1 addition & 1 deletion tests/rpc/rpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func Test_DefaultClient_GetStateBalance(t *testing.T) {
"uref-7b12008bb757ee32caefb3f7a1f77d9f659ee7a4e21ad4950c4e0294000492eb-007",
)
require.NoError(t, err)
assert.NotEmpty(t, result.BalanceValue)
assert.Equal(t, "93000000000", result.BalanceValue.String())
}

func Test_DefaultClient_GetStateBalance_WithEmptyStateRootHash(t *testing.T) {
Expand Down