eth_args.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // Copyright 2015 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 api
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "math/big"
  21. "strconv"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/rpc/shared"
  27. )
  28. const (
  29. defaultLogLimit = 100
  30. defaultLogOffset = 0
  31. )
  32. type GetBalanceArgs struct {
  33. Address string
  34. BlockNumber int64
  35. }
  36. func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
  37. var obj []interface{}
  38. if err := json.Unmarshal(b, &obj); err != nil {
  39. return shared.NewDecodeParamError(err.Error())
  40. }
  41. if len(obj) < 1 {
  42. return shared.NewInsufficientParamsError(len(obj), 1)
  43. }
  44. addstr, ok := obj[0].(string)
  45. if !ok {
  46. return shared.NewInvalidTypeError("address", "not a string")
  47. }
  48. args.Address = addstr
  49. if len(obj) > 1 {
  50. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  51. return err
  52. }
  53. } else {
  54. args.BlockNumber = -1
  55. }
  56. return nil
  57. }
  58. type GetStorageArgs struct {
  59. Address string
  60. BlockNumber int64
  61. }
  62. func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
  63. var obj []interface{}
  64. if err := json.Unmarshal(b, &obj); err != nil {
  65. return shared.NewDecodeParamError(err.Error())
  66. }
  67. if len(obj) < 1 {
  68. return shared.NewInsufficientParamsError(len(obj), 1)
  69. }
  70. addstr, ok := obj[0].(string)
  71. if !ok {
  72. return shared.NewInvalidTypeError("address", "not a string")
  73. }
  74. args.Address = addstr
  75. if len(obj) > 1 {
  76. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  77. return err
  78. }
  79. } else {
  80. args.BlockNumber = -1
  81. }
  82. return nil
  83. }
  84. type GetStorageAtArgs struct {
  85. Address string
  86. BlockNumber int64
  87. Key string
  88. }
  89. func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
  90. var obj []interface{}
  91. if err := json.Unmarshal(b, &obj); err != nil {
  92. return shared.NewDecodeParamError(err.Error())
  93. }
  94. if len(obj) < 2 {
  95. return shared.NewInsufficientParamsError(len(obj), 2)
  96. }
  97. addstr, ok := obj[0].(string)
  98. if !ok {
  99. return shared.NewInvalidTypeError("address", "not a string")
  100. }
  101. args.Address = addstr
  102. keystr, ok := obj[1].(string)
  103. if !ok {
  104. return shared.NewInvalidTypeError("key", "not a string")
  105. }
  106. args.Key = keystr
  107. if len(obj) > 2 {
  108. if err := blockHeight(obj[2], &args.BlockNumber); err != nil {
  109. return err
  110. }
  111. } else {
  112. args.BlockNumber = -1
  113. }
  114. return nil
  115. }
  116. type GetTxCountArgs struct {
  117. Address string
  118. BlockNumber int64
  119. }
  120. func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
  121. var obj []interface{}
  122. if err := json.Unmarshal(b, &obj); err != nil {
  123. return shared.NewDecodeParamError(err.Error())
  124. }
  125. if len(obj) < 1 {
  126. return shared.NewInsufficientParamsError(len(obj), 1)
  127. }
  128. addstr, ok := obj[0].(string)
  129. if !ok {
  130. return shared.NewInvalidTypeError("address", "not a string")
  131. }
  132. args.Address = addstr
  133. if len(obj) > 1 {
  134. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  135. return err
  136. }
  137. } else {
  138. args.BlockNumber = -1
  139. }
  140. return nil
  141. }
  142. type SubmitHashRateArgs struct {
  143. Id string
  144. Rate uint64
  145. }
  146. func (args *SubmitHashRateArgs) UnmarshalJSON(b []byte) (err error) {
  147. var obj []interface{}
  148. if err := json.Unmarshal(b, &obj); err != nil {
  149. return shared.NewDecodeParamError(err.Error())
  150. }
  151. if len(obj) < 2 {
  152. return shared.NewInsufficientParamsError(len(obj), 2)
  153. }
  154. arg0, ok := obj[0].(string)
  155. if !ok {
  156. return shared.NewInvalidTypeError("hash", "not a string")
  157. }
  158. args.Id = arg0
  159. arg1, ok := obj[1].(string)
  160. if !ok {
  161. return shared.NewInvalidTypeError("rate", "not a string")
  162. }
  163. args.Rate = common.String2Big(arg1).Uint64()
  164. return nil
  165. }
  166. type HashArgs struct {
  167. Hash string
  168. }
  169. func (args *HashArgs) UnmarshalJSON(b []byte) (err error) {
  170. var obj []interface{}
  171. if err := json.Unmarshal(b, &obj); err != nil {
  172. return shared.NewDecodeParamError(err.Error())
  173. }
  174. if len(obj) < 1 {
  175. return shared.NewInsufficientParamsError(len(obj), 1)
  176. }
  177. arg0, ok := obj[0].(string)
  178. if !ok {
  179. return shared.NewInvalidTypeError("hash", "not a string")
  180. }
  181. args.Hash = arg0
  182. return nil
  183. }
  184. type BlockNumArg struct {
  185. BlockNumber int64
  186. }
  187. func (args *BlockNumArg) UnmarshalJSON(b []byte) (err error) {
  188. var obj []interface{}
  189. if err := json.Unmarshal(b, &obj); err != nil {
  190. return shared.NewDecodeParamError(err.Error())
  191. }
  192. if len(obj) < 1 {
  193. return shared.NewInsufficientParamsError(len(obj), 1)
  194. }
  195. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  196. return err
  197. }
  198. return nil
  199. }
  200. type GetDataArgs struct {
  201. Address string
  202. BlockNumber int64
  203. }
  204. func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
  205. var obj []interface{}
  206. if err := json.Unmarshal(b, &obj); err != nil {
  207. return shared.NewDecodeParamError(err.Error())
  208. }
  209. if len(obj) < 1 {
  210. return shared.NewInsufficientParamsError(len(obj), 1)
  211. }
  212. addstr, ok := obj[0].(string)
  213. if !ok {
  214. return shared.NewInvalidTypeError("address", "not a string")
  215. }
  216. args.Address = addstr
  217. if len(obj) > 1 {
  218. if err := blockHeight(obj[1], &args.BlockNumber); err != nil {
  219. return err
  220. }
  221. } else {
  222. args.BlockNumber = -1
  223. }
  224. return nil
  225. }
  226. type NewDataArgs struct {
  227. Data string
  228. }
  229. func (args *NewDataArgs) UnmarshalJSON(b []byte) (err error) {
  230. var obj []interface{}
  231. if err := json.Unmarshal(b, &obj); err != nil {
  232. return shared.NewDecodeParamError(err.Error())
  233. }
  234. // Check for sufficient params
  235. if len(obj) < 1 {
  236. return shared.NewInsufficientParamsError(len(obj), 1)
  237. }
  238. data, ok := obj[0].(string)
  239. if !ok {
  240. return shared.NewInvalidTypeError("data", "not a string")
  241. }
  242. args.Data = data
  243. if len(args.Data) == 0 {
  244. return shared.NewValidationError("data", "is required")
  245. }
  246. return nil
  247. }
  248. type NewSigArgs struct {
  249. From string
  250. Data string
  251. }
  252. func (args *NewSigArgs) UnmarshalJSON(b []byte) (err error) {
  253. var obj []interface{}
  254. if err := json.Unmarshal(b, &obj); err != nil {
  255. return shared.NewDecodeParamError(err.Error())
  256. }
  257. // Check for sufficient params
  258. if len(obj) < 1 {
  259. return shared.NewInsufficientParamsError(len(obj), 1)
  260. }
  261. from, ok := obj[0].(string)
  262. if !ok {
  263. return shared.NewInvalidTypeError("from", "not a string")
  264. }
  265. args.From = from
  266. if len(args.From) == 0 {
  267. return shared.NewValidationError("from", "is required")
  268. }
  269. data, ok := obj[1].(string)
  270. if !ok {
  271. return shared.NewInvalidTypeError("data", "not a string")
  272. }
  273. args.Data = data
  274. if len(args.Data) == 0 {
  275. return shared.NewValidationError("data", "is required")
  276. }
  277. return nil
  278. }
  279. type NewTxArgs struct {
  280. From string
  281. To string
  282. Nonce *big.Int
  283. Value *big.Int
  284. Gas *big.Int
  285. GasPrice *big.Int
  286. Data string
  287. BlockNumber int64
  288. }
  289. func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
  290. var obj []json.RawMessage
  291. var ext struct {
  292. From string
  293. To string
  294. Nonce interface{}
  295. Value interface{}
  296. Gas interface{}
  297. GasPrice interface{}
  298. Data string
  299. }
  300. // Decode byte slice to array of RawMessages
  301. if err := json.Unmarshal(b, &obj); err != nil {
  302. return shared.NewDecodeParamError(err.Error())
  303. }
  304. // Check for sufficient params
  305. if len(obj) < 1 {
  306. return shared.NewInsufficientParamsError(len(obj), 1)
  307. }
  308. // Decode 0th RawMessage to temporary struct
  309. if err := json.Unmarshal(obj[0], &ext); err != nil {
  310. return shared.NewDecodeParamError(err.Error())
  311. }
  312. if len(ext.From) == 0 {
  313. return shared.NewValidationError("from", "is required")
  314. }
  315. args.From = ext.From
  316. args.To = ext.To
  317. args.Data = ext.Data
  318. var num *big.Int
  319. if ext.Nonce != nil {
  320. num, err = numString(ext.Nonce)
  321. if err != nil {
  322. return err
  323. }
  324. }
  325. args.Nonce = num
  326. if ext.Value == nil {
  327. num = big.NewInt(0)
  328. } else {
  329. num, err = numString(ext.Value)
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. args.Value = num
  335. num = nil
  336. if ext.Gas != nil {
  337. if num, err = numString(ext.Gas); err != nil {
  338. return err
  339. }
  340. }
  341. args.Gas = num
  342. num = nil
  343. if ext.GasPrice != nil {
  344. if num, err = numString(ext.GasPrice); err != nil {
  345. return err
  346. }
  347. }
  348. args.GasPrice = num
  349. // Check for optional BlockNumber param
  350. if len(obj) > 1 {
  351. if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil {
  352. return err
  353. }
  354. } else {
  355. args.BlockNumber = -1
  356. }
  357. return nil
  358. }
  359. type SourceArgs struct {
  360. Source string
  361. }
  362. func (args *SourceArgs) UnmarshalJSON(b []byte) (err error) {
  363. var obj []interface{}
  364. if err := json.Unmarshal(b, &obj); err != nil {
  365. return shared.NewDecodeParamError(err.Error())
  366. }
  367. if len(obj) < 1 {
  368. return shared.NewInsufficientParamsError(len(obj), 1)
  369. }
  370. arg0, ok := obj[0].(string)
  371. if !ok {
  372. return shared.NewInvalidTypeError("source code", "not a string")
  373. }
  374. args.Source = arg0
  375. return nil
  376. }
  377. type CallArgs struct {
  378. From string
  379. To string
  380. Value *big.Int
  381. Gas *big.Int
  382. GasPrice *big.Int
  383. Data string
  384. BlockNumber int64
  385. }
  386. func (args *CallArgs) UnmarshalJSON(b []byte) (err error) {
  387. var obj []json.RawMessage
  388. var ext struct {
  389. From string
  390. To string
  391. Value interface{}
  392. Gas interface{}
  393. GasPrice interface{}
  394. Data string
  395. }
  396. // Decode byte slice to array of RawMessages
  397. if err := json.Unmarshal(b, &obj); err != nil {
  398. return shared.NewDecodeParamError(err.Error())
  399. }
  400. // Check for sufficient params
  401. if len(obj) < 1 {
  402. return shared.NewInsufficientParamsError(len(obj), 1)
  403. }
  404. // Decode 0th RawMessage to temporary struct
  405. if err := json.Unmarshal(obj[0], &ext); err != nil {
  406. return shared.NewDecodeParamError(err.Error())
  407. }
  408. args.From = ext.From
  409. args.To = ext.To
  410. var num *big.Int
  411. if ext.Value == nil {
  412. num = big.NewInt(0)
  413. } else {
  414. if num, err = numString(ext.Value); err != nil {
  415. return err
  416. }
  417. }
  418. args.Value = num
  419. if ext.Gas != nil {
  420. if num, err = numString(ext.Gas); err != nil {
  421. return err
  422. }
  423. } else {
  424. num = nil
  425. }
  426. args.Gas = num
  427. if ext.GasPrice != nil {
  428. if num, err = numString(ext.GasPrice); err != nil {
  429. return err
  430. }
  431. } else {
  432. num = nil
  433. }
  434. args.GasPrice = num
  435. args.Data = ext.Data
  436. // Check for optional BlockNumber param
  437. if len(obj) > 1 {
  438. if err := blockHeightFromJson(obj[1], &args.BlockNumber); err != nil {
  439. return err
  440. }
  441. } else {
  442. args.BlockNumber = -1
  443. }
  444. return nil
  445. }
  446. type HashIndexArgs struct {
  447. Hash string
  448. Index int64
  449. }
  450. func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) {
  451. var obj []interface{}
  452. if err := json.Unmarshal(b, &obj); err != nil {
  453. return shared.NewDecodeParamError(err.Error())
  454. }
  455. if len(obj) < 2 {
  456. return shared.NewInsufficientParamsError(len(obj), 2)
  457. }
  458. arg0, ok := obj[0].(string)
  459. if !ok {
  460. return shared.NewInvalidTypeError("hash", "not a string")
  461. }
  462. args.Hash = arg0
  463. arg1, ok := obj[1].(string)
  464. if !ok {
  465. return shared.NewInvalidTypeError("index", "not a string")
  466. }
  467. args.Index = common.Big(arg1).Int64()
  468. return nil
  469. }
  470. type BlockNumIndexArgs struct {
  471. BlockNumber int64
  472. Index int64
  473. }
  474. func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) {
  475. var obj []interface{}
  476. if err := json.Unmarshal(b, &obj); err != nil {
  477. return shared.NewDecodeParamError(err.Error())
  478. }
  479. if len(obj) < 2 {
  480. return shared.NewInsufficientParamsError(len(obj), 2)
  481. }
  482. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  483. return err
  484. }
  485. var arg1 *big.Int
  486. if arg1, err = numString(obj[1]); err != nil {
  487. return err
  488. }
  489. args.Index = arg1.Int64()
  490. return nil
  491. }
  492. type GetBlockByHashArgs struct {
  493. BlockHash string
  494. IncludeTxs bool
  495. }
  496. func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
  497. var obj []interface{}
  498. if err := json.Unmarshal(b, &obj); err != nil {
  499. return shared.NewDecodeParamError(err.Error())
  500. }
  501. if len(obj) < 2 {
  502. return shared.NewInsufficientParamsError(len(obj), 2)
  503. }
  504. argstr, ok := obj[0].(string)
  505. if !ok {
  506. return shared.NewInvalidTypeError("blockHash", "not a string")
  507. }
  508. args.BlockHash = argstr
  509. args.IncludeTxs = obj[1].(bool)
  510. return nil
  511. }
  512. type GetBlockByNumberArgs struct {
  513. BlockNumber int64
  514. IncludeTxs bool
  515. }
  516. func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
  517. var obj []interface{}
  518. if err := json.Unmarshal(b, &obj); err != nil {
  519. return shared.NewDecodeParamError(err.Error())
  520. }
  521. if len(obj) < 2 {
  522. return shared.NewInsufficientParamsError(len(obj), 2)
  523. }
  524. if err := blockHeight(obj[0], &args.BlockNumber); err != nil {
  525. return err
  526. }
  527. args.IncludeTxs = obj[1].(bool)
  528. return nil
  529. }
  530. type BlockFilterArgs struct {
  531. Earliest int64
  532. Latest int64
  533. Address []string
  534. Topics [][]string
  535. Skip int
  536. Max int
  537. }
  538. func (args *BlockFilterArgs) UnmarshalJSON(b []byte) (err error) {
  539. var obj []struct {
  540. FromBlock interface{} `json:"fromBlock"`
  541. ToBlock interface{} `json:"toBlock"`
  542. Limit interface{} `json:"limit"`
  543. Offset interface{} `json:"offset"`
  544. Address interface{} `json:"address"`
  545. Topics interface{} `json:"topics"`
  546. }
  547. if err = json.Unmarshal(b, &obj); err != nil {
  548. return shared.NewDecodeParamError(err.Error())
  549. }
  550. if len(obj) < 1 {
  551. return shared.NewInsufficientParamsError(len(obj), 1)
  552. }
  553. // args.Earliest, err = toNumber(obj[0].ToBlock)
  554. // if err != nil {
  555. // return shared.NewDecodeParamError(fmt.Sprintf("FromBlock %v", err))
  556. // }
  557. // args.Latest, err = toNumber(obj[0].FromBlock)
  558. // if err != nil {
  559. // return shared.NewDecodeParamError(fmt.Sprintf("ToBlock %v", err))
  560. var num int64
  561. var numBig *big.Int
  562. // if blank then latest
  563. if obj[0].FromBlock == nil {
  564. num = -1
  565. } else {
  566. if err := blockHeight(obj[0].FromBlock, &num); err != nil {
  567. return err
  568. }
  569. }
  570. // if -2 or other "silly" number, use latest
  571. if num < 0 {
  572. args.Earliest = -1 //latest block
  573. } else {
  574. args.Earliest = num
  575. }
  576. // if blank than latest
  577. if obj[0].ToBlock == nil {
  578. num = -1
  579. } else {
  580. if err := blockHeight(obj[0].ToBlock, &num); err != nil {
  581. return err
  582. }
  583. }
  584. args.Latest = num
  585. if obj[0].Limit == nil {
  586. numBig = big.NewInt(defaultLogLimit)
  587. } else {
  588. if numBig, err = numString(obj[0].Limit); err != nil {
  589. return err
  590. }
  591. }
  592. args.Max = int(numBig.Int64())
  593. if obj[0].Offset == nil {
  594. numBig = big.NewInt(defaultLogOffset)
  595. } else {
  596. if numBig, err = numString(obj[0].Offset); err != nil {
  597. return err
  598. }
  599. }
  600. args.Skip = int(numBig.Int64())
  601. if obj[0].Address != nil {
  602. marg, ok := obj[0].Address.([]interface{})
  603. if ok {
  604. v := make([]string, len(marg))
  605. for i, arg := range marg {
  606. argstr, ok := arg.(string)
  607. if !ok {
  608. return shared.NewInvalidTypeError(fmt.Sprintf("address[%d]", i), "is not a string")
  609. }
  610. v[i] = argstr
  611. }
  612. args.Address = v
  613. } else {
  614. argstr, ok := obj[0].Address.(string)
  615. if ok {
  616. v := make([]string, 1)
  617. v[0] = argstr
  618. args.Address = v
  619. } else {
  620. return shared.NewInvalidTypeError("address", "is not a string or array")
  621. }
  622. }
  623. }
  624. if obj[0].Topics != nil {
  625. other, ok := obj[0].Topics.([]interface{})
  626. if ok {
  627. topicdbl := make([][]string, len(other))
  628. for i, iv := range other {
  629. if argstr, ok := iv.(string); ok {
  630. // Found a string, push into first element of array
  631. topicsgl := make([]string, 1)
  632. topicsgl[0] = argstr
  633. topicdbl[i] = topicsgl
  634. } else if argarray, ok := iv.([]interface{}); ok {
  635. // Found an array of other
  636. topicdbl[i] = make([]string, len(argarray))
  637. for j, jv := range argarray {
  638. if v, ok := jv.(string); ok {
  639. topicdbl[i][j] = v
  640. } else if jv == nil {
  641. topicdbl[i][j] = ""
  642. } else {
  643. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d][%d]", i, j), "is not a string")
  644. }
  645. }
  646. } else if iv == nil {
  647. topicdbl[i] = []string{""}
  648. } else {
  649. return shared.NewInvalidTypeError(fmt.Sprintf("topic[%d]", i), "not a string or array")
  650. }
  651. }
  652. args.Topics = topicdbl
  653. return nil
  654. } else {
  655. return shared.NewInvalidTypeError("topic", "is not a string or array")
  656. }
  657. }
  658. return nil
  659. }
  660. type FilterIdArgs struct {
  661. Id int
  662. }
  663. func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) {
  664. var obj []interface{}
  665. if err := json.Unmarshal(b, &obj); err != nil {
  666. return shared.NewDecodeParamError(err.Error())
  667. }
  668. if len(obj) < 1 {
  669. return shared.NewInsufficientParamsError(len(obj), 1)
  670. }
  671. var num *big.Int
  672. if num, err = numString(obj[0]); err != nil {
  673. return err
  674. }
  675. args.Id = int(num.Int64())
  676. return nil
  677. }
  678. type LogRes struct {
  679. Address *hexdata `json:"address"`
  680. Topics []*hexdata `json:"topics"`
  681. Data *hexdata `json:"data"`
  682. BlockNumber *hexnum `json:"blockNumber"`
  683. LogIndex *hexnum `json:"logIndex"`
  684. BlockHash *hexdata `json:"blockHash"`
  685. TransactionHash *hexdata `json:"transactionHash"`
  686. TransactionIndex *hexnum `json:"transactionIndex"`
  687. }
  688. func NewLogRes(log *vm.Log) LogRes {
  689. var l LogRes
  690. l.Topics = make([]*hexdata, len(log.Topics))
  691. for j, topic := range log.Topics {
  692. l.Topics[j] = newHexData(topic)
  693. }
  694. l.Address = newHexData(log.Address)
  695. l.Data = newHexData(log.Data)
  696. l.BlockNumber = newHexNum(log.Number)
  697. l.LogIndex = newHexNum(log.Index)
  698. l.TransactionHash = newHexData(log.TxHash)
  699. l.TransactionIndex = newHexNum(log.TxIndex)
  700. l.BlockHash = newHexData(log.BlockHash)
  701. return l
  702. }
  703. func NewLogsRes(logs vm.Logs) (ls []LogRes) {
  704. ls = make([]LogRes, len(logs))
  705. for i, log := range logs {
  706. ls[i] = NewLogRes(log)
  707. }
  708. return
  709. }
  710. func NewHashesRes(hs []common.Hash) []string {
  711. hashes := make([]string, len(hs))
  712. for i, hash := range hs {
  713. hashes[i] = hash.Hex()
  714. }
  715. return hashes
  716. }
  717. type SubmitWorkArgs struct {
  718. Nonce uint64
  719. Header string
  720. Digest string
  721. }
  722. func (args *SubmitWorkArgs) UnmarshalJSON(b []byte) (err error) {
  723. var obj []interface{}
  724. if err = json.Unmarshal(b, &obj); err != nil {
  725. return shared.NewDecodeParamError(err.Error())
  726. }
  727. if len(obj) < 3 {
  728. return shared.NewInsufficientParamsError(len(obj), 3)
  729. }
  730. var objstr string
  731. var ok bool
  732. if objstr, ok = obj[0].(string); !ok {
  733. return shared.NewInvalidTypeError("nonce", "not a string")
  734. }
  735. args.Nonce = common.String2Big(objstr).Uint64()
  736. if objstr, ok = obj[1].(string); !ok {
  737. return shared.NewInvalidTypeError("header", "not a string")
  738. }
  739. args.Header = objstr
  740. if objstr, ok = obj[2].(string); !ok {
  741. return shared.NewInvalidTypeError("digest", "not a string")
  742. }
  743. args.Digest = objstr
  744. return nil
  745. }
  746. type tx struct {
  747. tx *types.Transaction
  748. To string `json:"to"`
  749. From string `json:"from"`
  750. Nonce string `json:"nonce"`
  751. Value string `json:"value"`
  752. Data string `json:"data"`
  753. GasLimit string `json:"gas"`
  754. GasPrice string `json:"gasPrice"`
  755. Hash string `json:"hash"`
  756. }
  757. func newTx(t *types.Transaction) *tx {
  758. from, _ := t.From()
  759. var to string
  760. if t := t.To(); t != nil {
  761. to = t.Hex()
  762. }
  763. return &tx{
  764. tx: t,
  765. To: to,
  766. From: from.Hex(),
  767. Value: t.Value().String(),
  768. Nonce: strconv.Itoa(int(t.Nonce())),
  769. Data: "0x" + common.Bytes2Hex(t.Data()),
  770. GasLimit: t.Gas().String(),
  771. GasPrice: t.GasPrice().String(),
  772. Hash: t.Hash().Hex(),
  773. }
  774. }
  775. type ResendArgs struct {
  776. Tx *tx
  777. GasPrice string
  778. GasLimit string
  779. }
  780. func (tx *tx) UnmarshalJSON(b []byte) (err error) {
  781. var fields map[string]interface{}
  782. if err := json.Unmarshal(b, &fields); err != nil {
  783. return shared.NewDecodeParamError(err.Error())
  784. }
  785. var (
  786. nonce uint64
  787. to common.Address
  788. amount = new(big.Int).Set(common.Big0)
  789. gasLimit = new(big.Int).Set(common.Big0)
  790. gasPrice = new(big.Int).Set(common.Big0)
  791. data []byte
  792. contractCreation = true
  793. )
  794. if val, found := fields["Hash"]; found {
  795. if hashVal, ok := val.(string); ok {
  796. tx.Hash = hashVal
  797. }
  798. }
  799. if val, found := fields["To"]; found {
  800. if strVal, ok := val.(string); ok && len(strVal) > 0 {
  801. tx.To = strVal
  802. to = common.HexToAddress(strVal)
  803. contractCreation = false
  804. }
  805. }
  806. if val, found := fields["From"]; found {
  807. if strVal, ok := val.(string); ok {
  808. tx.From = strVal
  809. }
  810. }
  811. if val, found := fields["Nonce"]; found {
  812. if strVal, ok := val.(string); ok {
  813. tx.Nonce = strVal
  814. if nonce, err = strconv.ParseUint(strVal, 10, 64); err != nil {
  815. return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err))
  816. }
  817. }
  818. } else {
  819. return shared.NewDecodeParamError("tx.Nonce not found")
  820. }
  821. var parseOk bool
  822. if val, found := fields["Value"]; found {
  823. if strVal, ok := val.(string); ok {
  824. tx.Value = strVal
  825. if _, parseOk = amount.SetString(strVal, 0); !parseOk {
  826. return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err))
  827. }
  828. }
  829. }
  830. if val, found := fields["Data"]; found {
  831. if strVal, ok := val.(string); ok {
  832. tx.Data = strVal
  833. if strings.HasPrefix(strVal, "0x") {
  834. data = common.Hex2Bytes(strVal[2:])
  835. } else {
  836. data = common.Hex2Bytes(strVal)
  837. }
  838. }
  839. }
  840. if val, found := fields["GasLimit"]; found {
  841. if strVal, ok := val.(string); ok {
  842. tx.GasLimit = strVal
  843. if _, parseOk = gasLimit.SetString(strVal, 0); !parseOk {
  844. return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err))
  845. }
  846. }
  847. }
  848. if val, found := fields["GasPrice"]; found {
  849. if strVal, ok := val.(string); ok {
  850. tx.GasPrice = strVal
  851. if _, parseOk = gasPrice.SetString(strVal, 0); !parseOk {
  852. return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err))
  853. }
  854. }
  855. }
  856. if contractCreation {
  857. tx.tx = types.NewContractCreation(nonce, amount, gasLimit, gasPrice, data)
  858. } else {
  859. tx.tx = types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
  860. }
  861. return nil
  862. }
  863. func (args *ResendArgs) UnmarshalJSON(b []byte) (err error) {
  864. var obj []interface{}
  865. if err = json.Unmarshal(b, &obj); err != nil {
  866. return shared.NewDecodeParamError(err.Error())
  867. }
  868. if len(obj) < 1 {
  869. return shared.NewInsufficientParamsError(len(obj), 1)
  870. }
  871. data, err := json.Marshal(obj[0])
  872. if err != nil {
  873. return shared.NewDecodeParamError("Unable to parse transaction object")
  874. }
  875. trans := new(tx)
  876. err = json.Unmarshal(data, trans)
  877. if err != nil {
  878. return shared.NewDecodeParamError("Unable to parse transaction object")
  879. }
  880. if trans == nil || trans.tx == nil {
  881. return shared.NewDecodeParamError("Unable to parse transaction object")
  882. }
  883. gasLimit, gasPrice := trans.GasLimit, trans.GasPrice
  884. if len(obj) > 1 && obj[1] != nil {
  885. if gp, ok := obj[1].(string); ok {
  886. gasPrice = gp
  887. } else {
  888. return shared.NewInvalidTypeError("gasPrice", "not a string")
  889. }
  890. }
  891. if len(obj) > 2 && obj[2] != nil {
  892. if gl, ok := obj[2].(string); ok {
  893. gasLimit = gl
  894. } else {
  895. return shared.NewInvalidTypeError("gasLimit", "not a string")
  896. }
  897. }
  898. args.Tx = trans
  899. args.GasPrice = gasPrice
  900. args.GasLimit = gasLimit
  901. return nil
  902. }