eth_args.go 21 KB

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