Help us shape the future of Go! Take the 2026 Go Developer Survey

state

package
v1.24.29 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package state manages persistent state for the DEX VM.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAccountNotFound     = errors.New("account not found")
	ErrInsufficientBalance = errors.New("insufficient balance")
	ErrStateCorrupted      = errors.New("state corrupted")
)

Functions

This section is empty.

Types

type Account

type Account struct {
	Address    ids.ShortID       `json:"address"`
	Nonce      uint64            `json:"nonce"`
	Balances   map[ids.ID]uint64 `json:"balances"`   // token -> balance
	OpenOrders []ids.ID          `json:"openOrders"` // list of open order IDs
	LPTokens   map[ids.ID]uint64 `json:"lpTokens"`   // pool -> LP token balance
	CreatedAt  int64             `json:"createdAt"`
}

Account represents a user account in the DEX.

type State

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

State manages the persistent state of the DEX VM.

func New

func New(db database.Database) *State

New creates a new state manager.

func (*State) AddOpenOrder

func (s *State) AddOpenOrder(addr ids.ShortID, orderID ids.ID) error

AddOpenOrder adds an order ID to an account's open orders.

func (*State) Close

func (s *State) Close() error

Close closes the state manager.

func (*State) Commit

func (s *State) Commit() error

Commit commits all pending changes to the database.

func (*State) Credit

func (s *State) Credit(addr ids.ShortID, token ids.ID, amount uint64) error

Credit adds tokens to an account.

func (*State) CreditLPTokens

func (s *State) CreditLPTokens(addr ids.ShortID, poolID ids.ID, amount uint64) error

CreditLPTokens adds LP tokens to an account.

func (*State) Debit

func (s *State) Debit(addr ids.ShortID, token ids.ID, amount uint64) error

Debit removes tokens from an account.

func (*State) DebitLPTokens

func (s *State) DebitLPTokens(addr ids.ShortID, poolID ids.ID, amount uint64) error

DebitLPTokens removes LP tokens from an account.

func (*State) GetAccount

func (s *State) GetAccount(addr ids.ShortID) (*Account, error)

GetAccount returns an account by address.

func (*State) GetBalance

func (s *State) GetBalance(addr ids.ShortID, token ids.ID) (uint64, error)

GetBalance returns the balance of a token for an account.

func (*State) GetLPBalance

func (s *State) GetLPBalance(addr ids.ShortID, poolID ids.ID) (uint64, error)

GetLPBalance returns the LP token balance for a pool.

func (*State) GetLastBlock

func (s *State) GetLastBlock() (ids.ID, uint64)

GetLastBlock returns the last accepted block ID and height.

func (*State) GetNonce

func (s *State) GetNonce(addr ids.ShortID) (uint64, error)

GetNonce returns the current nonce for an account.

func (*State) GetOrCreateAccount

func (s *State) GetOrCreateAccount(addr ids.ShortID) *Account

GetOrCreateAccount returns an existing account or creates a new one.

func (*State) IncrementNonce

func (s *State) IncrementNonce(addr ids.ShortID) error

IncrementNonce increments the nonce for an account.

func (*State) Initialize

func (s *State) Initialize() error

Initialize initializes state from database.

func (*State) RemoveOpenOrder

func (s *State) RemoveOpenOrder(addr ids.ShortID, orderID ids.ID) error

RemoveOpenOrder removes an order ID from an account's open orders.

func (*State) SaveAccount

func (s *State) SaveAccount(acc *Account) error

SaveAccount saves an account to database.

func (*State) SetLastBlock

func (s *State) SetLastBlock(blockID ids.ID, height uint64) error

SetLastBlock sets the last accepted block.

func (*State) Transfer

func (s *State) Transfer(from, to ids.ShortID, token ids.ID, amount uint64) error

Transfer transfers tokens between accounts.

Jump to

Keyboard shortcuts

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