GitHubGet started

package economy

import "github.com/nsgo-dev/game/pkg/online/economy"

Overview

Package economy is experimental: its API and documented formats may change in a minor release. See the API stability policy. provides a concurrency-safe, idempotent wallet core for a server-owned economy service.

Index

Variables

var (
	// ErrInsufficient is returned by Spend and Exchange when the balance would go below zero.
	ErrInsufficient = errors.New("economy: insufficient balance")
	// ErrCapExceeded is returned by Grant and Exchange when the balance would go over the currency's cap or past the largest int64.
	ErrCapExceeded = errors.New("economy: balance cap exceeded")
	// ErrUnknownCurrency is returned for a currency the wallet's Config does not declare.
	ErrUnknownCurrency = errors.New("economy: unknown currency")
	// ErrDuplicateKey is returned when an idempotency key already used for one change arrives with a different operation, currency or amount.
	ErrDuplicateKey = errors.New("economy: idempotency key already used")
	// ErrInvalidAmount is returned for an amount that is zero or negative, or an empty idempotency key.
	ErrInvalidAmount = errors.New("economy: invalid amount or idempotency key")
)

Types

type Config struct {
	Version    int        `yaml:"version"`
	Currencies []Currency `yaml:"currencies"`
}

Config declares the currencies of an economy. Its Version is 1.

func (c Config) Validate() error

Validate reports an error unless the version is 1 and every currency has a unique, non-empty ID and a non-negative cap.

type Currency struct {
	ID  string `yaml:"id"`
	Cap int64  `yaml:"cap,omitempty"`
}

Currency declares one currency of the economy by ID, with the most a wallet may hold of it (0 for no cap).

type Result struct {
	Currency string
	Amount   int64
	Balance  int64
}

Result is the outcome of one wallet change: the currency, the signed amount applied and the balance after it.

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

Wallet holds one account's balances in the currencies of a Config. Every change carries an idempotency key, so a retried request applies once. It is safe for concurrent use.

func NewWallet(c Config) (*Wallet, error)

NewWallet validates c and returns an empty wallet for its currencies.

func (w *Wallet) Balance(currency string) (int64, error)

Balance returns the wallet's balance of currency, or ErrUnknownCurrency.

func (w *Wallet) Exchange(from, to string, amount, receive int64, key string) (Result, Result, error)

Exchange spends amount of from and grants receive of to as one change under the idempotency key, returning both results. A key used before for the same exchange returns that exchange's results and changes nothing, and a key used for any other change is ErrDuplicateKey.

func (w *Wallet) Grant(currency string, amount int64, key string) (Result, error)

Grant adds amount of currency under the idempotency key: a key used before for the same grant returns that change's result and changes nothing, and a key used for any other change is ErrDuplicateKey. A balance over the currency's cap is ErrCapExceeded, and an amount that is not positive is ErrInvalidAmount.

func (w *Wallet) Spend(currency string, amount int64, key string) (Result, error)

Spend removes amount of currency under the idempotency key, as Grant does; a balance that would go negative is ErrInsufficient.