forked from walkbean/vsys-sdk-go
/
contract_system.go
81 lines (71 loc) · 2.08 KB
/
contract_system.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
package vsys
const (
FuncIdxSysSend = 0
FuncIdxSysDeposit = 1
FuncIdxSysWithdraw = 2
FuncIdxSysTransfer = 3
)
type SystemContract struct {
Sender string
ContractId string
Recipient string
Amount int64
}
func (c *SystemContract) BuildSendData(recipient string, amount int64) []byte {
data := DataEncoder{}
data.EncodeArgAmount(2)
data.Encode(recipient, DeTypeAddress)
data.Encode(amount, DeTypeAmount)
return data.Result()
}
func (c *SystemContract) BuildDepositData(sender string, contractId string, amount int64) []byte {
data := DataEncoder{}
data.EncodeArgAmount(3)
data.Encode(sender, DeTypeAddress)
data.Encode(contractId, DeTypeContractAccount)
data.Encode(amount, DeTypeAmount)
return data.Result()
}
func (c *SystemContract) BuildWithdrawData(contractId string, recipient string, amount int64) []byte {
data := DataEncoder{}
data.EncodeArgAmount(3)
data.Encode(contractId, DeTypeContractAccount)
data.Encode(recipient, DeTypeAddress)
data.Encode(amount, DeTypeAmount)
return data.Result()
}
func (c *SystemContract) BuildTransferData(sender string, recipient string, amount int64) []byte {
data := DataEncoder{}
data.EncodeArgAmount(3)
data.Encode(sender, DeTypeAddress)
data.Encode(recipient, DeTypeAddress)
data.Encode(amount, DeTypeAmount)
return data.Result()
}
func (c *SystemContract) DecodeSend(data []byte) {
de := DataEncoder{}
list := de.Decode(data)
c.Recipient = list[0].Value.(string)
c.Amount = list[1].Value.(int64)
}
func (c *SystemContract) DecodeDeposit(data []byte) {
de := DataEncoder{}
list := de.Decode(data)
c.Sender = list[0].Value.(string)
c.ContractId = list[1].Value.(string)
c.Amount = list[2].Value.(int64)
}
func (c *SystemContract) DecodeWithdraw(data []byte) {
de := DataEncoder{}
list := de.Decode(data)
c.ContractId = list[0].Value.(string)
c.Recipient = list[1].Value.(string)
c.Amount = list[2].Value.(int64)
}
func (c *SystemContract) DecodeTransfer(data []byte) {
de := DataEncoder{}
list := de.Decode(data)
c.Sender = list[0].Value.(string)
c.Recipient = list[1].Value.(string)
c.Amount = list[1].Value.(int64)
}