args.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "math/big"
  6. "github.com/ethereum/go-ethereum/common"
  7. )
  8. func blockNumber(raw json.RawMessage, number *int64) (err error) {
  9. var str string
  10. if err = json.Unmarshal(raw, &str); err != nil {
  11. return NewDecodeParamError(err.Error())
  12. }
  13. switch str {
  14. case "latest":
  15. *number = -1
  16. case "pending":
  17. *number = 0
  18. default:
  19. *number = common.String2Big(str).Int64()
  20. }
  21. return nil
  22. }
  23. type GetBlockByHashArgs struct {
  24. BlockHash string
  25. Transactions bool
  26. }
  27. func (args *GetBlockByHashArgs) UnmarshalJSON(b []byte) (err error) {
  28. var obj []interface{}
  29. r := bytes.NewReader(b)
  30. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  31. return NewDecodeParamError(err.Error())
  32. }
  33. if len(obj) < 1 {
  34. return NewInsufficientParamsError(len(obj), 1)
  35. }
  36. argstr, ok := obj[0].(string)
  37. if !ok {
  38. return NewDecodeParamError("BlockHash not a string")
  39. }
  40. args.BlockHash = argstr
  41. if len(obj) > 1 {
  42. args.Transactions = obj[1].(bool)
  43. }
  44. return nil
  45. }
  46. type GetBlockByNumberArgs struct {
  47. BlockNumber int64
  48. Transactions bool
  49. }
  50. func (args *GetBlockByNumberArgs) UnmarshalJSON(b []byte) (err error) {
  51. var obj []interface{}
  52. r := bytes.NewReader(b)
  53. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  54. return NewDecodeParamError(err.Error())
  55. }
  56. if len(obj) < 1 {
  57. return NewInsufficientParamsError(len(obj), 1)
  58. }
  59. if v, ok := obj[0].(float64); ok {
  60. args.BlockNumber = int64(v)
  61. } else {
  62. args.BlockNumber = common.Big(obj[0].(string)).Int64()
  63. }
  64. if len(obj) > 1 {
  65. args.Transactions = obj[1].(bool)
  66. }
  67. return nil
  68. }
  69. type NewTxArgs struct {
  70. From string
  71. To string
  72. Value *big.Int
  73. Gas *big.Int
  74. GasPrice *big.Int
  75. Data string
  76. BlockNumber int64
  77. }
  78. func (args *NewTxArgs) UnmarshalJSON(b []byte) (err error) {
  79. var obj struct{ From, To, Value, Gas, GasPrice, Data string }
  80. if err = UnmarshalRawMessages(b, &obj, &args.BlockNumber); err != nil {
  81. return err
  82. }
  83. args.From = obj.From
  84. args.To = obj.To
  85. args.Value = common.Big(obj.Value)
  86. args.Gas = common.Big(obj.Gas)
  87. args.GasPrice = common.Big(obj.GasPrice)
  88. args.Data = obj.Data
  89. return nil
  90. }
  91. type GetStorageArgs struct {
  92. Address string
  93. BlockNumber int64
  94. }
  95. func (args *GetStorageArgs) UnmarshalJSON(b []byte) (err error) {
  96. if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
  97. return NewDecodeParamError(err.Error())
  98. }
  99. return nil
  100. }
  101. func (args *GetStorageArgs) requirements() error {
  102. if len(args.Address) == 0 {
  103. return NewValidationError("Address", "cannot be blank")
  104. }
  105. return nil
  106. }
  107. type GetStorageAtArgs struct {
  108. Address string
  109. Key string
  110. BlockNumber int64
  111. }
  112. func (args *GetStorageAtArgs) UnmarshalJSON(b []byte) (err error) {
  113. var obj []string
  114. if err = UnmarshalRawMessages(b, &obj, &args.BlockNumber); err != nil {
  115. return NewDecodeParamError(err.Error())
  116. }
  117. if len(obj) < 2 {
  118. return NewInsufficientParamsError(len(obj), 2)
  119. }
  120. args.Address = obj[0]
  121. args.Key = obj[1]
  122. return nil
  123. }
  124. func (args *GetStorageAtArgs) requirements() error {
  125. if len(args.Address) == 0 {
  126. return NewValidationError("Address", "cannot be blank")
  127. }
  128. if len(args.Key) == 0 {
  129. return NewValidationError("Key", "cannot be blank")
  130. }
  131. return nil
  132. }
  133. type GetTxCountArgs struct {
  134. Address string
  135. BlockNumber int64
  136. }
  137. func (args *GetTxCountArgs) UnmarshalJSON(b []byte) (err error) {
  138. if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
  139. return NewDecodeParamError(err.Error())
  140. }
  141. return nil
  142. }
  143. func (args *GetTxCountArgs) requirements() error {
  144. if len(args.Address) == 0 {
  145. return NewValidationError("Address", "cannot be blank")
  146. }
  147. return nil
  148. }
  149. type GetBalanceArgs struct {
  150. Address string
  151. BlockNumber int64
  152. }
  153. func (args *GetBalanceArgs) UnmarshalJSON(b []byte) (err error) {
  154. var obj []interface{}
  155. r := bytes.NewReader(b)
  156. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  157. return NewDecodeParamError(err.Error())
  158. }
  159. if len(obj) < 1 {
  160. return NewInsufficientParamsError(len(obj), 1)
  161. }
  162. addstr, ok := obj[0].(string)
  163. if !ok {
  164. return NewDecodeParamError("Address is not a string")
  165. }
  166. args.Address = addstr
  167. if len(obj) > 1 {
  168. if obj[1].(string) == "latest" {
  169. args.BlockNumber = -1
  170. } else {
  171. args.BlockNumber = common.Big(obj[1].(string)).Int64()
  172. }
  173. }
  174. // if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
  175. // return NewDecodeParamError(err.Error())
  176. // }
  177. return nil
  178. }
  179. func (args *GetBalanceArgs) requirements() error {
  180. if len(args.Address) == 0 {
  181. return NewValidationError("Address", "cannot be blank")
  182. }
  183. return nil
  184. }
  185. type GetDataArgs struct {
  186. Address string
  187. BlockNumber int64
  188. }
  189. func (args *GetDataArgs) UnmarshalJSON(b []byte) (err error) {
  190. if err = UnmarshalRawMessages(b, &args.Address, &args.BlockNumber); err != nil {
  191. return NewDecodeParamError(err.Error())
  192. }
  193. return nil
  194. }
  195. func (args *GetDataArgs) requirements() error {
  196. if len(args.Address) == 0 {
  197. return NewValidationError("Address", "cannot be blank")
  198. }
  199. return nil
  200. }
  201. type BlockNumIndexArgs struct {
  202. BlockNumber int64
  203. Index int64
  204. }
  205. func (args *BlockNumIndexArgs) UnmarshalJSON(b []byte) (err error) {
  206. var obj []interface{}
  207. r := bytes.NewReader(b)
  208. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  209. return NewDecodeParamError(err.Error())
  210. }
  211. if len(obj) < 1 {
  212. return NewInsufficientParamsError(len(obj), 1)
  213. }
  214. arg0, ok := obj[0].(string)
  215. if !ok {
  216. return NewDecodeParamError("BlockNumber is not string")
  217. }
  218. args.BlockNumber = common.Big(arg0).Int64()
  219. if len(obj) > 1 {
  220. arg1, ok := obj[1].(string)
  221. if !ok {
  222. return NewDecodeParamError("Index not a string")
  223. }
  224. args.Index = common.Big(arg1).Int64()
  225. }
  226. return nil
  227. }
  228. type HashIndexArgs struct {
  229. Hash string
  230. Index int64
  231. }
  232. func (args *HashIndexArgs) UnmarshalJSON(b []byte) (err error) {
  233. var obj []interface{}
  234. r := bytes.NewReader(b)
  235. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  236. return NewDecodeParamError(err.Error())
  237. }
  238. if len(obj) < 1 {
  239. return NewInsufficientParamsError(len(obj), 1)
  240. }
  241. arg0, ok := obj[0].(string)
  242. if !ok {
  243. return NewDecodeParamError("Hash not a string")
  244. }
  245. args.Hash = arg0
  246. if len(obj) > 1 {
  247. arg1, ok := obj[1].(string)
  248. if !ok {
  249. return NewDecodeParamError("Index not a string")
  250. }
  251. args.Index = common.Big(arg1).Int64()
  252. }
  253. return nil
  254. }
  255. type Sha3Args struct {
  256. Data string
  257. }
  258. func (args *Sha3Args) UnmarshalJSON(b []byte) (err error) {
  259. var obj []interface{}
  260. r := bytes.NewReader(b)
  261. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  262. return NewDecodeParamError(err.Error())
  263. }
  264. if len(obj) < 1 {
  265. return NewInsufficientParamsError(len(obj), 1)
  266. }
  267. args.Data = obj[0].(string)
  268. return nil
  269. }
  270. // type FilterArgs struct {
  271. // FromBlock uint64
  272. // ToBlock uint64
  273. // Limit uint64
  274. // Offset uint64
  275. // Address string
  276. // Topics []string
  277. // }
  278. // func (args *FilterArgs) UnmarshalJSON(b []byte) (err error) {
  279. // var obj []struct {
  280. // FromBlock string `json:"fromBlock"`
  281. // ToBlock string `json:"toBlock"`
  282. // Limit string `json:"limit"`
  283. // Offset string `json:"offset"`
  284. // Address string `json:"address"`
  285. // Topics []string `json:"topics"`
  286. // }
  287. // if err = json.Unmarshal(b, &obj); err != nil {
  288. // return errDecodeArgs
  289. // }
  290. // if len(obj) < 1 {
  291. // return errArguments
  292. // }
  293. // args.FromBlock = uint64(common.Big(obj[0].FromBlock).Int64())
  294. // args.ToBlock = uint64(common.Big(obj[0].ToBlock).Int64())
  295. // args.Limit = uint64(common.Big(obj[0].Limit).Int64())
  296. // args.Offset = uint64(common.Big(obj[0].Offset).Int64())
  297. // args.Address = obj[0].Address
  298. // args.Topics = obj[0].Topics
  299. // return nil
  300. // }
  301. type FilterOptions struct {
  302. Earliest int64
  303. Latest int64
  304. Address interface{}
  305. Topics []interface{}
  306. Skip int
  307. Max int
  308. }
  309. func (args *FilterOptions) UnmarshalJSON(b []byte) (err error) {
  310. var obj []struct {
  311. FromBlock string `json:"fromBlock"`
  312. ToBlock string `json:"toBlock"`
  313. Limit string `json:"limit"`
  314. Offset string `json:"offset"`
  315. Address string `json:"address"`
  316. Topics []interface{} `json:"topics"`
  317. }
  318. if err = json.Unmarshal(b, &obj); err != nil {
  319. return NewDecodeParamError(err.Error())
  320. }
  321. if len(obj) < 1 {
  322. return NewInsufficientParamsError(len(obj), 1)
  323. }
  324. args.Earliest = int64(common.Big(obj[0].FromBlock).Int64())
  325. args.Latest = int64(common.Big(obj[0].ToBlock).Int64())
  326. args.Max = int(common.Big(obj[0].Limit).Int64())
  327. args.Skip = int(common.Big(obj[0].Offset).Int64())
  328. args.Address = obj[0].Address
  329. args.Topics = obj[0].Topics
  330. return nil
  331. }
  332. // type FilterChangedArgs struct {
  333. // n int
  334. // }
  335. type DbArgs struct {
  336. Database string
  337. Key string
  338. Value string
  339. }
  340. func (args *DbArgs) UnmarshalJSON(b []byte) (err error) {
  341. var obj []interface{}
  342. r := bytes.NewReader(b)
  343. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  344. return NewDecodeParamError(err.Error())
  345. }
  346. if len(obj) < 2 {
  347. return NewInsufficientParamsError(len(obj), 2)
  348. }
  349. args.Database = obj[0].(string)
  350. args.Key = obj[1].(string)
  351. if len(obj) > 2 {
  352. args.Value = obj[2].(string)
  353. }
  354. return nil
  355. }
  356. func (a *DbArgs) requirements() error {
  357. if len(a.Database) == 0 {
  358. return NewValidationError("Database", "cannot be blank")
  359. }
  360. if len(a.Key) == 0 {
  361. return NewValidationError("Key", "cannot be blank")
  362. }
  363. return nil
  364. }
  365. type WhisperMessageArgs struct {
  366. Payload string
  367. To string
  368. From string
  369. Topics []string
  370. Priority uint32
  371. Ttl uint32
  372. }
  373. func (args *WhisperMessageArgs) UnmarshalJSON(b []byte) (err error) {
  374. var obj []struct {
  375. Payload string
  376. To string
  377. From string
  378. Topics []string
  379. Priority string
  380. Ttl string
  381. }
  382. if err = json.Unmarshal(b, &obj); err != nil {
  383. return NewDecodeParamError(err.Error())
  384. }
  385. if len(obj) < 1 {
  386. return NewInsufficientParamsError(len(obj), 1)
  387. }
  388. args.Payload = obj[0].Payload
  389. args.To = obj[0].To
  390. args.From = obj[0].From
  391. args.Topics = obj[0].Topics
  392. args.Priority = uint32(common.Big(obj[0].Priority).Int64())
  393. args.Ttl = uint32(common.Big(obj[0].Ttl).Int64())
  394. return nil
  395. }
  396. type CompileArgs struct {
  397. Source string
  398. }
  399. func (args *CompileArgs) UnmarshalJSON(b []byte) (err error) {
  400. var obj []interface{}
  401. r := bytes.NewReader(b)
  402. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  403. return NewDecodeParamError(err.Error())
  404. }
  405. if len(obj) > 0 {
  406. args.Source = obj[0].(string)
  407. }
  408. return nil
  409. }
  410. type FilterStringArgs struct {
  411. Word string
  412. }
  413. func (args *FilterStringArgs) UnmarshalJSON(b []byte) (err error) {
  414. var obj []interface{}
  415. r := bytes.NewReader(b)
  416. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  417. return NewDecodeParamError(err.Error())
  418. }
  419. if len(obj) < 1 {
  420. return NewInsufficientParamsError(len(obj), 1)
  421. }
  422. var argstr string
  423. argstr, ok := obj[0].(string)
  424. if !ok {
  425. return NewDecodeParamError("Filter is not a string")
  426. }
  427. args.Word = argstr
  428. return nil
  429. }
  430. type FilterIdArgs struct {
  431. Id int
  432. }
  433. func (args *FilterIdArgs) UnmarshalJSON(b []byte) (err error) {
  434. var obj []string
  435. r := bytes.NewReader(b)
  436. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  437. return NewDecodeParamError(err.Error())
  438. }
  439. if len(obj) < 1 {
  440. return NewInsufficientParamsError(len(obj), 1)
  441. }
  442. args.Id = int(common.Big(obj[0]).Int64())
  443. return nil
  444. }
  445. type WhisperIdentityArgs struct {
  446. Identity string
  447. }
  448. func (args *WhisperIdentityArgs) UnmarshalJSON(b []byte) (err error) {
  449. var obj []string
  450. r := bytes.NewReader(b)
  451. if err := json.NewDecoder(r).Decode(&obj); err != nil {
  452. return NewDecodeParamError(err.Error())
  453. }
  454. if len(obj) < 1 {
  455. return NewInsufficientParamsError(len(obj), 1)
  456. }
  457. args.Identity = obj[0]
  458. return nil
  459. }
  460. type WhisperFilterArgs struct {
  461. To string `json:"to"`
  462. From string
  463. Topics []string
  464. }
  465. func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
  466. var obj []struct {
  467. To string
  468. From string
  469. Topics []string
  470. }
  471. if err = json.Unmarshal(b, &obj); err != nil {
  472. return NewDecodeParamError(err.Error())
  473. }
  474. if len(obj) < 1 {
  475. return NewInsufficientParamsError(len(obj), 1)
  476. }
  477. args.To = obj[0].To
  478. args.From = obj[0].From
  479. args.Topics = obj[0].Topics
  480. return nil
  481. }
  482. // func (req *RpcRequest) ToRegisterArgs() (string, error) {
  483. // if len(req.Params) < 1 {
  484. // return "", errArguments
  485. // }
  486. // var args string
  487. // err := json.Unmarshal(req.Params, &args)
  488. // if err != nil {
  489. // return "", err
  490. // }
  491. // return args, nil
  492. // }
  493. // func (req *RpcRequest) ToWatchTxArgs() (string, error) {
  494. // if len(req.Params) < 1 {
  495. // return "", errArguments
  496. // }
  497. // var args string
  498. // err := json.Unmarshal(req.Params, &args)
  499. // if err != nil {
  500. // return "", err
  501. // }
  502. // return args, nil
  503. // }