types.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2018 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 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "encoding/json"
  19. "strings"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/accounts"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/hexutil"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. )
  26. type Accounts []Account
  27. func (as Accounts) String() string {
  28. var output []string
  29. for _, a := range as {
  30. output = append(output, a.String())
  31. }
  32. return strings.Join(output, "\n")
  33. }
  34. type Account struct {
  35. Typ string `json:"type"`
  36. URL accounts.URL `json:"url"`
  37. Address common.Address `json:"address"`
  38. }
  39. func (a Account) String() string {
  40. s, err := json.Marshal(a)
  41. if err == nil {
  42. return string(s)
  43. }
  44. return err.Error()
  45. }
  46. type ValidationInfo struct {
  47. Typ string `json:"type"`
  48. Message string `json:"message"`
  49. }
  50. type ValidationMessages struct {
  51. Messages []ValidationInfo
  52. }
  53. // SendTxArgs represents the arguments to submit a transaction
  54. type SendTxArgs struct {
  55. From common.MixedcaseAddress `json:"from"`
  56. To *common.MixedcaseAddress `json:"to"`
  57. Gas hexutil.Uint64 `json:"gas"`
  58. GasPrice hexutil.Big `json:"gasPrice"`
  59. Value hexutil.Big `json:"value"`
  60. Nonce hexutil.Uint64 `json:"nonce"`
  61. // We accept "data" and "input" for backwards-compatibility reasons.
  62. Data *hexutil.Bytes `json:"data"`
  63. Input *hexutil.Bytes `json:"input"`
  64. }
  65. func (args SendTxArgs) String() string {
  66. s, err := json.Marshal(args)
  67. if err == nil {
  68. return string(s)
  69. }
  70. return err.Error()
  71. }
  72. func (args *SendTxArgs) toTransaction() *types.Transaction {
  73. var input []byte
  74. if args.Data != nil {
  75. input = *args.Data
  76. } else if args.Input != nil {
  77. input = *args.Input
  78. }
  79. if args.To == nil {
  80. return types.NewContractCreation(uint64(args.Nonce), (*big.Int)(&args.Value), uint64(args.Gas), (*big.Int)(&args.GasPrice), input)
  81. }
  82. return types.NewTransaction(uint64(args.Nonce), args.To.Address(), (*big.Int)(&args.Value), (uint64)(args.Gas), (*big.Int)(&args.GasPrice), input)
  83. }