2021-09-09 14:28:54 +00:00
|
|
|
package transfer
|
2019-07-15 11:16:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2023-05-19 15:31:45 +00:00
|
|
|
w_common "github.com/status-im/status-go/services/wallet/common"
|
2019-07-15 11:16:07 +00:00
|
|
|
)
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
// 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"`
|
2023-05-19 15:31:45 +00:00
|
|
|
Type w_common.Type `json:"type"`
|
2021-09-09 14:28:54 +00:00
|
|
|
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"`
|
2021-09-09 14:28:54 +00:00
|
|
|
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-05-19 15:31:45 +00:00
|
|
|
Value *hexutil.Big `json:"value"` // Only used for Type EthTransfer and Erc20Transfer
|
|
|
|
TokenID *hexutil.Big `json:"tokenId"` // Only used for Type Erc721Transfer
|
2021-09-09 14:28:54 +00:00
|
|
|
From common.Address `json:"from"`
|
|
|
|
To common.Address `json:"to"`
|
|
|
|
Contract common.Address `json:"contract"`
|
|
|
|
NetworkID uint64 `json:"networkId"`
|
2023-02-15 12:28:19 +00:00
|
|
|
MultiTransactionID int64 `json:"multiTransactionID"`
|
2022-09-09 18:22:59 +00:00
|
|
|
BaseGasFees string `json:"base_gas_fee"`
|
2021-09-09 14:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func castToTransferViews(transfers []Transfer) []View {
|
2023-06-02 20:08:45 +00:00
|
|
|
views := make([]View, 0, len(transfers))
|
|
|
|
for _, tx := range transfers {
|
|
|
|
switch tx.Type {
|
2023-05-19 15:31:45 +00:00
|
|
|
case w_common.EthTransfer, w_common.Erc20Transfer, w_common.Erc721Transfer:
|
2023-06-02 20:08:45 +00:00
|
|
|
view := CastToTransferView(tx)
|
|
|
|
views = append(views, view)
|
|
|
|
}
|
2019-07-15 11:16:07 +00:00
|
|
|
}
|
|
|
|
return views
|
|
|
|
}
|
|
|
|
|
2021-09-09 14:28:54 +00:00
|
|
|
func CastToTransferView(t Transfer) View {
|
|
|
|
view := View{}
|
2019-07-15 11:16:07 +00:00
|
|
|
view.ID = t.ID
|
2023-02-28 03:09:40 +00:00
|
|
|
view.Type = getFixedTransferType(t)
|
2019-07-15 11:16:07 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-08-12 16:41:42 +00:00
|
|
|
view.MaxFeePerGas = (*hexutil.Big)(t.Transaction.GasFeeCap())
|
|
|
|
view.MaxPriorityFeePerGas = (*hexutil.Big)(t.Transaction.GasTipCap())
|
2019-07-15 11:16:07 +00:00
|
|
|
view.GasLimit = hexutil.Uint64(t.Transaction.Gas())
|
|
|
|
view.GasUsed = hexutil.Uint64(t.Receipt.GasUsed)
|
2022-09-09 18:22:59 +00:00
|
|
|
view.BaseGasFees = t.BaseGasFees
|
2019-07-15 11:16:07 +00:00
|
|
|
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()
|
2020-10-28 07:56:14 +00:00
|
|
|
view.NetworkID = t.NetworkID
|
2023-02-28 03:09:40 +00:00
|
|
|
|
|
|
|
value := new(hexutil.Big)
|
2023-02-28 03:10:38 +00:00
|
|
|
tokenID := new(hexutil.Big)
|
2023-02-28 03:09:40 +00:00
|
|
|
|
|
|
|
switch view.Type {
|
2023-05-19 15:31:45 +00:00
|
|
|
case w_common.EthTransfer:
|
2019-07-15 11:16:07 +00:00
|
|
|
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())
|
2019-07-15 11:16:07 +00:00
|
|
|
view.Contract = t.Receipt.ContractAddress
|
2023-05-19 15:31:45 +00:00
|
|
|
case w_common.Erc20Transfer:
|
2019-07-15 11:16:07 +00:00
|
|
|
view.Contract = t.Log.Address
|
2023-05-19 15:31:45 +00:00
|
|
|
from, to, valueInt := w_common.ParseErc20TransferLog(t.Log)
|
2023-02-28 03:09:40 +00:00
|
|
|
view.From, view.To, value = from, to, (*hexutil.Big)(valueInt)
|
2023-05-19 15:31:45 +00:00
|
|
|
case w_common.Erc721Transfer:
|
2023-02-28 03:09:40 +00:00
|
|
|
view.Contract = t.Log.Address
|
2023-05-19 15:31:45 +00:00
|
|
|
from, to, tokenIDInt := w_common.ParseErc721TransferLog(t.Log)
|
2023-02-28 03:10:38 +00:00
|
|
|
view.From, view.To, tokenID = from, to, (*hexutil.Big)(tokenIDInt)
|
2019-07-15 11:16:07 +00:00
|
|
|
}
|
2023-02-15 12:28:19 +00:00
|
|
|
|
|
|
|
view.MultiTransactionID = int64(t.MultiTransactionID)
|
2023-02-28 03:09:40 +00:00
|
|
|
view.Value = value
|
2023-02-28 03:10:38 +00:00
|
|
|
view.TokenID = tokenID
|
2023-02-28 03:09:40 +00:00
|
|
|
|
2019-07-15 11:16:07 +00:00
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
2023-05-19 15:31:45 +00:00
|
|
|
func getFixedTransferType(tx Transfer) w_common.Type {
|
2023-06-02 20:08:45 +00:00
|
|
|
// 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-05-19 15:31:45 +00:00
|
|
|
if tx.Type == w_common.Erc20Transfer {
|
|
|
|
eventType := w_common.GetEventType(tx.Log)
|
|
|
|
return w_common.EventTypeToSubtransactionType(eventType)
|
2023-02-28 03:09:40 +00:00
|
|
|
}
|
|
|
|
return tx.Type
|
|
|
|
}
|