Documentation
¶
Index ¶
- Constants
- Variables
- func RecoverCheque(cheque *SignedCheque, chaindID int64) (common.Address, error)
- func VerifyErrorLabel(err error) string
- type CashChequeResult
- type CashoutService
- type CashoutStatus
- type Cheque
- type ChequeSigner
- type ChequeStore
- type CodeReader
- type Factory
- type LastCashout
- type PeerChequebookRegistryLookup
- type RecoverChequeFunc
- type Registry
- type SendChequeFunc
- type Service
- type SignedCheque
- type Verifier
- type VerifierConfig
Constants ¶
const (
ChequebookDeploymentKey = "swap_chequebook_transaction_deployment"
)
Variables ¶
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") )
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") // ErrChequeValueTooLow is the error returned if the after deduction value of a cheque did not cover 1 accounting credit ErrChequeValueTooLow = errors.New("cheque value lower than acceptable") )
var ( ErrInvalidFactory = errors.New("not a valid factory contract") ErrNotDeployedByFactory = errors.New("chequebook not deployed by factory") )
var ( ErrChequebookIssuerMismatch = errors.New("chequebook issuer does not match peer ethereum address") ErrChequebookBytecodeMismatch = errors.New("chequebook deployed bytecode hash not in accepted set") ErrChequebookInsufficientBalance = errors.New("chequebook token balance below minimum threshold") ErrChequebookAlreadyAssociated = errors.New("chequebook already associated with a different peer") ErrChequebookAddressMissing = errors.New("chequebook address missing from address record") )
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
var ( // ErrNoCashout is the error if there has not been any cashout action for the chequebook ErrNoCashout = errors.New("no prior cashout") )
Functions ¶
func RecoverCheque ¶
func RecoverCheque(cheque *SignedCheque, chaindID int64) (common.Address, error)
RecoverCheque recovers the issuer ethereum address from a signed cheque
func VerifyErrorLabel ¶ added in v2.8.0
VerifyErrorLabel returns the metric label for a non-nil Verifier.Verify result. Unknown errors map to "verify_error".
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 whether 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 {
Last *LastCashout // last cashout for a chequebook
UncashedAmount *big.Int // amount not yet cashed out
}
CashoutStatus is information about the last cashout and uncashed amounts
type Cheque ¶
type Cheque struct {
Chequebook common.Address
Beneficiary common.Address
CumulativePayout *big.Int
}
Cheque represents a cheque for a SimpleSwap chequebook
type ChequeSigner ¶
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 total amount earned.
ReceiveCheque(ctx context.Context, cheque *SignedCheque, exchangeRate, deduction *big.Int) (*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, factory Factory, chainID int64, beneficiary common.Address, transactionService transaction.Service, recoverChequeFunc RecoverChequeFunc) ChequeStore
NewChequeStore creates new ChequeStore
type CodeReader ¶ added in v2.8.0
type CodeReader interface {
CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
}
CodeReader returns the deployed bytecode at a contract address. Satisfied by transaction.Backend and ethclient.Client.
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, nonce common.Hash) (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)
// 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 LastCashout ¶
type LastCashout 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
}
LastCashout contains information about the last cashout
type PeerChequebookRegistryLookup ¶ added in v2.8.0
type PeerChequebookRegistryLookup interface {
Peer(chequebook common.Address) (peer swarm.Address, known bool)
}
PeerChequebookRegistryLookup is the subset of swap.Addressbook used by the uniqueness check.
type RecoverChequeFunc ¶
type RecoverChequeFunc func(cheque *SignedCheque, chainID int64) (common.Address, error)
type Registry ¶ added in v2.8.0
type Registry struct {
// contains filtered or unexported fields
}
Registry is an in-memory chequebook↔overlay map used by the Verifier for the uniqueness check. Entries are removed on peer disconnect so a peer can reconnect with a different overlay using the same chequebook.
Implements both AddressbookLookup (for the Verifier) and the hive ChequebookStorer (for storing on successful verification).
func NewRegistry ¶ added in v2.8.0
func NewRegistry() *Registry
func (*Registry) Peer ¶ added in v2.8.0
Peer returns the overlay currently associated with chequebook.
func (*Registry) Put ¶ added in v2.8.0
func (r *Registry) Put(peer swarm.Address, chequebook common.Address, timestamp int64, source bzz.TimestampSource, writeAddressbook func() error) error
Put atomically updates the in-memory chequebook + last-seen-timestamp mapping and runs writeAddressbook (if non-nil) under the same mutex. Holding the mutex across the callback prevents an older record from overwriting a newer one in the addressbook when two Put calls race for the same peer. Zero chequebook skips the registry mapping but still runs writeAddressbook.
The callback runs under the registry mutex, so writeAddressbook should be a single bounded I/O operation; long-running work belongs outside Put.
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)
// LastCheques 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 log.Logger, swapInitialDeposit *big.Int, transactionService transaction.Service, swapBackend transaction.Backend, chainId int64, overlayEthAddress common.Address, chequeSigner ChequeSigner, erc20Service erc20.Service, ) (chequebookService Service, err error)
Init initialises the chequebook service.
func New ¶
func New(transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer, chequeSigner ChequeSigner, erc20Service erc20.Service) (Service, error)
New creates a new chequebook service for the provided chequebook contract.
type SignedCheque ¶
SignedCheque represents a cheque together with its signature
func (*SignedCheque) Equal ¶
func (cheque *SignedCheque) Equal(other *SignedCheque) bool
type Verifier ¶ added in v2.8.0
type Verifier interface {
Verify(ctx context.Context, chequebookAddress, peerEthereumAddress common.Address, overlay swarm.Address, verified bool) error
}
Verifier runs the chequebook checks from the spec: issuer match, deployed bytecode hash, minimum balance, and uniqueness. When the addressbook already holds an entry for (overlay, chequebook), the issuer and bytecode RPCs are skipped — only uniqueness and balance are re-checked. Returns the typed error of the first failing check.
func NewVerifier ¶ added in v2.8.0
func NewVerifier(transactionService transaction.Service, codeReader CodeReader, addressbook PeerChequebookRegistryLookup, cfg VerifierConfig) (Verifier, error)