Skip to content

Latest commit

 

History

History
1163 lines (744 loc) · 33.2 KB

README.md

File metadata and controls

1163 lines (744 loc) · 33.2 KB

ledger

import "github.com/formancehq/ledger/internal"

Index

Constants

const (
    MetaTargetTypeAccount     = "ACCOUNT"
    MetaTargetTypeTransaction = "TRANSACTION"
)

const (
    DefaultBucket = "_default"
)

const (
    WORLD = "world"
)

Variables

var Zero = big.NewInt(0)

func ComputeIdempotencyHash(inputs any) string

type Account

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

    Address          string            `json:"address" bun:"address"`
    Metadata         metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb,default:'{}'"`
    FirstUsage       time.Time         `json:"-" bun:"first_usage,nullzero"`
    InsertionDate    time.Time         `json:"-" bun:"insertion_date,nullzero"`
    UpdatedAt        time.Time         `json:"-" bun:"updated_at,nullzero"`
    Volumes          VolumesByAssets   `json:"volumes,omitempty" bun:"volumes,scanonly"`
    EffectiveVolumes VolumesByAssets   `json:"effectiveVolumes,omitempty" bun:"effective_volumes,scanonly"`
}

func (Account) GetAddress

func (a Account) GetAddress() string

type AccountMetadata map[string]metadata.Metadata

type AccountsVolumes struct {
    bun.BaseModel `bun:"accounts_volumes"`

    Account string   `bun:"accounts_address,type:varchar"`
    Asset   string   `bun:"asset,type:varchar"`
    Input   *big.Int `bun:"input,type:numeric"`
    Output  *big.Int `bun:"output,type:numeric"`
}

type AggregatedVolumes struct {
    Aggregated VolumesByAssets `bun:"aggregated,type:jsonb"`
}

type BalancesByAssets map[string]*big.Int

type BalancesByAssetsByAccounts map[string]BalancesByAssets

type Configuration struct {
    Bucket   string              `json:"bucket" bun:"bucket,type:varchar(255)"`
    Metadata metadata.Metadata   `json:"metadata" bun:"metadata,type:jsonb,nullzero"`
    Features features.FeatureSet `json:"features" bun:"features,type:jsonb"`
}

func NewDefaultConfiguration() Configuration

func (*Configuration) SetDefaults

func (c *Configuration) SetDefaults()

func (*Configuration) Validate

func (c *Configuration) Validate() error

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

func (CreatedTransaction) GetMemento

func (p CreatedTransaction) GetMemento() any

func (CreatedTransaction) Type

func (p CreatedTransaction) Type() LogType

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

func (DeletedMetadata) Type

func (s DeletedMetadata) Type() LogType

func (*DeletedMetadata) UnmarshalJSON

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

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

func (ErrInvalidBucketName) Error

func (e ErrInvalidBucketName) Error() string

func (ErrInvalidBucketName) Is

func (e ErrInvalidBucketName) Is(err error) bool

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

func (ErrInvalidLedgerName) Error

func (e ErrInvalidLedgerName) Error() string

func (ErrInvalidLedgerName) Is

func (e ErrInvalidLedgerName) Is(err error) bool

type Ledger

type Ledger struct {
    bun.BaseModel `bun:"_system.ledgers,alias:ledgers"`

    Configuration
    ID      int       `json:"id" bun:"id,type:int,scanonly"`
    Name    string    `json:"name" bun:"name,type:varchar(255),pk"`
    AddedAt time.Time `json:"addedAt" bun:"added_at,type:timestamp,nullzero"`
}

func MustNewWithDefault(name string) Ledger

func New

func New(name string, configuration Configuration) (*Ledger, error)

func NewWithDefaults(name string) (*Ledger, error)

func (Ledger) HasFeature

func (l Ledger) HasFeature(feature, value string) bool

func (Ledger) WithMetadata

func (l Ledger) WithMetadata(m metadata.Metadata) Ledger

type Log

Log represents atomic actions made on the ledger.

type Log struct {
    bun.BaseModel `bun:"table:logs,alias:logs"`

    Type           LogType    `json:"type" bun:"type,type:log_type"`
    Data           LogPayload `json:"data" bun:"data,type:jsonb"`
    Date           time.Time  `json:"date" bun:"date,type:timestamptz,nullzero"`
    IdempotencyKey string     `json:"idempotencyKey" bun:"idempotency_key,type:varchar(256),unique,nullzero"`
    // IdempotencyHash is a signature used when using IdempotencyKey.
    // It allows to check if the usage of IdempotencyKey match inputs given on the first idempotency key usage.
    IdempotencyHash string `json:"idempotencyHash" bun:"idempotency_hash,unique,nullzero"`
    ID              int    `json:"id" bun:"id,unique,type:numeric"`
    Hash            []byte `json:"hash" bun:"hash,type:bytea"`
}

func NewLog

func NewLog(payload LogPayload) Log

func (Log) ChainLog

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

func (*Log) ComputeHash

func (l *Log) ComputeHash(previous *Log)

func (*Log) UnmarshalJSON

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

func (l Log) WithIdempotencyKey(key string) Log

type LogPayload interface {
    Type() LogType
}

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

type LogType

type LogType int16

const (
    SetMetadataLogType         LogType = iota // "SET_METADATA"
    NewLogType                                // "NEW_TRANSACTION"
    RevertedTransactionLogType                // "REVERTED_TRANSACTION"
    DeleteMetadataLogType
)

func LogTypeFromString(logType string) LogType

func (LogType) MarshalJSON

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

func (*LogType) Scan

func (lt *LogType) Scan(src interface{}) error

func (LogType) String

func (lt LogType) String() string

func (*LogType) UnmarshalJSON

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

func (LogType) Value

func (lt LogType) Value() (driver.Value, error)

type Memento

type Memento interface {
    GetMemento() any
}

type Move

type Move struct {
    bun.BaseModel `bun:"table:moves"`

    TransactionID              int                 `bun:"transactions_id,type:bigint"`
    IsSource                   bool                `bun:"is_source,type:bool"`
    Account                    string              `bun:"accounts_address,type:varchar"`
    Amount                     *bunpaginate.BigInt `bun:"amount,type:numeric"`
    Asset                      string              `bun:"asset,type:varchar"`
    InsertionDate              time.Time           `bun:"insertion_date,type:timestamp,nullzero"`
    EffectiveDate              time.Time           `bun:"effective_date,type:timestamp,nullzero"`
    PostCommitVolumes          *Volumes            `bun:"post_commit_volumes,type:jsonb"`
    PostCommitEffectiveVolumes *Volumes            `bun:"post_commit_effective_volumes,type:jsonb,scanonly"`
}

type Moves

type Moves []*Move

func (m Moves) ComputePostCommitEffectiveVolumes() PostCommitVolumes

type PostCommitVolumes map[string]VolumesByAssets

func (PostCommitVolumes) AddInput

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

func (PostCommitVolumes) AddOutput

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

func (PostCommitVolumes) Copy

func (a PostCommitVolumes) Copy() PostCommitVolumes

func (PostCommitVolumes) Merge

func (a PostCommitVolumes) Merge(volumes PostCommitVolumes) PostCommitVolumes

type Posting

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

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

type Postings []Posting

func (Postings) Reverse

func (p Postings) Reverse() Postings

func (Postings) Validate

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

type RevertedTransaction struct {
    RevertedTransaction Transaction `json:"revertedTransaction"`
    RevertTransaction   Transaction `json:"transaction"`
}

func (RevertedTransaction) GetMemento

func (r RevertedTransaction) GetMemento() any

func (RevertedTransaction) Type

func (r RevertedTransaction) Type() LogType

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

func (SavedMetadata) Type

func (s SavedMetadata) Type() LogType

func (*SavedMetadata) UnmarshalJSON

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

type Transaction struct {
    bun.BaseModel `bun:"table:transactions,alias:transactions"`

    TransactionData
    ID         int        `json:"id" bun:"id,type:numeric"`
    RevertedAt *time.Time `json:"revertedAt,omitempty" bun:"reverted_at,type:timestamp without time zone"`
    // PostCommitVolumes are the volumes of each account/asset after a transaction has been committed.
    // Those volumes will never change as those are computed in flight.
    PostCommitVolumes PostCommitVolumes `json:"postCommitVolumes,omitempty" bun:"post_commit_volumes,type:jsonb"`
    // PostCommitEffectiveVolumes are the volumes of each account/asset after the transaction TransactionData.Timestamp.
    // Those volumes are also computed in flight, but can be updated if a transaction is inserted in the past.
    PostCommitEffectiveVolumes PostCommitVolumes `json:"postCommitEffectiveVolumes,omitempty" bun:"post_commit_effective_volumes,type:jsonb,scanonly"`
}

func NewTransaction() Transaction

func (Transaction) InvolvedAccounts

func (tx Transaction) InvolvedAccounts() []string

func (Transaction) InvolvedDestinations

func (tx Transaction) InvolvedDestinations() map[string][]string

func (Transaction) IsReverted

func (tx Transaction) IsReverted() bool

func (Transaction) JSONSchemaExtend

func (Transaction) JSONSchemaExtend(schema *jsonschema.Schema)

func (Transaction) MarshalJSON

func (tx Transaction) MarshalJSON() ([]byte, error)

func (Transaction) Reverse

func (tx Transaction) Reverse() Transaction

func (Transaction) VolumeUpdates

func (tx Transaction) VolumeUpdates() []AccountsVolumes

func (Transaction) WithInsertedAt

func (tx Transaction) WithInsertedAt(date time.Time) Transaction

func (Transaction) WithMetadata

func (tx Transaction) WithMetadata(m metadata.Metadata) Transaction

func (tx Transaction) WithPostCommitEffectiveVolumes(volumes PostCommitVolumes) Transaction

func (Transaction) WithPostings

func (tx Transaction) WithPostings(postings ...Posting) Transaction

func (Transaction) WithReference

func (tx Transaction) WithReference(ref string) Transaction

func (Transaction) WithRevertedAt

func (tx Transaction) WithRevertedAt(timestamp time.Time) Transaction

func (Transaction) WithTimestamp

func (tx Transaction) WithTimestamp(ts time.Time) Transaction

type TransactionData struct {
    Postings   Postings          `json:"postings" bun:"postings,type:jsonb"`
    Metadata   metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb,default:'{}'"`
    Timestamp  time.Time         `json:"timestamp" bun:"timestamp,type:timestamp without time zone,nullzero"`
    Reference  string            `json:"reference,omitempty" bun:"reference,type:varchar,unique,nullzero"`
    InsertedAt time.Time         `json:"insertedAt,omitempty" bun:"inserted_at,type:timestamp without time zone,nullzero"`
}

func NewTransactionData() TransactionData

func (TransactionData) WithPostings

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

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

type Volumes

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

func NewEmptyVolumes() Volumes

func NewVolumesInt64(input, output int64) Volumes

func (Volumes) Balance

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

func (Volumes) Copy

func (v Volumes) Copy() Volumes

func (Volumes) JSONSchemaExtend

func (Volumes) JSONSchemaExtend(schema *jsonschema.Schema)

func (Volumes) MarshalJSON

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

func (*Volumes) Scan

func (v *Volumes) Scan(src interface{}) error

func (Volumes) Value

func (v Volumes) Value() (driver.Value, error)

type VolumesByAssets map[string]Volumes

func (VolumesByAssets) Balances

func (v VolumesByAssets) Balances() BalancesByAssets

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

type VolumesWithBalanceByAssetByAccount struct {
    Account string `json:"account" bun:"account"`
    Asset   string `json:"asset" bun:"asset"`
    VolumesWithBalance
}

type VolumesWithBalanceByAssets map[string]*VolumesWithBalance

Generated by gomarkdoc