|
| 1 | +package substrate |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/centrifuge/go-substrate-rpc-client/v4/types" |
| 7 | + "github.com/pkg/errors" |
| 8 | +) |
| 9 | + |
| 10 | +func (s *Substrate) KVStoreSet(identity Identity, key string, value string) error { |
| 11 | + cl, meta, err := s.GetClient() |
| 12 | + if err != nil { |
| 13 | + return err |
| 14 | + } |
| 15 | + |
| 16 | + c, err := types.NewCall(meta, "TFKVStore.set", |
| 17 | + key, value, |
| 18 | + ) |
| 19 | + if err != nil { |
| 20 | + return errors.Wrap(err, "failed to create call") |
| 21 | + } |
| 22 | + |
| 23 | + res, err := s.Call(cl, meta, identity, c) |
| 24 | + if err != nil { |
| 25 | + return errors.Wrap(err, "failed to create contract") |
| 26 | + } |
| 27 | + |
| 28 | + if err := s.checkForError(res); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + return nil |
| 33 | +} |
| 34 | + |
| 35 | +func (s *Substrate) KVStoreDelete(identity Identity, key string) error { |
| 36 | + cl, meta, err := s.GetClient() |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + c, err := types.NewCall(meta, "TFKVStore.delete", |
| 42 | + key, |
| 43 | + ) |
| 44 | + if err != nil { |
| 45 | + return errors.Wrap(err, "failed to create call") |
| 46 | + } |
| 47 | + |
| 48 | + res, err := s.Call(cl, meta, identity, c) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrap(err, "failed to create contract") |
| 51 | + } |
| 52 | + |
| 53 | + if err := s.checkForError(res); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (s *Substrate) KVStoreGet(identity Identity, key string) ([]byte, error) { |
| 60 | + cl, meta, err := s.GetClient() |
| 61 | + if err != nil { |
| 62 | + return nil, err |
| 63 | + } |
| 64 | + |
| 65 | + bytes, err := Encode(key) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + storageKey, err := types.CreateStorageKey(meta, "TFKVStore", "TFKVStore", identity.PublicKey(), bytes) |
| 71 | + if err != nil { |
| 72 | + return nil, errors.Wrap(err, "failed to create substrate query key") |
| 73 | + } |
| 74 | + |
| 75 | + var value []byte |
| 76 | + ok, err := cl.RPC.State.GetStorageLatest(storageKey, &value) |
| 77 | + if err != nil { |
| 78 | + return nil, errors.Wrap(err, "failed to lookup entity") |
| 79 | + } |
| 80 | + |
| 81 | + if !ok { |
| 82 | + return nil, errors.Wrap(ErrNotFound, "key not found") |
| 83 | + } |
| 84 | + |
| 85 | + return value, nil |
| 86 | +} |
| 87 | + |
| 88 | +type Val struct { |
| 89 | + Id string |
| 90 | + Key string |
| 91 | +} |
| 92 | + |
| 93 | +func (s *Substrate) KVStoreList(identity Identity) (map[string]string, error) { |
| 94 | + cl, meta, err := s.GetClient() |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + bytes, err := Encode(types.NewOptionBytes1024Empty()) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + storageKey, err := types.CreateStorageKey(meta, "TFKVStore", "TFKVStore", identity.PublicKey(), bytes) |
| 105 | + if err != nil { |
| 106 | + return nil, errors.Wrap(err, "failed to create substrate query key") |
| 107 | + } |
| 108 | + |
| 109 | + keys, err := cl.RPC.State.GetKeysLatest(storageKey) |
| 110 | + if err != nil { |
| 111 | + return nil, errors.Wrap(err, "failed to lookup entity") |
| 112 | + } |
| 113 | + |
| 114 | + fmt.Println(len(keys)) |
| 115 | + |
| 116 | + query, err := cl.RPC.State.QueryStorageAtLatest(keys) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + pairs := make(map[string]string) |
| 122 | + for _, q := range query { |
| 123 | + for _, c := range q.Changes { |
| 124 | + pairs[string(c.StorageKey)] = string(c.StorageData) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return pairs, nil |
| 129 | +} |
0 commit comments