github.com/turingchain2020/turingchain@v1.1.21/system/dapp/coins/autotest/withdrawCase.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 autotest
     6  
     7  import (
     8  	"strconv"
     9  
    10  	"github.com/turingchain2020/turingchain/cmd/autotest/types"
    11  )
    12  
    13  // WithdrawCase defines the withdraw case
    14  type WithdrawCase struct {
    15  	types.BaseCase
    16  	Addr   string `toml:"addr"`
    17  	Amount string `toml:"amount"`
    18  }
    19  
    20  // WithdrawPack defines the withdraw pack
    21  type WithdrawPack struct {
    22  	types.BaseCasePack
    23  }
    24  
    25  // SendCommand send command of withdrawcase
    26  func (testCase *WithdrawCase) SendCommand(packID string) (types.PackFunc, error) {
    27  
    28  	return types.DefaultSend(testCase, &WithdrawPack{}, packID)
    29  }
    30  
    31  // GetCheckHandlerMap get check handler for map
    32  func (pack *WithdrawPack) GetCheckHandlerMap() interface{} {
    33  
    34  	funcMap := make(types.CheckHandlerMapDiscard, 1)
    35  	funcMap["balance"] = pack.checkBalance
    36  
    37  	return funcMap
    38  }
    39  
    40  func (pack *WithdrawPack) checkBalance(txInfo map[string]interface{}) bool {
    41  
    42  	/*fromAddr := txInfo["tx"].(map[string]interface{})["from"].(string)
    43  	toAddr := txInfo["tx"].(map[string]interface{})["to"].(string)*/
    44  	interCase := pack.TCase.(*WithdrawCase)
    45  	feeStr := txInfo["tx"].(map[string]interface{})["fee"].(string)
    46  	withdrawFrom := txInfo["fromaddr"].(string)
    47  	logArr := txInfo["receipt"].(map[string]interface{})["logs"].([]interface{})
    48  	logFee := logArr[0].(map[string]interface{})["log"].(map[string]interface{})
    49  
    50  	logWithdraw := logArr[1].(map[string]interface{})["log"].(map[string]interface{})
    51  	logSend := logArr[2].(map[string]interface{})["log"].(map[string]interface{})
    52  	logRecv := logArr[3].(map[string]interface{})["log"].(map[string]interface{})
    53  	fee, err := strconv.ParseFloat(feeStr, 64)
    54  	if err != nil {
    55  		return false
    56  	}
    57  	Amount, err := strconv.ParseFloat(interCase.Amount, 64)
    58  	if err != nil {
    59  		return false
    60  	}
    61  
    62  	pack.FLog.Info("WithdrawBalanceDetails", "TestID", pack.PackID,
    63  		"Fee", feeStr, "Amount", Amount, "Addr", interCase.Addr, "ExecAddr", withdrawFrom,
    64  		"WithdrawPrev", logWithdraw["prev"].(map[string]interface{})["balance"].(string),
    65  		"WithdrawCurr", logWithdraw["current"].(map[string]interface{})["balance"].(string),
    66  		"FromPrev", logSend["prev"].(map[string]interface{})["balance"].(string),
    67  		"FromCurr", logSend["current"].(map[string]interface{})["balance"].(string),
    68  		"ToPrev", logRecv["prev"].(map[string]interface{})["balance"].(string),
    69  		"ToCurr", logRecv["current"].(map[string]interface{})["balance"].(string))
    70  
    71  	return types.CheckBalanceDeltaWithAddr(logFee, interCase.Addr, -fee) &&
    72  		types.CheckBalanceDeltaWithAddr(logWithdraw, interCase.Addr, -Amount) &&
    73  		types.CheckBalanceDeltaWithAddr(logSend, withdrawFrom, -Amount) &&
    74  		types.CheckBalanceDeltaWithAddr(logRecv, interCase.Addr, Amount)
    75  }