github.com/turingchain2020/turingchain@v1.1.21/system/dapp/coins/executor/exec.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package executor
     6  
     7  import (
     8  	"github.com/turingchain2020/turingchain/common/address"
     9  	drivers "github.com/turingchain2020/turingchain/system/dapp"
    10  	"github.com/turingchain2020/turingchain/types"
    11  )
    12  
    13  // Exec_Transfer transfer of exec
    14  func (c *Coins) Exec_Transfer(transfer *types.AssetsTransfer, tx *types.Transaction, index int) (*types.Receipt, error) {
    15  	from := tx.From()
    16  	//to 是 execs 合约地址
    17  	if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) {
    18  		return c.GetCoinsAccount().TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
    19  	}
    20  	return c.GetCoinsAccount().Transfer(from, tx.GetRealToAddr(), transfer.Amount)
    21  }
    22  
    23  // Exec_TransferToExec the transfer to exec address
    24  func (c *Coins) Exec_TransferToExec(transfer *types.AssetsTransferToExec, tx *types.Transaction, index int) (*types.Receipt, error) {
    25  	types.AssertConfig(c.GetAPI())
    26  	cfg := c.GetAPI().GetConfig()
    27  	if !cfg.IsFork(c.GetHeight(), "ForkTransferExec") {
    28  		return nil, types.ErrActionNotSupport
    29  	}
    30  	from := tx.From()
    31  	//to 是 execs 合约地址
    32  	if !isExecAddrMatch(transfer.ExecName, tx.GetRealToAddr()) {
    33  		return nil, types.ErrToAddrNotSameToExecAddr
    34  	}
    35  	return c.GetCoinsAccount().TransferToExec(from, tx.GetRealToAddr(), transfer.Amount)
    36  }
    37  
    38  // Exec_Withdraw withdraw exec
    39  func (c *Coins) Exec_Withdraw(withdraw *types.AssetsWithdraw, tx *types.Transaction, index int) (*types.Receipt, error) {
    40  	types.AssertConfig(c.GetAPI())
    41  	cfg := c.GetAPI().GetConfig()
    42  	if !cfg.IsFork(c.GetHeight(), "ForkWithdraw") {
    43  		withdraw.ExecName = ""
    44  	}
    45  	from := tx.From()
    46  	//to 是 execs 合约地址
    47  	if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) || isExecAddrMatch(withdraw.ExecName, tx.GetRealToAddr()) {
    48  		return c.GetCoinsAccount().TransferWithdraw(from, tx.GetRealToAddr(), withdraw.Amount)
    49  	}
    50  	return nil, types.ErrActionNotSupport
    51  }
    52  
    53  // Exec_Genesis genesis of exec
    54  func (c *Coins) Exec_Genesis(genesis *types.AssetsGenesis, tx *types.Transaction, index int) (*types.Receipt, error) {
    55  	if c.GetHeight() == 0 {
    56  		if drivers.IsDriverAddress(tx.GetRealToAddr(), c.GetHeight()) {
    57  			return c.GetCoinsAccount().GenesisInitExec(genesis.ReturnAddress, genesis.Amount, tx.GetRealToAddr())
    58  		}
    59  		return c.GetCoinsAccount().GenesisInit(tx.GetRealToAddr(), genesis.Amount)
    60  	}
    61  	return nil, types.ErrReRunGenesis
    62  }
    63  
    64  func isExecAddrMatch(name string, to string) bool {
    65  	toaddr := address.ExecAddress(name)
    66  	return toaddr == to
    67  }