Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/pullpaymentdemo/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186411 max_deposit = "5000000ugnot"
pullpaymentdemo.gno
// Package pullpaymentdemo is a small gnoweb demo of the escrow ledger provided
// by the [p/moul/x/daily/pullpayment](/p/nym-moultest123/x/daily/pullpayment/v0) library:
// crediting a split, claiming it, and why a reentrant claim gets nothing.
//
// It contains no ledger logic of its own and holds no coins. Stateless, so
// Render is deterministic — which is precisely what the library is for.
package pullpaymentdemo
import (
"strconv"
"strings"
"gno.land/p/nym-moultest123/x/daily/pullpayment/v0"
)
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Pull Payment\n\n")
b.WriteString("Credit and let recipients withdraw, demoing the ")
b.WriteString("[`p/moul/x/daily/pullpayment`](/p/nym-moultest123/x/daily/pullpayment/v0) library.\n\n")
b.WriteString("## Why not just send?\n\n")
b.WriteString("Pushing value hands control to the recipient in the middle of your ")
b.WriteString("state transition, and a hostile recipient re-enters before you have ")
b.WriteString("finished updating. Pulling inverts it: you record a debt, and the ")
b.WriteString("recipient's own withdrawal is the only state being touched.\n\n")
l := pullpayment.New()
l.CreditMany([]string{"alice", "bob", "carol"}, []int64{500, 300, 200})
b.WriteString("## A split, credited\n\n")
b.WriteString(table(l))
b.WriteString("\nThe holding realm must keep `")
b.WriteString(strconv.FormatInt(l.TotalOwed(), 10))
b.WriteString("` in reserve — that is what `TotalOwed` is for.\n\n")
b.WriteString("## Bob claims\n\n")
amt, _ := l.Withdraw("bob")
b.WriteString("`Withdraw(\"bob\")` → `" + strconv.FormatInt(amt, 10) + "`, and the ledger ")
b.WriteString("is updated **before** the caller transfers anything:\n\n")
b.WriteString(table(l))
b.WriteString("\nReserve is now `" + strconv.FormatInt(l.TotalOwed(), 10) + "`.\n\n")
b.WriteString("## The reentrancy attempt\n\n")
b.WriteString("Bob's handler calls straight back in, before the transfer completes:\n\n")
again, err := l.Withdraw("bob")
b.WriteString("- second `Withdraw(\"bob\")` → `" + strconv.FormatInt(again, 10) + "`, error: `")
if err != nil {
b.WriteString(err.Error())
} else {
b.WriteString("<nil>")
}
b.WriteString("`\n\n")
b.WriteString("Nothing left to take. The balance was deleted before control left the ")
b.WriteString("function — checks, effects, *then* interactions. Lifetime withdrawn is ")
b.WriteString("still `" + strconv.FormatInt(l.TotalWithdrawn(), 10) + "`, not double.\n\n")
b.WriteString("## Credits accumulate\n\n")
l.Credit("alice", 100)
b.WriteString("`Credit(\"alice\", 100)` on top of her existing balance:\n\n")
b.WriteString(table(l))
b.WriteString("\nOne withdrawal collects the lot — no dust left behind.\n\n")
b.WriteString("## Batch credits are all-or-nothing\n\n")
b.WriteString("A split that is partly invalid applies **none** of itself; a ledger ")
b.WriteString("half-agreeing with the funds it guards is worse than a rejected call:\n\n")
before := l.TotalOwed()
batchErr := l.CreditMany([]string{"dave", "eve"}, []int64{10, -1})
b.WriteString("- `CreditMany([dave, eve], [10, -1])` → `")
if batchErr != nil {
b.WriteString(batchErr.Error())
}
b.WriteString("`\n- total owed before: `" + strconv.FormatInt(before, 10) + "`, after: `")
b.WriteString(strconv.FormatInt(l.TotalOwed(), 10) + "` — unchanged, and `dave` was not credited\n")
return b.String()
}
func table(l *pullpayment.Ledger) string {
var b strings.Builder
b.WriteString("| payee | owed |\n|---|---|\n")
l.Iterate(func(p string, amt int64) bool {
b.WriteString("| `" + p + "` | " + strconv.FormatInt(amt, 10) + " |\n")
return false
})
return b.String()
}
render_example_test.gno
package pullpaymentdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Pull Payment
//
// Credit and let recipients withdraw, demoing the [`p/moul/x/daily/pullpayment`](/p/nym-moultest123/x/daily/pullpayment/v0) library.
//
// ## Why not just send?
//
// Pushing value hands control to the recipient in the middle of your state transition, and a hostile recipient re-enters before you have finished updating. Pulling inverts it: you record a debt, and the recipient's own withdrawal is the only state being touched.
//
// ## A split, credited
//
// | payee | owed |
// |---|---|
// | `alice` | 500 |
// | `bob` | 300 |
// | `carol` | 200 |
//
// The holding realm must keep `1000` in reserve — that is what `TotalOwed` is for.
//
// ## Bob claims
//
// `Withdraw("bob")` → `300`, and the ledger is updated **before** the caller transfers anything:
//
// | payee | owed |
// |---|---|
// | `alice` | 500 |
// | `carol` | 200 |
//
// Reserve is now `700`.
//
// ## The reentrancy attempt
//
// Bob's handler calls straight back in, before the transfer completes:
//
// - second `Withdraw("bob")` → `0`, error: `pullpayment: nothing to withdraw`
//
// Nothing left to take. The balance was deleted before control left the function — checks, effects, *then* interactions. Lifetime withdrawn is still `300`, not double.
//
// ## Credits accumulate
//
// `Credit("alice", 100)` on top of her existing balance:
//
// | payee | owed |
// |---|---|
// | `alice` | 600 |
// | `carol` | 200 |
//
// One withdrawal collects the lot — no dust left behind.
//
// ## Batch credits are all-or-nothing
//
// A split that is partly invalid applies **none** of itself; a ledger half-agreeing with the funds it guards is worse than a rejected call:
//
// - `CreditMany([dave, eve], [10, -1])` → `pullpayment: amount must be positive`
// - total owed before: `800`, after: `800` — unchanged, and `dave` was not credited
}