trade

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// NoOpHandler no operation handler
	NoOpHandler = func(_ Trade) error { return nil }
	// InterruptHandler interrupt trade handler
	InterruptHandler = func(_ Trade) error { return ErrInterruptTrade }
	// DefaultStageHandlers default handlers
	DefaultStageHandlers = StageHandlerMap{
		stages.Done:         func(_ Trade) error { return nil },
		stages.GenerateKeys: func(tr Trade) error { return tr.GenerateKeys() },
		stages.GenerateToken: func(tr Trade) error {
			btr, err := tr.Buyer()
			if err != nil {
				return err
			}
			_, err = btr.GenerateToken()
			return err
		},
	}
	// ErrInterruptTrade is returned when a trade interruption happens
	ErrInterruptTrade = errors.New("trade interrupted")
)
View Source
var (
	// ErrNotABuyerTrade is returned if the trade is not a buy
	ErrNotABuyerTrade = errors.New("not a buyer trade")

	// ErrNotASellerTrade is returned if the trade is not a sell
	ErrNotASellerTrade = errors.New("not a seller trade")
)
View Source
var (
	// ErrMismatchTokenHash is returned when there is a token hash mismatch
	ErrMismatchTokenHash = errors.New("mismatching token hash")

	// ErrInvalidLockInterval is returned when the time lock interval is invalid
	ErrInvalidLockInterval = errors.New("invalid lock interval")

	// ErrMismatchKeyData is returned when there is a mismatch in the key data
	ErrMismatchKeyData = errors.New("mismatching key data")
)
View Source
var ErrInvalidLockScript = errors.New("invalid lock script")

ErrInvalidLockScript is returns when the lock script is invalid

View Source
var ErrNotEnoughBytes = errors.New("not enough bytes")

ErrNotEnoughBytes is returned the is not possible to read enough random bytes

View Source
var ErrNotUTXO = errors.New("not a utxo crypto")

ErrNotUTXO is returned in the case the crypto is not a utxo crypto

Functions

func TokenHash

func TokenHash(t []byte) []byte

TokenHash returns the hash for the given token

Types

type BuyProposal

type BuyProposal struct {
	Buyer           *BuyProposalInfo `yaml:"buyer"`
	Seller          *BuyProposalInfo `yaml:"seller"`
	TokenHash       types.Bytes      `yaml:"token_hash"`
	RedeemKeyData   key.KeyData      `yaml:"redeem_key_data"`
	RecoveryKeyData key.KeyData      `yaml:"recovery_key_data"`
}

BuyProposal represents a buy proposal

func UnamrshalBuyProposal

func UnamrshalBuyProposal(b []byte) (*BuyProposal, error)

UnamrshalBuyProposal unmarshals a buy proposal

type BuyProposalInfo

type BuyProposalInfo struct {
	Crypto       *cryptos.Crypto   `yaml:"crypto"`
	Amount       types.Amount      `yaml:"amount"`
	LockDuration duration.Duration `yaml:"lock_duration"`
}

BuyProposalInfo represents a buy proposal trader info

type BuyProposalResponse

type BuyProposalResponse struct {
	Buyer  Lock `yaml:"buyer"`
	Seller Lock `yaml:"seller"`
}

BuyProposalResponse is returned when a buy proposal is accepted

func UnamrshalBuyProposalResponse

func UnamrshalBuyProposalResponse(buyer, seller *cryptos.Crypto, b []byte) (*BuyProposalResponse, error)

UnamrshalBuyProposalResponse unmarshals a buy proposal response

type BuyerTrade

type BuyerTrade interface {
	// GenerateToken generates a new token
	GenerateToken() (types.Bytes, error)
	// GenerateBuyProposal generates a buy proposal
	GenerateBuyProposal() (*BuyProposal, error)
	// SetLocks sets the locks for the trade
	SetLocks(locks *BuyProposalResponse) error
}

BuyerTrade represents a buyer trade

type FundsData

type FundsData interface {
	// AddFunds adds funds to the manager
	AddFunds(funds interface{})
	// Funds returns the fund on the manager
	Funds() interface{}
	// SetLock sets the lock for the funds
	SetLock(lock Lock)
	// Lock returns the lock for the funds
	Lock() Lock
}

FundsData holds information about funds

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is a trade handler

func NewHandler

func NewHandler(hm StageHandlerMap) *Handler

NewHandler returns a new *Handler

func NewHandlerDefaults

func NewHandlerDefaults(hm StageHandlerMap) *Handler

NewHandlerDefaults returns a new *Handler with the default handlers installed

func (*Handler) HandleStage

func (sh *Handler) HandleStage(s stages.Stage, t Trade) error

HandleStage handles a single trade stage

func (*Handler) HandleTrade

func (sh *Handler) HandleTrade(t Trade) error

HandleTrade handles a trade

func (*Handler) InstallStageHandler

func (sh *Handler) InstallStageHandler(s stages.Stage, h StageHandlerFunc)

InstallStageHandler installs a stage handler

func (*Handler) InstallStageHandlers

func (sh *Handler) InstallStageHandlers(hm StageHandlerMap)

InstallStageHandlers installs multiple handlers from a stage handler map

func (*Handler) Unhandled

func (sh *Handler) Unhandled(s ...stages.Stage) []stages.Stage

Unhandled returns the unhandler stages of the trade

type Lock

type Lock interface {
	// Bytes returns the lock bytes
	Bytes() types.Bytes
	// LockData returns the lock data
	LockData() (*LockData, error)
	// Address returns the address for the given chain
	Address(chain params.Chain) (string, error)
}

Lock represents a cryptocurrency lock

type LockData

type LockData struct {
	LockTime        time.Time
	TokenHash       types.Bytes
	RedeemKeyData   key.KeyData
	RecoveryKeyData key.KeyData
}

LockData represents a lock

type OnChainTrade

type OnChainTrade struct {
	// contains filtered or unexported fields
}

func (OnChainTrade) AcceptBuyProposal

func (bt OnChainTrade) AcceptBuyProposal(prop *BuyProposal) error

AcceptBuyProposal implement SellerTrade

func (*OnChainTrade) Buyer

func (t *OnChainTrade) Buyer() (BuyerTrade, error)

func (*OnChainTrade) Duration

func (t *OnChainTrade) Duration() duration.Duration

func (OnChainTrade) GenerateBuyProposal

func (bt OnChainTrade) GenerateBuyProposal() (*BuyProposal, error)

GenerateBuyProposal implement BuyerTrade

func (OnChainTrade) GenerateKeys

func (bt OnChainTrade) GenerateKeys() error

GenerateKeys implement Trade

func (OnChainTrade) GenerateToken

func (bt OnChainTrade) GenerateToken() (types.Bytes, error)

GenerateKeys implement BuyerTrade

func (OnChainTrade) Locks

func (bt OnChainTrade) Locks() *BuyProposalResponse

Locks implement SellerTrade

func (*OnChainTrade) MarshalYAML

func (t *OnChainTrade) MarshalYAML() (interface{}, error)

func (*OnChainTrade) OwnInfo

func (t *OnChainTrade) OwnInfo() *TraderInfo

func (*OnChainTrade) RecoverableFunds

func (t *OnChainTrade) RecoverableFunds() FundsData

func (*OnChainTrade) RecoveryKey

func (t *OnChainTrade) RecoveryKey() key.Private

func (OnChainTrade) RecoveryTx

func (bt OnChainTrade) RecoveryTx(lockScript []byte, feePerByte uint64) (tx.Tx, error)

RecoveryTx implement Trade

func (OnChainTrade) RecoveryTxFixedFee

func (bt OnChainTrade) RecoveryTxFixedFee(lockScript []byte, fee uint64) (tx.Tx, error)

RecoveryTxFixedFee implement Trade

func (*OnChainTrade) RedeemKey

func (t *OnChainTrade) RedeemKey() key.Private

func (OnChainTrade) RedeemTx

func (bt OnChainTrade) RedeemTx(lockScript []byte, feePerByte uint64) (tx.Tx, error)

RedeemTx implement Trade

func (OnChainTrade) RedeemTxFixedFee

func (bt OnChainTrade) RedeemTxFixedFee(lockScript []byte, fee uint64) (tx.Tx, error)

RedeemTxFixedFee implement Trade

func (*OnChainTrade) RedeemableFunds

func (t *OnChainTrade) RedeemableFunds() FundsData

func (*OnChainTrade) Role

func (t *OnChainTrade) Role() roles.Role

func (*OnChainTrade) Seller

func (t *OnChainTrade) Seller() (SellerTrade, error)

func (OnChainTrade) SetLocks

func (bt OnChainTrade) SetLocks(locks *BuyProposalResponse) error

SetLocks implement BuyerTrade

func (OnChainTrade) SetToken

func (bt OnChainTrade) SetToken(token types.Bytes)

SetToken sets the token

func (*OnChainTrade) Stager

func (t *OnChainTrade) Stager() *stages.Stager

func (*OnChainTrade) Token

func (t *OnChainTrade) Token() types.Bytes

func (*OnChainTrade) TokenHash

func (t *OnChainTrade) TokenHash() types.Bytes

func (*OnChainTrade) TraderInfo

func (t *OnChainTrade) TraderInfo() *TraderInfo

func (*OnChainTrade) UnmarshalYAML

func (t *OnChainTrade) UnmarshalYAML(unmarshal func(interface{}) error) error

type Output

type Output struct {
	TxID   types.Bytes `yaml:"txid"`
	N      uint32      `yaml:"n"`
	Amount uint64      `yaml:"amount"`
}

Output represents an output

type SellerTrade

type SellerTrade interface {
	// AcceptBuyProposal accepts a buy proposal
	AcceptBuyProposal(prop *BuyProposal) error
	// Locks returns the locks for the trade
	Locks() *BuyProposalResponse
}

SellerTrade represents a seller trade

type StageHandlerFunc

type StageHandlerFunc = func(trade Trade) error

StageHandlerFunc is a trade stage handler function

type StageHandlerMap

type StageHandlerMap = map[stages.Stage]StageHandlerFunc

StageHandlerMap is a map of stage handlers

type StagesNotHandledError

type StagesNotHandledError []stages.Stage

StagesNotHandledError represents an error related to unhandled stages

func (StagesNotHandledError) Error

func (e StagesNotHandledError) Error() string

Error implement error

type Trade

type Trade interface {
	// Role returns the user role in the trade
	Role() roles.Role
	// Duration returns the trade duration
	Duration() duration.Duration
	// Token returns the token
	Token() types.Bytes
	// TokenHash returns the token hash
	TokenHash() types.Bytes
	// OwnInfo returns the trader info for the user
	OwnInfo() *TraderInfo
	// TraderInfo returns the trader info for the trader
	TraderInfo() *TraderInfo
	// RedeemKey returns the redeem key
	RedeemKey() key.Private
	// RecoveryKey returns the recovery key
	RecoveryKey() key.Private
	// RedeemableFunds returns the FundsData for the redeemable funds
	RedeemableFunds() FundsData
	// RecoverableFunds returns the FundsData for the recoverable funds
	RecoverableFunds() FundsData
	// Stager returns the trade *stages.Stager
	Stager() *stages.Stager
	// GenerateKeys generates both redeem and recovery keys
	GenerateKeys() error
	// SetToken sets the trade token (and token hash)
	SetToken(token types.Bytes)
	// RedeemTxFixedFee generates a redeem transaction with fixed fee
	RedeemTxFixedFee(lockScript []byte, fee uint64) (tx.Tx, error)
	// RedeemTx generates a redeem transaction with fee per byte
	RedeemTx(lockScript []byte, feePerByte uint64) (tx.Tx, error)
	// RecoveryTxFixedFee generates a recovery transaction with fixed fee
	RecoveryTxFixedFee(lockScript []byte, fee uint64) (tx.Tx, error)
	// RecoveryTx generates a recovery transaction with fee per byte
	RecoveryTx(lockScript []byte, feePerByte uint64) (tx.Tx, error)
	// Buyer returns a buyer trade
	Buyer() (BuyerTrade, error)
	// Seller returns a seller trade
	Seller() (SellerTrade, error)
}

Trade represents a trade

func NewOnChainBuy

func NewOnChainBuy(
	ownAmount types.Amount,
	ownCrypto *cryptos.Crypto,
	traderAmount types.Amount,
	traderCrypto *cryptos.Crypto,
	dur time.Duration,
) (Trade, error)

NewOnChainBuy returns a new buyer on-chain trade

func NewOnChainSell

func NewOnChainSell() Trade

NewOnChainSell returns a new seller on-chain trade

type TraderInfo

type TraderInfo struct {
	Crypto *cryptos.Crypto `yaml:"crypto"`
	Amount types.Amount    `yaml:"amount"`
}

TraderInfo represents a trader information

Jump to

Keyboard shortcuts

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