forked from crustio/go-substrate-rpc-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_local_storage.go
51 lines (39 loc) · 1 KB
/
get_local_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package offchain
import (
"fmt"
"github.com/MyronFanQiu/go-substrate-rpc-client/v3/types"
)
// StorageKind ...
type StorageKind string
const (
// Persistent storage
Persistent StorageKind = "PERSISTENT"
// Local storage
Local StorageKind = "LOCAL"
)
// LocalStorageGet retrieves the stored data
func (c *Offchain) LocalStorageGet(kind StorageKind, key []byte) (*types.StorageDataRaw, error) {
var res string
err := c.client.Call(&res, "offchain_localStorageGet", kind, fmt.Sprintf("%#x", key))
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
b, err := types.HexDecodeString(res)
if err != nil {
return nil, err
}
data := types.NewStorageDataRaw(b)
return &data, nil
}
// LocalStorageSet saves the data
func (c *Offchain) LocalStorageSet(kind StorageKind, key []byte, value []byte) error {
var res string
err := c.client.Call(&res, "offchain_localStorageSet", kind, fmt.Sprintf("%#x", key), fmt.Sprintf("%#x", value))
if err != nil {
return err
}
return nil
}