status-go/services/wallet/transfer/view.go

118 lines
4.1 KiB
Go
Raw Normal View History

package transfer
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// View stores only fields used by a client and ensures that all relevant fields are
// encoded in hex.
type View struct {
ID common.Hash `json:"id"`
Type Type `json:"type"`
Address common.Address `json:"address"`
BlockNumber *hexutil.Big `json:"blockNumber"`
BlockHash common.Hash `json:"blockhash"`
Timestamp hexutil.Uint64 `json:"timestamp"`
GasPrice *hexutil.Big `json:"gasPrice"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
2022-09-26 10:04:19 +00:00
EffectiveTip *hexutil.Big `json:"effectiveTip"`
EffectiveGasPrice *hexutil.Big `json:"effectiveGasPrice"`
GasLimit hexutil.Uint64 `json:"gasLimit"`
GasUsed hexutil.Uint64 `json:"gasUsed"`
Nonce hexutil.Uint64 `json:"nonce"`
TxStatus hexutil.Uint64 `json:"txStatus"`
Input hexutil.Bytes `json:"input"`
TxHash common.Hash `json:"txHash"`
2023-02-28 03:09:40 +00:00
Value *hexutil.Big `json:"value"` // Only used for Type ethTransfer and erc20Transfer
TokenID *hexutil.Big `json:"tokenId"` // Only used for Type erc721Transfer
From common.Address `json:"from"`
To common.Address `json:"to"`
Contract common.Address `json:"contract"`
NetworkID uint64 `json:"networkId"`
MultiTransactionID int64 `json:"multiTransactionID"`
BaseGasFees string `json:"base_gas_fee"`
}
func castToTransferViews(transfers []Transfer) []View {
views := make([]View, 0, len(transfers))
for _, tx := range transfers {
switch tx.Type {
case ethTransfer, erc20Transfer, erc721Transfer:
view := CastToTransferView(tx)
views = append(views, view)
}
}
return views
}
func CastToTransferView(t Transfer) View {
view := View{}
view.ID = t.ID
2023-02-28 03:09:40 +00:00
view.Type = getFixedTransferType(t)
view.Address = t.Address
view.BlockNumber = (*hexutil.Big)(t.BlockNumber)
view.BlockHash = t.BlockHash
view.Timestamp = hexutil.Uint64(t.Timestamp)
view.GasPrice = (*hexutil.Big)(t.Transaction.GasPrice())
2022-09-26 10:04:19 +00:00
if t.BaseGasFees != "" {
baseFee := new(big.Int)
baseFee.SetString(t.BaseGasFees[2:], 16)
tip := t.Transaction.EffectiveGasTipValue(baseFee)
view.EffectiveTip = (*hexutil.Big)(tip)
price := new(big.Int).Add(baseFee, tip)
view.EffectiveGasPrice = (*hexutil.Big)(price)
}
view.MaxFeePerGas = (*hexutil.Big)(t.Transaction.GasFeeCap())
view.MaxPriorityFeePerGas = (*hexutil.Big)(t.Transaction.GasTipCap())
view.GasLimit = hexutil.Uint64(t.Transaction.Gas())
view.GasUsed = hexutil.Uint64(t.Receipt.GasUsed)
view.BaseGasFees = t.BaseGasFees
view.Nonce = hexutil.Uint64(t.Transaction.Nonce())
view.TxStatus = hexutil.Uint64(t.Receipt.Status)
view.Input = hexutil.Bytes(t.Transaction.Data())
view.TxHash = t.Transaction.Hash()
view.NetworkID = t.NetworkID
2023-02-28 03:09:40 +00:00
value := new(hexutil.Big)
tokenID := new(hexutil.Big)
2023-02-28 03:09:40 +00:00
switch view.Type {
case ethTransfer:
view.From = t.From
if t.Transaction.To() != nil {
view.To = *t.Transaction.To()
}
2023-02-28 03:09:40 +00:00
value = (*hexutil.Big)(t.Transaction.Value())
view.Contract = t.Receipt.ContractAddress
case erc20Transfer:
view.Contract = t.Log.Address
from, to, valueInt := parseErc20TransferLog(t.Log)
2023-02-28 03:09:40 +00:00
view.From, view.To, value = from, to, (*hexutil.Big)(valueInt)
case erc721Transfer:
view.Contract = t.Log.Address
from, to, tokenIDInt := parseErc721TransferLog(t.Log)
view.From, view.To, tokenID = from, to, (*hexutil.Big)(tokenIDInt)
}
view.MultiTransactionID = int64(t.MultiTransactionID)
2023-02-28 03:09:40 +00:00
view.Value = value
view.TokenID = tokenID
2023-02-28 03:09:40 +00:00
return view
}
2023-02-28 03:09:40 +00:00
func getFixedTransferType(tx Transfer) Type {
// erc721 transfers share signature with erc20 ones, so they both used to be categorized as erc20
// by the Downloader. We fix this here since they might be mis-categorized in the db.
2023-02-28 03:09:40 +00:00
if tx.Type == erc20Transfer {
eventType := GetEventType(tx.Log)
return EventTypeToSubtransactionType(eventType)
2023-02-28 03:09:40 +00:00
}
return tx.Type
}