Documentation
¶
Overview ¶
Package pnl computes average-cost positions and profit and loss from the swaps an agent made, in exact rational arithmetic. It does no I/O.
Index ¶
- Constants
- func Decimal(r *big.Rat, places int) string
- func FromRecords(records []*journal.OnchainRecord, agent string, nativeAliases ...string) ([]Swap, Skipped)
- func Key(token string, nativeAliases ...string) string
- func Scale(r *big.Rat, decimals int) *big.Rat
- type Book
- type Position
- type Skipped
- type Swap
Constants ¶
const Native = "native"
Native identifies the chain's native asset in Swap and Position.
Variables ¶
This section is empty.
Functions ¶
func Decimal ¶
Decimal renders r with at most places fraction digits, rounded half away from zero, without trailing zeros. Nil renders as "".
func FromRecords ¶
func FromRecords(records []*journal.OnchainRecord, agent string, nativeAliases ...string) ([]Swap, Skipped)
FromRecords converts an agent's journal swaps (journal.ListOnchainSwaps) to Swaps. A record with a receipt fill uses the measured amount and gas; one without falls back to MinOut and is Approximate.
Types ¶
type Book ¶
type Book struct {
// contains filtered or unexported fields
}
Book holds every token's position.
func Compute ¶
Compute replays swaps in order with the average-cost method. Token keys must already be normalized with Key. Swaps with invalid amounts, or with native or the same token on both sides, are skipped.
type Position ¶
type Position struct {
Token string
// Held is the tracked quantity: what the agent's own swaps bought and
// have not sold.
Held *big.Rat
// Cost is the remaining cost basis of Held.
Cost *big.Rat
// Realized is proceeds minus average cost of every tracked sale, net of
// the swap gas attributed to this token.
Realized *big.Rat
// Gas is the native wei paid by the swaps attributed to this token.
Gas *big.Int
// GasUnknown counts attributed swaps whose gas is not recorded.
GasUnknown int
Bought *big.Rat
Sold *big.Rat
// UntrackedSold is sold quantity the agent's swaps never bought (it came
// from outside the agent). Its proceeds are not in Realized.
UntrackedSold *big.Rat
// UntrackedBought is received quantity whose cost is unknown, because a
// token -> token input had no price and no tracked basis. It is not in Held.
UntrackedBought *big.Rat
// Approximate is set when any swap of this token used the slippage floor
// or an estimated input value.
Approximate bool
Swaps int
}
Position is the running state of one token. Quantities are raw units and values native wei.
func (*Position) AvgCost ¶
AvgCost returns native wei per raw unit of Held, or nil when nothing is held.
type Skipped ¶
type Skipped struct {
// OtherRecipient swaps delivered their output to an address other than
// the agent, so they did not change the agent's position.
OtherRecipient int `json:"other_recipient"`
// Invalid swaps had unparseable amounts or tokens.
Invalid int `json:"invalid"`
}
Skipped counts journal swaps FromRecords left out.
type Swap ¶
type Swap struct {
ID string
InToken string
OutToken string
AmountIn *big.Int
// AmountOut is what the recipient received. Approximate marks a value
// that is the slippage floor rather than the measured fill.
AmountOut *big.Int
Approximate bool
// Gas is the native wei paid for the swap's transactions, nil if unknown.
Gas *big.Int
// InValue is the native wei value of the input of a token -> token swap
// at the time of the swap, nil if unknown. It is ignored otherwise.
InValue *big.Rat
}
Swap is one executed swap. Amounts are raw units of their token (wei for native).