/
transfer_v5.go
101 lines (87 loc) · 2.92 KB
/
transfer_v5.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
package tx
import (
"bytes"
"encoding/json"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
"github.com/amolabs/amoabci/amo/code"
"github.com/amolabs/amoabci/amo/store"
"github.com/amolabs/amoabci/amo/types"
)
type TransferParamV5 struct {
To crypto.Address `json:"to"`
UDC uint32 `json:"udc,omitempty"`
Amount types.Currency `json:"amount,omitempty"`
Parcel tmbytes.HexBytes `json:"parcel,omitempty"`
}
func parseTransferParamV5(raw []byte) (TransferParamV5, error) {
var param TransferParamV5
err := json.Unmarshal(raw, ¶m)
return param, err
}
type TxTransferV5 struct {
TxBase
Param TransferParamV5 `json:"-"`
}
var _ Tx = &TxTransferV5{}
func (t *TxTransferV5) Check() (uint32, string) {
txParam, err := parseTransferParamV5(t.getPayload())
if err != nil {
return code.TxCodeBadParam, err.Error()
}
if len(txParam.To) != crypto.AddressSize {
return code.TxCodeBadParam, "wrong recipient address size"
}
if bytes.Equal(t.GetSender(), txParam.To) {
return code.TxCodeSelfTransaction, "tried to transfer to self"
}
if len(txParam.Parcel) == 0 && txParam.Amount.Equals(zero) {
return code.TxCodeBadParam, "either parcel or coin should be specifed"
}
return code.TxCodeOK, "ok"
}
func (t *TxTransferV5) Execute(store *store.Store) (uint32, string, []abci.Event) {
txParam, err := parseTransferParamV5(t.getPayload())
if err != nil {
return code.TxCodeBadParam, err.Error(), nil
}
if len(txParam.Parcel) > 0 {
return t.TransferParcel(store, txParam)
} else if txParam.Amount.GreaterThan(zero) {
return t.TransferCoin(store, txParam)
} else {
return code.TxCodeBadParam,
"either parcel or coin should be specifed",
nil
}
}
func (t *TxTransferV5) TransferCoin(store *store.Store, txParam TransferParamV5) (uint32, string, []abci.Event) {
udc := txParam.UDC
udcLock := store.GetUDCLock(udc, t.GetSender(), false)
fromBalance := store.GetUDCBalance(udc, t.GetSender(), false)
required := udcLock
required.Add(&txParam.Amount)
if fromBalance.LessThan(required) {
return code.TxCodeNotEnoughBalance, "not enough balance", nil
}
toBalance := store.GetUDCBalance(txParam.UDC, txParam.To, false)
fromBalance.Sub(&txParam.Amount)
toBalance.Add(&txParam.Amount)
store.SetUDCBalance(txParam.UDC, t.GetSender(), fromBalance)
store.SetUDCBalance(txParam.UDC, txParam.To, toBalance)
return code.TxCodeOK, "ok", nil
}
func (t *TxTransferV5) TransferParcel(store *store.Store, txParam TransferParamV5) (uint32, string, []abci.Event) {
parcel := store.GetParcel(txParam.Parcel, false)
if parcel == nil {
return code.TxCodeParcelNotFound, "parcel not found", nil
}
sender := t.GetSender()
if !bytes.Equal(sender, parcel.Owner) {
return code.TxCodePermissionDenied, "permission denied", nil
}
parcel.Owner = txParam.To
store.SetParcel(txParam.Parcel, parcel)
return code.TxCodeOK, "ok", nil
}