github.com/status-im/status-go@v1.1.0/services/wallet/transfer/view.go (about)

     1  package transfer
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  	"github.com/ethereum/go-ethereum/common/hexutil"
     8  	w_common "github.com/status-im/status-go/services/wallet/common"
     9  )
    10  
    11  // View stores only fields used by a client and ensures that all relevant fields are
    12  // encoded in hex.
    13  type View struct {
    14  	ID                   common.Hash    `json:"id"`
    15  	Type                 w_common.Type  `json:"type"`
    16  	Address              common.Address `json:"address"`
    17  	BlockNumber          *hexutil.Big   `json:"blockNumber"`
    18  	BlockHash            common.Hash    `json:"blockhash"`
    19  	Timestamp            hexutil.Uint64 `json:"timestamp"`
    20  	GasPrice             *hexutil.Big   `json:"gasPrice"`
    21  	MaxFeePerGas         *hexutil.Big   `json:"maxFeePerGas"`
    22  	MaxPriorityFeePerGas *hexutil.Big   `json:"maxPriorityFeePerGas"`
    23  	EffectiveTip         *hexutil.Big   `json:"effectiveTip"`
    24  	EffectiveGasPrice    *hexutil.Big   `json:"effectiveGasPrice"`
    25  	GasLimit             hexutil.Uint64 `json:"gasLimit"`
    26  	GasUsed              hexutil.Uint64 `json:"gasUsed"`
    27  	Nonce                hexutil.Uint64 `json:"nonce"`
    28  	TxStatus             hexutil.Uint64 `json:"txStatus"`
    29  	Input                hexutil.Bytes  `json:"input"`
    30  	TxHash               common.Hash    `json:"txHash"`
    31  	Value                *hexutil.Big   `json:"value"`   // Only used for Type EthTransfer and Erc20Transfer
    32  	TokenID              *hexutil.Big   `json:"tokenId"` // Only used for Type Erc721Transfer
    33  	From                 common.Address `json:"from"`
    34  	To                   common.Address `json:"to"`
    35  	Contract             common.Address `json:"contract"`
    36  	NetworkID            uint64         `json:"networkId"`
    37  	MultiTransactionID   int64          `json:"multiTransactionID"`
    38  	BaseGasFees          string         `json:"base_gas_fee"`
    39  }
    40  
    41  func castToTransferViews(transfers []Transfer) []View {
    42  	views := make([]View, 0, len(transfers))
    43  	for _, tx := range transfers {
    44  		switch tx.Type {
    45  		case w_common.EthTransfer, w_common.Erc20Transfer, w_common.Erc721Transfer:
    46  			view := CastToTransferView(tx)
    47  			views = append(views, view)
    48  		}
    49  	}
    50  	return views
    51  }
    52  
    53  func CastToTransferView(t Transfer) View {
    54  	view := View{}
    55  	view.ID = t.ID
    56  	view.Type = getFixedTransferType(t)
    57  	view.Address = t.Address
    58  	view.BlockNumber = (*hexutil.Big)(t.BlockNumber)
    59  	view.BlockHash = t.BlockHash
    60  	view.Timestamp = hexutil.Uint64(t.Timestamp)
    61  	view.GasPrice = (*hexutil.Big)(t.Transaction.GasPrice())
    62  	if t.BaseGasFees != "" {
    63  		baseFee := new(big.Int)
    64  		baseFee.SetString(t.BaseGasFees[2:], 16)
    65  		tip := t.Transaction.EffectiveGasTipValue(baseFee)
    66  
    67  		view.EffectiveTip = (*hexutil.Big)(tip)
    68  		price := new(big.Int).Add(baseFee, tip)
    69  		view.EffectiveGasPrice = (*hexutil.Big)(price)
    70  	}
    71  	view.MaxFeePerGas = (*hexutil.Big)(t.Transaction.GasFeeCap())
    72  	view.MaxPriorityFeePerGas = (*hexutil.Big)(t.Transaction.GasTipCap())
    73  	view.GasLimit = hexutil.Uint64(t.Transaction.Gas())
    74  	view.GasUsed = hexutil.Uint64(t.Receipt.GasUsed)
    75  	view.BaseGasFees = t.BaseGasFees
    76  	view.Nonce = hexutil.Uint64(t.Transaction.Nonce())
    77  	view.TxStatus = hexutil.Uint64(t.Receipt.Status)
    78  	view.Input = hexutil.Bytes(t.Transaction.Data())
    79  	view.TxHash = t.Transaction.Hash()
    80  	view.NetworkID = t.NetworkID
    81  
    82  	value := new(hexutil.Big)
    83  	tokenID := new(hexutil.Big)
    84  
    85  	switch view.Type {
    86  	case w_common.EthTransfer:
    87  		view.From = t.From
    88  		if t.Transaction.To() != nil {
    89  			view.To = *t.Transaction.To()
    90  		}
    91  		value = (*hexutil.Big)(t.Transaction.Value())
    92  		view.Contract = t.Receipt.ContractAddress
    93  	case w_common.Erc20Transfer:
    94  		view.Contract = t.Log.Address
    95  		from, to, valueInt := w_common.ParseErc20TransferLog(t.Log)
    96  		view.From, view.To, value = from, to, (*hexutil.Big)(valueInt)
    97  	case w_common.Erc721Transfer:
    98  		view.Contract = t.Log.Address
    99  		from, to, tokenIDInt := w_common.ParseErc721TransferLog(t.Log)
   100  		view.From, view.To, tokenID = from, to, (*hexutil.Big)(tokenIDInt)
   101  	}
   102  
   103  	view.MultiTransactionID = int64(t.MultiTransactionID)
   104  	view.Value = value
   105  	view.TokenID = tokenID
   106  
   107  	return view
   108  }
   109  
   110  func getFixedTransferType(tx Transfer) w_common.Type {
   111  	// erc721 transfers share signature with erc20 ones, so they both used to be categorized as erc20
   112  	// by the Downloader. We fix this here since they might be mis-categorized in the db.
   113  	if tx.Type == w_common.Erc20Transfer {
   114  		eventType := w_common.GetEventType(tx.Log)
   115  		return w_common.EventTypeToSubtransactionType(eventType)
   116  	}
   117  	return tx.Type
   118  }