chequebook

package
v0.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 20, 2021 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrOutOfFunds is the error when the chequebook has not enough free funds for a cheque
	ErrOutOfFunds = errors.New("chequebook out of funds")
	// ErrInsufficientFunds is the error when the chequebook has not enough free funds for a user action
	ErrInsufficientFunds = errors.New("insufficient token balance")
)
View Source
var (
	// ErrNoCheque is the error returned if there is no prior cheque for a chequebook or beneficiary.
	ErrNoCheque = errors.New("no cheque")
	// ErrChequeNotIncreasing is the error returned if the cheque amount is the same or lower.
	ErrChequeNotIncreasing = errors.New("cheque cumulativePayout is not increasing")
	// ErrChequeInvalid is the error returned if the cheque itself is invalid.
	ErrChequeInvalid = errors.New("invalid cheque")
	// ErrWrongBeneficiary is the error returned if the cheque has the wrong beneficiary.
	ErrWrongBeneficiary = errors.New("wrong beneficiary")
	// ErrBouncingCheque is the error returned if the chequebook is demonstrably illiquid.
	ErrBouncingCheque = errors.New("bouncing cheque")
)
View Source
var (
	ErrInvalidFactory       = errors.New("not a valid factory contract")
	ErrNotDeployedByFactory = errors.New("chequebook not deployed by factory")
)
View Source
var ChequeTypes = eip712.Types{
	"EIP712Domain": eip712.EIP712DomainType,
	"Cheque": []eip712.Type{
		{
			Name: "chequebook",
			Type: "address",
		},
		{
			Name: "beneficiary",
			Type: "address",
		},
		{
			Name: "cumulativePayout",
			Type: "uint256",
		},
	},
}

ChequeTypes are the needed type descriptions for cheque signing

View Source
var (
	// ErrNoCashout is the error if there has not been any cashout action for the chequebook
	ErrNoCashout = errors.New("no prior cashout")
)

Functions

func DiscoverFactoryAddress

func DiscoverFactoryAddress(chainID int64) (common.Address, bool)

DiscoverFactoryAddress returns the canonical factory for this chainID

func RecoverCheque

func RecoverCheque(cheque *SignedCheque, chaindID int64) (common.Address, error)

RecoverCheque recovers the issuer ethereum address from a signed cheque

Types

type CashChequeResult

type CashChequeResult struct {
	Beneficiary      common.Address // beneficiary of the cheque
	Recipient        common.Address // address which received the funds
	Caller           common.Address // caller of cashCheque
	TotalPayout      *big.Int       // total amount that was paid out in this call
	CumulativePayout *big.Int       // cumulative payout of the cheque that was cashed
	CallerPayout     *big.Int       // payout for the caller of cashCheque
	Bounced          bool           // indicates wether parts of the cheque bounced
}

CashChequeResult summarizes the result of a CashCheque or CashChequeBeneficiary call

func (*CashChequeResult) Equal

func (r *CashChequeResult) Equal(o *CashChequeResult) bool

Equal compares to CashChequeResults

type CashoutService

type CashoutService interface {
	// CashCheque sends a cashing transaction for the last cheque of the chequebook
	CashCheque(ctx context.Context, chequebook common.Address, recipient common.Address) (common.Hash, error)
	// CashoutStatus gets the status of the latest cashout transaction for the chequebook
	CashoutStatus(ctx context.Context, chequebookAddress common.Address) (*CashoutStatus, error)
}

CashoutService is the service responsible for managing cashout actions

func NewCashoutService

func NewCashoutService(
	store storage.StateStorer,
	backend transaction.Backend,
	transactionService transaction.Service,
	chequeStore ChequeStore,
) CashoutService

NewCashoutService creates a new CashoutService

type CashoutStatus

type CashoutStatus struct {
	TxHash   common.Hash
	Cheque   SignedCheque // the cheque that was used to cashout which may be different from the latest cheque
	Result   *CashChequeResult
	Reverted bool
}

CashoutStatus is the action plus its result

type Cheque

type Cheque struct {
	Chequebook       common.Address
	Beneficiary      common.Address
	CumulativePayout *big.Int
}

Cheque represents a cheque for a SimpleSwap chequebook

func (*Cheque) Equal

func (cheque *Cheque) Equal(other *Cheque) bool

func (*Cheque) String

func (cheque *Cheque) String() string

type ChequeSigner

type ChequeSigner interface {
	// Sign signs a cheque
	Sign(cheque *Cheque) ([]byte, error)
}

ChequeSigner signs cheque

func NewChequeSigner

func NewChequeSigner(signer crypto.Signer, chainID int64) ChequeSigner

NewChequeSigner creates a new cheque signer for the given chainID.

type ChequeStore

type ChequeStore interface {
	// ReceiveCheque verifies and stores a cheque. It returns the totam amount earned.
	ReceiveCheque(ctx context.Context, cheque *SignedCheque) (*big.Int, error)
	// LastCheque returns the last cheque we received from a specific chequebook.
	LastCheque(chequebook common.Address) (*SignedCheque, error)
	// LastCheques returns the last received cheques from every known chequebook.
	LastCheques() (map[common.Address]*SignedCheque, error)
}

ChequeStore handles the verification and storage of received cheques

func NewChequeStore

func NewChequeStore(
	store storage.StateStorer,
	backend transaction.Backend,
	factory Factory,
	chainID int64,
	beneficiary common.Address,
	simpleSwapBindingFunc SimpleSwapBindingFunc,
	recoverChequeFunc RecoverChequeFunc) ChequeStore

NewChequeStore creates new ChequeStore

type Factory

type Factory interface {
	// ERC20Address returns the token for which this factory deploys chequebooks.
	ERC20Address(ctx context.Context) (common.Address, error)
	// Deploy deploys a new chequebook and returns once the transaction has been submitted.
	Deploy(ctx context.Context, issuer common.Address, defaultHardDepositTimeoutDuration *big.Int) (common.Hash, error)
	// WaitDeployed waits for the deployment transaction to confirm and returns the chequebook address
	WaitDeployed(ctx context.Context, txHash common.Hash) (common.Address, error)
	// VerifyBytecode checks that the factory is valid.
	VerifyBytecode(ctx context.Context) error
	// VerifyChequebook checks that the supplied chequebook has been deployed by this factory.
	VerifyChequebook(ctx context.Context, chequebook common.Address) error
}

Factory is the main interface for interacting with the chequebook factory.

func NewFactory

func NewFactory(backend transaction.Backend, transactionService transaction.Service, address common.Address) Factory

NewFactory creates a new factory service for the provided factory contract.

type RecoverChequeFunc

type RecoverChequeFunc func(cheque *SignedCheque, chainID int64) (common.Address, error)

type SendChequeFunc

type SendChequeFunc func(cheque *SignedCheque) error

SendChequeFunc is a function to send cheques.

type Service

type Service interface {
	// Deposit starts depositing erc20 token into the chequebook. This returns once the transactions has been broadcast.
	Deposit(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
	// Withdraw starts withdrawing erc20 token from the chequebook. This returns once the transactions has been broadcast.
	Withdraw(ctx context.Context, amount *big.Int) (hash common.Hash, err error)
	// WaitForDeposit waits for the deposit transaction to confirm and verifies the result.
	WaitForDeposit(ctx context.Context, txHash common.Hash) error
	// Balance returns the token balance of the chequebook.
	Balance(ctx context.Context) (*big.Int, error)
	// AvailableBalance returns the token balance of the chequebook which is not yet used for uncashed cheques.
	AvailableBalance(ctx context.Context) (*big.Int, error)
	// Address returns the address of the used chequebook contract.
	Address() common.Address
	// Issue a new cheque for the beneficiary with an cumulativePayout amount higher than the last.
	Issue(ctx context.Context, beneficiary common.Address, amount *big.Int, sendChequeFunc SendChequeFunc) (*big.Int, error)
	// LastCheque returns the last cheque we issued for the beneficiary.
	LastCheque(beneficiary common.Address) (*SignedCheque, error)
	// LastCheque returns the last cheques for all beneficiaries.
	LastCheques() (map[common.Address]*SignedCheque, error)
}

Service is the main interface for interacting with the nodes chequebook.

func Init

func Init(
	ctx context.Context,
	chequebookFactory Factory,
	stateStore storage.StateStorer,
	logger logging.Logger,
	swapInitialDeposit *big.Int,
	transactionService transaction.Service,
	swapBackend transaction.Backend,
	chainId int64,
	overlayEthAddress common.Address,
	chequeSigner ChequeSigner,
	simpleSwapBindingFunc SimpleSwapBindingFunc,
) (chequebookService Service, err error)

Init initialises the chequebook service.

func New

func New(backend transaction.Backend, transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service, simpleSwapBindingFunc SimpleSwapBindingFunc) (Service, error)

New creates a new chequebook service for the provided chequebook contract.

type SignedCheque

type SignedCheque struct {
	Cheque
	Signature []byte
}

SignedCheque represents a cheque together with its signature

func (*SignedCheque) Equal

func (cheque *SignedCheque) Equal(other *SignedCheque) bool

type SimpleSwapBinding

type SimpleSwapBinding interface {
	Balance(*bind.CallOpts) (*big.Int, error)
	Issuer(*bind.CallOpts) (common.Address, error)
	TotalPaidOut(*bind.CallOpts) (*big.Int, error)
	PaidOut(*bind.CallOpts, common.Address) (*big.Int, error)
}

SimpleSwapBinding is the interface for the generated go bindings for ERC20SimpleSwap

func NewSimpleSwapBindings

func NewSimpleSwapBindings(address common.Address, backend bind.ContractBackend) (SimpleSwapBinding, error)

NewSimpleSwapBindings generates the default go bindings

type SimpleSwapBindingFunc

type SimpleSwapBindingFunc = func(common.Address, bind.ContractBackend) (SimpleSwapBinding, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL