ledger

package
v1.10.0-beta.2....-1a3534f Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MetaTargetTypeAccount     = "ACCOUNT"
	MetaTargetTypeTransaction = "TRANSACTION"
)
View Source
const (
	DatePrecision = time.Microsecond
	DateFormat    = time.RFC3339Nano
)
View Source
const AccountPattern = "^" + AccountSegmentRegex + "(:" + AccountSegmentRegex + ")*$"
View Source
const AccountSegmentRegex = "[a-zA-Z0-9_]+(?:-[a-zA-Z0-9_]+)*"
View Source
const AssetPattern = `[A-Z][A-Z0-9]{0,16}(\/\d{1,6})?`
View Source
const (
	WORLD = "world"
)

Variables

View Source
var AssetRegexp = regexp.MustCompile("^" + AssetPattern + "$")
View Source
var (
	ErrNoPostings = errors.New("invalid payload: should contain either postings or script")
)
View Source
var Zero = big.NewInt(0)

Functions

func AssetIsValid

func AssetIsValid(v string) bool

func ComputeMetadata

func ComputeMetadata(key, value string) metadata.Metadata

func HydrateLog

func HydrateLog(_type LogType, data []byte) (any, error)

func MarkReverts

func MarkReverts(m metadata.Metadata, txID *big.Int) metadata.Metadata

func RevertMetadata

func RevertMetadata(tx *big.Int) metadata.Metadata

func RevertMetadataSpecKey

func RevertMetadataSpecKey() string

func SpecMetadata

func SpecMetadata(name string) string

func ValidateAddress

func ValidateAddress(addr string) bool

Types

type Account

type Account struct {
	bun.BaseModel `bun:"table:accounts,alias:accounts"`

	Address  string            `json:"address"`
	Metadata metadata.Metadata `json:"metadata"`
}

func NewAccount

func NewAccount(address string) Account

type AccountMetadata

type AccountMetadata map[string]metadata.Metadata

type Accounts

type Accounts map[string]Account

type AccountsAssetsVolumes

type AccountsAssetsVolumes map[string]VolumesByAssets

func (*AccountsAssetsVolumes) AddInput

func (a *AccountsAssetsVolumes) AddInput(account, asset string, input *big.Int)

func (*AccountsAssetsVolumes) AddOutput

func (a *AccountsAssetsVolumes) AddOutput(account, asset string, output *big.Int)

func (AccountsAssetsVolumes) Balances

func (AccountsAssetsVolumes) Copy

func (AccountsAssetsVolumes) GetVolumes

func (a AccountsAssetsVolumes) GetVolumes(account, asset string) *Volumes

func (AccountsAssetsVolumes) HasAccount

func (a AccountsAssetsVolumes) HasAccount(account string) bool

func (AccountsAssetsVolumes) HasAccountAndAsset

func (a AccountsAssetsVolumes) HasAccountAndAsset(account, asset string) bool

func (*AccountsAssetsVolumes) Scan

func (a *AccountsAssetsVolumes) Scan(value interface{}) error

Scan - Implement the database/sql scanner interface

func (*AccountsAssetsVolumes) SetVolumes

func (a *AccountsAssetsVolumes) SetVolumes(account, asset string, volumes *Volumes)

type BalancesByAssets

type BalancesByAssets map[string]*big.Int

type BalancesByAssetsByAccounts

type BalancesByAssetsByAccounts map[string]BalancesByAssets

type ChainedLog

type ChainedLog struct {
	Log
	ID        *big.Int `json:"id"`
	Projected bool     `json:"-"`
	Hash      []byte   `json:"hash"`
}

func ChainLogs

func ChainLogs(logs ...*Log) []*ChainedLog

func (*ChainedLog) ComputeHash

func (l *ChainedLog) ComputeHash(previous *ChainedLog)

func (*ChainedLog) UnmarshalJSON

func (l *ChainedLog) UnmarshalJSON(data []byte) error

func (*ChainedLog) WithID

func (l *ChainedLog) WithID(id uint64) *ChainedLog

type DeleteMetadataLogPayload

type DeleteMetadataLogPayload struct {
	TargetType string `json:"targetType"`
	TargetID   any    `json:"targetId"`
	Key        string `json:"key"`
}

type ExpandedAccount

type ExpandedAccount struct {
	Account          `bun:",extend"`
	Volumes          VolumesByAssets `json:"volumes,omitempty" bun:"volumes,type:jsonb"`
	EffectiveVolumes VolumesByAssets `json:"effectiveVolumes,omitempty" bun:"effective_volumes,type:jsonb"`
}

func NewExpandedAccount

func NewExpandedAccount(address string) ExpandedAccount

func (ExpandedAccount) Copy

type ExpandedTransaction

type ExpandedTransaction struct {
	Transaction
	PreCommitVolumes           AccountsAssetsVolumes `json:"preCommitVolumes,omitempty"`
	PostCommitVolumes          AccountsAssetsVolumes `json:"postCommitVolumes,omitempty"`
	PreCommitEffectiveVolumes  AccountsAssetsVolumes `json:"preCommitEffectiveVolumes,omitempty"`
	PostCommitEffectiveVolumes AccountsAssetsVolumes `json:"postCommitEffectiveVolumes,omitempty"`
}

func ExpandTransaction

func ExpandTransaction(tx *Transaction, preCommitVolumes AccountsAssetsVolumes) ExpandedTransaction

func (*ExpandedTransaction) AppendPosting

func (t *ExpandedTransaction) AppendPosting(p Posting)

type Log

type Log struct {
	Type           LogType `json:"type"`
	Data           any     `json:"data"`
	Date           Time    `json:"date"`
	IdempotencyKey string  `json:"idempotencyKey"`
}

func NewDeleteMetadataLog

func NewDeleteMetadataLog(at Time, payload DeleteMetadataLogPayload) *Log

func NewRevertedTransactionLog

func NewRevertedTransactionLog(at Time, revertedTxID *big.Int, tx *Transaction) *Log

func NewSetMetadataLog

func NewSetMetadataLog(at Time, metadata SetMetadataLogPayload) *Log

func NewSetMetadataOnAccountLog

func NewSetMetadataOnAccountLog(at Time, account string, metadata metadata.Metadata) *Log

func NewSetMetadataOnTransactionLog

func NewSetMetadataOnTransactionLog(at Time, txID *big.Int, metadata metadata.Metadata) *Log

func NewTransactionLog

func NewTransactionLog(tx *Transaction, accountMetadata map[string]metadata.Metadata) *Log

func NewTransactionLogWithDate

func NewTransactionLogWithDate(tx *Transaction, accountMetadata map[string]metadata.Metadata, time Time) *Log

func (*Log) ChainLog

func (l *Log) ChainLog(previous *ChainedLog) *ChainedLog

func (*Log) WithDate

func (l *Log) WithDate(date Time) *Log

func (*Log) WithIdempotencyKey

func (l *Log) WithIdempotencyKey(key string) *Log

type LogType

type LogType int16
const (
	// TODO(gfyrag): Create dedicated log type for account and metadata
	SetMetadataLogType         LogType = iota // "SET_METADATA"
	NewTransactionLogType                     // "NEW_TRANSACTION"
	RevertedTransactionLogType                // "REVERTED_TRANSACTION"
	DeleteMetadataLogType
)

func LogTypeFromString

func LogTypeFromString(logType string) LogType

func (LogType) MarshalJSON

func (lt LogType) MarshalJSON() ([]byte, error)

Needed in order to keep the compatibility with the openapi response for ListLogs.

func (LogType) String

func (l LogType) String() string

func (*LogType) UnmarshalJSON

func (lt *LogType) UnmarshalJSON(data []byte) error

type NewTransactionLogPayload

type NewTransactionLogPayload struct {
	Transaction     *Transaction    `json:"transaction"`
	AccountMetadata AccountMetadata `json:"accountMetadata"`
}

type Posting

type Posting struct {
	Source      string   `json:"source"`
	Destination string   `json:"destination"`
	Amount      *big.Int `json:"amount"`
	Asset       string   `json:"asset"`
}

func NewPosting

func NewPosting(source string, destination string, asset string, amount *big.Int) Posting

type Postings

type Postings []Posting

func (Postings) Reverse

func (p Postings) Reverse()

func (*Postings) Scan

func (p *Postings) Scan(value interface{}) error

Scan - Implement the database/sql scanner interface

func (Postings) Validate

func (p Postings) Validate() (int, error)

type RevertedTransactionLogPayload

type RevertedTransactionLogPayload struct {
	RevertedTransactionID *big.Int     `json:"revertedTransactionID"`
	RevertTransaction     *Transaction `json:"transaction"`
}

type RunScript

type RunScript struct {
	Script
	Timestamp Time              `json:"timestamp"`
	Metadata  metadata.Metadata `json:"metadata"`
	Reference string            `json:"reference"`
}

func TxToScriptData

func TxToScriptData(txData TransactionData, allowUnboundedOverdrafts bool) RunScript

type Script

type Script struct {
	Plain string            `json:"plain"`
	Vars  map[string]string `json:"vars" swaggertype:"object"`
}

type ScriptV1

type ScriptV1 struct {
	Script
	Vars map[string]any `json:"vars"`
}

func (ScriptV1) ToCore

func (s ScriptV1) ToCore() Script

type SetMetadataLogPayload

type SetMetadataLogPayload struct {
	TargetType string            `json:"targetType"`
	TargetID   any               `json:"targetId"`
	Metadata   metadata.Metadata `json:"metadata"`
}

func (*SetMetadataLogPayload) UnmarshalJSON

func (s *SetMetadataLogPayload) UnmarshalJSON(data []byte) error

type Time

type Time struct {
	time.Time
}

func Now

func Now() Time

func ParseTime

func ParseTime(v string) (Time, error)

func (Time) Add

func (t Time) Add(d time.Duration) Time

func (Time) After

func (t Time) After(t2 Time) bool

func (Time) Before

func (t Time) Before(t2 Time) bool

func (Time) Equal

func (t Time) Equal(t2 Time) bool

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (Time) Round

func (t Time) Round(precision time.Duration) Time

func (*Time) Scan

func (t *Time) Scan(src interface{}) (err error)

func (Time) Sub

func (t Time) Sub(t2 Time) time.Duration

func (Time) UTC

func (t Time) UTC() Time

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

func (Time) Value

func (t Time) Value() (driver.Value, error)

type Transaction

type Transaction struct {
	TransactionData
	ID       *big.Int `json:"id"`
	Reverted bool     `json:"reverted"`
}

func NewTransaction

func NewTransaction() *Transaction

func (*Transaction) WithDate

func (t *Transaction) WithDate(ts Time) *Transaction

func (*Transaction) WithID

func (t *Transaction) WithID(id *big.Int) *Transaction

func (*Transaction) WithIDUint64

func (t *Transaction) WithIDUint64(id uint64) *Transaction

func (*Transaction) WithMetadata

func (t *Transaction) WithMetadata(m metadata.Metadata) *Transaction

func (*Transaction) WithPostings

func (t *Transaction) WithPostings(postings ...Posting) *Transaction

func (*Transaction) WithReference

func (t *Transaction) WithReference(ref string) *Transaction

type TransactionData

type TransactionData struct {
	Postings  Postings          `json:"postings"`
	Metadata  metadata.Metadata `json:"metadata"`
	Timestamp Time              `json:"timestamp"`
	Reference string            `json:"reference,omitempty"`
}

func NewTransactionData

func NewTransactionData() TransactionData

func (*TransactionData) Reverse

func (t *TransactionData) Reverse() TransactionData

func (TransactionData) WithDate

func (d TransactionData) WithDate(now Time) TransactionData

func (TransactionData) WithPostings

func (d TransactionData) WithPostings(postings ...Posting) TransactionData

type TransactionRequest

type TransactionRequest struct {
	Postings  Postings          `json:"postings"`
	Script    ScriptV1          `json:"script"`
	Timestamp Time              `json:"timestamp"`
	Reference string            `json:"reference"`
	Metadata  metadata.Metadata `json:"metadata" swaggertype:"object"`
}

func (*TransactionRequest) ToRunScript

func (req *TransactionRequest) ToRunScript() *RunScript

type Transactions

type Transactions struct {
	Transactions []TransactionData `json:"transactions"`
}

type Volumes

type Volumes struct {
	Input  *big.Int `json:"input"`
	Output *big.Int `json:"output"`
}

func NewEmptyVolumes

func NewEmptyVolumes() *Volumes

func NewVolumesInt64

func NewVolumesInt64(input, output int64) *Volumes

func (Volumes) Balance

func (v Volumes) Balance() *big.Int

func (Volumes) CopyWithZerosIfNeeded

func (v Volumes) CopyWithZerosIfNeeded() *Volumes

func (Volumes) MarshalJSON

func (v Volumes) MarshalJSON() ([]byte, error)

func (Volumes) WithInput

func (v Volumes) WithInput(input *big.Int) *Volumes

func (Volumes) WithInputInt64

func (v Volumes) WithInputInt64(value int64) *Volumes

func (Volumes) WithOutput

func (v Volumes) WithOutput(output *big.Int) *Volumes

func (Volumes) WithOutputInt64

func (v Volumes) WithOutputInt64(value int64) *Volumes

type VolumesByAssets

type VolumesByAssets map[string]*Volumes

func (VolumesByAssets) Balances

func (v VolumesByAssets) Balances() BalancesByAssets

type VolumesWithBalance

type VolumesWithBalance struct {
	Input   *big.Int `json:"input"`
	Output  *big.Int `json:"output"`
	Balance *big.Int `json:"balance"`
}

Directories

Path Synopsis
api
backend
Code generated by MockGen.
Code generated by MockGen.
v1
v2
examples command
vm
Provides `Machine`, which executes programs and outputs postings.
Provides `Machine`, which executes programs and outputs postings.
opentelemetry

Jump to

Keyboard shortcuts

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