Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: format code #5

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified eth-wallet
Binary file not shown.
144 changes: 72 additions & 72 deletions wallet/node/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,46 +100,44 @@ func DialEthClient(ctx context.Context, rpcUrl string) (EthClient, error) {
return &clnt{rpc: NewRPC(rpcClient)}, nil
}

func (c *clnt) BlockHeaderByHash(hash common.Hash) (*types.Header, error) {
func (c *clnt) BlockHeaderByNumber(number *big.Int) (*types.Header, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()

var header *types.Header
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByHash", hash, false)
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", toBlockNumArg(number), false)
if err != nil {
log.Error("Call eth_getBlockByNumber method fail", "err", err)
return nil, err
} else if header == nil {
log.Warn("header not found")
return nil, ethereum.NotFound
}

if header.Hash() != hash {
return nil, errors.New("header mismatch")
}

return header, nil
}

func (c *clnt) LatestSafeBlockHeader() (*types.Header, error) {
func (c *clnt) BlockByNumber(number *big.Int) (*RpcBlock, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()

var header *types.Header
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", "safe", false)
var block *RpcBlock
err := c.rpc.CallContext(ctxwt, &block, "eth_getBlockByNumber", toBlockNumArg(number), true)
if err != nil {
log.Error("Call eth_getBlockByNumber method fail", "err", err)
return nil, err
} else if header == nil {
} else if block == nil {
log.Warn("header not found")
return nil, ethereum.NotFound
}

return header, nil
return block, nil
}

func (c *clnt) LatestFinalizedBlockHeader() (*types.Header, error) {
func (c *clnt) LatestSafeBlockHeader() (*types.Header, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()

var header *types.Header
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", "finalized", false)
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", "safe", false)
if err != nil {
return nil, err
} else if header == nil {
Expand All @@ -149,78 +147,37 @@ func (c *clnt) LatestFinalizedBlockHeader() (*types.Header, error) {
return header, nil
}

func (c *clnt) BlockByNumber(number *big.Int) (*RpcBlock, error) {
func (c *clnt) LatestFinalizedBlockHeader() (*types.Header, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var block *RpcBlock
err := c.rpc.CallContext(ctxwt, &block, "eth_getBlockByNumber", toBlockNumArg(number), true)
if err != nil {
log.Error("Call eth_getBlockByNumber method fail", "err", err)
return nil, err
} else if block == nil {
log.Warn("header not found")
return nil, ethereum.NotFound
}
return block, nil
}

func (c *clnt) TxCountByAddress(address common.Address) (hexutil.Uint64, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var nonce hexutil.Uint64
err := c.rpc.CallContext(ctxwt, &nonce, "eth_getTransactionCount", address, "latest")
var header *types.Header
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", "finalized", false)
if err != nil {
log.Error("Call eth_getTransactionCount method fail", "err", err)
return 0, err
}
log.Info("get nonce by address success", "nonce", nonce)
return nonce, err
}

func (c *clnt) SuggestGasPrice() (*big.Int, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var hex hexutil.Big
if err := c.rpc.CallContext(ctxwt, &hex, "eth_gasPrice"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}

func (c *clnt) SuggestGasTipCap() (*big.Int, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var hex hexutil.Big
if err := c.rpc.CallContext(ctxwt, &hex, "eth_maxPriorityFeePerGas"); err != nil {
return nil, err
} else if header == nil {
return nil, ethereum.NotFound
}
return (*big.Int)(&hex), nil
}

func (c *clnt) SendRawTransaction(rawTx string) error {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
if err := c.rpc.CallContext(ctxwt, nil, "eth_sendRawTransaction", rawTx); err != nil {
return err
}
log.Info("send tx to ethereum success")
return nil
return header, nil
}

func (c *clnt) BlockHeaderByNumber(number *big.Int) (*types.Header, error) {
func (c *clnt) BlockHeaderByHash(hash common.Hash) (*types.Header, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()

var header *types.Header
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByNumber", toBlockNumArg(number), false)
err := c.rpc.CallContext(ctxwt, &header, "eth_getBlockByHash", hash, false)
if err != nil {
log.Error("Call eth_getBlockByNumber method fail", "err", err)
return nil, err
} else if header == nil {
log.Warn("header not found")
return nil, ethereum.NotFound
}

if header.Hash() != hash {
return nil, errors.New("header mismatch")
}

return header, nil
}

Expand Down Expand Up @@ -336,10 +293,6 @@ func (c *clnt) StorageHash(address common.Address, blockNumber *big.Int) (common
return proof.StorageHash, nil
}

func (c *clnt) Close() {
c.rpc.Close()
}

func (c *clnt) FilterLogs(query ethereum.FilterQuery, chainId uint) (Logs, error) {
arg, err := toFilterArg(query)
if err != nil {
Expand Down Expand Up @@ -375,6 +328,53 @@ func (c *clnt) FilterLogs(query ethereum.FilterQuery, chainId uint) (Logs, error
return Logs{Logs: logs, ToBlockHeader: &header}, nil
}

func (c *clnt) TxCountByAddress(address common.Address) (hexutil.Uint64, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var nonce hexutil.Uint64
err := c.rpc.CallContext(ctxwt, &nonce, "eth_getTransactionCount", address, "latest")
if err != nil {
log.Error("Call eth_getTransactionCount method fail", "err", err)
return 0, err
}
log.Info("get nonce by address success", "nonce", nonce)
return nonce, err
}

func (c *clnt) SendRawTransaction(rawTx string) error {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
if err := c.rpc.CallContext(ctxwt, nil, "eth_sendRawTransaction", rawTx); err != nil {
return err
}
log.Info("send tx to ethereum success")
return nil
}

func (c *clnt) SuggestGasPrice() (*big.Int, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var hex hexutil.Big
if err := c.rpc.CallContext(ctxwt, &hex, "eth_gasPrice"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}

func (c *clnt) SuggestGasTipCap() (*big.Int, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var hex hexutil.Big
if err := c.rpc.CallContext(ctxwt, &hex, "eth_maxPriorityFeePerGas"); err != nil {
return nil, err
}
return (*big.Int)(&hex), nil
}

func (c *clnt) Close() {
c.rpc.Close()
}

func NewRPC(client *rpc.Client) RPC {
return &rpcClient{client}
}
Expand Down
Loading