Playground
disperse.gno
// Code generated by smelt (Solidity -> Gno) from example-sol/cryptobatch.disperse.sol
// and linked to the shared on-chain runtime gno.land/p/nym-smelt001/solrt.
package disperse
import (
"chain"
"chain/banker"
"chain/runtime/unsafe"
"strconv"
"strings"
"gno.land/p/moul/txlink/v0"
"gno.land/p/nt/ufmt/v0"
"gno.land/p/nym-smelt001/solrt"
)
var (
totalBatches uint64
totalRecipients uint64
totalDispersed solrt.U256
)
func init() {
totalDispersed = solrt.U256Zero()
}
// DisperseNative sends amounts[i] ugnot to recipients[i] in a single atomic transaction.
// Attached ugnot via banker OriginSend must exactly equal the sum of amounts.
func DisperseNative(cur realm, recipients []address, amounts []solrt.U256) {
solrt.Require(len(recipients) == len(amounts), "LengthMismatch")
solrt.Require(len(recipients) > 0, "EmptyBatch")
total := solrt.U256Zero()
b := banker.NewBanker(banker.BankerTypeOriginSend, cur)
originSent := unsafe.OriginSend().AmountOf("ugnot")
sent := solrt.U256FromU64(uint64(originSent))
for i := 0; i < len(recipients); i++ {
total = total.Add(amounts[i])
}
solrt.Require(total.Eq(sent), "ValueMismatch")
realmAddr := cur.Address()
for i := 0; i < len(recipients); i++ {
amt := int64(amounts[i].Uint64())
solrt.Require(amt > 0, "ZeroAmount")
b.SendCoins(realmAddr, recipients[i], chain.NewCoins(chain.NewCoin("ugnot", amt)))
}
recordBatch(uint64(len(recipients)), total)
}
// DisperseNativeSimple sends the same ugnot amount to each recipient atomically.
func DisperseNativeSimple(cur realm, recipients []address, amount solrt.U256) {
solrt.Require(len(recipients) > 0, "EmptyBatch")
total := amount.Mul(solrt.U256FromU64(uint64(len(recipients))))
b := banker.NewBanker(banker.BankerTypeOriginSend, cur)
originSent := unsafe.OriginSend().AmountOf("ugnot")
sent := solrt.U256FromU64(uint64(originSent))
solrt.Require(total.Eq(sent), "ValueMismatch")
amt := int64(amount.Uint64())
solrt.Require(amt > 0, "ZeroAmount")
realmAddr := cur.Address()
for i := 0; i < len(recipients); i++ {
b.SendCoins(realmAddr, recipients[i], chain.NewCoins(chain.NewCoin("ugnot", amt)))
}
recordBatch(uint64(len(recipients)), total)
}
// DisperseCSV is a gnoweb / Adena-friendly entrypoint accepting comma-separated
// Bech32 recipient addresses and equal ugnot amount per recipient.
func DisperseCSV(cur realm, recipientsCSV string, amountEach int64) {
solrt.Require(amountEach > 0, "ZeroAmount")
parts := strings.Split(recipientsCSV, ",")
var recipients []address
for _, raw := range parts {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
continue
}
addr := address(trimmed)
solrt.Require(addr.IsValid(), "InvalidAddress")
recipients = append(recipients, addr)
}
DisperseNativeSimple(cur, recipients, solrt.U256FromU64(uint64(amountEach)))
}
// Stats returns lifetime batch transfer metrics.
func Stats() (batches uint64, recipients uint64, ugnotTotal string) {
return totalBatches, totalRecipients, totalDispersed.String()
}
func recordBatch(recipientCount uint64, amount solrt.U256) {
totalBatches++
totalRecipients += recipientCount
totalDispersed = totalDispersed.Add(amount)
chain.Emit(
"Dispersed",
"batch", strconv.FormatUint(totalBatches, 10),
"recipients", strconv.FormatUint(recipientCount, 10),
"ugnot", amount.String(),
)
}
// Render exposes a live Markdown dashboard in gnoweb with 1-click Adena txlinks.
func Render(path string) string {
csvURL := txlink.NewLink("DisperseCSV").
AddArgs("recipientsCSV", "", "amountEach", "1000").
SetSend("2000ugnot").
URL()
var b strings.Builder
b.WriteString("# Smelt BatchDisperse\n\n")
b.WriteString("Atomic multi-recipient `ugnot` sender — transpiled from Solidity (`BatchDisperse.sol`) with **[Smelt](https://smeltcli.xyz)** and powered by the shared on-chain [`gno.land/p/nym-smelt001/solrt`](/p/nym-smelt001/solrt) `U256` runtime.\n\n")
b.WriteString("| Batches Executed | Total Recipients | Total Dispersed (ugnot) |\n")
b.WriteString("| :---: | :---: | :---: |\n")
b.WriteString(ufmt.Sprintf("| **%d** | **%d** | **%s** |\n\n", totalBatches, totalRecipients, totalDispersed.String()))
b.WriteString(ufmt.Sprintf("**Try it on Testnet:** [Send Equal Batch via Adena (`DisperseCSV`)](%s)\n\n", csvURL))
b.WriteString("### Entrypoints\n")
b.WriteString("- `DisperseCSV(cur, recipientsCSV, amountEach)` — send equal `ugnot` to comma-separated `g1...` addresses (attach `amountEach * N` `ugnot`).\n")
b.WriteString("- `DisperseNative(cur, []address, []solrt.U256)` — exact Solidity signature with per-recipient `U256` amounts.\n")
b.WriteString("- `DisperseNativeSimple(cur, []address, solrt.U256)` — exact Solidity signature with uniform `U256` amount.\n\n")
b.WriteString("---\n\n")
b.WriteString("[← Browse Smelt PortBook](/r/nym-smelt001/portbook) · [Inspect `solrt` Package](/p/nym-smelt001/solrt)\n")
return b.String()
}
disperse_test.gno
package disperse
import (
"chain"
"chain/banker"
"strings"
"testing"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nym-smelt001/solrt"
)
func TestDisperseCSV(cur realm, t *testing.T) {
sender := testutils.TestAddress("sender_csv")
alice := testutils.TestAddress("alice_csv")
bob := testutils.TestAddress("bob_csv")
testing.SetRealm(testing.NewUserRealm(sender))
testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 2000)))
realmAddr := chain.PackageAddress("gno.land/r/nym-smelt001/disperse")
testing.IssueCoins(realmAddr, chain.NewCoins(chain.NewCoin("ugnot", 2000)))
DisperseCSV(cross(cur), string(alice)+", "+string(bob), 1000)
ro := banker.NewReadonlyBanker()
if got := ro.GetCoin(alice, "ugnot"); got != 1000 {
t.Fatalf("alice expected 1000 ugnot, got %d", got)
}
if got := ro.GetCoin(bob, "ugnot"); got != 1000 {
t.Fatalf("bob expected 1000 ugnot, got %d", got)
}
rendered := Render("")
if !strings.Contains(rendered, "Smelt BatchDisperse") || !strings.Contains(rendered, "DisperseCSV") {
t.Fatalf("unexpected Render output:\n%s", rendered)
}
}
func TestDisperseNativeU256(cur realm, t *testing.T) {
sender := testutils.TestAddress("sender_native")
alice := testutils.TestAddress("alice_native")
bob := testutils.TestAddress("bob_native")
testing.SetRealm(testing.NewUserRealm(sender))
testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 3000)))
realmAddr := chain.PackageAddress("gno.land/r/nym-smelt001/disperse")
testing.IssueCoins(realmAddr, chain.NewCoins(chain.NewCoin("ugnot", 3000)))
DisperseNative(
cross(cur),
[]address{alice, bob},
[]solrt.U256{solrt.U256FromU64(1200), solrt.U256FromU64(1800)},
)
ro := banker.NewReadonlyBanker()
if got := ro.GetCoin(alice, "ugnot"); got != 1200 {
t.Fatalf("alice expected 1200 ugnot, got %d", got)
}
if got := ro.GetCoin(bob, "ugnot"); got != 1800 {
t.Fatalf("bob expected 1800 ugnot, got %d", got)
}
batches, recipients, _ := Stats()
if batches == 0 || recipients < 2 {
t.Fatalf("expected updated stats, got batches=%d recipients=%d", batches, recipients)
}
}
func TestDisperseRejectsValueMismatch(cur realm, t *testing.T) {
sender := testutils.TestAddress("sender_bad")
alice := testutils.TestAddress("alice_bad")
testing.SetRealm(testing.NewUserRealm(sender))
testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 500)))
r := revive(func() {
DisperseNative(cross(cur), []address{alice}, []solrt.U256{solrt.U256FromU64(1000)})
})
if r == nil {
t.Fatalf("expected ValueMismatch panic when attached ugnot != sum")
}
}
gnomod.toml
module = "gno.land/r/nym-smelt001/disperse" gno = "0.9" [addpkg] creator = "g1klc60zh8m3a7v688qt0m5lessx28fttxm0s0y9" height = 1501182 max_deposit = "20000000ugnot"