0

How do I get from address using ethClient? just like http JSON RPC-API always contains the from address.

I tried something like below

for _, txHeader := range block.Transactions() {
    transaction, isPending, err := client.TransactionByHash(context.Background(), txHeader.Hash())
    msg, err := transaction.AsMessage(types.NewEIP155Signer(transaction.ChainId(), nil)
    fmt.Println(msg.From().Hex())
}

It passes through some transactions but then it also fails with some transactions with an error message saying transaction not supported. And some transactions it just says exit 1 why is getting from Address so complicated in go ethereum..the json RPC API eth_getTransactionByHash always has the from like below

// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"],"id":1}'

// Result
{
  "jsonrpc":"2.0",
  "id":1,
  "result":{
    "blockHash":"0x1d59ff54b1eb26b013ce3cb5fc9dab3705b415a67127a003c3e61eb445bb8df2",
    "blockNumber":"0x5daf3b", // 6139707
    "from":"0xa7d9ddbe1f17865597fbd27ec712455208b6b76d",
    "gas":"0xc350", // 50000
    "gasPrice":"0x4a817c800", // 20000000000
    "hash":"0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b",
    "input":"0x68656c6c6f21",
    "nonce":"0x15", // 21
    "to":"0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb",
    "transactionIndex":"0x41", // 65
    "value":"0xf3dbb76162000", // 4290000000000000
    "v":"0x25", // 37
    "r":"0x1b5e176d927f8e9ab405058b2d2457392da3e20f328b16ddabcebc33eaac5fea",
    "s":"0x4ba69724e8f69de52f0125ad8b3c5c2cef33019bac3249e2c0a2192766d1721c"
  }
}

How can I get the from address in the easiest fashion using ethClient?

7
  • Hello. You should provide the code that you use to send the transaction. The from is the address that should be the sender of the transaction. Jul 5 at 14:37
  • I am not sending any transactions. I am just subscribing to new blocks using ethClient and for each block I get I pull all the transactions which is the for loop above. Jul 5 at 14:39
  • @user1870400 if you are not sending transactions or calls, why would you need to know the from address ? Jul 5 at 14:40
  • building a dapp Jul 5 at 14:41
  • I just want the same thing as the curl command where even though I am not a sender i get the from address except I am using ethClient Jul 5 at 14:46
0

you can use this code if it helps:

var bnum := int(1100000) // sample block number


var head *types.Header
ctx := context.Background()
var raw json.RawMessage
err := rpcclient.CallContext(ctx, &raw,"eth_getBlockByNumber", hexutil.EncodeBig(big.NewInt(int64(bnum))),true)
if err != nil {
    return common.Hash{},head,make([]*MyTX,0,0),err
}   
var body rpcBlock
err = json.Unmarshal(raw, &body);
if err != nil {
    Error.Printf("Error unmarshalling transactions of the block: %v\n",err)
    return common.Hash{},head, make([]*MyTX,0,0),err
}   
err = json.Unmarshal(raw,&head)
if err!= nil {
    Error.Printf("Error unmarshalling hash of the block: %v\n",err)
    return body.Hash,head,make([]*MyTX,0,0),err
}   
txs := make([]*MyTX, len(body.Transactions))
for i, tx := range body.Transactions {
    mytx := new(MyTX)
    mytx.BlockNum = bnum
    mytx.TxHash = tx.txExtraInfo.Hash.String()
    mytx.From = tx.txExtraInfo.From.String()
    if tx.tx.To() != nil {
        mytx.To  = tx.tx.To().String()
    } else {
        mytx.CtrctCreate = true
        mytx.To = "0x0000000000000000000000000000000000000000"
    }
    mytx.Value = tx.tx.Value().String()
    mytx.Input = tx.tx.Data()
    mytx.GasPrice = tx.tx.GasPrice().String()
    txs[i]=mytx
}   
return txs

---- types

type txExtraInfo struct {
    BlockNumber *string         `json:"blockNumber,omitempty"`
    BlockHash   *common.Hash    `json:"blockHash,omitempty"`
    From        *common.Address `json:"from,omitempty"`
    Hash        *common.Hash    `json:"hash,omitempty"`
}
type rpcBlock struct {
    Hash         common.Hash      `json:"hash"`
    Transactions []rpcTransaction `json:"transactions"`
    UncleHashes  []common.Hash    `json:"uncles"`
}
type receiptCallResult struct {
    receipt     *types.Receipt
    err         error
    idx         int
}
type rpcTransaction struct {
    tx *types.Transaction
    txExtraInfo
}

type MyTX struct {   

    BlockNum            int64
    GasUsed             int64
    TimeStamp           int64
    TxIndex             int32
    NumLogs             int32
    CtrctCreate         bool
    GasPrice            string
    TxHash              string
    From                string
    To                  string
    Value               string
    Input               []byte
}

/* copied and modified from original ethereum sources, for speed */

12
  • Hi, Thanks for this. few questions 1. Where do I get the bnum from? 2. I want to subscribe to newBlocks via websocket? 3. how many network calls does this code make to get all transactions in a block? since you are iterating through body of transactions can I assuming it is making one network call or a network call for each transaction? Jul 5 at 23:18
  • bnum is the block you want to download (I updated the answer)
    – Nulik
    Jul 6 at 1:15
  • this code doesn't support events. Internally in Geth the new blocks are being intercepted by listening to chain events listed here: github.com/ethereum/go-ethereum/blob/…
    – Nulik
    Jul 6 at 1:18
  • the problem with using events is that you can miss one event and the integrity of your database will be lost. That's why I opted to dismiss all the subscription functionality in Geth and go for my own validation at the parent hash level.
    – Nulik
    Jul 6 at 1:19
  • Every time you receive a block you validate that parent hash (of the block yhou are processing currently) matches the hash of the previous block and this way you guarantee 100% that you won't miss any chain split if it happens, and they happen twice or three times a day
    – Nulik
    Jul 6 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.