ethclient.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package ethclient provides a client for the Ethereum RPC API.
  17. package ethclient
  18. import (
  19. "context"
  20. "encoding/json"
  21. "errors"
  22. "fmt"
  23. "math/big"
  24. "github.com/ethereum/go-ethereum"
  25. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  26. "github.com/ethereum/go-ethereum/common"
  27. "github.com/ethereum/go-ethereum/common/hexutil"
  28. "github.com/ethereum/go-ethereum/core/types"
  29. "github.com/ethereum/go-ethereum/rpc"
  30. )
  31. // Client defines typed wrappers for the Ethereum RPC API.
  32. type Client struct {
  33. c *rpc.Client
  34. pc privateTransactionManagerClient // Tessera/Constellation client
  35. }
  36. // Dial connects a client to the given URL.
  37. func Dial(rawurl string) (*Client, error) {
  38. return DialContext(context.Background(), rawurl)
  39. }
  40. func DialContext(ctx context.Context, rawurl string) (*Client, error) {
  41. c, err := rpc.DialContext(ctx, rawurl)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return NewClient(c), nil
  46. }
  47. // NewClient creates a client that uses the given RPC client.
  48. func NewClient(c *rpc.Client) *Client {
  49. return &Client{c, nil}
  50. }
  51. // Quorum
  52. //
  53. // NewClientWithPTM creates a client that uses the given RPC client and the privateTransactionManager client
  54. func NewClientWithPTM(c *rpc.Client, ptm privateTransactionManagerClient) *Client {
  55. return &Client{c, ptm}
  56. }
  57. // provides support for private transactions
  58. func (ec *Client) WithPrivateTransactionManager(rawurl string) (*Client, error) {
  59. var err error
  60. ec.pc, err = newPrivateTransactionManagerClient(rawurl)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return ec, nil
  65. }
  66. // End Quorum
  67. func (ec *Client) Close() {
  68. ec.c.Close()
  69. }
  70. // Blockchain Access
  71. // ChainId retrieves the current chain ID for transaction replay protection.
  72. func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) {
  73. var result hexutil.Big
  74. err := ec.c.CallContext(ctx, &result, "eth_chainId")
  75. if err != nil {
  76. return nil, err
  77. }
  78. return (*big.Int)(&result), err
  79. }
  80. // BlockByHash returns the given full block.
  81. //
  82. // Note that loading full blocks requires two requests. Use HeaderByHash
  83. // if you don't need all transactions or uncle headers.
  84. func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
  85. return ec.getBlock(ctx, "eth_getBlockByHash", hash, true)
  86. }
  87. // BlockByNumber returns a block from the current canonical chain. If number is nil, the
  88. // latest known block is returned.
  89. //
  90. // Note that loading full blocks requires two requests. Use HeaderByNumber
  91. // if you don't need all transactions or uncle headers.
  92. func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
  93. return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
  94. }
  95. // BlockNumber returns the most recent block number
  96. func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) {
  97. var result hexutil.Uint64
  98. err := ec.c.CallContext(ctx, &result, "eth_blockNumber")
  99. return uint64(result), err
  100. }
  101. type rpcBlock struct {
  102. Hash common.Hash `json:"hash"`
  103. Transactions []rpcTransaction `json:"transactions"`
  104. UncleHashes []common.Hash `json:"uncles"`
  105. }
  106. func (ec *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
  107. var raw json.RawMessage
  108. err := ec.c.CallContext(ctx, &raw, method, args...)
  109. if err != nil {
  110. return nil, err
  111. } else if len(raw) == 0 {
  112. return nil, ethereum.NotFound
  113. }
  114. // Decode header and transactions.
  115. var head *types.Header
  116. var body rpcBlock
  117. if err := json.Unmarshal(raw, &head); err != nil {
  118. return nil, err
  119. }
  120. if err := json.Unmarshal(raw, &body); err != nil {
  121. return nil, err
  122. }
  123. // Quick-verify transaction and uncle lists. This mostly helps with debugging the server.
  124. if head.UncleHash == types.EmptyUncleHash && len(body.UncleHashes) > 0 {
  125. return nil, fmt.Errorf("server returned non-empty uncle list but block header indicates no uncles")
  126. }
  127. if head.UncleHash != types.EmptyUncleHash && len(body.UncleHashes) == 0 {
  128. return nil, fmt.Errorf("server returned empty uncle list but block header indicates uncles")
  129. }
  130. if head.TxHash == types.EmptyRootHash && len(body.Transactions) > 0 {
  131. return nil, fmt.Errorf("server returned non-empty transaction list but block header indicates no transactions")
  132. }
  133. if head.TxHash != types.EmptyRootHash && len(body.Transactions) == 0 {
  134. return nil, fmt.Errorf("server returned empty transaction list but block header indicates transactions")
  135. }
  136. // Load uncles because they are not included in the block response.
  137. var uncles []*types.Header
  138. if len(body.UncleHashes) > 0 {
  139. uncles = make([]*types.Header, len(body.UncleHashes))
  140. reqs := make([]rpc.BatchElem, len(body.UncleHashes))
  141. for i := range reqs {
  142. reqs[i] = rpc.BatchElem{
  143. Method: "eth_getUncleByBlockHashAndIndex",
  144. Args: []interface{}{body.Hash, hexutil.EncodeUint64(uint64(i))},
  145. Result: &uncles[i],
  146. }
  147. }
  148. if err := ec.c.BatchCallContext(ctx, reqs); err != nil {
  149. return nil, err
  150. }
  151. for i := range reqs {
  152. if reqs[i].Error != nil {
  153. return nil, reqs[i].Error
  154. }
  155. if uncles[i] == nil {
  156. return nil, fmt.Errorf("got null header for uncle %d of block %x", i, body.Hash[:])
  157. }
  158. }
  159. }
  160. // Fill the sender cache of transactions in the block.
  161. txs := make([]*types.Transaction, len(body.Transactions))
  162. for i, tx := range body.Transactions {
  163. if tx.From != nil {
  164. setSenderFromServer(tx.tx, *tx.From, body.Hash)
  165. }
  166. txs[i] = tx.tx
  167. }
  168. return types.NewBlockWithHeader(head).WithBody(txs, uncles), nil
  169. }
  170. // HeaderByHash returns the block header with the given hash.
  171. func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
  172. var head *types.Header
  173. err := ec.c.CallContext(ctx, &head, "eth_getBlockByHash", hash, false)
  174. if err == nil && head == nil {
  175. err = ethereum.NotFound
  176. }
  177. return head, err
  178. }
  179. // HeaderByNumber returns a block header from the current canonical chain. If number is
  180. // nil, the latest known header is returned.
  181. func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  182. var head *types.Header
  183. err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)
  184. if err == nil && head == nil {
  185. err = ethereum.NotFound
  186. }
  187. return head, err
  188. }
  189. type rpcTransaction struct {
  190. tx *types.Transaction
  191. txExtraInfo
  192. }
  193. type txExtraInfo struct {
  194. BlockNumber *string `json:"blockNumber,omitempty"`
  195. BlockHash *common.Hash `json:"blockHash,omitempty"`
  196. From *common.Address `json:"from,omitempty"`
  197. }
  198. func (tx *rpcTransaction) UnmarshalJSON(msg []byte) error {
  199. if err := json.Unmarshal(msg, &tx.tx); err != nil {
  200. return err
  201. }
  202. return json.Unmarshal(msg, &tx.txExtraInfo)
  203. }
  204. // TransactionByHash returns the transaction with the given hash.
  205. func (ec *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
  206. var json *rpcTransaction
  207. err = ec.c.CallContext(ctx, &json, "eth_getTransactionByHash", hash)
  208. if err != nil {
  209. return nil, false, err
  210. } else if json == nil {
  211. return nil, false, ethereum.NotFound
  212. } else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
  213. return nil, false, fmt.Errorf("server returned transaction without signature")
  214. }
  215. if json.From != nil && json.BlockHash != nil {
  216. setSenderFromServer(json.tx, *json.From, *json.BlockHash)
  217. }
  218. return json.tx, json.BlockNumber == nil, nil
  219. }
  220. // TransactionSender returns the sender address of the given transaction. The transaction
  221. // must be known to the remote node and included in the blockchain at the given block and
  222. // index. The sender is the one derived by the protocol at the time of inclusion.
  223. //
  224. // There is a fast-path for transactions retrieved by TransactionByHash and
  225. // TransactionInBlock. Getting their sender address can be done without an RPC interaction.
  226. func (ec *Client) TransactionSender(ctx context.Context, tx *types.Transaction, block common.Hash, index uint) (common.Address, error) {
  227. // Try to load the address from the cache.
  228. sender, err := types.Sender(&senderFromServer{blockhash: block}, tx)
  229. if err == nil {
  230. return sender, nil
  231. }
  232. var meta struct {
  233. Hash common.Hash
  234. From common.Address
  235. }
  236. if err = ec.c.CallContext(ctx, &meta, "eth_getTransactionByBlockHashAndIndex", block, hexutil.Uint64(index)); err != nil {
  237. return common.Address{}, err
  238. }
  239. if meta.Hash == (common.Hash{}) || meta.Hash != tx.Hash() {
  240. return common.Address{}, errors.New("wrong inclusion block/index")
  241. }
  242. return meta.From, nil
  243. }
  244. // TransactionCount returns the total number of transactions in the given block.
  245. func (ec *Client) TransactionCount(ctx context.Context, blockHash common.Hash) (uint, error) {
  246. var num hexutil.Uint
  247. err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByHash", blockHash)
  248. return uint(num), err
  249. }
  250. // TransactionInBlock returns a single transaction at index in the given block.
  251. func (ec *Client) TransactionInBlock(ctx context.Context, blockHash common.Hash, index uint) (*types.Transaction, error) {
  252. var json *rpcTransaction
  253. err := ec.c.CallContext(ctx, &json, "eth_getTransactionByBlockHashAndIndex", blockHash, hexutil.Uint64(index))
  254. if err != nil {
  255. return nil, err
  256. }
  257. if json == nil {
  258. return nil, ethereum.NotFound
  259. } else if _, r, _ := json.tx.RawSignatureValues(); r == nil {
  260. return nil, fmt.Errorf("server returned transaction without signature")
  261. }
  262. if json.From != nil && json.BlockHash != nil {
  263. setSenderFromServer(json.tx, *json.From, *json.BlockHash)
  264. }
  265. return json.tx, err
  266. }
  267. // TransactionReceipt returns the receipt of a transaction by transaction hash.
  268. // Note that the receipt is not available for pending transactions.
  269. func (ec *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
  270. var r *types.Receipt
  271. err := ec.c.CallContext(ctx, &r, "eth_getTransactionReceipt", txHash)
  272. if err == nil {
  273. if r == nil {
  274. return nil, ethereum.NotFound
  275. }
  276. }
  277. return r, err
  278. }
  279. func toBlockNumArg(number *big.Int) string {
  280. if number == nil {
  281. return "latest"
  282. }
  283. pending := big.NewInt(-1)
  284. if number.Cmp(pending) == 0 {
  285. return "pending"
  286. }
  287. return hexutil.EncodeBig(number)
  288. }
  289. type rpcProgress struct {
  290. StartingBlock hexutil.Uint64
  291. CurrentBlock hexutil.Uint64
  292. HighestBlock hexutil.Uint64
  293. PulledStates hexutil.Uint64
  294. KnownStates hexutil.Uint64
  295. }
  296. // SyncProgress retrieves the current progress of the sync algorithm. If there's
  297. // no sync currently running, it returns nil.
  298. func (ec *Client) SyncProgress(ctx context.Context) (*ethereum.SyncProgress, error) {
  299. var raw json.RawMessage
  300. if err := ec.c.CallContext(ctx, &raw, "eth_syncing"); err != nil {
  301. return nil, err
  302. }
  303. // Handle the possible response types
  304. var syncing bool
  305. if err := json.Unmarshal(raw, &syncing); err == nil {
  306. return nil, nil // Not syncing (always false)
  307. }
  308. var progress *rpcProgress
  309. if err := json.Unmarshal(raw, &progress); err != nil {
  310. return nil, err
  311. }
  312. return &ethereum.SyncProgress{
  313. StartingBlock: uint64(progress.StartingBlock),
  314. CurrentBlock: uint64(progress.CurrentBlock),
  315. HighestBlock: uint64(progress.HighestBlock),
  316. PulledStates: uint64(progress.PulledStates),
  317. KnownStates: uint64(progress.KnownStates),
  318. }, nil
  319. }
  320. // SubscribeNewHead subscribes to notifications about the current blockchain head
  321. // on the given channel.
  322. func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) {
  323. return ec.c.EthSubscribe(ctx, ch, "newHeads")
  324. }
  325. // State Access
  326. // NetworkID returns the network ID (also known as the chain ID) for this chain.
  327. func (ec *Client) NetworkID(ctx context.Context) (*big.Int, error) {
  328. version := new(big.Int)
  329. var ver string
  330. if err := ec.c.CallContext(ctx, &ver, "net_version"); err != nil {
  331. return nil, err
  332. }
  333. if _, ok := version.SetString(ver, 10); !ok {
  334. return nil, fmt.Errorf("invalid net_version result %q", ver)
  335. }
  336. return version, nil
  337. }
  338. // BalanceAt returns the wei balance of the given account.
  339. // The block number can be nil, in which case the balance is taken from the latest known block.
  340. func (ec *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
  341. var result hexutil.Big
  342. err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, toBlockNumArg(blockNumber))
  343. return (*big.Int)(&result), err
  344. }
  345. // StorageAt returns the value of key in the contract storage of the given account.
  346. // The block number can be nil, in which case the value is taken from the latest known block.
  347. func (ec *Client) StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error) {
  348. var result hexutil.Bytes
  349. err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, toBlockNumArg(blockNumber))
  350. return result, err
  351. }
  352. // CodeAt returns the contract code of the given account.
  353. // The block number can be nil, in which case the code is taken from the latest known block.
  354. func (ec *Client) CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error) {
  355. var result hexutil.Bytes
  356. err := ec.c.CallContext(ctx, &result, "eth_getCode", account, toBlockNumArg(blockNumber))
  357. return result, err
  358. }
  359. // NonceAt returns the account nonce of the given account.
  360. // The block number can be nil, in which case the nonce is taken from the latest known block.
  361. func (ec *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
  362. var result hexutil.Uint64
  363. err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, toBlockNumArg(blockNumber))
  364. return uint64(result), err
  365. }
  366. // Filters
  367. // FilterLogs executes a filter query.
  368. func (ec *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
  369. var result []types.Log
  370. arg, err := toFilterArg(q)
  371. if err != nil {
  372. return nil, err
  373. }
  374. err = ec.c.CallContext(ctx, &result, "eth_getLogs", arg)
  375. return result, err
  376. }
  377. // SubscribeFilterLogs subscribes to the results of a streaming filter query.
  378. func (ec *Client) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
  379. arg, err := toFilterArg(q)
  380. if err != nil {
  381. return nil, err
  382. }
  383. return ec.c.EthSubscribe(ctx, ch, "logs", arg)
  384. }
  385. func toFilterArg(q ethereum.FilterQuery) (interface{}, error) {
  386. arg := map[string]interface{}{
  387. "address": q.Addresses,
  388. "topics": q.Topics,
  389. }
  390. if q.BlockHash != nil {
  391. arg["blockHash"] = *q.BlockHash
  392. if q.FromBlock != nil || q.ToBlock != nil {
  393. return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
  394. }
  395. } else {
  396. if q.FromBlock == nil {
  397. arg["fromBlock"] = "0x0"
  398. } else {
  399. arg["fromBlock"] = toBlockNumArg(q.FromBlock)
  400. }
  401. arg["toBlock"] = toBlockNumArg(q.ToBlock)
  402. }
  403. return arg, nil
  404. }
  405. // Pending State
  406. // PendingBalanceAt returns the wei balance of the given account in the pending state.
  407. func (ec *Client) PendingBalanceAt(ctx context.Context, account common.Address) (*big.Int, error) {
  408. var result hexutil.Big
  409. err := ec.c.CallContext(ctx, &result, "eth_getBalance", account, "pending")
  410. return (*big.Int)(&result), err
  411. }
  412. // PendingStorageAt returns the value of key in the contract storage of the given account in the pending state.
  413. func (ec *Client) PendingStorageAt(ctx context.Context, account common.Address, key common.Hash) ([]byte, error) {
  414. var result hexutil.Bytes
  415. err := ec.c.CallContext(ctx, &result, "eth_getStorageAt", account, key, "pending")
  416. return result, err
  417. }
  418. // PendingCodeAt returns the contract code of the given account in the pending state.
  419. func (ec *Client) PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error) {
  420. var result hexutil.Bytes
  421. err := ec.c.CallContext(ctx, &result, "eth_getCode", account, "pending")
  422. return result, err
  423. }
  424. // PendingNonceAt returns the account nonce of the given account in the pending state.
  425. // This is the nonce that should be used for the next transaction.
  426. func (ec *Client) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
  427. var result hexutil.Uint64
  428. err := ec.c.CallContext(ctx, &result, "eth_getTransactionCount", account, "pending")
  429. return uint64(result), err
  430. }
  431. // PendingTransactionCount returns the total number of transactions in the pending state.
  432. func (ec *Client) PendingTransactionCount(ctx context.Context) (uint, error) {
  433. var num hexutil.Uint
  434. err := ec.c.CallContext(ctx, &num, "eth_getBlockTransactionCountByNumber", "pending")
  435. return uint(num), err
  436. }
  437. // TODO: SubscribePendingTransactions (needs server side)
  438. // Contract Calling
  439. // CallContract executes a message call transaction, which is directly executed in the VM
  440. // of the node, but never mined into the blockchain.
  441. //
  442. // blockNumber selects the block height at which the call runs. It can be nil, in which
  443. // case the code is taken from the latest known block. Note that state from very old
  444. // blocks might not be available.
  445. func (ec *Client) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
  446. var hex hexutil.Bytes
  447. err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), toBlockNumArg(blockNumber))
  448. if err != nil {
  449. return nil, err
  450. }
  451. return hex, nil
  452. }
  453. // PendingCallContract executes a message call transaction using the EVM.
  454. // The state seen by the contract call is the pending state.
  455. func (ec *Client) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
  456. var hex hexutil.Bytes
  457. err := ec.c.CallContext(ctx, &hex, "eth_call", toCallArg(msg), "pending")
  458. if err != nil {
  459. return nil, err
  460. }
  461. return hex, nil
  462. }
  463. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  464. // execution of a transaction.
  465. func (ec *Client) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
  466. var hex hexutil.Big
  467. if err := ec.c.CallContext(ctx, &hex, "eth_gasPrice"); err != nil {
  468. return nil, err
  469. }
  470. return (*big.Int)(&hex), nil
  471. }
  472. // EstimateGas tries to estimate the gas needed to execute a specific transaction based on
  473. // the current pending state of the backend blockchain. There is no guarantee that this is
  474. // the true gas limit requirement as other transactions may be added or removed by miners,
  475. // but it should provide a basis for setting a reasonable default.
  476. func (ec *Client) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
  477. var hex hexutil.Uint64
  478. err := ec.c.CallContext(ctx, &hex, "eth_estimateGas", toCallArg(msg))
  479. if err != nil {
  480. return 0, err
  481. }
  482. return uint64(hex), nil
  483. }
  484. // SendTransaction injects a signed transaction into the pending pool for execution.
  485. //
  486. // If the transaction was a contract creation use the TransactionReceipt method to get the
  487. // contract address after the transaction has been mined.
  488. func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction, args bind.PrivateTxArgs) error {
  489. data, err := tx.MarshalBinary()
  490. if err != nil {
  491. return err
  492. }
  493. if args.PrivateFor != nil {
  494. return ec.c.CallContext(ctx, nil, "eth_sendRawPrivateTransaction", hexutil.Encode(data), bind.PrivateTxArgs{PrivateFor: args.PrivateFor})
  495. } else {
  496. return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
  497. }
  498. }
  499. // Quorum
  500. //
  501. // Retrieve encrypted payload hash from the private transaction manager if configured
  502. func (ec *Client) PreparePrivateTransaction(data []byte, privateFrom string) (common.EncryptedPayloadHash, error) {
  503. if ec.pc == nil {
  504. return common.EncryptedPayloadHash{}, errors.New("missing private transaction manager client configuration")
  505. }
  506. payLoadHash, err := ec.pc.StoreRaw(data, privateFrom)
  507. return payLoadHash, err
  508. }
  509. func (ec *Client) DistributeTransaction(ctx context.Context, tx *types.Transaction, args bind.PrivateTxArgs) (string, error) {
  510. data, err := tx.MarshalBinary()
  511. if err != nil {
  512. return "", err
  513. }
  514. retVal := ""
  515. err = ec.c.CallContext(ctx, &retVal, "eth_distributePrivateTransaction", hexutil.Encode(data), bind.PrivateTxArgs{PrivateFor: args.PrivateFor})
  516. return retVal, err
  517. }
  518. func (ec *Client) GetPrivateTransaction(ctx context.Context, pmtHash common.Hash) (*types.Transaction, error) {
  519. var privateTx types.Transaction
  520. err := ec.c.CallContext(ctx, &privateTx, "eth_getPrivateTransactionByHash", pmtHash)
  521. return &privateTx, err
  522. }
  523. func toCallArg(msg ethereum.CallMsg) interface{} {
  524. arg := map[string]interface{}{
  525. "from": msg.From,
  526. "to": msg.To,
  527. }
  528. if len(msg.Data) > 0 {
  529. arg["data"] = hexutil.Bytes(msg.Data)
  530. }
  531. if msg.Value != nil {
  532. arg["value"] = (*hexutil.Big)(msg.Value)
  533. }
  534. if msg.Gas != 0 {
  535. arg["gas"] = hexutil.Uint64(msg.Gas)
  536. }
  537. if msg.GasPrice != nil {
  538. arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice)
  539. }
  540. return arg
  541. }