-
Notifications
You must be signed in to change notification settings - Fork 39
/
transferhandler.go
98 lines (87 loc) · 3.07 KB
/
transferhandler.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
/*
* Copyright 2021 ICON Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package icon
import (
"math/big"
"github.com/icon-project/goloop/common/codec"
"github.com/icon-project/goloop/common/intconv"
"github.com/icon-project/goloop/common/log"
"github.com/icon-project/goloop/module"
"github.com/icon-project/goloop/service/contract"
"github.com/icon-project/goloop/service/scoreresult"
"github.com/icon-project/goloop/service/state"
"github.com/icon-project/goloop/service/txresult"
)
type TransferHandler struct {
*contract.CommonHandler
}
func newTransferHandler(from, to module.Address, value *big.Int, call bool, logger log.Logger) *TransferHandler {
return &TransferHandler{
contract.NewCommonHandler(from, to, value, call, logger),
}
}
func (h *TransferHandler) ExecuteSync(cc contract.CallContext) (err error, ro *codec.TypedObj, addr module.Address) {
h.Log.TSystemf("TRANSFER start from=%s to=%s value=%s", h.From, h.To, h.Value)
defer func() {
if err != nil {
h.Log.TSystemf("TRANSFER done status=%s msg=%v", err.Error(), err)
}
}()
if err2 := h.ApplyStepsForInterCall(cc); err2 != nil {
return err2, nil, nil
}
return h.DoExecuteSync(cc)
}
func (h *TransferHandler) DoExecuteSync(cc contract.CallContext) (err error, ro *codec.TypedObj, addr module.Address) {
if cc.ReadOnlyMode() {
return scoreresult.AccessDeniedError.New("TransferIsNotAllowed"), nil, nil
}
as1 := cc.GetAccountState(h.From.ID())
if as1.IsContract() != h.From.IsContract() {
return scoreresult.InvalidParameterError.Errorf(
"InvalidAddress(%s)", h.From.String()), nil, nil
}
if h.Value.Sign() == -1 {
return scoreresult.InvalidParameterError.Errorf(
"InvalidValue(value=%s)", h.Value.String()), nil, nil
}
bal1 := as1.GetBalance()
if bal1.Cmp(h.Value) < 0 {
return scoreresult.ErrOutOfBalance, nil, nil
}
as1.SetBalance(new(big.Int).Sub(bal1, h.Value))
as2 := cc.GetAccountState(h.To.ID())
opType := module.Transfer
recipient := h.To
if as2.IsContract() {
cc.Logger().Debugf("LOST transfer address=%s", h.To.String())
as2 = cc.GetAccountState(state.LostID)
opType = module.Lost
recipient = state.LostAddress
}
bal2 := as2.GetBalance()
as2.SetBalance(new(big.Int).Add(bal2, h.Value))
if h.From.IsContract() && h.Value.Sign() > 0 {
indexed := make([][]byte, 4)
indexed[0] = []byte(txresult.EventLogICXTransfer)
indexed[1] = h.From.Bytes()
indexed[2] = h.To.Bytes()
indexed[3] = intconv.BigIntToBytes(h.Value)
cc.OnEvent(h.From, indexed, make([][]byte, 0))
}
h.Log.OnBalanceChange(opType, h.From, recipient, h.Value)
return nil, nil, nil
}