Skip to content

Commit

Permalink
Added NewRawTransactionFromMap and NewSimpleTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
dszlachta committed Jul 24, 2023
1 parent 7589ab9 commit a8584b2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 81 deletions.
49 changes: 10 additions & 39 deletions src/apps/chifra/pkg/rpcClient/get_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package rpcClient
import (
"errors"
"fmt"
"math/big"
"strconv"

"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
Expand Down Expand Up @@ -71,60 +70,32 @@ func GetBlockByNumberWithTxs(chain string, bn uint64, options *Options) (types.S
block.Transactions = make([]types.SimpleTransaction, 0, len(rawBlock.Transactions))
for _, rawTx := range rawBlock.Transactions {
// cast transaction to a concrete type
t, ok := rawTx.(map[string]any)
rawData, ok := rawTx.(map[string]any)
if !ok {
err = errors.New("cannot cast raw block transaction into map")
return block, err
}

txHash := base.HexToHash(fmt.Sprint(t["hash"]))
txGasPrice := mustParseUint(t["gasPrice"])
input := fmt.Sprint(t["input"])
value := big.NewInt(0)
value.SetString(fmt.Sprint(t["value"]), 0)
txIndex := mustParseUint(t["transactionIndex"])
hasToken := IsTokenRelated(input)
raw := types.NewRawTransactionFromMap(rawData)

// Get the receipt
var receipt types.SimpleReceipt
receipt, err = GetTransactionReceipt(chain, ReceiptQuery{
Bn: uint64(bn),
Txid: uint64(txIndex),
TxHash: &txHash,
GasPrice: txGasPrice,
Txid: uint64(raw.TxIndex()),
TxHash: raw.TxHash(),
GasPrice: raw.TxGasPrice(),
NeedsTs: true,
Ts: ts,
}, options)
if err != nil {
return block, err
}

tx := types.SimpleTransaction{
Hash: txHash,
BlockHash: base.HexToHash(fmt.Sprint(t["blockHash"])),
BlockNumber: mustParseUint(t["blockNumber"]),
TransactionIndex: mustParseUint(t["transactionIndex"]),
Nonce: mustParseUint(t["nonce"]),
Timestamp: ts,
From: base.HexToAddress(fmt.Sprint(t["from"])),
To: base.HexToAddress(fmt.Sprint(t["to"])),
Value: *value,
Gas: mustParseUint(t["gas"]),
GasPrice: txGasPrice,
GasUsed: receipt.GasUsed,
MaxFeePerGas: mustParseUint(t["maxFeePerGas"]),
MaxPriorityFeePerGas: mustParseUint(t["maxPriorityFeePerGas"]),
Input: input,
IsError: receipt.IsError,
HasToken: hasToken,
Receipt: &receipt,
// Traces []SimpleTrace `json:"traces"`
// ArticulatedTx *SimpleFunction `json:"articulatedTx"`
}
tx.SetGasCost(&receipt)
block.Transactions = append(block.Transactions, tx)
tx := types.NewSimpleTransaction(raw, &receipt, ts, IsTokenRelated(raw.Input))
block.Transactions = append(block.Transactions, *tx)

if options.HasStore() && !options.TransactionWriteDisabled {
options.Store.Write(&tx, writeOptions)
options.Store.Write(tx, writeOptions)
}
}

Expand Down Expand Up @@ -233,7 +204,7 @@ func getRawBlock(chain string, bn uint64, withTxs bool) (*types.RawBlock, error)
if bn == 0 {
// The RPC does not return a timestamp for the zero block, so we make one
block.Timestamp = fmt.Sprintf("0x%x", rpc.GetBlockTimestamp(chain, utils.PointerOf(uint64(0))))
} else if mustParseUint(block.Timestamp) == 0 {
} else if utils.MustParseUint(block.Timestamp) == 0 {
return &types.RawBlock{}, fmt.Errorf("block at %s returned an error: %w", fmt.Sprintf("%d", bn), ethereum.NotFound)
}

Expand Down
45 changes: 3 additions & 42 deletions src/apps/chifra/pkg/rpcClient/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func GetAppearanceFromHash(chain string, hash string) (uint64, uint64, error) {
if rawTx, err := GetRawTransactionByHash(chain, base.HexToHash(hash)); err != nil {
return 0, 0, err
} else {
return mustParseUint(rawTx.BlockNumber), mustParseUint(rawTx.TransactionIndex), nil
return utils.MustParseUint(rawTx.BlockNumber), utils.MustParseUint(rawTx.TransactionIndex), nil
}
}

Expand Down Expand Up @@ -292,26 +292,7 @@ func GetTransactionByAppearance(chain string, appearance *types.RawAppearance, f
return
}

tx = &types.SimpleTransaction{
Hash: base.HexToHash(rawTx.Hash),
BlockHash: base.HexToHash(rawTx.BlockHash),
BlockNumber: bn,
TransactionIndex: txid,
Nonce: mustParseUint(rawTx.Nonce),
Timestamp: blockTs,
From: base.HexToAddress(rawTx.From),
To: base.HexToAddress(rawTx.To),
Gas: mustParseUint(rawTx.Gas),
GasPrice: mustParseUint(rawTx.GasPrice),
GasUsed: receipt.GasUsed,
Input: rawTx.Input,
IsError: receipt.IsError,
HasToken: IsTokenRelated(rawTx.Input),
Receipt: &receipt,
}
tx.Value.SetString(rawTx.Value, 0)
tx.SetGasCost(&receipt)
tx.SetRaw(rawTx)
tx = types.NewSimpleTransaction(rawTx, &receipt, blockTs, IsTokenRelated(rawTx.Input))

if rpcOptions.HasStore() && !rpcOptions.TransactionWriteDisabled {
rpcOptions.Store.Write(tx, writeOptions)
Expand Down Expand Up @@ -365,27 +346,7 @@ func GetTransactionByBlockAndId(chain string, bn base.Blknum, txid uint64, rpcOp
return
}

// TODO: this gets repeated
tx = &types.SimpleTransaction{
Hash: base.HexToHash(rawTx.Hash),
BlockHash: base.HexToHash(rawTx.BlockHash),
BlockNumber: bn,
TransactionIndex: txid,
Nonce: mustParseUint(rawTx.Nonce),
Timestamp: blockTs,
From: base.HexToAddress(rawTx.From),
To: base.HexToAddress(rawTx.To),
Gas: mustParseUint(rawTx.Gas),
GasPrice: mustParseUint(rawTx.GasPrice),
GasUsed: receipt.GasUsed,
Input: rawTx.Input,
IsError: receipt.IsError,
HasToken: IsTokenRelated(rawTx.Input),
Receipt: &receipt,
}
tx.Value.SetString(rawTx.Value, 0)
tx.SetGasCost(&receipt)
tx.SetRaw(rawTx)
tx = types.NewSimpleTransaction(rawTx, &receipt, blockTs, IsTokenRelated(rawTx.Input))

if rpcOptions.HasStore() && !rpcOptions.TransactionWriteDisabled {
rpcOptions.Store.Write(tx, writeOptions)
Expand Down
73 changes: 73 additions & 0 deletions src/apps/chifra/pkg/types/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,79 @@ func (s *SimpleTransaction) ReadFrom(r io.Reader) (n int64, err error) {
// EXISTING_CODE
//

// NewRawTransactionFromMap is useful when we get a map of transaction properties, e.g.
// from a call to eth_getBlockByHash [0x..., true]
func NewRawTransactionFromMap(input map[string]any) (r *RawTransaction) {
r = &RawTransaction{}

r.BlockHash = fmt.Sprint(input["blockHash"])
r.BlockNumber = fmt.Sprint(input["blockNumber"])
// Missing in block query
if _, ok := input["chainId"]; ok {
r.ChainId = fmt.Sprint(input["chainId"])
}
r.From = fmt.Sprint(input["from"])
r.Gas = fmt.Sprint(input["gas"])
r.GasPrice = fmt.Sprint(input["gasPrice"])
r.Hash = fmt.Sprint(input["hash"])
r.Input = fmt.Sprint(input["input"])
r.MaxFeePerGas = fmt.Sprint(input["maxFeePerGas"])
r.MaxPriorityFeePerGas = fmt.Sprint(input["maxPriorityFeePerGas"])
r.Nonce = fmt.Sprint(input["nonce"])
r.To = fmt.Sprint(input["to"])
r.TransactionIndex = fmt.Sprint(input["transactionIndex"])
r.Value = fmt.Sprint(input["value"])

return
}

func (r *RawTransaction) TxIndex() uint64 {
return utils.MustParseUint(r.TransactionIndex)
}

func (r *RawTransaction) TxGasPrice() uint64 {
return utils.MustParseUint(r.GasPrice)
}

func (r *RawTransaction) TxHash() *base.Hash {
hash := base.HexToHash(r.Hash)
return &hash
}

// NewSimpleTransaction builds SimpleTransaction using data from raw and receipt. Receipt can be nil.
// Transaction timestamp and HasToken flag will be set to timestamp and hasToken.
func NewSimpleTransaction(raw *RawTransaction, receipt *SimpleReceipt, timestamp base.Timestamp, hasToken bool) (s *SimpleTransaction) {
s = &SimpleTransaction{}

// TODO: use RawTransaction methods
s.Hash = base.HexToHash(raw.Hash)
s.BlockHash = base.HexToHash(raw.BlockHash)
s.BlockNumber = utils.MustParseUint(raw.BlockNumber)
s.TransactionIndex = utils.MustParseUint(raw.TransactionIndex)
s.Nonce = utils.MustParseUint(raw.Nonce)
s.Timestamp = timestamp
s.From = base.HexToAddress(raw.From)
s.To = base.HexToAddress(raw.To)
s.Value.SetString(raw.Value, 0)
s.Gas = utils.MustParseUint(raw.Gas)
s.GasPrice = utils.MustParseUint(raw.GasPrice)
s.MaxFeePerGas = utils.MustParseUint(raw.MaxFeePerGas)
s.MaxPriorityFeePerGas = utils.MustParseUint(raw.MaxPriorityFeePerGas)
s.Input = raw.Input

s.HasToken = hasToken
if receipt != nil {
s.GasUsed = receipt.GasUsed
s.IsError = receipt.IsError
s.Receipt = receipt

s.SetGasCost(receipt)
}
s.SetRaw(raw)

return
}

func (s *SimpleTransaction) SetGasCost(receipt *SimpleReceipt) base.Gas {
if receipt == nil {
return 0
Expand Down

0 comments on commit a8584b2

Please sign in to comment.