Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

disperse.gno

4.81 Kb · 132 lines
  1// Code generated by smelt (Solidity -> Gno) from example-sol/cryptobatch.disperse.sol
  2// and linked to the shared on-chain runtime gno.land/p/nym-smelt001/solrt.
  3package disperse
  4
  5import (
  6	"chain"
  7	"chain/banker"
  8	"chain/runtime/unsafe"
  9	"strconv"
 10	"strings"
 11
 12	"gno.land/p/moul/txlink/v0"
 13	"gno.land/p/nt/ufmt/v0"
 14	"gno.land/p/nym-smelt001/solrt"
 15)
 16
 17var (
 18	totalBatches    uint64
 19	totalRecipients uint64
 20	totalDispersed  solrt.U256
 21)
 22
 23func init() {
 24	totalDispersed = solrt.U256Zero()
 25}
 26
 27// DisperseNative sends amounts[i] ugnot to recipients[i] in a single atomic transaction.
 28// Attached ugnot via banker OriginSend must exactly equal the sum of amounts.
 29func DisperseNative(cur realm, recipients []address, amounts []solrt.U256) {
 30	solrt.Require(len(recipients) == len(amounts), "LengthMismatch")
 31	solrt.Require(len(recipients) > 0, "EmptyBatch")
 32
 33	total := solrt.U256Zero()
 34	b := banker.NewBanker(banker.BankerTypeOriginSend, cur)
 35	originSent := unsafe.OriginSend().AmountOf("ugnot")
 36	sent := solrt.U256FromU64(uint64(originSent))
 37
 38	for i := 0; i < len(recipients); i++ {
 39		total = total.Add(amounts[i])
 40	}
 41	solrt.Require(total.Eq(sent), "ValueMismatch")
 42
 43	realmAddr := cur.Address()
 44	for i := 0; i < len(recipients); i++ {
 45		amt := int64(amounts[i].Uint64())
 46		solrt.Require(amt > 0, "ZeroAmount")
 47		b.SendCoins(realmAddr, recipients[i], chain.NewCoins(chain.NewCoin("ugnot", amt)))
 48	}
 49
 50	recordBatch(uint64(len(recipients)), total)
 51}
 52
 53// DisperseNativeSimple sends the same ugnot amount to each recipient atomically.
 54func DisperseNativeSimple(cur realm, recipients []address, amount solrt.U256) {
 55	solrt.Require(len(recipients) > 0, "EmptyBatch")
 56
 57	total := amount.Mul(solrt.U256FromU64(uint64(len(recipients))))
 58	b := banker.NewBanker(banker.BankerTypeOriginSend, cur)
 59	originSent := unsafe.OriginSend().AmountOf("ugnot")
 60	sent := solrt.U256FromU64(uint64(originSent))
 61	solrt.Require(total.Eq(sent), "ValueMismatch")
 62
 63	amt := int64(amount.Uint64())
 64	solrt.Require(amt > 0, "ZeroAmount")
 65
 66	realmAddr := cur.Address()
 67	for i := 0; i < len(recipients); i++ {
 68		b.SendCoins(realmAddr, recipients[i], chain.NewCoins(chain.NewCoin("ugnot", amt)))
 69	}
 70
 71	recordBatch(uint64(len(recipients)), total)
 72}
 73
 74// DisperseCSV is a gnoweb / Adena-friendly entrypoint accepting comma-separated
 75// Bech32 recipient addresses and equal ugnot amount per recipient.
 76func DisperseCSV(cur realm, recipientsCSV string, amountEach int64) {
 77	solrt.Require(amountEach > 0, "ZeroAmount")
 78	parts := strings.Split(recipientsCSV, ",")
 79	var recipients []address
 80	for _, raw := range parts {
 81		trimmed := strings.TrimSpace(raw)
 82		if trimmed == "" {
 83			continue
 84		}
 85		addr := address(trimmed)
 86		solrt.Require(addr.IsValid(), "InvalidAddress")
 87		recipients = append(recipients, addr)
 88	}
 89	DisperseNativeSimple(cur, recipients, solrt.U256FromU64(uint64(amountEach)))
 90}
 91
 92// Stats returns lifetime batch transfer metrics.
 93func Stats() (batches uint64, recipients uint64, ugnotTotal string) {
 94	return totalBatches, totalRecipients, totalDispersed.String()
 95}
 96
 97func recordBatch(recipientCount uint64, amount solrt.U256) {
 98	totalBatches++
 99	totalRecipients += recipientCount
100	totalDispersed = totalDispersed.Add(amount)
101	chain.Emit(
102		"Dispersed",
103		"batch", strconv.FormatUint(totalBatches, 10),
104		"recipients", strconv.FormatUint(recipientCount, 10),
105		"ugnot", amount.String(),
106	)
107}
108
109// Render exposes a live Markdown dashboard in gnoweb with 1-click Adena txlinks.
110func Render(path string) string {
111	csvURL := txlink.NewLink("DisperseCSV").
112		AddArgs("recipientsCSV", "", "amountEach", "1000").
113		SetSend("2000ugnot").
114		URL()
115
116	var b strings.Builder
117	b.WriteString("# Smelt BatchDisperse\n\n")
118	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")
119
120	b.WriteString("| Batches Executed | Total Recipients | Total Dispersed (ugnot) |\n")
121	b.WriteString("| :---: | :---: | :---: |\n")
122	b.WriteString(ufmt.Sprintf("| **%d** | **%d** | **%s** |\n\n", totalBatches, totalRecipients, totalDispersed.String()))
123
124	b.WriteString(ufmt.Sprintf("**Try it on Testnet:** [Send Equal Batch via Adena (`DisperseCSV`)](%s)\n\n", csvURL))
125	b.WriteString("### Entrypoints\n")
126	b.WriteString("- `DisperseCSV(cur, recipientsCSV, amountEach)` — send equal `ugnot` to comma-separated `g1...` addresses (attach `amountEach * N` `ugnot`).\n")
127	b.WriteString("- `DisperseNative(cur, []address, []solrt.U256)` — exact Solidity signature with per-recipient `U256` amounts.\n")
128	b.WriteString("- `DisperseNativeSimple(cur, []address, solrt.U256)` — exact Solidity signature with uniform `U256` amount.\n\n")
129	b.WriteString("---\n\n")
130	b.WriteString("[← Browse Smelt PortBook](/r/nym-smelt001/portbook) · [Inspect `solrt` Package](/p/nym-smelt001/solrt)\n")
131	return b.String()
132}