Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(examples): add disperse #1414

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/gno.land/r/demo/disperse/disperse.gno
Original file line number Diff line number Diff line change
@@ -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}}

}
13 changes: 13 additions & 0 deletions examples/gno.land/r/demo/disperse/errors.gno
Original file line number Diff line number Diff line change
@@ -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")
)
1 change: 1 addition & 0 deletions examples/gno.land/r/demo/disperse/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/r/demo/disperse
35 changes: 35 additions & 0 deletions examples/gno.land/r/demo/disperse/util.gno
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions gno.land/cmd/gnoland/testdata/disperse.txtar
Original file line number Diff line number Diff line change
@@ -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{}



}
Loading