Skip to content

Commit 32a0df6

Browse files
Merge pull request #13 from InjectiveLabs/band-ibc
2 parents acded7c + f698137 commit 32a0df6

File tree

17 files changed

+14677
-0
lines changed

17 files changed

+14677
-0
lines changed

bandchain/hooks/price/types.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package price
2+
3+
import "github.com/InjectiveLabs/sdk-go/bandchain/oracle/types"
4+
5+
type Input struct {
6+
Symbols []string `json:"symbols"`
7+
Multiplier uint64 `json:"multiplier"`
8+
}
9+
10+
type Output struct {
11+
Pxs []uint64 `json:"pxs"`
12+
}
13+
14+
type Price struct {
15+
Symbol string `json:"symbol"`
16+
Multiplier uint64 `json:"multiplier"`
17+
Px uint64 `json:"px"`
18+
RequestID types.RequestID `json:"request_id"`
19+
ResolveTime int64 `json:"resolve_time"`
20+
}
21+
22+
func NewPrice(symbol string, multiplier uint64, px uint64, reqID types.RequestID, resolveTime int64) Price {
23+
return Price{
24+
Symbol: symbol,
25+
Multiplier: multiplier,
26+
Px: px,
27+
RequestID: reqID,
28+
ResolveTime: resolveTime,
29+
}
30+
}

bandchain/obi/decode.go

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package obi
2+
3+
import (
4+
"encoding/binary"
5+
"errors"
6+
"fmt"
7+
"reflect"
8+
)
9+
10+
func decodeImpl(data []byte, v interface{}) ([]byte, error) {
11+
rv := reflect.ValueOf(v)
12+
if rv.Kind() != reflect.Ptr || rv.IsNil() {
13+
return nil, errors.New("obi: decode into non-ptr type")
14+
}
15+
ev := rv.Elem()
16+
switch ev.Kind() {
17+
case reflect.Uint8:
18+
val, rem, err := DecodeUnsigned8(data)
19+
ev.SetUint(uint64(val))
20+
return rem, err
21+
case reflect.Uint16:
22+
val, rem, err := DecodeUnsigned16(data)
23+
ev.SetUint(uint64(val))
24+
return rem, err
25+
case reflect.Uint32:
26+
val, rem, err := DecodeUnsigned32(data)
27+
ev.SetUint(uint64(val))
28+
return rem, err
29+
case reflect.Uint64:
30+
val, rem, err := DecodeUnsigned64(data)
31+
ev.SetUint(uint64(val))
32+
return rem, err
33+
case reflect.Int8:
34+
val, rem, err := DecodeSigned8(data)
35+
ev.SetInt(int64(val))
36+
return rem, err
37+
case reflect.Int16:
38+
val, rem, err := DecodeSigned16(data)
39+
ev.SetInt(int64(val))
40+
return rem, err
41+
case reflect.Int32:
42+
val, rem, err := DecodeSigned32(data)
43+
ev.SetInt(int64(val))
44+
return rem, err
45+
case reflect.Int64:
46+
val, rem, err := DecodeSigned64(data)
47+
ev.SetInt(int64(val))
48+
return rem, err
49+
case reflect.String:
50+
val, rem, err := DecodeString(data)
51+
ev.SetString(val)
52+
return rem, err
53+
case reflect.Slice:
54+
if ev.Type().Elem().Kind() == reflect.Uint8 {
55+
val, rem, err := DecodeBytes(data)
56+
ev.SetBytes(val)
57+
return rem, err
58+
}
59+
length, rem, err := DecodeUnsigned32(data)
60+
if err != nil {
61+
return nil, err
62+
}
63+
slice := reflect.MakeSlice(ev.Type(), int(length), int(length))
64+
for idx := 0; idx < int(length); idx++ {
65+
var err error
66+
rem, err = decodeImpl(rem, slice.Index(idx).Addr().Interface())
67+
if err != nil {
68+
return nil, err
69+
}
70+
}
71+
ev.Set(slice)
72+
return rem, nil
73+
case reflect.Struct:
74+
rem := data
75+
for idx := 0; idx < ev.NumField(); idx++ {
76+
var err error
77+
rem, err = decodeImpl(rem, ev.Field(idx).Addr().Interface())
78+
if err != nil {
79+
return nil, err
80+
}
81+
}
82+
return rem, nil
83+
default:
84+
return nil, fmt.Errorf("obi: unsupported value type: %s", ev.Kind())
85+
}
86+
}
87+
88+
// Decode uses obi encoding scheme to decode the given input(s).
89+
func Decode(data []byte, v ...interface{}) error {
90+
var err error
91+
rem := data
92+
for _, each := range v {
93+
rem, err = decodeImpl(rem, each)
94+
if err != nil {
95+
return err
96+
}
97+
}
98+
if len(rem) != 0 {
99+
return errors.New("obi: not all data was consumed while decoding")
100+
}
101+
return nil
102+
}
103+
104+
// MustDecode uses obi encoding scheme to decode the given input. Panics on error.
105+
func MustDecode(data []byte, v ...interface{}) {
106+
err := Decode(data, v...)
107+
if err != nil {
108+
panic(err)
109+
}
110+
}
111+
112+
// DecodeUnsigned16 decodes the input bytes into `uint8` and returns the remaining bytes.
113+
func DecodeUnsigned8(data []byte) (uint8, []byte, error) {
114+
if len(data) < 1 {
115+
return 0, nil, errors.New("obi: out of range")
116+
}
117+
return data[0], data[1:], nil
118+
}
119+
120+
// DecodeUnsigned16 decodes the input bytes into `uint16` and returns the remaining bytes.
121+
func DecodeUnsigned16(data []byte) (uint16, []byte, error) {
122+
if len(data) < 2 {
123+
return 0, nil, errors.New("obi: out of range")
124+
}
125+
return binary.BigEndian.Uint16(data[:2]), data[2:], nil
126+
}
127+
128+
// DecodeUnsigned32 decodes the input bytes into `uint32` and returns the remaining bytes.
129+
func DecodeUnsigned32(data []byte) (uint32, []byte, error) {
130+
if len(data) < 4 {
131+
return 0, nil, errors.New("obi: out of range")
132+
}
133+
return binary.BigEndian.Uint32(data[:4]), data[4:], nil
134+
}
135+
136+
// DecodeUnsigned64 decodes the input bytes into `uint64` and returns the remaining bytes.
137+
func DecodeUnsigned64(data []byte) (uint64, []byte, error) {
138+
if len(data) < 8 {
139+
return 0, nil, errors.New("obi: out of range")
140+
}
141+
return binary.BigEndian.Uint64(data[:8]), data[8:], nil
142+
}
143+
144+
// DecodeSigned8 decodes the input bytes into `uint64` and returns the remaining bytes.
145+
func DecodeSigned8(data []byte) (int8, []byte, error) {
146+
unsigned, rem, err := DecodeUnsigned8(data)
147+
return int8(unsigned), rem, err
148+
}
149+
150+
// DecodeSigned16 decodes the input bytes into `uint64` and returns the remaining bytes.
151+
func DecodeSigned16(data []byte) (int16, []byte, error) {
152+
unsigned, rem, err := DecodeUnsigned16(data)
153+
return int16(unsigned), rem, err
154+
}
155+
156+
// DecodeSigned32 decodes the input bytes into `uint64` and returns the remaining bytes.
157+
func DecodeSigned32(data []byte) (int32, []byte, error) {
158+
unsigned, rem, err := DecodeUnsigned32(data)
159+
return int32(unsigned), rem, err
160+
}
161+
162+
// DecodeSigned64 decodes the input bytes into `uint64` and returns the remaining bytes.
163+
func DecodeSigned64(data []byte) (int64, []byte, error) {
164+
unsigned, rem, err := DecodeUnsigned64(data)
165+
return int64(unsigned), rem, err
166+
}
167+
168+
// DecodeBytes decodes the input bytes and returns bytes result and the remaining bytes.
169+
func DecodeBytes(data []byte) ([]byte, []byte, error) {
170+
length, rem, err := DecodeUnsigned32(data)
171+
if err != nil {
172+
return nil, nil, err
173+
}
174+
if uint32(len(rem)) < length {
175+
return nil, nil, errors.New("obi: out of range")
176+
}
177+
return rem[:length], rem[length:], nil
178+
}
179+
180+
// DecodeString decodes the input bytes and returns string result and the remaining bytes.
181+
func DecodeString(data []byte) (string, []byte, error) {
182+
length, rem, err := DecodeUnsigned32(data)
183+
if err != nil {
184+
return "", nil, err
185+
}
186+
if uint32(len(rem)) < length {
187+
return "", nil, errors.New("obi: out of range")
188+
}
189+
return string(rem[:length]), rem[length:], nil
190+
}

bandchain/obi/encode.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package obi
2+
3+
import (
4+
"encoding/binary"
5+
"fmt"
6+
"reflect"
7+
)
8+
9+
// Encode uses obi encoding scheme to encode the given input into bytes.
10+
func encodeImpl(v interface{}) ([]byte, error) {
11+
rv := reflect.ValueOf(v)
12+
switch rv.Kind() {
13+
case reflect.Uint8:
14+
return EncodeUnsigned8(uint8(rv.Uint())), nil
15+
case reflect.Uint16:
16+
return EncodeUnsigned16(uint16(rv.Uint())), nil
17+
case reflect.Uint32:
18+
return EncodeUnsigned32(uint32(rv.Uint())), nil
19+
case reflect.Uint64:
20+
return EncodeUnsigned64(uint64(rv.Uint())), nil
21+
case reflect.Int8:
22+
return EncodeSigned8(int8(rv.Int())), nil
23+
case reflect.Int16:
24+
return EncodeSigned16(int16(rv.Int())), nil
25+
case reflect.Int32:
26+
return EncodeSigned32(int32(rv.Int())), nil
27+
case reflect.Int64:
28+
return EncodeSigned64(int64(rv.Int())), nil
29+
case reflect.String:
30+
return EncodeString(rv.String()), nil
31+
case reflect.Slice:
32+
if rv.Type().Elem().Kind() == reflect.Uint8 {
33+
return EncodeBytes(rv.Bytes()), nil
34+
}
35+
36+
res := EncodeUnsigned32(uint32(rv.Len()))
37+
for idx := 0; idx < rv.Len(); idx++ {
38+
each, err := Encode(rv.Index(idx).Interface())
39+
if err != nil {
40+
return nil, err
41+
}
42+
res = append(res, each...)
43+
}
44+
return res, nil
45+
case reflect.Struct:
46+
res := []byte{}
47+
for idx := 0; idx < rv.NumField(); idx++ {
48+
each, err := Encode(rv.Field(idx).Interface())
49+
if err != nil {
50+
return nil, err
51+
}
52+
res = append(res, each...)
53+
}
54+
return res, nil
55+
default:
56+
return nil, fmt.Errorf("obi: unsupported value type: %s", rv.Kind())
57+
}
58+
}
59+
60+
// Encode uses obi encoding scheme to encode the given input(s) into bytes.
61+
func Encode(v ...interface{}) ([]byte, error) {
62+
res := []byte{}
63+
for _, each := range v {
64+
encoded, err := encodeImpl(each)
65+
if err != nil {
66+
return nil, err
67+
}
68+
res = append(res, encoded...)
69+
}
70+
return res, nil
71+
}
72+
73+
// MustEncode uses obi encoding scheme to encode the given input into bytes. Panics on error.
74+
func MustEncode(v ...interface{}) []byte {
75+
res, err := Encode(v...)
76+
if err != nil {
77+
panic(err)
78+
}
79+
return res
80+
}
81+
82+
// EncodeUnsigned8 takes an `uint8` variable and encodes it into a byte array
83+
func EncodeUnsigned8(v uint8) []byte {
84+
return []byte{v}
85+
}
86+
87+
// EncodeUnsigned16 takes an `uint16` variable and encodes it into a byte array
88+
func EncodeUnsigned16(v uint16) []byte {
89+
bytes := make([]byte, 2)
90+
binary.BigEndian.PutUint16(bytes, v)
91+
return bytes
92+
}
93+
94+
// EncodeUnsigned32 takes an `uint32` variable and encodes it into a byte array
95+
func EncodeUnsigned32(v uint32) []byte {
96+
bytes := make([]byte, 4)
97+
binary.BigEndian.PutUint32(bytes, v)
98+
return bytes
99+
}
100+
101+
// EncodeUnsigned64 takes an `uint64` variable and encodes it into a byte array
102+
func EncodeUnsigned64(v uint64) []byte {
103+
bytes := make([]byte, 8)
104+
binary.BigEndian.PutUint64(bytes, v)
105+
return bytes
106+
}
107+
108+
// EncodeSigned8 takes an `int8` variable and encodes it into a byte array
109+
func EncodeSigned8(v int8) []byte {
110+
return EncodeUnsigned8(uint8(v))
111+
}
112+
113+
// EncodeSigned16 takes an `int16` variable and encodes it into a byte array
114+
func EncodeSigned16(v int16) []byte {
115+
return EncodeUnsigned16(uint16(v))
116+
}
117+
118+
// EncodeSigned32 takes an `int32` variable and encodes it into a byte array
119+
func EncodeSigned32(v int32) []byte {
120+
return EncodeUnsigned32(uint32(v))
121+
}
122+
123+
// EncodeSigned64 takes an `int64` variable and encodes it into a byte array
124+
func EncodeSigned64(v int64) []byte {
125+
return EncodeUnsigned64(uint64(v))
126+
}
127+
128+
// EncodeBytes takes a `[]byte` variable and encodes it into a byte array
129+
func EncodeBytes(v []byte) []byte {
130+
return append(EncodeUnsigned32(uint32(len(v))), v...)
131+
}
132+
133+
// EncodeString takes a `string` variable and encodes it into a byte array
134+
func EncodeString(v string) []byte {
135+
return append(EncodeUnsigned32(uint32(len(v))), []byte(v)...)
136+
}

0 commit comments

Comments
 (0)