eth_args.go 22 KB

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