xrp.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package xrp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "math/big"
  7. "net/http"
  8. "github.com/pkg/errors"
  9. )
  10. type Xrp struct {
  11. serverUrl string
  12. }
  13. func New(serverUrl string) *Xrp {
  14. return &Xrp{
  15. serverUrl: serverUrl,
  16. }
  17. }
  18. func (this *Xrp) CreateWallet() (addr, seed string, err error) {
  19. resp, err := http.Get(this.serverUrl + "/wallet/create")
  20. if err != nil {
  21. return
  22. }
  23. wallet := make(map[string]string)
  24. body, err := ioutil.ReadAll(resp.Body)
  25. if err != nil {
  26. return
  27. }
  28. if resp.StatusCode != 200 {
  29. err = errors.New(string(body))
  30. }
  31. json.Unmarshal(body, &wallet)
  32. addr = wallet["addr"]
  33. seed = wallet["seed"]
  34. return
  35. }
  36. func (this *Xrp) GetBalance(addr string) (balance *big.Int, ownerCount int64, err error) {
  37. resp, err := http.Get(this.serverUrl + "/wallet/getBalance?address=" + addr)
  38. if err != nil {
  39. return
  40. }
  41. body, err := ioutil.ReadAll(resp.Body)
  42. if err != nil {
  43. return
  44. }
  45. if resp.StatusCode != 200 {
  46. err = errors.New(string(body))
  47. }
  48. s := struct {
  49. Balance string
  50. OwnerCount int64
  51. }{}
  52. json.Unmarshal(body, &s)
  53. balance, _ = new(big.Int).SetString(s.Balance, 10)
  54. return balance, s.OwnerCount, err
  55. }
  56. func (this *Xrp) SendToWithDestinationTag(seed, toAddr string, amount *big.Int, dstTag int) (err error) {
  57. resp, err := http.Get(this.serverUrl + fmt.Sprintf("/wallet/send?fromSeed=%s&toAddr=%s&amount=%s&destTag=%d", seed, toAddr, amount.String(), dstTag))
  58. if err != nil {
  59. return
  60. }
  61. body, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. return
  64. }
  65. if resp.StatusCode != 200 {
  66. err = errors.New(string(body))
  67. }
  68. return
  69. }
  70. func (this *Xrp) SendTo(seed, toAddr string, amount *big.Int) (err error) {
  71. resp, err := http.Get(this.serverUrl + fmt.Sprintf("/wallet/send?fromSeed=%s&toAddr=%s&amount=%s", seed, toAddr, amount.String()))
  72. if err != nil {
  73. return
  74. }
  75. body, err := ioutil.ReadAll(resp.Body)
  76. if err != nil {
  77. return
  78. }
  79. if resp.StatusCode != 200 {
  80. err = errors.New(string(body))
  81. }
  82. return
  83. }
  84. func (this *Xrp) GetTxFee(seed, toAddr string, amount *big.Int) (fee *big.Int, err error) {
  85. resp, err := http.Get(this.serverUrl + fmt.Sprintf("/wallet/getTxFee?fromSeed=%s&toAddr=%s&amount=%s", seed, toAddr, amount.String()))
  86. if err != nil {
  87. return
  88. }
  89. body, err := ioutil.ReadAll(resp.Body)
  90. if err != nil {
  91. return
  92. }
  93. if resp.StatusCode != 200 {
  94. err = errors.New(string(body))
  95. return
  96. }
  97. fee = big.NewInt(0)
  98. fee, _ = fee.SetString(string(body), 10)
  99. return
  100. }
  101. func (this *Xrp) GetReserve() (reserveBase *big.Int, reserveInc *big.Int, err error) {
  102. resp, err := http.Get(this.serverUrl + "/wallet/getReserve")
  103. if err != nil {
  104. return
  105. }
  106. body, err := ioutil.ReadAll(resp.Body)
  107. if err != nil {
  108. return
  109. }
  110. if resp.StatusCode != 200 {
  111. err = errors.New(string(body))
  112. }
  113. m := map[string]int64{}
  114. json.Unmarshal(body, &m)
  115. reserveBase = big.NewInt(m["reserve_base"])
  116. reserveBase.Mul(reserveBase, big.NewInt(1000000))
  117. reserveInc = big.NewInt(m["reserve_inc"])
  118. reserveInc.Mul(reserveInc, big.NewInt(1000000))
  119. return
  120. }