-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathuser.go
More file actions
60 lines (50 loc) · 1.28 KB
/
user.go
File metadata and controls
60 lines (50 loc) · 1.28 KB
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
52
53
54
55
56
57
58
59
60
package substrate
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/pkg/errors"
)
// User from substrate
type User struct {
Versioned
ID types.U32 `json:"id"`
Name string `json:"name"`
CountryID types.U32 `json:"country_id"`
CityID types.U32 `json:"city_id"`
Address AccountID `json:"address"`
}
// GetUser with id
func (s *Substrate) GetUser(id uint32) (*User, error) {
cl, meta, err := s.GetClient()
if err != nil {
return nil, err
}
bytes, err := Encode(id)
if err != nil {
return nil, errors.Wrap(err, "substrate: encoding error building query arguments")
}
key, err := types.CreateStorageKey(meta, "TfgridModule", "Entities", bytes, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create substrate query key")
}
raw, err := cl.RPC.State.GetStorageRawLatest(key)
if err != nil {
return nil, errors.Wrap(err, "failed to lookup entity")
}
if len(*raw) == 0 {
return nil, errors.Wrap(ErrNotFound, "entity not found")
}
version, err := s.getVersion(*raw)
if err != nil {
return nil, err
}
var user User
switch version {
case 1:
if err := Decode(*raw, &user); err != nil {
return nil, errors.Wrap(err, "failed to load object")
}
default:
return nil, ErrUnknownVersion
}
return &user, nil
}