-
Notifications
You must be signed in to change notification settings - Fork 253
/
exec.go
71 lines (64 loc) · 2.55 KB
/
exec.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
// 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/33cn/chain33/common/address"
drivers "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
)
// Exec_Transfer transfer of exec
func (c *Coins) Exec_Transfer(transfer *types.AssetsTransfer, tx *types.Transaction, index int) (*types.Receipt, error) {
from := tx.From()
//to 是 execs 合约地址
var receipt *types.Receipt
var err error
if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) {
receipt, err = c.GetCoinsAccount().TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
} else {
receipt, err = c.GetCoinsAccount().Transfer(from, tx.GetRealToAddr(), transfer.Amount)
}
return receipt, err
}
// Exec_TransferToExec the transfer to exec address
func (c *Coins) Exec_TransferToExec(transfer *types.AssetsTransferToExec, tx *types.Transaction, index int) (*types.Receipt, error) {
types.AssertConfig(c.GetAPI())
cfg := c.GetAPI().GetConfig()
if !cfg.IsFork(c.GetHeight(), "ForkTransferExec") {
return nil, types.ErrActionNotSupport
}
from := tx.From()
//to 是 execs 合约地址
if !isExecAddrMatch(transfer.ExecName, tx.GetRealToAddr()) {
return nil, types.ErrToAddrNotSameToExecAddr
}
return c.GetCoinsAccount().TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
}
// Exec_Withdraw withdraw exec
func (c *Coins) Exec_Withdraw(withdraw *types.AssetsWithdraw, tx *types.Transaction, index int) (*types.Receipt, error) {
types.AssertConfig(c.GetAPI())
cfg := c.GetAPI().GetConfig()
if !cfg.IsFork(c.GetHeight(), "ForkWithdraw") {
withdraw.ExecName = ""
}
from := tx.From()
//to 是 execs 合约地址
if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) || isExecAddrMatch(withdraw.ExecName, tx.GetRealToAddr()) {
return c.GetCoinsAccount().TransferWithdraw(from, tx.GetRealToAddr(), withdraw.Amount)
}
return nil, types.ErrActionNotSupport
}
// Exec_Genesis genesis of exec
func (c *Coins) Exec_Genesis(genesis *types.AssetsGenesis, tx *types.Transaction, index int) (*types.Receipt, error) {
if c.GetHeight() == 0 {
if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) {
return c.GetCoinsAccount().GenesisInitExec(genesis.ReturnAddress, genesis.Amount, tx.GetRealToAddr())
}
return c.GetCoinsAccount().GenesisInit(tx.GetRealToAddr(), genesis.Amount)
}
return nil, types.ErrReRunGenesis
}
func isExecAddrMatch(name string, to string) bool {
toaddr := address.ExecAddress(name)
return toaddr == to
}