wallet/pkg/eth/eth_block.go

38 lines
777 B
Go
Raw Normal View History

2024-08-31 14:46:20 +00:00
package eth
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
func EthLastBlock() (uint64, error) {
client, err := ethclient.Dial(ethNode.rpcNode)
if err != nil {
return 0, err
}
last_eth_block, err := client.BlockNumber(context.Background())
if err != nil {
return 0, err
}
return last_eth_block, nil
}
func EthBlockByNumber(blockNumber uint64) (*types.Block, error) {
client, err := ethclient.Dial(ethNode.rpcNode)
if err != nil {
return nil, err
}
_blockNumber := big.NewInt(int64(blockNumber))
fmt.Println("Block fetch: ", _blockNumber)
block, err := client.BlockByNumber(context.Background(), _blockNumber)
if err != nil {
return nil, err
}
return block, nil
}