Playground
gnomod.toml
module = "gno.land/p/nym-moultest123/x/storagecost/v1" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186409 max_deposit = "8000000ugnot"
storagecost.gno
// Package storagecost answers one question: is it worth paying gas to delete
// on-chain state?
//
// On gno.land every byte of realm state locks GNOT, and the lock is refunded
// to whoever signs the transaction that frees the byte. Deleting state is
// therefore paid work, and whether a given deletion pays depends on two prices
// that move independently: the storage price, a chain parameter, and the gas
// price of the day. A contract cannot know the second one, so it cannot decide
// on its own behalf whether to compact, reindex or reap. It can only publish
// the size of the prize and let a caller do the arithmetic.
//
// This package is that arithmetic. It is pure integer maths with no chain
// imports, so a realm can call it inside a Render and an off-chain bot can
// reuse the identical formula.
//
// The single fact worth remembering is the ratio. One byte freed refunds 100
// ugnot, and at the lowest gas price mainnet has actually accepted one ugnot
// buys 1000 gas. So a byte is worth 100,000 gas, and any deletion costing less
// than that per byte pays for itself.
//
// Demo realms: gno.land/r/nym-moultest123/x/reaper for reaping expired state, and
// gno.land/r/nym-moultest123/x/compact for the compaction half.
package storagecost
import (
"gno.land/p/nym-moultest123/kit/num/v0"
"gno.land/p/nt/ufmt/v0"
)
const (
// DefaultStoragePrice is the ugnot locked per byte of realm state, the
// default of the chain's vm:p:storage_price parameter. It is governance
// settable, so read it from the chain rather than trusting this constant
// when real money depends on the answer.
DefaultStoragePrice int64 = 100
// GasPerUgnotFloor is how much gas one ugnot buys at the lowest gas price
// mainnet has been observed to accept, 0.001 ugnot per gas. It is a floor,
// not a promise: the fee a node requires tracks gas_wanted, so asking for
// more headroom raises the fee proportionally.
GasPerUgnotFloor int64 = 1000
// payloadOverheadNum/payloadOverheadDen approximate what a payload really
// costs once the realm has wrapped it in an object. Measured at 1.85x: ten
// 1,024-byte strings in a realm slice cost 18,984 bytes of state, 1,898
// each. It is an estimate and nothing more. The authoritative number is
// the chain's own, from the vm/qstorage query or a StorageDepositEvent.
payloadOverheadNum int64 = 185
payloadOverheadDen int64 = 100
// BytesPerTreeNode is the realm state one balanced-tree container node
// occupies, for the tree containers in this namespace (p/moul/ulist and
// what is built on it).
//
// Measured twice, agreeing exactly. Compacting 8 dead nodes on mainnet
// freed 6,848 bytes (2026-09-23), and 31 nodes in the local integration
// harness freed 26,536. Both give 856.
//
// It is a property of the node, not of the element: the node costs this
// whether the value it carried was 8 bytes or 1,024. That is what makes
// EstimateNodes accurate where EstimateBytes is only indicative.
BytesPerTreeNode int64 = 856
ugnotPerGNOT int64 = 1_000_000
)
// Refund is the deposit returned for freeing bytes at the given price per
// byte. Returns 0 for non-positive inputs rather than panicking, so a Render
// on a realm with no state still works.
func Refund(bytes, pricePerByte int64) int64 {
if bytes <= 0 || pricePerByte <= 0 {
return 0
}
return bytes * pricePerByte
}
// BreakEvenBytes is the fewest bytes whose refund covers a fee: the point
// where a cleanup stops costing money and starts making it. Below this many
// bytes the transaction is charity.
func BreakEvenBytes(feeUgnot, pricePerByte int64) int64 {
if pricePerByte <= 0 {
return 0
}
if feeUgnot <= 0 {
return 0
}
// Round up: freeing exactly feeUgnot/pricePerByte bytes must cover the fee.
return (feeUgnot + pricePerByte - 1) / pricePerByte
}
// GasFee is the fee a transaction asking for gasWanted must pay at a gas price
// of num/den ugnot per gas, rounded up.
//
// The fee tracks gas_wanted rather than gas_used, so unused headroom is paid
// for. Pass the ceiling you will actually put in the transaction, not what you
// expect to burn.
func GasFee(gasWanted, num, den int64) int64 {
if gasWanted <= 0 || num <= 0 || den <= 0 {
return 0
}
return (gasWanted*num + den - 1) / den
}
// FloorGasFee is GasFee at the lowest gas price mainnet has accepted.
func FloorGasFee(gasWanted int64) int64 {
return GasFee(gasWanted, 1, GasPerUgnotFloor)
}
// EstimateBytes guesses the realm state a payload of this many bytes will
// occupy, applying the measured object overhead.
//
// It is for sizing a bounty in a Render, where being within a factor of two
// beats reporting the raw payload length. Never settle an accounting question
// with it.
func EstimateBytes(payloadBytes int64) int64 {
if payloadBytes <= 0 {
return 0
}
return payloadBytes * payloadOverheadNum / payloadOverheadDen
}
// EstimateNodes is the realm state a count of dead container nodes occupies,
// and so what compacting them frees.
//
// Prefer it to EstimateBytes wherever the caller can count, which for a
// compaction it always can. EstimateBytes scales a payload length by a ratio
// measured at one size and is wrong at the others: a per-entry floor dominates
// at the small end, and a realm advertising a bounty that way under-reported
// by 25x against what the cleanup actually returned on chain (2026-09-23).
// Counting nodes has no such failure mode. The container reports the exact
// number, and every node costs the same.
//
// Still an estimate. BytesPerTreeNode is measured rather than derived, and the
// chain's own StorageDepositEvent remains the only settlement.
func EstimateNodes(nodes int64) int64 {
if nodes <= 0 {
return 0
}
return nodes * BytesPerTreeNode
}
// Quote is a complete answer for one candidate cleanup.
type Quote struct {
Bytes int64 // bytes the cleanup would free
Refund int64 // ugnot returned for them
Fee int64 // ugnot the transaction will cost
Net int64 // Refund - Fee; negative means it costs more than it pays
BreakEven int64 // bytes needed to cover Fee
}
// Worth reports whether the cleanup pays for itself.
func (q Quote) Worth() bool { return q.Net > 0 }
// String renders the quote as one line of markdown-safe text, for a Render.
func (q Quote) String() string {
verdict := "not worth it yet"
if q.Worth() {
verdict = "worth " + FormatGNOT(q.Net)
}
return ufmt.Sprintf(
"%d bytes, refunds %s against %s of gas, break-even %d bytes: %s",
q.Bytes, FormatGNOT(q.Refund), FormatGNOT(q.Fee), q.BreakEven, verdict,
)
}
// Evaluate prices one cleanup: freeing bytes in a transaction asking for
// gasWanted, at a storage price of pricePerByte and a gas price of num/den
// ugnot per gas.
func Evaluate(bytes, pricePerByte, gasWanted, num, den int64) Quote {
fee := GasFee(gasWanted, num, den)
refund := Refund(bytes, pricePerByte)
return Quote{
Bytes: bytes,
Refund: refund,
Fee: fee,
Net: refund - fee,
BreakEven: BreakEvenBytes(fee, pricePerByte),
}
}
// EvaluateAtFloor is Evaluate at the default storage price and the floor gas
// price: the best case, and the one to quote when advertising a bounty.
func EvaluateAtFloor(bytes, gasWanted int64) Quote {
return Evaluate(bytes, DefaultStoragePrice, gasWanted, 1, GasPerUgnotFloor)
}
// FormatGNOT renders ugnot as GNOT with trailing zeros trimmed, because a
// bounty shown in ugnot is unreadable and gno has no floats.
//
// It delegates to [p/moul/kit/num], which owns amount formatting. v0 did it by
// hand with `amount = -amount`, and since that leaves math.MinInt64 negative
// both the whole and the fractional part then carried their own sign:
// FormatGNOT(math.MinInt64) returned "--9223372036854.-775808 GNOT". No caller
// in this repo can reach it, but the function is exported and a formatter that
// can return a non-number is not one.
func FormatGNOT(amount int64) string {
return num.GNOTf(amount)
}
storagecost_test.gno
package storagecost
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func TestRefund(t *testing.T) {
tests := []struct {
name string
bytes int64
price int64
want int64
}{
{"one byte at the default price", 1, DefaultStoragePrice, 100},
{"the measured reap", 17835, DefaultStoragePrice, 1783500},
{"the measured compact", 830, DefaultStoragePrice, 83000},
{"zero bytes", 0, DefaultStoragePrice, 0},
{"negative bytes are not a credit", -100, DefaultStoragePrice, 0},
{"a free chain refunds nothing", 1000, 0, 0},
{"a ten-fold price rise", 1000, 1000, 1000000},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uassert.Equal(t, tt.want, Refund(tt.bytes, tt.price))
})
}
}
func TestBreakEvenBytes(t *testing.T) {
tests := []struct {
name string
fee int64
want int64
}{
// A 2.6M gas call at the floor costs 2,600 ugnot, so 26 bytes.
{"the canonical 2.6M gas call", 2600, 26},
{"rounds up, never down", 101, 2},
{"exactly one byte", 100, 1},
{"a fee below one byte still needs a byte", 1, 1},
{"no fee, no threshold", 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := BreakEvenBytes(tt.fee, DefaultStoragePrice)
uassert.Equal(t, tt.want, got)
// The definition: this many bytes covers the fee, one fewer does not.
if tt.want > 0 {
uassert.True(t, Refund(got, DefaultStoragePrice) >= tt.fee)
uassert.False(t, Refund(got-1, DefaultStoragePrice) >= tt.fee)
}
})
}
uassert.Equal(t, int64(0), BreakEvenBytes(1000, 0))
}
func TestGasFee(t *testing.T) {
// The floor mainnet has actually accepted: 1 ugnot per 1000 gas.
uassert.Equal(t, int64(5000), FloorGasFee(5_000_000))
uassert.Equal(t, int64(2500), FloorGasFee(2_500_000))
// Rounds up, so a fee is never short.
uassert.Equal(t, int64(1), FloorGasFee(1))
uassert.Equal(t, int64(0), FloorGasFee(0))
// The harness price used in the measurements, 1.2 ugnot per gas.
uassert.Equal(t, int64(6_000_000), GasFee(5_000_000, 12, 10))
uassert.Equal(t, int64(0), GasFee(5_000_000, 0, 10))
uassert.Equal(t, int64(0), GasFee(5_000_000, 1, 0))
}
func TestEvaluateReproducesTheMeasuredReap(t *testing.T) {
// The measured run: 17,835 bytes freed by a call that used 4,120,876 gas,
// so gas_wanted 5,000,000 at the floor. Net +1,778,500 ugnot.
q := EvaluateAtFloor(17835, 5_000_000)
uassert.Equal(t, int64(1783500), q.Refund)
uassert.Equal(t, int64(5000), q.Fee)
uassert.Equal(t, int64(1778500), q.Net)
uassert.Equal(t, int64(50), q.BreakEven)
uassert.True(t, q.Worth())
}
func TestEvaluateReproducesTheMeasuredCompact(t *testing.T) {
q := EvaluateAtFloor(830, 2_500_000)
uassert.Equal(t, int64(83000), q.Refund)
uassert.Equal(t, int64(2500), q.Fee)
uassert.Equal(t, int64(80500), q.Net)
uassert.True(t, q.Worth())
}
func TestEvaluateAtTheHarnessPriceIsALoss(t *testing.T) {
// The same reap priced at 1.2 ugnot/gas, 1200x the floor, loses money.
// This is why the gas price cannot be baked into a contract.
q := Evaluate(17835, DefaultStoragePrice, 5_000_000, 12, 10)
uassert.Equal(t, int64(6_000_000), q.Fee)
uassert.False(t, q.Worth())
uassert.True(t, q.Net < 0)
}
func TestQuoteString(t *testing.T) {
worth := EvaluateAtFloor(17835, 5_000_000)
uassert.Equal(t,
"17835 bytes, refunds 1.7835 GNOT against 0.005 GNOT of gas, break-even 50 bytes: worth 1.7785 GNOT",
worth.String())
loss := Evaluate(10, DefaultStoragePrice, 5_000_000, 12, 10)
uassert.Equal(t,
"10 bytes, refunds 0.001 GNOT against 6 GNOT of gas, break-even 60000 bytes: not worth it yet",
loss.String())
}
func TestFormatGNOT(t *testing.T) {
tests := []struct {
amount int64
want string
}{
{0, "0 GNOT"},
{1, "0.000001 GNOT"},
{100, "0.0001 GNOT"},
{1_000_000, "1 GNOT"},
{1_500_000, "1.5 GNOT"},
{1_783_500, "1.7835 GNOT"},
{978_743_800, "978.7438 GNOT"},
{-2_100, "-0.0021 GNOT"},
{1_000_001, "1.000001 GNOT"},
}
for _, tt := range tests {
uassert.Equal(t, tt.want, FormatGNOT(tt.amount))
}
}
func TestEstimateBytes(t *testing.T) {
// Measured: a 1,024-byte payload occupied 1,898 bytes of realm state.
uassert.Equal(t, int64(1894), EstimateBytes(1024))
uassert.Equal(t, int64(0), EstimateBytes(0))
uassert.Equal(t, int64(0), EstimateBytes(-5))
// Monotonic, which is all a bounty estimate really needs.
uassert.True(t, EstimateBytes(2048) > EstimateBytes(1024))
}
// TestFormatGNOTIsTotal pins what v0 could not do. v0 negated in int64, so at
// math.MinInt64 both halves kept a sign and it returned
// "--9223372036854.-775808 GNOT". Delegating to kit/num is the fix; this
// asserts the contract rather than the delegation.
func TestFormatGNOTIsTotal(t *testing.T) {
const minInt64 = -9223372036854775808
uassert.Equal(t, "-9223372036854.775808 GNOT", FormatGNOT(minInt64))
uassert.Equal(t, "-1.5 GNOT", FormatGNOT(-1500000))
uassert.Equal(t, "-0.000001 GNOT", FormatGNOT(-1))
uassert.Equal(t, "1.5 GNOT", FormatGNOT(1500000))
uassert.Equal(t, "0 GNOT", FormatGNOT(0))
}
func TestEstimateNodes(t *testing.T) {
cases := []struct {
name string
nodes int64
want int64
}{
{"none", 0, 0},
{"negative is not an error", -5, 0},
// The mainnet measurement this constant comes from: compacting a
// drained 8-node board returned 6,848 bytes.
{"the mainnet sample", 8, 6848},
// And the local harness, which agreed to the byte.
{"the local harness sample", 31, 26536},
}
for _, tc := range cases {
uassert.Equal(t, tc.want, EstimateNodes(tc.nodes), tc.name)
}
}
// TestEstimateNodesBeatsEstimateBytesOnTheCaseThatBurnedUs pins the reason
// EstimateNodes exists, not just what it computes.
//
// A realm advertised a bounty by running the payload length of five small
// entries through EstimateBytes and got 481 bytes. Reaping and compacting them
// actually returned 11,972. The payload path cannot see either the per-entry
// floor or the container, and no existing test could catch that because they
// all fed EstimateBytes the 1,024-byte payloads its ratio was measured on.
func TestEstimateNodesBeatsEstimateBytesOnTheCaseThatBurnedUs(t *testing.T) {
const payload = 8 + 231 + 7 + 5 + 9 // the five note bodies, 260 bytes
const reallyFreed = 11972
guess := EstimateBytes(payload)
uassert.True(t, guess*20 < reallyFreed,
"EstimateBytes is still supposed to be the bad one here")
// The container half is the part that is countable, and counting it gets
// most of the way there on its own: 8 nodes were the larger share.
counted := EstimateNodes(8)
uassert.True(t, counted > guess*10,
"counting nodes should dominate guessing from payload")
uassert.True(t, counted <= reallyFreed,
"counting nodes must never over-advertise")
}