Skip to content

Commit

Permalink
add new rpc to goclient
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Mar 17, 2021
1 parent 0597d3e commit 791a622
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 30 deletions.
6 changes: 6 additions & 0 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const (
ReceiptStatusSuccessful = uint64(1)
)

// Receipt represents the results of a transaction.
type OriginalDataAndReceipt struct {
Receipt Receipt `json:"receipt"`
TxData Transaction `json:"tx_data"`
}

// Receipt represents the results of a transaction.
type Receipt struct {
// Consensus fields: These fields are defined by the Yellow Paper
Expand Down
38 changes: 38 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,44 @@ func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash,
return json.tx, err
}

// TransactionInBlock returns a single transaction at index in the given block.
func (ec *Client) TransactionsInBlock(ctx context.Context, number *big.Int) ([]*types.Transaction, error) {
var rpcTxs []*rpcTransaction
err := ec.c.CallContext(ctx, &rpcTxs, "eth_getTransactionsByBlockNumber", toBlockNumArg(number))
if err != nil {
return nil, err
}
fmt.Println(len(rpcTxs))
txs := make([]*types.Transaction, 0, len(rpcTxs))
for _, tx := range rpcTxs {
txs = append(txs, tx.tx)
}
return txs, err
}

// TransactionInBlock returns a single transaction at index in the given block.
func (ec *Client) TransactionRecipientsInBlock(ctx context.Context, number *big.Int) ([]*types.Receipt, error) {
var rs []*types.Receipt
err := ec.c.CallContext(ctx, &rs, "eth_getTransactionReceiptsByBlockNumber", toBlockNumArg(number))
if err != nil {
return nil, err
}
return rs, err
}

// TransactionDataAndReceipt returns the original data and receipt of a transaction by transaction hash.
// Note that the receipt is not available for pending transactions.
func (ec *Client) TransactionDataAndReceipt(ctx context.Context, txHash common.Hash) (*types.OriginalDataAndReceipt, error) {
var r *types.OriginalDataAndReceipt
err := ec.c.CallContext(ctx, &r, "eth_getTransactionDataAndReceipt", txHash)
if err == nil {
if r == nil {
return nil, ethereum.NotFound
}
}
return r, err
}

// TransactionReceipt returns the receipt of a transaction by transaction hash.
// Note that the receipt is not available for pending transactions.
func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
Expand Down
18 changes: 12 additions & 6 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,10 +1342,16 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx conte
if err != nil {
return nil, err
}
block, _ := s.b.BlockByHash(ctx, blockHash)
block, err := s.b.BlockByHash(ctx, blockHash)
if err != nil {
return nil, err
}
txs := block.Transactions()
if len(txs) != len(receipts) {
return nil, fmt.Errorf("txs length doesn't equal to receipts' length")
}

txRecipients := make([]map[string]interface{}, 0, len(txs))
txReceipts := make([]map[string]interface{}, 0, len(txs))
for idx, receipt := range receipts {
tx := txs[idx]
var signer types.Signer = types.FrontierSigner{}
Expand Down Expand Up @@ -1382,10 +1388,10 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx conte
fields["contractAddress"] = receipt.ContractAddress
}

txRecipients = append(txRecipients, fields)
txReceipts = append(txReceipts, fields)
}

return txRecipients, nil
return txReceipts, nil
}

// GetTransactionDataAndReceipt returns the original transaction data and transaction receipt for the given transaction hash.
Expand Down Expand Up @@ -1456,8 +1462,8 @@ func (s *PublicTransactionPoolAPI) GetTransactionDataAndReceipt(ctx context.Cont
fields["contractAddress"] = receipt.ContractAddress
}
result := map[string]interface{}{
"tx_data": txData,
"recipient": fields,
"tx_data": txData,
"receipt": fields,
}
return result, nil
}
Expand Down
37 changes: 13 additions & 24 deletions internal/jsre/deps/bindata.go

Large diffs are not rendered by default.

104 changes: 104 additions & 0 deletions internal/jsre/deps/web3.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 791a622

Please sign in to comment.