forked from joeqian10/neo3-gogogo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oracleResponse.go
82 lines (71 loc) · 2.08 KB
/
oracleResponse.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
package tx
import (
"fmt"
"github.com/mghnmtt/neo3-gogogo/helper"
"github.com/mghnmtt/neo3-gogogo/io"
"github.com/mghnmtt/neo3-gogogo/sc"
)
// only oracle can use this attribute
type OracleResponseAttribute struct {
FixedScript []byte
Id uint64
Code OracleResponseCode
Result []byte
}
func NewOracleResponseAttribute() (*OracleResponseAttribute, error) {
orcleContractHash, _ := helper.UInt160FromString("0x8dc0e742cbdfdeda51ff8a8b78d46829144c80ee")
sb := sc.NewScriptBuilder()
sb.EmitDynamicCall(orcleContractHash, "finish", nil) // 0x8dc0e742cbdfdeda51ff8a8b78d46829144c80ee
b, err := sb.ToArray()
if err != nil {
return nil, err
}
return &OracleResponseAttribute{
FixedScript: b,
}, nil
}
func (o *OracleResponseAttribute) GetMaxResultSize() int {
return 65535
}
func (o *OracleResponseAttribute) GetAttributeType() TransactionAttributeType {
return OracleResponse
}
func (o *OracleResponseAttribute) AllowMultiple() bool {
return false
}
func (o *OracleResponseAttribute) GetAttributeSize() int {
return 1 + // base size
8 + // Id
1 + // ResponseCode
sc.ByteSlice(o.Result).GetVarSize() // Result
}
func (o *OracleResponseAttribute) Deserialize(br *io.BinaryReader) {
if br.ReadByte() != byte(OracleResponse) {
br.Err = fmt.Errorf("format error: not HighPriority")
}
o.DeserializeWithoutType(br)
}
func (o *OracleResponseAttribute) Serialize(bw *io.BinaryWriter) {
bw.WriteLE(byte(OracleResponse))
o.SerializeWithoutType(bw)
}
func (o *OracleResponseAttribute) DeserializeWithoutType(br *io.BinaryReader) {
br.ReadLE(&o.Id)
code := br.ReadByte()
if OracleResponseCode(code).IsDefined() {
o.Code = OracleResponseCode(code)
} else {
br.Err = fmt.Errorf("format error: oracle response code is not defined")
return
}
result := br.ReadVarBytesWithMaxLimit(o.GetMaxResultSize())
if code != byte(Success) && len(result) > 0 {
br.Err = fmt.Errorf("format error: wrong result")
}
o.Result = result
}
func (o *OracleResponseAttribute) SerializeWithoutType(bw *io.BinaryWriter) {
bw.WriteLE(o.Id)
bw.WriteLE(byte(o.Code))
bw.WriteVarBytes(o.Result)
}