wallet

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EntityTypeMerchant EntityType = "merchant"
	EntityTypeWallet   EntityType = "wallet"

	OperationIncrement BalanceOperation = "increment"
	OperationDecrement BalanceOperation = "decrement"

	MetaOperation       MetaDataKey = "operation"
	MetaAmountRaw       MetaDataKey = "amountRaw"
	MetaAmountFormatted MetaDataKey = "amountFormatted"

	MetaTransactionID     MetaDataKey = "transactionID"
	MetaSenderWalletID    MetaDataKey = "senderWallerID"
	MetaRecipientWalletID MetaDataKey = "recipientWalletID"
)

Variables

View Source
var (
	ErrNotFound                     = errors.New("wallet not found")
	ErrBalanceNotFound              = errors.New("balance not found")
	ErrInvalidBlockchain            = errors.New("invalid blockchain provided")
	ErrInvalidType                  = errors.New("invalid type provided")
	ErrInsufficientBalance          = errors.New("insufficient balance")
	ErrInsufficienceMerchantBalance = errors.Wrap(ErrInsufficientBalance, "merchant")
)
View Source
var (
	ErrTxConfirm  = errors.New("nothing to confirm")
	ErrTxRollback = errors.New("nothing to rollback")
)

Functions

func ReleaseLock

func ReleaseLock(ctx context.Context, q repository.Querier, walletID int64, currency, networkID string) error

Types

type Balance

type Balance struct {
	ID           int64
	UUID         uuid.UUID
	EntityType   EntityType
	EntityID     int64
	CreatedAt    time.Time
	UpdatedAt    time.Time
	Network      string
	NetworkID    string
	CurrencyType money.CryptoCurrencyType
	Currency     string
	Amount       money.Money

	// UsdAmount eager-loaded. See Service.loadUSDBalances
	UsdAmount *money.Money
}

func UpdateBalance

func UpdateBalance(ctx context.Context, q repository.Querier, params UpdateBalanceQuery) (*Balance, error)

UpdateBalance increments/decrements balance of entity (wallet / merchant) and optionally adds audit log. increment works based on postgres "upsert" feature: "INSERT ... ON CONFLICT (...) DO UPDATE ...". Balance is created if not exists.

Warning: this function is meant to be used only in db transactions

func (*Balance) Blockchain added in v0.3.0

func (b *Balance) Blockchain() money.Blockchain

func (*Balance) Covers

func (b *Balance) Covers(expenses ...money.Money) error

Covers check if balance covers provided expenses.

type BalanceOperation

type BalanceOperation string

type BlockchainService

type BlockchainService interface {
	blockchain.Convertor
}

type EntityType

type EntityType string

type MetaData

type MetaData map[MetaDataKey]string

func (MetaData) ToJSONB

func (m MetaData) ToJSONB() pgtype.JSONB

type MetaDataKey

type MetaDataKey string

type Pagination

type Pagination struct {
	Start              int64
	Limit              int32
	FilterByBlockchain kmswallet.Blockchain
	FilterByType       Type
}

type Service

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

func New

func New(
	kmsClient kmsclient.ClientService,
	blockchainService BlockchainService,
	store *repository.Store,
	logger *zerolog.Logger,
) *Service

func (*Service) AcquireLock

func (s *Service) AcquireLock(ctx context.Context, merchantID int64, currency money.CryptoCurrency, isTest bool) (*Wallet, error)

AcquireLock finds and locks wallet of specified type currency for selected merchantID. If required wallet isn't found, then the new one is being created. This method uses transactions.

func (*Service) BulkCreateWallets

func (s *Service) BulkCreateWallets(ctx context.Context, bc kmswallet.Blockchain, amount int64) ([]*Wallet, error)

func (*Service) Create

func (s *Service) Create(ctx context.Context, bc kmswallet.Blockchain, walletType Type) (*Wallet, error)

func (*Service) CreateSignedTransaction

func (s *Service) CreateSignedTransaction(
	ctx context.Context,
	sender *Wallet,
	recipient string,
	currency money.CryptoCurrency,
	amount money.Money,
	fee blockchain.Fee,
	isTest bool,
) (string, error)

func (*Service) DecrementPendingTransaction

func (s *Service) DecrementPendingTransaction(ctx context.Context, walletID int64, isTest bool) error

func (*Service) EnsureBalance

func (s *Service) EnsureBalance(
	ctx context.Context,
	entityType EntityType,
	entityID int64,
	currency money.CryptoCurrency,
	isTest bool,
) (*Balance, error)

func (*Service) EnsureOutboundWallet

func (s *Service) EnsureOutboundWallet(ctx context.Context, bc kmswallet.Blockchain) (*Wallet, bool, error)

EnsureOutboundWallet finds or creates outbound wallet for specified blockchain. Outbound wallets are used for funds withdrawal.

func (*Service) GetBalanceByID

func (s *Service) GetBalanceByID(ctx context.Context, entityType EntityType, entityID, balanceID int64) (*Balance, error)

func (*Service) GetBalanceByUUID

func (s *Service) GetBalanceByUUID(ctx context.Context, entityType EntityType, entityID int64, balanceID uuid.UUID) (*Balance, error)

func (*Service) GetByID

func (s *Service) GetByID(ctx context.Context, id int64) (*Wallet, error)

func (*Service) GetByUUID

func (s *Service) GetByUUID(ctx context.Context, id uuid.UUID) (*Wallet, error)

func (*Service) GetMerchantBalance

func (s *Service) GetMerchantBalance(ctx context.Context, merchantID int64, currency, networkID string) (*Balance, error)

func (*Service) GetMerchantBalanceByUUID

func (s *Service) GetMerchantBalanceByUUID(ctx context.Context, merchantID int64, balanceID uuid.UUID) (*Balance, error)

func (*Service) GetWalletsBalance

func (s *Service) GetWalletsBalance(ctx context.Context, walletID int64, currency, networkID string) (*Balance, error)

func (*Service) IncrementConfirmedTransaction

func (s *Service) IncrementConfirmedTransaction(ctx context.Context, walletID int64, isTest bool) error

func (*Service) IncrementPendingTransaction

func (s *Service) IncrementPendingTransaction(ctx context.Context, walletID int64, isTest bool) (int, error)

IncrementPendingTransaction updates Wallet's nonce parameter and returns nonce for next tx.

func (*Service) List

func (s *Service) List(ctx context.Context, pagination Pagination) ([]*Wallet, *int64, error)

func (*Service) ListBalances

func (s *Service) ListBalances(ctx context.Context, entityType EntityType, entityID int64, withUSD bool) ([]*Balance, error)

func (*Service) ReleaseLock

func (s *Service) ReleaseLock(ctx context.Context, walletID int64, currency, networkID string) error

ReleaseLock does the opposite of AcquireLock.

func (*Service) UpdateBalanceByID

func (s *Service) UpdateBalanceByID(ctx context.Context, id int64, params UpdateBalanceByIDQuery) (*Balance, error)

func (*Service) UpdateBalancesForWithdrawal

func (s *Service) UpdateBalancesForWithdrawal(ctx context.Context, params UpdateBalancesForWithdrawal) error

func (*Service) UpdateTatumSubscription

func (s *Service) UpdateTatumSubscription(ctx context.Context, wallet *Wallet, subscription TatumSubscription) error

UpdateTatumSubscription updates tatum_* fields and reflects changes in *Wallet argument.

type TatumSubscription

type TatumSubscription struct {
	MainnetSubscriptionID string
	TestnetSubscriptionID string
}

type Type

type Type string
const (
	TypeInbound  Type = "inbound"
	TypeOutbound Type = "outbound"
)

type UpdateBalanceByIDQuery

type UpdateBalanceByIDQuery struct {
	Operation BalanceOperation
	Amount    money.Money
	Comment   string
	MetaData  MetaData
}

type UpdateBalanceQuery

type UpdateBalanceQuery struct {
	EntityID   int64
	EntityType EntityType

	Operation BalanceOperation

	Currency money.CryptoCurrency
	Amount   money.Money

	Comment  string
	MetaData MetaData

	IsTest bool
}

type UpdateBalancesForWithdrawal

type UpdateBalancesForWithdrawal struct {
	Operation     BalanceOperation
	TransactionID int64
	System        *Balance
	Merchant      *Balance
	Amount        money.Money
	ServiceFee    money.Money
	Comment       string
}

type Wallet

type Wallet struct {
	ID                           int64
	CreatedAt                    time.Time
	UUID                         uuid.UUID
	Address                      string
	Blockchain                   kmswallet.Blockchain
	Type                         Type
	TatumSubscription            TatumSubscription
	ConfirmedMainnetTransactions int64
	PendingMainnetTransactions   int64
	ConfirmedTestnetTransactions int64
	PendingTestnetTransactions   int64
}

Jump to

Keyboard shortcuts

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