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

How to get StorageDoubleMap keys under a primary key prefix? #365

Open
khssnv opened this issue Nov 16, 2023 · 1 comment
Open

How to get StorageDoubleMap keys under a primary key prefix? #365

khssnv opened this issue Nov 16, 2023 · 1 comment

Comments

@khssnv
Copy link

khssnv commented Nov 16, 2023

Hello. The types.CreateStorageKey returns an error in an attempt to create a key without secondary key of the StorageDoubleMap specified. Please find a minimal example below.

At the same time state_getKeys method allows to query by pallet name + storage item name for all keys and by pallet name + storage item name + primary key for all secondary keys under the primary key.

How to create a key for querying for all the secondary keys under a specific primary key prefix?

Minimal example.

Querying FRAME pallet-staking pallet, ErasStakers storage item with 1261 era index.

package main

import (
	gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
	"github.com/centrifuge/go-substrate-rpc-client/v4/types"
	"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)

func main() {
	api, _ := gsrpc.NewSubstrateAPI("wss://rpc.polkadot.io")
	meta, _ := api.RPC.State.GetMetadataLatest()

	var eraIndex types.U32 = 1261
	bytes, _ := codec.Encode(eraIndex)

	key, err := types.CreateStorageKey(meta, "Staking", "ErasStakers", bytes, nil)
	if err != nil {
		panic(err)
	}
	println(key)
}

Output.

$ go run .
2023/11/16 11:45:32 Connecting to wss://rpc.polkadot.io...
panic: Staking:ErasStakers is a map, therefore requires that number of arguments should exactly match number of hashers in metadata. Expected: 2, received: 1

goroutine 1 [running]:
main.main()
        /example/main.go:18 +0x12e
exit status 2

Keys that works with state_getKeys call.

pallet-staking module: 0x5f3e4907f716ac89b6347d15ececedca
pallet-staking module + ErasStakers method: 0x5f3e4907f716ac89b6347d15ececedca8bde0a0ea8864605e3b68ed9cb2da01b
pallet-staking module + ErasStakers method + 1261 era index primary key: 0x5f3e4907f716ac89b6347d15ececedca8bde0a0ea8864605e3b68ed9cb2da01bba856e6711ab17a6ed040000
@cdamian
Copy link
Contributor

cdamian commented Dec 5, 2023

Hi @khssnv,

Please refer to the answers provided in - #300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants