Skip to content

Commit 86c1d37

Browse files
committed
feat: add disperse gnot and token
1 parent c9f5ed5 commit 86c1d37

File tree

5 files changed

+132
-30
lines changed

5 files changed

+132
-30
lines changed

examples/gno.land/r/demo/disperse/disperse.gno

+66-18
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,31 @@ package disperse
22

33
import (
44
"std"
5+
6+
tokens "gno.land/r/demo/grc20factory"
57
)
68

7-
// SendSingleCoin parses receivers and amounts and sends out a single coin
8-
func SendSingleCoin(addresses []std.Address, coins std.Coins) {
9+
// DisperseGnot parses receivers and amounts and sends out gnot
10+
func DisperseGnot(addresses []std.Address, coins std.Coins) {
911
coinSent := std.GetOrigSend() // get Coins sent with call
1012
caller := std.GetOrigCaller() // get tx sender
1113

12-
if len(coinSent) != len(coins) {
13-
panic(ErrArgLenAndSentLenMismatch)
14-
}
15-
16-
if len(addresses) != len(coins) {
17-
panic(errNumAddrValMismatch)
18-
}
19-
20-
for _, coin := range coins {
21-
if coins.AmountOf(coin.Denom) != coinSent.AmountOf(coin.Denom) {
22-
panic(ErrWrongAmount)
23-
}
24-
}
25-
2614
// Get address of Disperse realm
2715
realmAddr := std.CurrentRealm().Addr()
2816

2917
// Get Banker
3018
banker := std.GetBanker(std.BankerTypeOrigSend)
3119

20+
var totalAmount int64
21+
for _, coin := range coins {
22+
totalAmount += coin.Amount
23+
}
24+
25+
// Check if total amount of coins sent is bigger or equal to total amount of coins to be sent
26+
if totalAmount > coinSent.AmountOf("ugnot") {
27+
panic(ErrWrongAmount)
28+
}
29+
3230
// Send coins
3331
for i, _ := range addresses {
3432
banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]})
@@ -44,8 +42,58 @@ func SendSingleCoin(addresses []std.Address, coins std.Coins) {
4442
}
4543
}
4644

47-
func main() {
45+
func DisperseToken(addresses []std.Address, coins std.Coins) {
46+
caller := std.GetOrigCaller() // get tx sender
47+
48+
var totalAmount uint64
49+
for _, coin := range coins {
50+
totalAmount += uint64(coin.Amount)
51+
}
4852

49-
toSend := std.Coins{{"ugnot", 1000}}
53+
tokens.TransferFrom(coins[0].Denom, caller, std.CurrentRealm().Addr(), totalAmount)
54+
for i, address := range addresses {
55+
for y, coin := range coins {
56+
if y == i {
57+
tokens.Transfer(coin.Denom, address, uint64(coin.Amount))
58+
}
59+
}
60+
}
61+
}
62+
63+
func DisperseTokenString(symbol string, addresses string, amounts string) {
64+
parsedAddresses, err := parseAddresses(addresses)
65+
if err != nil {
66+
panic(err)
67+
}
68+
69+
parsedAmounts, err := parseAmounts(amounts)
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
coins := make(std.Coins, len(parsedAmounts))
75+
for i, amount := range parsedAmounts {
76+
coins[i] = std.Coin{symbol, amount}
77+
}
78+
79+
DisperseToken(parsedAddresses, coins)
80+
}
81+
82+
func DisperseGnotString(addresses string, amounts string) {
83+
parsedAddresses, err := parseAddresses(addresses)
84+
if err != nil {
85+
panic(err)
86+
}
87+
88+
parsedAmounts, err := parseAmounts(amounts)
89+
if err != nil {
90+
panic(err)
91+
}
92+
93+
coins := make(std.Coins, len(parsedAmounts))
94+
for i, amount := range parsedAmounts {
95+
coins[i] = std.Coin{"ugnot", amount}
96+
}
5097

98+
DisperseGnot(parsedAddresses, coins)
5199
}

examples/gno.land/r/demo/disperse/errors.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ var (
1010
errBalanceNotZero = errors.New("balance needs to be equal to zero")
1111
ErrArgLenAndSentLenMismatch = errors.New("mismatch between coins sent and args called")
1212
ErrWrongAmount = errors.New("wrong coin amount")
13-
)
13+
)
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
module gno.land/r/demo/disperse
2+
3+
require (
4+
"gno.land/p/demo/ufmt" v0.0.0-latest
5+
"gno.land/r/demo/grc20factory" v0.0.0-latest
6+
)

examples/gno.land/r/demo/disperse/util.gno

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ func parseAmounts(amounts string) ([]int64, error) {
3232
}
3333

3434
return ret, nil
35-
}
35+
}
+59-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,70 @@
1+
# Test for disperse
2+
3+
loadpkg gno.land/r/demo/disperse
4+
loadpkg gno.land/r/demo/grc20factory
5+
16
gnoland start
27

3-
gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script.gno
8+
### Execute disperse gnot
9+
10+
# Verifying default balance
11+
gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
12+
stdout 'height: 0'
13+
stdout 'data: "9999990000000ugnot"'
14+
15+
# Executing disperse gnot
16+
gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseGnotString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "200ugnot" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150,50" test1
17+
18+
# Verifying balances after disperse
19+
20+
gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
21+
stdout 'height: 0'
22+
stdout 'data: "9999988999800ugnot"'
23+
24+
gnokey query bank/balances/g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0
25+
stdout 'height: 0'
26+
stdout 'data: "150ugnot"'
27+
28+
gnokey query bank/balances/g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c
29+
stdout 'height: 0'
30+
stdout 'data: "50ugnot"'
31+
32+
### Execute disperse gnot (leftover gnot)
33+
34+
# Execute disperse with 'send' of 200ugnot but total of 199ugnot
35+
gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseGnotString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "200ugnot" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0" -args "199" test1
36+
37+
# Verify balance after disperse
38+
gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5
39+
stdout 'height: 0'
40+
stdout 'data: "9999987999601ugnot"'
441

5-
-- script.gno --
6-
package main
42+
### Execute disperse gnot (invalid coins)
743

8-
import (
9-
"gno.land/r/demo/disperse"
10-
"std"
11-
)
44+
# Execute disperse with 'send' of 200ugnot but total of 201ugnot
45+
! gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseGnotString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "200ugnot" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0" -args "201" test1
1246

13-
func main() {
47+
### Execute disperse token
1448

49+
# Create new token
50+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "New" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "test" -args "TEST" -args 4 -args 1000000000 -args 0 test1
51+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Mint" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args 1000000000 test1
1552

53+
# Verify balance in new token after mint
54+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1
55+
stdout '(2000000000 uint64)'
1656

17-
toSend := std.Coins{}
57+
# Approve allowance to smart contract
58+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Approve" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1yryw6qs8h9anvguu4dfdc0u7zh4gvv8vqf59sj" -args "200" test1
59+
# Execute disperse token
60+
gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150,50" test1
1861

62+
# Verify balances after disperse
63+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1
64+
stdout '(1999999800 uint64)'
1965

66+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0" test1
67+
stdout '(150 uint64)'
2068

21-
}
69+
gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST" -args "g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" test1
70+
stdout '(50 uint64)'

0 commit comments

Comments
 (0)