forked from coinbase/mesh-geth-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
182 lines (156 loc) · 4.92 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright 2022 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package client
import (
"context"
"fmt"
"github.com/coinbase/rosetta-geth-sdk/configuration"
"github.com/coinbase/rosetta-geth-sdk/services"
RosettaTypes "github.com/coinbase/rosetta-sdk-go/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"log"
"math/big"
evmClient "github.com/coinbase/rosetta-geth-sdk/client"
sdkTypes "github.com/coinbase/rosetta-geth-sdk/types"
EthTypes "github.com/ethereum/go-ethereum/core/types"
)
type EthereumClient struct {
// Use embedding for inheritance. So all the methods of the SDKClient
// are instantly available on EthereumClient.
evmClient.SDKClient
}
func (c *EthereumClient) ParseOps(
tx *evmClient.LoadedTransaction,
) ([]*RosettaTypes.Operation, error) {
var ops []*RosettaTypes.Operation
// Compute fee operations
feeOps := services.FeeOps(tx)
ops = append(ops, feeOps...)
traceOps := services.TraceOps(tx.Trace, len(ops))
ops = append(ops, traceOps...)
return ops, nil
}
func (c *EthereumClient) GetBlockReceipts(
ctx context.Context,
blockHash common.Hash,
txs []evmClient.RPCTransaction,
baseFee *big.Int,
) ([]*evmClient.RosettaTxReceipt, error) {
receipts := make([]*evmClient.RosettaTxReceipt, len(txs))
if len(txs) == 0 {
return receipts, nil
}
ethReceipts := make([]*EthTypes.Receipt, len(txs))
reqs := make([]rpc.BatchElem, len(txs))
for i := range reqs {
reqs[i] = rpc.BatchElem{
Method: "eth_getTransactionReceipt",
Args: []interface{}{txs[i].TxExtraInfo.TxHash.String()},
Result: ðReceipts[i],
}
}
if err := c.BatchCallContext(ctx, reqs); err != nil {
return nil, err
}
for i := range reqs {
if reqs[i].Error != nil {
return nil, reqs[i].Error
}
gasPrice, err := evmClient.EffectiveGasPrice(txs[i].Tx, baseFee)
if err != nil {
return nil, err
}
gasUsed := new(big.Int).SetUint64(ethReceipts[i].GasUsed)
feeAmount := new(big.Int).Mul(gasUsed, gasPrice)
receipt := &evmClient.RosettaTxReceipt{
Type: ethReceipts[i].Type,
GasPrice: gasPrice,
GasUsed: gasUsed,
Logs: ethReceipts[i].Logs,
RawMessage: nil,
TransactionFee: feeAmount,
}
receipts[i] = receipt
if ethReceipts[i] == nil {
return nil, fmt.Errorf("got empty receipt for %x", txs[i].Tx.Hash().Hex())
}
if ethReceipts[i].BlockHash != blockHash {
return nil, fmt.Errorf(
"expected block hash %s for Transaction but got %s: %w",
blockHash.Hex(),
ethReceipts[i].BlockHash.Hex(),
sdkTypes.ErrClientBlockOrphaned,
)
}
}
return receipts, nil
}
func (c *EthereumClient) GetTransactionReceipt(
ctx context.Context,
tx *evmClient.LoadedTransaction,
) (*evmClient.RosettaTxReceipt, error) {
var r *EthTypes.Receipt
err := c.CallContext(ctx, &r, "eth_getTransactionReceipt", tx.TxHash)
if err == nil {
if r == nil {
return nil, ethereum.NotFound
}
}
gasPrice, err := evmClient.EffectiveGasPrice(tx.Transaction, tx.BaseFee)
if err != nil {
return nil, err
}
gasUsed := new(big.Int).SetUint64(r.GasUsed)
feeAmount := new(big.Int).Mul(gasUsed, gasPrice)
return &evmClient.RosettaTxReceipt{
GasPrice: gasPrice,
GasUsed: gasUsed,
Logs: r.Logs,
RawMessage: nil,
TransactionFee: feeAmount,
}, err
}
// GetNativeTransferGasLimit is Ethereum's custom implementation of estimating gas.
func (c *EthereumClient) GetNativeTransferGasLimit(ctx context.Context, toAddress string,
fromAddress string, value *big.Int) (uint64, error) {
if len(toAddress) == 0 || value == nil {
// We guard against malformed inputs that may have been generated using
// a previous version of asset's rosetta
return 21000, nil
}
to := common.HexToAddress(toAddress)
return c.EstimateGas(ctx, ethereum.CallMsg{
From: common.HexToAddress(fromAddress),
To: &to,
Value: big.NewInt(0),
})
}
// NewEthereumClient creates a eth client that can interact with
// Ethereum network.
func NewEthereumClient(cfg *configuration.Configuration) (*EthereumClient, error) {
// Use SDK to quickly create a client that support JSON RPC calls
evmClient, err := evmClient.NewClient(cfg, nil)
if err != nil {
log.Fatalln("cannot initialize client: %w", err)
return nil, err
}
// Use embedding for inheritance. So all the methods of the SDKClient
// are instantly available on EthereumClient.
p := &EthereumClient{
*evmClient,
}
return p, err
}