diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno new file mode 100644 index 00000000000..09624862d86 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -0,0 +1,51 @@ +package disperse + +import ( + "std" +) + +// SendSingleCoin parses receivers and amounts and sends out a single coin +func SendSingleCoin(addresses []std.Address, coins std.Coins) { + coinSent := std.GetOrigSend() // get Coins sent with call + caller := std.GetOrigCaller() // get tx sender + + if len(coinSent) != len(coins) { + panic(ErrArgLenAndSentLenMismatch) + } + + if len(addresses) != len(coins) { + panic(errNumAddrValMismatch) + } + + for _, coin := range coins { + if coins.AmountOf(coin.Denom) != coinSent.AmountOf(coin.Denom) { + panic(ErrWrongAmount) + } + } + + // Get address of Disperse realm + realmAddr := std.CurrentRealm().Addr() + + // Get Banker + banker := std.GetBanker(std.BankerTypeOrigSend) + + // Send coins + for i, _ := range addresses { + banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]}) + } + + // Return possible leftover coins + for _, coin := range coinSent { + leftoverAmt := banker.GetCoins(realmAddr).AmountOf(coin.Denom) + if leftoverAmt > 0 { + send := std.Coins{{coin.Denom, leftoverAmt}} + banker.SendCoins(realmAddr, caller, send) + } + } +} + +func main() { + + toSend := std.Coins{{"ugnot", 1000}} + +} diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno new file mode 100644 index 00000000000..a6f3ada91c6 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -0,0 +1,13 @@ +package disperse + +import "errors" + +var ( + errNotEnoughCoin = errors.New("not enough coin sent in") + errNumAddrValMismatch = errors.New("number of addresses and values to send doesn't match") + errInvalidAddress = errors.New("invalid address") + errNegativeCoinAmount = errors.New("coin amount cannot be negative") + errBalanceNotZero = errors.New("balance needs to be equal to zero") + ErrArgLenAndSentLenMismatch = errors.New("mismatch between coins sent and args called") + ErrWrongAmount = errors.New("wrong coin amount") +) diff --git a/examples/gno.land/r/demo/disperse/gno.mod b/examples/gno.land/r/demo/disperse/gno.mod new file mode 100644 index 00000000000..06e81884dfa --- /dev/null +++ b/examples/gno.land/r/demo/disperse/gno.mod @@ -0,0 +1 @@ +module gno.land/r/demo/disperse diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno new file mode 100644 index 00000000000..6ed265890d2 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -0,0 +1,35 @@ +package disperse + +import ( + "std" + "strconv" + "strings" +) + +func parseAddresses(addresses string) ([]std.Address, error) { + var ret []std.Address + + for _, str := range strings.Split(addresses, ",") { + addr := std.Address(str) + if !addr.IsValid() { + return nil, errInvalidAddress + } + ret = append(ret, addr) + } + + return ret, nil +} + +func parseAmounts(amounts string) ([]int64, error) { + var ret []int64 + + for _, amt := range strings.Split(amounts, ",") { + amount, _ := strconv.Atoi(amt) + if amount < 0 { + return nil, errNegativeCoinAmount + } + ret = append(ret, int64(amount)) + } + + return ret, nil +} diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar new file mode 100644 index 00000000000..435952cfec3 --- /dev/null +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -0,0 +1,21 @@ +gnoland start + +gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script.gno + +-- script.gno -- +package main + +import ( + "gno.land/r/demo/disperse" + "std" +) + +func main() { + + + + toSend := std.Coins{} + + + +}