Playground
challenge.gno
package till
import (
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/hall"
)
var info = flag.Info{
Slug: "till",
Title: "The Empty Till",
Difficulty: flag.Hard,
Category: "coins",
Brief: "A round at the bar costs 0.1 GNOT, and the barkeep checks the money you sent before he " +
"pours. At closing, the auditor counts the till after every round: a round poured with less " +
"than its price in the till is a round stolen. Get yourself into the auditor's book.",
}
var captures = flag.NewLedger()
type challenge struct{}
func (challenge) Info() flag.Info { return info }
func (challenge) Captured(player address) bool { return captures.Captured(player) }
// Challenge describes this realm to a hall.
func Challenge() flag.Challenge { return challenge{} }
func init(cur realm) { hall.Enlist(cross(cur), challenge{}) }
gnomod.toml
module = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/till" gno = "0.9" [addpkg] creator = "g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf" height = 1457783 max_deposit = "100000000ugnot"
till.gno
// Package till is a Cellar challenge about native coins and the origin
// send envelope.
package till
import (
"chain"
"chain/banker"
"chain/runtime/unsafe"
"strconv"
"strings"
"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
"gno.land/p/nt/avl/v0"
)
// Price of a round, in ugnot (0.1 GNOT).
const Price int64 = 100_000
var (
strongbox = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf") // where the till is emptied
rounds = avl.NewTree() // address -> rounds poured
stolen int // rounds the auditor flagged
)
// BuyRound pours you a round once the barkeep sees you sent the price.
// Send the coins with the call: gnokey maketx call -send 100000ugnot.
func BuyRound(cur realm) {
player := flag.MustPlayer(cur.Previous())
if unsafe.OriginSend().AmountOf("ugnot") < Price {
panic("till: a round is " + strconv.FormatInt(Price, 10) + "ugnot; send it with the call")
}
n, _ := rounds.Get(player.String()).(int)
rounds.Set(player.String(), n+1)
audit(cur, player)
}
// audit counts the till after a round and empties it into the strongbox.
// A round poured with less than its price in the till was not paid for.
func audit(cur realm, player address) {
bank := banker.NewBanker(banker.BankerTypeRealmSend, cur)
inTill := bank.GetCoin(cur.Address(), "ugnot")
if inTill < Price {
stolen++
captures.Record(player)
}
if inTill > 0 {
bank.SendCoins(cur.Address(), strongbox, chain.Coins{chain.NewCoin("ugnot", inTill)})
}
}
// Rounds returns how many rounds addr has been poured.
func Rounds(addr address) int {
n, _ := rounds.Get(addr.String()).(int)
return n
}
// Render shows the bar's books.
func Render(_ string) string {
var b strings.Builder
b.WriteString(flag.Card(info, captures))
b.WriteString("\n**A round:** " + strconv.FormatInt(Price, 10) + "ugnot · **Rounds the auditor flagged:** " +
strconv.Itoa(stolen) + "\n\n## Rounds poured\n\n")
if rounds.Size() == 0 {
b.WriteString("*The bar is quiet.*\n")
}
rounds.Iterate("", "", func(addr string, v any) bool {
b.WriteString("- `" + addr + "`: " + strconv.Itoa(v.(int)) + "\n")
return false
})
b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
return b.String()
}
till_test.gno
package till
import (
"chain"
"testing"
"gno.land/p/nt/uassert/v0"
)
const ash = address("g1zkzjmyveh7euq83h5zhnhj6mg0d7kr35nphzd2")
var tillAddr = chain.PackageAddress("gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/till")
func TestNoMoneyNoRound(cur realm, t *testing.T) {
testing.SetRealm(testing.NewUserRealm(ash))
testing.SetOriginSend(chain.Coins{chain.NewCoin("ugnot", Price-1)})
uassert.AbortsContains(t, cur, "send it with the call", func() { BuyRound(cross(cur)) })
}
func TestPaidRoundIsNotStolen(cur realm, t *testing.T) {
paid := chain.Coins{chain.NewCoin("ugnot", Price)}
testing.SetRealm(testing.NewUserRealm(ash))
testing.SetOriginSend(paid)
testing.IssueCoins(tillAddr, paid) // the chain credits the till before the call runs
BuyRound(cross(cur))
uassert.Equal(t, 1, Rounds(ash))
uassert.False(t, captures.Captured(ash))
}