This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
result.go
84 lines (71 loc) · 1.82 KB
/
result.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package types
// TODO: Using Borsh decoder later
import (
"encoding/binary"
"errors"
)
type BorshDecoder struct {
data []byte
offset uint32
}
func NewBorshDecoder(data []byte) BorshDecoder {
return BorshDecoder{
data: data,
offset: 0,
}
}
func (decoder *BorshDecoder) Finished() bool {
return decoder.offset == uint32(len(decoder.data))
}
func (decoder *BorshDecoder) DecodeString() (string, error) {
length, err := decoder.DecodeU32()
if err != nil {
return "", err
}
if uint32(len(decoder.data)) < decoder.offset+length {
return "", errors.New("Borsh: out of range")
}
val := string(decoder.data[decoder.offset : decoder.offset+uint32(length)])
decoder.offset += length
return val, nil
}
func (decoder *BorshDecoder) DecodeU8() (uint8, error) {
if uint32(len(decoder.data)) < decoder.offset+1 {
return 0, errors.New("Borsh: out of range")
}
val := uint8(decoder.data[decoder.offset])
decoder.offset++
return val, nil
}
func (decoder *BorshDecoder) DecodeU32() (uint32, error) {
if uint32(len(decoder.data)) < decoder.offset+4 {
return 0, errors.New("Borsh: out of range")
}
val := binary.LittleEndian.Uint32(decoder.data[decoder.offset : decoder.offset+4])
decoder.offset += 4
return val, nil
}
func (decoder *BorshDecoder) DecodeU64() (uint64, error) {
if uint32(len(decoder.data)) < decoder.offset+8 {
return 0, errors.New("Borsh: out of range")
}
val := binary.LittleEndian.Uint64(decoder.data[decoder.offset : decoder.offset+8])
decoder.offset += 8
return val, nil
}
type Result struct {
Px uint64
}
func DecodeResult(data []byte) (Result, error) {
decoder := NewBorshDecoder(data)
px, err := decoder.DecodeU64()
if err != nil {
return Result{}, err
}
if !decoder.Finished() {
return Result{}, errors.New("Borsh: bytes left when decode result")
}
return Result{
Px: px,
}, nil
}