github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/contracts/chequebook/api.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package chequebook
    13  
    14  import (
    15  	"errors"
    16  	"math/big"
    17  
    18  	"github.com/Sberex/go-sberex/common"
    19  )
    20  
    21  const Version = "1.0"
    22  
    23  var errNoChequebook = errors.New("no chequebook")
    24  
    25  type Api struct {
    26  	chequebookf func() *Chequebook
    27  }
    28  
    29  func NewApi(ch func() *Chequebook) *Api {
    30  	return &Api{ch}
    31  }
    32  
    33  func (self *Api) Balance() (string, error) {
    34  	ch := self.chequebookf()
    35  	if ch == nil {
    36  		return "", errNoChequebook
    37  	}
    38  	return ch.Balance().String(), nil
    39  }
    40  
    41  func (self *Api) Issue(beneficiary common.Address, amount *big.Int) (cheque *Cheque, err error) {
    42  	ch := self.chequebookf()
    43  	if ch == nil {
    44  		return nil, errNoChequebook
    45  	}
    46  	return ch.Issue(beneficiary, amount)
    47  }
    48  
    49  func (self *Api) Cash(cheque *Cheque) (txhash string, err error) {
    50  	ch := self.chequebookf()
    51  	if ch == nil {
    52  		return "", errNoChequebook
    53  	}
    54  	return ch.Cash(cheque)
    55  }
    56  
    57  func (self *Api) Deposit(amount *big.Int) (txhash string, err error) {
    58  	ch := self.chequebookf()
    59  	if ch == nil {
    60  		return "", errNoChequebook
    61  	}
    62  	return ch.Deposit(amount)
    63  }