Playground
crowdfund.gno
// Package crowdfund is an accounting-only port of the classic Solidity
// Kickstarter-style crowdfunding contract to a gno.land realm.
//
// A creator Launches a campaign with a funding goal and a duration (in blocks).
// Backers Pledge (and may Unpledge before the deadline). After the deadline the
// creator can Claim if the goal was met, otherwise backers can Refund.
//
// No real coin moves: pledges are tracked as plain uint64 accounting, exactly
// like the mapping(address => uint) balances of the Solidity original.
package crowdfund
import (
"strconv"
"chain"
"chain/runtime"
"chain/runtime/unsafe"
"gno.land/p/nym-moultest123/kit/store/v0"
"gno.land/p/nt/avl/v0"
)
// Campaign holds the state of a single crowdfunding campaign. It carries no ID
// field: the id belongs to the store, which hands it back on lookup and
// iteration.
type Campaign struct {
Creator address
Goal uint64
Deadline int64 // block height after which the campaign is closed
Pledged uint64 // running total pledged
Claimed bool // creator already claimed the funds
pledges *avl.Tree // backer address (string) -> pledged uint64
}
// campaigns assigns the campaign ids. v0 kept its own nextID plus a key() that
// zero-padded to width 12, which stopped ordering Render past 10^12, and its
// own mustGet whose panic did not say which campaign was missing.
var campaigns = store.Named("campaign")
func mustGet(id int) *Campaign {
return campaigns.MustGet(store.ID(id)).(*Campaign)
}
// Launch creates a new campaign owned by the caller and returns its id.
func Launch(cur realm, goal uint64, durationBlocks int) int {
if goal == 0 {
panic("goal must be > 0")
}
if durationBlocks <= 0 {
panic("duration must be > 0")
}
caller := unsafe.PreviousRealm().Address()
id := campaigns.Add(&Campaign{
Creator: caller,
Goal: goal,
Deadline: runtime.ChainHeight() + int64(durationBlocks),
pledges: avl.NewTree(),
})
chain.Emit("Launch",
"id", id.String(),
"creator", caller.String(),
"goal", strconv.FormatUint(goal, 10))
return int(id)
}
// Pledge backs campaign id with amount (before the deadline).
func Pledge(cur realm, id int, amount uint64) {
if amount == 0 {
panic("amount must be > 0")
}
c := mustGet(id)
if runtime.ChainHeight() > c.Deadline {
panic("campaign ended")
}
caller := unsafe.PreviousRealm().Address()
prev := uint64(0)
if v := c.pledges.Get(caller.String()); v != nil {
prev = v.(uint64)
}
c.pledges.Set(caller.String(), prev+amount)
c.Pledged += amount
chain.Emit("Pledge",
"id", strconv.Itoa(id),
"from", caller.String(),
"amount", strconv.FormatUint(amount, 10))
}
// Unpledge withdraws amount from the caller's pledge (before the deadline).
func Unpledge(cur realm, id int, amount uint64) {
if amount == 0 {
panic("amount must be > 0")
}
c := mustGet(id)
if runtime.ChainHeight() > c.Deadline {
panic("campaign ended")
}
caller := unsafe.PreviousRealm().Address()
v := c.pledges.Get(caller.String())
if v == nil {
panic("nothing pledged")
}
have := v.(uint64)
if amount > have {
panic("amount exceeds pledge")
}
if remaining := have - amount; remaining == 0 {
c.pledges.Remove(caller.String())
} else {
c.pledges.Set(caller.String(), remaining)
}
c.Pledged -= amount
chain.Emit("Unpledge",
"id", strconv.Itoa(id),
"from", caller.String(),
"amount", strconv.FormatUint(amount, 10))
}
// Claim lets the creator take the funds if the goal was met after the deadline.
func Claim(cur realm, id int) {
c := mustGet(id)
if runtime.ChainHeight() <= c.Deadline {
panic("campaign not ended")
}
caller := unsafe.PreviousRealm().Address()
if caller != c.Creator {
panic("only creator can claim")
}
if c.Pledged < c.Goal {
panic("goal not reached")
}
if c.Claimed {
panic("already claimed")
}
c.Claimed = true
chain.Emit("Claim",
"id", strconv.Itoa(id),
"amount", strconv.FormatUint(c.Pledged, 10))
}
// Refund returns the caller's pledge if the campaign failed after the deadline.
func Refund(cur realm, id int) {
c := mustGet(id)
if runtime.ChainHeight() <= c.Deadline {
panic("campaign not ended")
}
if c.Pledged >= c.Goal {
panic("campaign succeeded, no refund")
}
caller := unsafe.PreviousRealm().Address()
v := c.pledges.Get(caller.String())
if v == nil {
panic("nothing to refund")
}
amount := v.(uint64)
c.pledges.Remove(caller.String())
c.Pledged -= amount
chain.Emit("Refund",
"id", strconv.Itoa(id),
"to", caller.String(),
"amount", strconv.FormatUint(amount, 10))
}
// --- pure/read-only helpers (also used by Render) ---
// pct returns the funded percentage (0..100).
func pct(pledged, goal uint64) int {
if goal == 0 {
return 0
}
p := int(pledged * 100 / goal)
if p > 100 {
p = 100
}
return p
}
// progressBar renders a fixed-width ▓░ bar for pledged/goal.
func progressBar(pledged, goal uint64) string {
const width = 20
filled := 0
if goal > 0 {
filled = int(pledged * width / goal)
if filled > width {
filled = width
}
}
bar := ""
for i := 0; i < width; i++ {
if i < filled {
bar += "▓"
} else {
bar += "░"
}
}
return bar
}
// state describes a campaign relative to a given block height.
func state(c *Campaign, height int64) string {
if height <= c.Deadline {
return "Active"
}
if c.Pledged >= c.Goal {
if c.Claimed {
return "Funded (claimed)"
}
return "Funded"
}
return "Failed"
}
// Render shows all campaigns with a progress bar, goal, pledged and state.
func Render(path string) string {
if campaigns.Len() == 0 {
return "# Crowdfund\n\nNo campaigns yet. Call `Launch(goal, durationBlocks)` to start one.\n"
}
height := runtime.ChainHeight()
out := "# Crowdfund\n\n"
out += "Current block height: **" + strconv.FormatInt(height, 10) + "**\n\n"
campaigns.Each(func(id store.ID, v any) {
c := v.(*Campaign)
out += "## Campaign #" + id.String() + "\n\n"
out += "`" + progressBar(c.Pledged, c.Goal) + "` " + strconv.Itoa(pct(c.Pledged, c.Goal)) + "%\n\n"
out += "- State: **" + state(c, height) + "**\n"
out += "- Goal: " + strconv.FormatUint(c.Goal, 10) + "\n"
out += "- Pledged: " + strconv.FormatUint(c.Pledged, 10) + "\n"
out += "- Deadline: block " + strconv.FormatInt(c.Deadline, 10) + "\n"
out += "- Creator: `" + c.Creator.String() + "`\n\n"
})
return out
}
crowdfund_test.gno
package crowdfund
import (
"testing"
"gno.land/p/nym-moultest123/kit/store/v0"
"gno.land/p/nt/uassert/v0"
)
func TestProgressBarAndPct(t *testing.T) {
uassert.Equal(t, "░░░░░░░░░░░░░░░░░░░░", progressBar(0, 100))
uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░", progressBar(50, 100))
uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓", progressBar(100, 100))
// over-funded is capped at full width
uassert.Equal(t, "▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓", progressBar(250, 100))
// zero goal never divides by zero
uassert.Equal(t, "░░░░░░░░░░░░░░░░░░░░", progressBar(10, 0))
uassert.Equal(t, 0, pct(0, 100))
uassert.Equal(t, 25, pct(25, 100))
uassert.Equal(t, 100, pct(100, 100))
uassert.Equal(t, 100, pct(500, 100)) // capped
uassert.Equal(t, 0, pct(5, 0)) // no goal
}
func TestState(t *testing.T) {
active := &Campaign{Goal: 100, Pledged: 10, Deadline: 100}
uassert.Equal(t, "Active", state(active, 50))
uassert.Equal(t, "Active", state(active, 100)) // deadline block still active
funded := &Campaign{Goal: 100, Pledged: 150, Deadline: 100}
uassert.Equal(t, "Funded", state(funded, 101))
claimed := &Campaign{Goal: 100, Pledged: 150, Deadline: 100, Claimed: true}
uassert.Equal(t, "Funded (claimed)", state(claimed, 101))
failed := &Campaign{Goal: 100, Pledged: 50, Deadline: 100}
uassert.Equal(t, "Failed", state(failed, 101))
// exactly meeting the goal counts as funded
exact := &Campaign{Goal: 100, Pledged: 100, Deadline: 100}
uassert.Equal(t, "Funded", state(exact, 200))
}
// v0 asserted that its own key() padded to width 12. That rule held only below
// the width: key(10^12) is 13 characters and sorts before key(10^12-1), so the
// campaign list would have rendered out of order from that id on. The store key
// has no width to outgrow.
func TestKeyOrdering(t *testing.T) {
uassert.True(t, store.ID(2).Key() < store.ID(10).Key())
uassert.True(t, store.ID(9).Key() < store.ID(100).Key())
uassert.True(t, store.ID(999999999999).Key() < store.ID(1000000000000).Key(),
"and it keeps holding one past v0's width-12 ceiling")
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/crowdfund/v1" gno = "0.9" private = true [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186410 max_deposit = "6000000ugnot"
render_example_test.gno
package crowdfund
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Crowdfund
//
// No campaigns yet. Call `Launch(goal, durationBlocks)` to start one.
}