-
Notifications
You must be signed in to change notification settings - Fork 1
/
transwithdraw.go
182 lines (171 loc) · 6.29 KB
/
transwithdraw.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package executor
import (
"github.com/assetcloud/chain/account"
"github.com/assetcloud/chain/common/address"
dbm "github.com/assetcloud/chain/common/db"
drivers "github.com/assetcloud/chain/system/dapp"
"github.com/assetcloud/chain/types"
tokenty "github.com/assetcloud/plugin/plugin/dapp/token/types"
)
func (t *token) ExecTransWithdraw(accountDB *account.DB, tx *types.Transaction, action *tokenty.TokenAction, index int) (*types.Receipt, error) {
cfg := t.GetAPI().GetConfig()
if (action.Ty == tokenty.ActionTransfer) && action.GetTransfer() != nil {
transfer := action.GetTransfer()
from := tx.From()
//to 是 execs 合约地址
if drivers.IsDriverAddress(tx.GetRealToAddr(), t.GetHeight()) {
return accountDB.TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
}
return accountDB.Transfer(from, tx.GetRealToAddr(), transfer.Amount)
} else if (action.Ty == tokenty.ActionWithdraw) && action.GetWithdraw() != nil {
withdraw := action.GetWithdraw()
if !cfg.IsFork(t.GetHeight(), "ForkWithdraw") {
withdraw.ExecName = ""
}
from := tx.From()
//to 是 execs 合约地址
if drivers.IsDriverAddress(tx.GetRealToAddr(), t.GetHeight()) || isExecAddrMatch(withdraw.ExecName, tx.GetRealToAddr()) {
return accountDB.TransferWithdraw(from, tx.GetRealToAddr(), withdraw.Amount)
}
return nil, types.ErrActionNotSupport
} else if (action.Ty == tokenty.ActionGenesis) && action.GetGenesis() != nil {
genesis := action.GetGenesis()
if t.GetHeight() == 0 {
if drivers.IsDriverAddress(tx.GetRealToAddr(), t.GetHeight()) {
return accountDB.GenesisInitExec(genesis.ReturnAddress, genesis.Amount, tx.GetRealToAddr())
}
return accountDB.GenesisInit(tx.GetRealToAddr(), genesis.Amount)
}
return nil, types.ErrReRunGenesis
} else if action.Ty == tokenty.TokenActionTransferToExec && action.GetTransferToExec() != nil {
if !cfg.IsFork(t.GetHeight(), "ForkTransferExec") {
return nil, types.ErrActionNotSupport
}
transfer := action.GetTransferToExec()
from := tx.From()
//to 是 execs 合约地址
if !isExecAddrMatch(transfer.ExecName, tx.GetRealToAddr()) {
return nil, types.ErrToAddrNotSameToExecAddr
}
return accountDB.TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
} else {
return nil, types.ErrActionNotSupport
}
}
func isExecAddrMatch(name string, to string) bool {
toaddr := address.ExecAddress(name)
return toaddr == to
}
//0: all tx
//1: from tx
//2: to tx
func (t *token) ExecLocalTransWithdraw(tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
if receipt.GetTy() != types.ExecOk {
return set, nil
}
//执行成功
var action tokenty.TokenAction
err := types.Decode(tx.GetPayload(), &action)
if err != nil {
panic(err)
}
var kv *types.KeyValue
if action.Ty == tokenty.ActionTransfer && action.GetTransfer() != nil {
transfer := action.GetTransfer()
kv, err = updateAddrReciver(t.GetLocalDB(), transfer.Cointoken, tx.GetRealToAddr(), transfer.Amount, true)
} else if action.Ty == tokenty.ActionWithdraw && action.GetWithdraw() != nil {
withdraw := action.GetWithdraw()
from := tx.From()
kv, err = updateAddrReciver(t.GetLocalDB(), withdraw.Cointoken, from, withdraw.Amount, true)
} else if action.Ty == tokenty.ActionGenesis && action.GetGenesis() != nil {
gen := action.GetGenesis()
kv, err = updateAddrReciver(t.GetLocalDB(), "token", tx.GetRealToAddr(), gen.Amount, true)
} else if action.Ty == tokenty.TokenActionTransferToExec && action.GetTransferToExec() != nil {
transfer := action.GetTransferToExec()
kv, err = updateAddrReciver(t.GetLocalDB(), transfer.Cointoken, tx.GetRealToAddr(), transfer.Amount, true)
}
if err != nil {
return set, nil
}
if kv != nil {
set.KV = append(set.KV, kv)
}
return set, nil
}
func (t *token) ExecDelLocalLocalTransWithdraw(tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
set := &types.LocalDBSet{}
if receipt.GetTy() != types.ExecOk {
return set, nil
}
//执行成功
var action tokenty.TokenAction
err := types.Decode(tx.GetPayload(), &action)
if err != nil {
panic(err)
}
var kv *types.KeyValue
if action.Ty == tokenty.ActionTransfer && action.GetTransfer() != nil {
transfer := action.GetTransfer()
kv, err = updateAddrReciver(t.GetLocalDB(), transfer.Cointoken, tx.GetRealToAddr(), transfer.Amount, false)
} else if action.Ty == tokenty.ActionWithdraw && action.GetWithdraw() != nil {
withdraw := action.GetWithdraw()
from := tx.From()
kv, err = updateAddrReciver(t.GetLocalDB(), withdraw.Cointoken, from, withdraw.Amount, false)
} else if action.Ty == tokenty.TokenActionTransferToExec && action.GetTransferToExec() != nil {
transfer := action.GetTransferToExec()
kv, err = updateAddrReciver(t.GetLocalDB(), transfer.Cointoken, tx.GetRealToAddr(), transfer.Amount, false)
}
if err != nil {
return set, nil
}
if kv != nil {
set.KV = append(set.KV, kv)
}
return set, nil
}
func getAddrReciverKV(token string, addr string, reciverAmount int64) *types.KeyValue {
reciver := &types.Int64{Data: reciverAmount}
amountbytes := types.Encode(reciver)
kv := &types.KeyValue{Key: calcAddrKey(token, addr), Value: amountbytes}
return kv
}
func getAddrReciver(db dbm.KVDB, token string, addr string) (int64, error) {
reciver := types.Int64{}
addrReciver, err := db.Get(calcAddrKey(token, addr))
if err != nil && err != types.ErrNotFound {
return 0, err
}
if len(addrReciver) == 0 {
return 0, nil
}
err = types.Decode(addrReciver, &reciver)
if err != nil {
return 0, err
}
return reciver.Data, nil
}
func setAddrReciver(db dbm.KVDB, token string, addr string, reciverAmount int64) error {
kv := getAddrReciverKV(addr, token, reciverAmount)
return db.Set(kv.Key, kv.Value)
}
func updateAddrReciver(cachedb dbm.KVDB, token string, addr string, amount int64, isadd bool) (*types.KeyValue, error) {
recv, err := getAddrReciver(cachedb, token, addr)
if err != nil && err != types.ErrNotFound {
return nil, err
}
if isadd {
recv += amount
} else {
recv -= amount
}
err = setAddrReciver(cachedb, token, addr, recv)
if err != nil {
return nil, err
}
//keyvalue
return getAddrReciverKV(token, addr, recv), nil
}