api.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package chequebook
  17. import (
  18. "errors"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. const Version = "1.0"
  23. var errNoChequebook = errors.New("no chequebook")
  24. type Api struct {
  25. chequebookf func() *Chequebook
  26. }
  27. func NewApi(ch func() *Chequebook) *Api {
  28. return &Api{ch}
  29. }
  30. func (self *Api) Balance() (string, error) {
  31. ch := self.chequebookf()
  32. if ch == nil {
  33. return "", errNoChequebook
  34. }
  35. return ch.Balance().String(), nil
  36. }
  37. func (self *Api) Issue(beneficiary common.Address, amount *big.Int) (cheque *Cheque, err error) {
  38. ch := self.chequebookf()
  39. if ch == nil {
  40. return nil, errNoChequebook
  41. }
  42. return ch.Issue(beneficiary, amount)
  43. }
  44. func (self *Api) Cash(cheque *Cheque) (txhash string, err error) {
  45. ch := self.chequebookf()
  46. if ch == nil {
  47. return "", errNoChequebook
  48. }
  49. return ch.Cash(cheque)
  50. }
  51. func (self *Api) Deposit(amount *big.Int) (txhash string, err error) {
  52. ch := self.chequebookf()
  53. if ch == nil {
  54. return "", errNoChequebook
  55. }
  56. return ch.Deposit(amount)
  57. }