-
Notifications
You must be signed in to change notification settings - Fork 199
/
smartContractResult.go
127 lines (103 loc) · 3.57 KB
/
smartContractResult.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package smartContractResult
import (
"io"
"math/big"
"github.com/ElrondNetwork/elrond-go/data/smartContractResult/capnp"
"github.com/glycerine/go-capnproto"
)
// SmartContractResult holds all the data needed for a value transfer
type SmartContractResult struct {
Nonce uint64 `capid:"0" json:"nonce"`
Value *big.Int `capid:"1" json:"value"`
RcvAddr []byte `capid:"2" json:"receiver"`
SndAddr []byte `capid:"3" json:"sender"`
Code []byte `capid:"4" json:"code,omitempty"`
Data string `capid:"5" json:"data,omitempty"`
TxHash []byte `capid:"6" json:"txHash"`
}
// Save saves the serialized data of a SmartContractResult into a stream through Capnp protocol
func (scr *SmartContractResult) Save(w io.Writer) error {
seg := capn.NewBuffer(nil)
SmartContractResultGoToCapn(seg, scr)
_, err := seg.WriteTo(w)
return err
}
// Load loads the data from the stream into a SmartContractResult object through Capnp protocol
func (scr *SmartContractResult) Load(r io.Reader) error {
capMsg, err := capn.ReadFromStream(r, nil)
if err != nil {
return err
}
z := capnp.ReadRootSmartContractResultCapn(capMsg)
SmartContractResultCapnToGo(z, scr)
return nil
}
// SmartContractResultCapnToGo is a helper function to copy fields from a SmartContractResultCapn object to a SmartContractResult object
func SmartContractResultCapnToGo(src capnp.SmartContractResultCapn, dest *SmartContractResult) *SmartContractResult {
if dest == nil {
dest = &SmartContractResult{}
}
if dest.Value == nil {
dest.Value = big.NewInt(0)
}
dest.Nonce = src.Nonce()
err := dest.Value.GobDecode(src.Value())
if err != nil {
return nil
}
dest.RcvAddr = src.RcvAddr()
dest.SndAddr = src.SndAddr()
dest.Data = string(src.Data())
dest.Code = src.Code()
dest.TxHash = src.TxHash()
return dest
}
// SmartContractResultGoToCapn is a helper function to copy fields from a SmartContractResult object to a SmartContractResultCapn object
func SmartContractResultGoToCapn(seg *capn.Segment, src *SmartContractResult) capnp.SmartContractResultCapn {
dest := capnp.AutoNewSmartContractResultCapn(seg)
value, _ := src.Value.GobEncode()
dest.SetNonce(src.Nonce)
dest.SetValue(value)
dest.SetRcvAddr(src.RcvAddr)
dest.SetSndAddr(src.SndAddr)
dest.SetData([]byte(src.Data))
dest.SetCode(src.Code)
dest.SetTxHash(src.TxHash)
return dest
}
// IsInterfaceNil verifies if underlying object is nil
func (scr *SmartContractResult) IsInterfaceNil() bool {
return scr == nil
}
// GetValue returns the value of the smart contract result
func (scr *SmartContractResult) GetValue() *big.Int {
return scr.Value
}
// GetData returns the data of the smart contract result
func (scr *SmartContractResult) GetData() string {
return scr.Data
}
// GetRecvAddress returns the receiver address from the smart contract result
func (scr *SmartContractResult) GetRecvAddress() []byte {
return scr.RcvAddr
}
// GetSndAddress returns the sender address from the smart contract result
func (scr *SmartContractResult) GetSndAddress() []byte {
return scr.SndAddr
}
// SetValue sets the value of the smart contract result
func (scr *SmartContractResult) SetValue(value *big.Int) {
scr.Value = value
}
// SetData sets the data of the smart contract result
func (scr *SmartContractResult) SetData(data string) {
scr.Data = data
}
// SetRecvAddress sets the receiver address of the smart contract result
func (scr *SmartContractResult) SetRecvAddress(addr []byte) {
scr.RcvAddr = addr
}
// SetSndAddress sets the sender address of the smart contract result
func (scr *SmartContractResult) SetSndAddress(addr []byte) {
scr.SndAddr = addr
}