From b427d46eb6b9e67d01b336c0a154ba19a1df84ea Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 28 Nov 2023 19:05:29 +0100 Subject: [PATCH 01/46] wip disperse --- examples/gno.land/r/demo/disperse/disperse.gno | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 examples/gno.land/r/demo/disperse/disperse.gno 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..59ba3fa219b --- /dev/null +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -0,0 +1,10 @@ +package disperse + +import "std" + +func SendNative(addresses []std.Address, values []uint64) { + + for _, receiver := range addresses { + + } +} From 0c80d55b8a11ad9331ad8e31867a4d5a9fcb0c55 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 5 Dec 2023 14:33:22 +0100 Subject: [PATCH 02/46] add disperse PoC --- .../gno.land/r/demo/disperse/disperse.gno | 43 ++++++++++++++++++- examples/gno.land/r/demo/disperse/errors.gno | 9 ++++ examples/gno.land/r/demo/disperse/gno.mod | 1 + 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 examples/gno.land/r/demo/disperse/errors.gno create mode 100644 examples/gno.land/r/demo/disperse/gno.mod diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 59ba3fa219b..20045aebe36 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -2,9 +2,48 @@ package disperse import "std" -func SendNative(addresses []std.Address, values []uint64) { +func SendGNOT(addresses []std.Address, sendValues []int64) { + std.AssertOriginCall() // assert the call was by a user? + sent := std.GetOrigSend() // get GNOT value sent with call + caller := std.GetOrigCaller() // get tx caller - for _, receiver := range addresses { + var totalToSend int64 + for _, val := range sendValues { + totalToSend += val + } + + if sent.AmountOf("ugnot") < totalToSend { // check if user sent enough GNOT// possibly not needed if banker breaks anyway? + panic(ErrNotEnoughGnot) + } + + // Check if args are good + if len(addresses) != len(sendValues) { + panic(ErrNumAddrValMismatch) + } + + // Get address of Disperse realm + pkgaddr := std.GetOrigPkgAddr() + + // Get Banker + banker := std.GetBanker(std.BankerTypeOrigSend) + + for i, val := range sendValues { + send := std.Coins{{"ugnot", val}} + + receiver := addresses[i] + if !receiver.IsValid() { + panic(ErrInvalidAddress) + } + + // Send GNOT from realm to receiver address + banker.SendCoins(pkgaddr, receiver, send) + } + + // Return possible leftover GNOT + leftover := banker.GetCoins(std.GetOrigPkgAddr()).AmountOf("ugnot") + if leftover > 0 { + send := std.Coins{{"ugnot", leftover}} + banker.SendCoins(pkgaddr, caller, send) } } 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..ce17cce7ad6 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -0,0 +1,9 @@ +package disperse + +import "errors" + +var ( + ErrNotEnoughGnot = errors.New("not enough gnot sent in") + ErrNumAddrValMismatch = errors.New("number of addresses and values to send doesn't match") + ErrInvalidAddress = errors.New("invalid address") +) 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 From 405f690f9d08e56ea92e8b2f4823a3f28b3e69c4 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 5 Dec 2023 15:23:39 +0100 Subject: [PATCH 03/46] GNOT disperse PoC complete --- .../gno.land/r/demo/disperse/disperse.gno | 48 +++++++++++-------- examples/gno.land/r/demo/disperse/errors.gno | 1 + examples/gno.land/r/demo/disperse/util.gno | 37 ++++++++++++++ 3 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 examples/gno.land/r/demo/disperse/util.gno diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 20045aebe36..d194afd04de 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -2,41 +2,49 @@ package disperse import "std" -func SendGNOT(addresses []std.Address, sendValues []int64) { - std.AssertOriginCall() // assert the call was by a user? - sent := std.GetOrigSend() // get GNOT value sent with call - caller := std.GetOrigCaller() // get tx caller - - var totalToSend int64 - for _, val := range sendValues { - totalToSend += val +// SendGNOT parses receivers and amounts and sends out GNOT +// Currently sending slices as arguments with `maketx call` is unsupported +func SendGNOT(addresses string, sendAmounts string) { + std.AssertOriginCall() // assert the call was by a user? + coinsSent := std.GetOrigSend() // get GNOT value sent with call + caller := std.GetOrigCaller() // get tx caller + + parsedAddresses, err := parseAddresses(addresses) + if err != nil { + panic(err) } - if sent.AmountOf("ugnot") < totalToSend { // check if user sent enough GNOT// possibly not needed if banker breaks anyway? - panic(ErrNotEnoughGnot) + parsedAmounts, err := parseAmounts(sendAmounts) + if err != nil { + panic(err) } // Check if args are good - if len(addresses) != len(sendValues) { + if len(parsedAddresses) != len(parsedAmounts) { panic(ErrNumAddrValMismatch) } + var totalToSend int64 + for _, val := range parsedAmounts { + totalToSend += val + } + + if coinsSent.AmountOf("ugnot") < totalToSend { // check if user sent enough GNOT// possibly not needed if banker breaks anyway? + panic(ErrNotEnoughGnot) + } + // Get address of Disperse realm - pkgaddr := std.GetOrigPkgAddr() + realmAddr := std.GetOrigPkgAddr() // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) - for i, val := range sendValues { + for i, val := range parsedAmounts { send := std.Coins{{"ugnot", val}} - - receiver := addresses[i] - if !receiver.IsValid() { - panic(ErrInvalidAddress) - } + receiver := parsedAddresses[i] // Send GNOT from realm to receiver address - banker.SendCoins(pkgaddr, receiver, send) + banker.SendCoins(realmAddr, receiver, send) } // Return possible leftover GNOT @@ -44,6 +52,6 @@ func SendGNOT(addresses []std.Address, sendValues []int64) { if leftover > 0 { send := std.Coins{{"ugnot", leftover}} - banker.SendCoins(pkgaddr, caller, send) + banker.SendCoins(realmAddr, caller, send) } } diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index ce17cce7ad6..2908b4f2f55 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -6,4 +6,5 @@ var ( ErrNotEnoughGnot = errors.New("not enough gnot 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") ) 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..9f6614005f4 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -0,0 +1,37 @@ +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 +} From b0889c7cc49d087b6ea3d76408a170bd930ffc92 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 5 Dec 2023 15:41:54 +0100 Subject: [PATCH 04/46] comments, formatting --- examples/gno.land/r/demo/disperse/disperse.gno | 9 ++++++--- examples/gno.land/r/demo/disperse/util.gno | 4 +--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index d194afd04de..055e1dd375b 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -3,9 +3,10 @@ package disperse import "std" // SendGNOT parses receivers and amounts and sends out GNOT -// Currently sending slices as arguments with `maketx call` is unsupported +// Currently sending slices as arguments with `maketx call` is unsupported, +// which is why we parse strings by delimiter here func SendGNOT(addresses string, sendAmounts string) { - std.AssertOriginCall() // assert the call was by a user? + std.AssertOriginCall() // assert the call was by a user coinsSent := std.GetOrigSend() // get GNOT value sent with call caller := std.GetOrigCaller() // get tx caller @@ -29,7 +30,9 @@ func SendGNOT(addresses string, sendAmounts string) { totalToSend += val } - if coinsSent.AmountOf("ugnot") < totalToSend { // check if user sent enough GNOT// possibly not needed if banker breaks anyway? + // Check if user sent enough GNOT + // FIXME: possibly not needed, added for error clarity + if coinsSent.AmountOf("ugnot") < totalToSend { panic(ErrNotEnoughGnot) } diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index 9f6614005f4..5bded5bc83c 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -14,7 +14,6 @@ func parseAddresses(addresses string) ([]std.Address, error) { if !addr.IsValid() { return nil, ErrInvalidAddress } - ret = append(ret, addr) } @@ -23,13 +22,12 @@ func parseAddresses(addresses string) ([]std.Address, error) { 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)) } From 70354ef11a16fcec9b880ee471bf089554c2869b Mon Sep 17 00:00:00 2001 From: leohhhn Date: Sun, 10 Dec 2023 11:22:42 +0100 Subject: [PATCH 05/46] save --- .../gno.land/r/demo/disperse/disperse.gno | 36 ++++++++++-------- .../r/demo/disperse/disperse_test.gno | 38 +++++++++++++++++++ examples/gno.land/r/demo/disperse/errors.gno | 8 ++-- examples/gno.land/r/demo/disperse/util.gno | 4 +- 4 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 examples/gno.land/r/demo/disperse/disperse_test.gno diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 055e1dd375b..0894c85c878 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -1,15 +1,26 @@ package disperse -import "std" +import ( + "std" +) // SendGNOT parses receivers and amounts and sends out GNOT // Currently sending slices as arguments with `maketx call` is unsupported, // which is why we parse strings by delimiter here -func SendGNOT(addresses string, sendAmounts string) { +func SendGNOT(addresses, sendAmounts string) { std.AssertOriginCall() // assert the call was by a user coinsSent := std.GetOrigSend() // get GNOT value sent with call caller := std.GetOrigCaller() // get tx caller + // Get address of Disperse realm + realmAddr := std.GetOrigPkgAddr() + + // Get Banker + banker := std.GetBanker(std.BankerTypeOrigSend) + + leftover := banker.GetCoins(realmAddr).AmountOf("ugnot") + println(leftover) + parsedAddresses, err := parseAddresses(addresses) if err != nil { panic(err) @@ -22,7 +33,7 @@ func SendGNOT(addresses string, sendAmounts string) { // Check if args are good if len(parsedAddresses) != len(parsedAmounts) { - panic(ErrNumAddrValMismatch) + panic(errNumAddrValMismatch) } var totalToSend int64 @@ -33,15 +44,9 @@ func SendGNOT(addresses string, sendAmounts string) { // Check if user sent enough GNOT // FIXME: possibly not needed, added for error clarity if coinsSent.AmountOf("ugnot") < totalToSend { - panic(ErrNotEnoughGnot) + panic(errNotEnoughGnot) } - // Get address of Disperse realm - realmAddr := std.GetOrigPkgAddr() - - // Get Banker - banker := std.GetBanker(std.BankerTypeOrigSend) - for i, val := range parsedAmounts { send := std.Coins{{"ugnot", val}} receiver := parsedAddresses[i] @@ -51,10 +56,9 @@ func SendGNOT(addresses string, sendAmounts string) { } // Return possible leftover GNOT - leftover := banker.GetCoins(std.GetOrigPkgAddr()).AmountOf("ugnot") - - if leftover > 0 { - send := std.Coins{{"ugnot", leftover}} - banker.SendCoins(realmAddr, caller, send) - } + // + //if leftover > 0 { + // send := std.Coins{{"ugnot", leftover}} + // banker.SendCoins(realmAddr, caller, send) + //} } diff --git a/examples/gno.land/r/demo/disperse/disperse_test.gno b/examples/gno.land/r/demo/disperse/disperse_test.gno new file mode 100644 index 00000000000..d8e518e5f74 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/disperse_test.gno @@ -0,0 +1,38 @@ +package disperse + +import ( + "std" + "strings" + "testing" +) + +func TestSendCoin(t *testing.T) { + sender := std.Address("g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de") + receiverArr := []string{"g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa", "g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j"} + amountArr := []string{"100", "150"} + + // Mint test GNOT + alice := testutils.TestAddress("alice") + bob := testutils.TestAddress("bob") + // Set caller to sender + std.TestSetOrigCaller(sender) + + std.TestSetOrigSend(std.Coins{{"ugnot", 250}}, nil) + + // Get readonly banker to check balances + //banker := std.GetBanker(std.BankerTypeReadonly) + + addresses := strings.Join(receiverArr, ",") + amounts := strings.Join(amountArr, ",") + + //balSenderBefore := banker.GetCoins(sender) + SendGNOT(addresses, amounts) + + //balSenderAfter := banker.GetCoins(sender) + // + //println("balSenderBefore:") + //println(balSenderBefore) + //println("balSenderAfter:") + //println(balSenderAfter) + +} diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 2908b4f2f55..075dd632bdb 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,8 +3,8 @@ package disperse import "errors" var ( - ErrNotEnoughGnot = errors.New("not enough gnot 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") + errNotEnoughGnot = errors.New("not enough gnot 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") ) diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index 5bded5bc83c..6ed265890d2 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -12,7 +12,7 @@ func parseAddresses(addresses string) ([]std.Address, error) { for _, str := range strings.Split(addresses, ",") { addr := std.Address(str) if !addr.IsValid() { - return nil, ErrInvalidAddress + return nil, errInvalidAddress } ret = append(ret, addr) } @@ -26,7 +26,7 @@ func parseAmounts(amounts string) ([]int64, error) { for _, amt := range strings.Split(amounts, ",") { amount, _ := strconv.Atoi(amt) if amount < 0 { - return nil, ErrNegativeCoinAmount + return nil, errNegativeCoinAmount } ret = append(ret, int64(amount)) } From a5a5324405e8e50b554bf9fa315fbca070b3fcb2 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Tue, 12 Dec 2023 13:23:20 +0100 Subject: [PATCH 06/46] wip add tests --- .../gno.land/r/demo/disperse/disperse.gno | 40 ++++----- .../r/demo/disperse/disperse_test.gno | 81 +++++++++++++++---- examples/gno.land/r/demo/disperse/errors.gno | 2 + 3 files changed, 88 insertions(+), 35 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 0894c85c878..c6d8af988c2 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -4,13 +4,20 @@ import ( "std" ) -// SendGNOT parses receivers and amounts and sends out GNOT +// SendCoin parses receivers and amounts and sends out GNOT // Currently sending slices as arguments with `maketx call` is unsupported, // which is why we parse strings by delimiter here -func SendGNOT(addresses, sendAmounts string) { - std.AssertOriginCall() // assert the call was by a user - coinsSent := std.GetOrigSend() // get GNOT value sent with call - caller := std.GetOrigCaller() // get tx caller +func SendCoin(addresses, sendAmounts string) { + std.AssertOriginCall() // assert the call was by a user + coinSent := std.GetOrigSend() // get Coin value & denom sent with call + caller := std.GetOrigCaller() // get tx caller + + if len(coinSent) != 1 { + panic(errMultipleCoinsSent) + } + + // Extract denom from Coins + denom := coinSent[0].Denom // Get address of Disperse realm realmAddr := std.GetOrigPkgAddr() @@ -18,9 +25,6 @@ func SendGNOT(addresses, sendAmounts string) { // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) - leftover := banker.GetCoins(realmAddr).AmountOf("ugnot") - println(leftover) - parsedAddresses, err := parseAddresses(addresses) if err != nil { panic(err) @@ -31,7 +35,7 @@ func SendGNOT(addresses, sendAmounts string) { panic(err) } - // Check if args are good + // Check if arg lengths are good if len(parsedAddresses) != len(parsedAmounts) { panic(errNumAddrValMismatch) } @@ -42,23 +46,23 @@ func SendGNOT(addresses, sendAmounts string) { } // Check if user sent enough GNOT - // FIXME: possibly not needed, added for error clarity - if coinsSent.AmountOf("ugnot") < totalToSend { + // Possibly not needed, added for error clarity + if coinSent.AmountOf(denom) < totalToSend { panic(errNotEnoughGnot) } for i, val := range parsedAmounts { - send := std.Coins{{"ugnot", val}} + send := std.Coins{{denom, val}} receiver := parsedAddresses[i] - // Send GNOT from realm to receiver address + // Send coin from realm to receiver address banker.SendCoins(realmAddr, receiver, send) } // Return possible leftover GNOT - // - //if leftover > 0 { - // send := std.Coins{{"ugnot", leftover}} - // banker.SendCoins(realmAddr, caller, send) - //} + leftover := banker.GetCoins(realmAddr).AmountOf(denom) + if leftover > 0 { + send := std.Coins{{denom, leftover}} + banker.SendCoins(realmAddr, caller, send) + } } diff --git a/examples/gno.land/r/demo/disperse/disperse_test.gno b/examples/gno.land/r/demo/disperse/disperse_test.gno index d8e518e5f74..c0bb3ae2ea2 100644 --- a/examples/gno.land/r/demo/disperse/disperse_test.gno +++ b/examples/gno.land/r/demo/disperse/disperse_test.gno @@ -1,38 +1,85 @@ package disperse import ( + "gno.land/p/demo/testutils" "std" "strings" "testing" ) func TestSendCoin(t *testing.T) { - sender := std.Address("g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de") - receiverArr := []string{"g127jydsh6cms3lrtdenydxsckh23a8d6emqcvfa", "g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j"} - amountArr := []string{"100", "150"} + //cleanUpRealm() - // Mint test GNOT + // Define test addresses alice := testutils.TestAddress("alice") bob := testutils.TestAddress("bob") - // Set caller to sender - std.TestSetOrigCaller(sender) + charlie := testutils.TestAddress("bob") - std.TestSetOrigSend(std.Coins{{"ugnot", 250}}, nil) + totalSendAmount := std.Coins{{"ugnot", 250}} + // Mint test GNOT + std.TestIssueCoins(alice, totalSendAmount) - // Get readonly banker to check balances - //banker := std.GetBanker(std.BankerTypeReadonly) + // Set tx caller to alice + std.TestSetOrigCaller(alice) + + // Send 250ugnot in total to Disperse realm + std.TestSetOrigSend(totalSendAmount, nil) + + // Format array for SendCoin function + receiverArr := []string{bob.String(), charlie.String()} + + // How many tokens to send to bob & charlie + amountArr := []string{"100", "150"} // TODO parse from & to coins properly addresses := strings.Join(receiverArr, ",") amounts := strings.Join(amountArr, ",") - //balSenderBefore := banker.GetCoins(sender) - SendGNOT(addresses, amounts) + banker := std.GetBanker(std.BankerTypeReadonly) + + // Get Disperse realm address + realmAddr := std.GetOrigPkgAddr() + + // Balance of realm should be zero + if initialRealmBalance := banker.GetCoins(realmAddr).AmountOf("ugnot"); initialRealmBalance != 0 { + t.Fatal(errBalanceNotZero) + } + + // Get all balances before sending + aliceBalBefore := banker.GetCoins(alice).AmountOf("ugnot") + bobBalBefore := banker.GetCoins(bob).AmountOf("ugnot") + charlieBalBefore := banker.GetCoins(charlie).AmountOf("ugnot") + + // Send coin + SendCoin(addresses, amounts) + + // Get all balances after sending + aliceBalAfter := banker.GetCoins(alice).AmountOf("ugnot") + //bobBalAfter := banker.GetCoins(bob).AmountOf("ugnot") + //charlieBalAfter := banker.GetCoins(charlie).AmountOf("ugnot") + + if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { + t.Fatal("alice's balance not correct") + } + + if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { + t.Fatal("alice's balance not correct") + } + +} + +// Remove any existing coin balances for testing purposes +func cleanUpRealm() { + // Get readonly banker to check balances + banker := std.GetBanker(std.BankerTypeRealmSend) + + // Get Disperse realm address + realmAddr := std.GetOrigPkgAddr() + + // Get initial realm balance + initialBalance := banker.GetCoins(realmAddr) - //balSenderAfter := banker.GetCoins(sender) - // - //println("balSenderBefore:") - //println(balSenderBefore) - //println("balSenderAfter:") - //println(balSenderAfter) + // Get random burn address + zeroAddr := testutils.TestAddress("0x0") + banker.SendCoins(realmAddr, zeroAddr, initialBalance) } diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 075dd632bdb..f4388c2a29e 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -7,4 +7,6 @@ var ( 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") + errMultipleCoinsSent = errors.New("cannot send multiple tokens") + errBalanceNotZero = errors.New("balance needs to be equal to zero") ) From fe220f3579e5f8e2041004dcb7f391eb472ae771 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 14 Dec 2023 18:41:52 +0100 Subject: [PATCH 07/46] wip tests --- .../r/demo/disperse/disperse_test.gno | 72 ++++++++---------- gno.land/data/keys.db/CURRENT | 1 + gno.land/data/keys.db/CURRENT.bak | 1 + gno.land/data/keys.db/LOCK | 0 gno.land/data/keys.db/LOG | 52 +++++++++++++ gno.land/data/keys.db/MANIFEST-000009 | Bin 0 -> 41 bytes 6 files changed, 87 insertions(+), 39 deletions(-) create mode 100644 gno.land/data/keys.db/CURRENT create mode 100644 gno.land/data/keys.db/CURRENT.bak create mode 100644 gno.land/data/keys.db/LOCK create mode 100644 gno.land/data/keys.db/LOG create mode 100644 gno.land/data/keys.db/MANIFEST-000009 diff --git a/examples/gno.land/r/demo/disperse/disperse_test.gno b/examples/gno.land/r/demo/disperse/disperse_test.gno index c0bb3ae2ea2..a13ff733661 100644 --- a/examples/gno.land/r/demo/disperse/disperse_test.gno +++ b/examples/gno.land/r/demo/disperse/disperse_test.gno @@ -8,7 +8,12 @@ import ( ) func TestSendCoin(t *testing.T) { - //cleanUpRealm() + // Get readonly banker to check balances + bankerTestIssueCoin := std.GetBanker(std.BankerTypeRealmIssue) + //cleanUpRealm(banker) + + // Get Disperse realm address + realmAddr := std.GetOrigPkgAddr() // Define test addresses alice := testutils.TestAddress("alice") @@ -16,6 +21,8 @@ func TestSendCoin(t *testing.T) { charlie := testutils.TestAddress("bob") totalSendAmount := std.Coins{{"ugnot", 250}} + //bankerRealmSend := std.GetBanker(std.BankerTypeRealmSend) + // Mint test GNOT std.TestIssueCoins(alice, totalSendAmount) @@ -23,63 +30,50 @@ func TestSendCoin(t *testing.T) { std.TestSetOrigCaller(alice) // Send 250ugnot in total to Disperse realm - std.TestSetOrigSend(totalSendAmount, nil) + std.TestSetOrigSend(totalSendAmount, nil) // todo add issue for this - // Format array for SendCoin function + //Format array for SendCoin function receiverArr := []string{bob.String(), charlie.String()} + // Get initial realm balance + //initialBalance := bankerRealmSend.GetCoins(realmAddr) + + // Get random burn address + //zeroAddr := testutils.TestAddress("0x0") + + bankerRealmSend.SendCoins(realmAddr, "g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de", initialBalance) + // How many tokens to send to bob & charlie amountArr := []string{"100", "150"} // TODO parse from & to coins properly addresses := strings.Join(receiverArr, ",") amounts := strings.Join(amountArr, ",") - banker := std.GetBanker(std.BankerTypeReadonly) - - // Get Disperse realm address - realmAddr := std.GetOrigPkgAddr() - // Balance of realm should be zero - if initialRealmBalance := banker.GetCoins(realmAddr).AmountOf("ugnot"); initialRealmBalance != 0 { - t.Fatal(errBalanceNotZero) - } + //if initialRealmBalance := banker.GetCoins(realmAddr).AmountOf("ugnot"); initialRealmBalance != 0 { + // t.Fatal(errBalanceNotZero) + //} // Get all balances before sending - aliceBalBefore := banker.GetCoins(alice).AmountOf("ugnot") - bobBalBefore := banker.GetCoins(bob).AmountOf("ugnot") - charlieBalBefore := banker.GetCoins(charlie).AmountOf("ugnot") + //aliceBalBefore := banker.GetCoins(alice).AmountOf("ugnot") + //bobBalBefore := banker.GetCoins(bob).AmountOf("ugnot") + //charlieBalBefore := banker.GetCoins(charlie).AmountOf("ugnot") // Send coin SendCoin(addresses, amounts) // Get all balances after sending - aliceBalAfter := banker.GetCoins(alice).AmountOf("ugnot") + //aliceBalAfter := banker.GetCoins(alice).AmountOf("ugnot") //bobBalAfter := banker.GetCoins(bob).AmountOf("ugnot") //charlieBalAfter := banker.GetCoins(charlie).AmountOf("ugnot") - if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { - t.Fatal("alice's balance not correct") - } - - if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { - t.Fatal("alice's balance not correct") - } - + //if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { + // t.Fatal("alice's balance not correct") + //} } -// Remove any existing coin balances for testing purposes -func cleanUpRealm() { - // Get readonly banker to check balances - banker := std.GetBanker(std.BankerTypeRealmSend) - - // Get Disperse realm address - realmAddr := std.GetOrigPkgAddr() - - // Get initial realm balance - initialBalance := banker.GetCoins(realmAddr) - - // Get random burn address - zeroAddr := testutils.TestAddress("0x0") - - banker.SendCoins(realmAddr, zeroAddr, initialBalance) -} +//// Remove any existing coin balances for testing purposes +//func cleanUpRealm(banker std.BankerTypeRealmSend) { +// +// +//} diff --git a/gno.land/data/keys.db/CURRENT b/gno.land/data/keys.db/CURRENT new file mode 100644 index 00000000000..6ba31a31e7d --- /dev/null +++ b/gno.land/data/keys.db/CURRENT @@ -0,0 +1 @@ +MANIFEST-000009 diff --git a/gno.land/data/keys.db/CURRENT.bak b/gno.land/data/keys.db/CURRENT.bak new file mode 100644 index 00000000000..875cf233554 --- /dev/null +++ b/gno.land/data/keys.db/CURRENT.bak @@ -0,0 +1 @@ +MANIFEST-000007 diff --git a/gno.land/data/keys.db/LOCK b/gno.land/data/keys.db/LOCK new file mode 100644 index 00000000000..e69de29bb2d diff --git a/gno.land/data/keys.db/LOG b/gno.land/data/keys.db/LOG new file mode 100644 index 00000000000..d4515eeba13 --- /dev/null +++ b/gno.land/data/keys.db/LOG @@ -0,0 +1,52 @@ +=============== Dec 6, 2023 (CET) =============== +14:33:40.760622 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed +14:33:40.768942 db@open opening +14:33:40.769814 version@stat F·[] S·0B[] Sc·[] +14:33:40.775912 db@janitor F·2 G·0 +14:33:40.775949 db@open done T·6.98825ms +14:33:40.776013 db@close closing +14:33:40.776082 db@close done T·65.875µs +=============== Dec 6, 2023 (CET) =============== +14:37:58.429171 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed +14:37:58.430096 version@stat F·[] S·0B[] Sc·[] +14:37:58.430110 db@open opening +14:37:58.430146 journal@recovery F·1 +14:37:58.432543 journal@recovery recovering @1 +14:37:58.433021 version@stat F·[] S·0B[] Sc·[] +14:37:58.438919 db@janitor F·2 G·0 +14:37:58.439117 db@open done T·9.001125ms +14:37:58.439514 db@close closing +14:37:58.439563 db@close done T·48.375µs +=============== Dec 6, 2023 (CET) =============== +14:38:17.182927 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed +14:38:17.183486 version@stat F·[] S·0B[] Sc·[] +14:38:17.183505 db@open opening +14:38:17.183555 journal@recovery F·1 +14:38:17.184133 journal@recovery recovering @2 +14:38:17.185192 version@stat F·[] S·0B[] Sc·[] +14:38:17.195685 db@janitor F·2 G·0 +14:38:17.195711 db@open done T·12.196083ms +14:38:17.195738 db@close closing +14:38:17.198153 db@close done T·2.414583ms +=============== Dec 6, 2023 (CET) =============== +14:58:23.794004 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed +14:58:23.794908 version@stat F·[] S·0B[] Sc·[] +14:58:23.794918 db@open opening +14:58:23.794948 journal@recovery F·1 +14:58:23.795655 journal@recovery recovering @4 +14:58:23.796212 version@stat F·[] S·0B[] Sc·[] +14:58:23.808272 db@janitor F·2 G·0 +14:58:23.808302 db@open done T·13.37775ms +14:58:23.808357 db@close closing +14:58:23.808404 db@close done T·46.208µs +=============== Dec 6, 2023 (CET) =============== +15:01:04.612603 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed +15:01:04.613703 version@stat F·[] S·0B[] Sc·[] +15:01:04.613722 db@open opening +15:01:04.613807 journal@recovery F·1 +15:01:04.614355 journal@recovery recovering @6 +15:01:04.615331 version@stat F·[] S·0B[] Sc·[] +15:01:04.624404 db@janitor F·2 G·0 +15:01:04.624440 db@open done T·10.7065ms +15:01:04.624467 db@close closing +15:01:04.624602 db@close done T·135.25µs diff --git a/gno.land/data/keys.db/MANIFEST-000009 b/gno.land/data/keys.db/MANIFEST-000009 new file mode 100644 index 0000000000000000000000000000000000000000..3dd087faafcf675e7e2353d1a64ac454a750fb85 GIT binary patch literal 41 wcmZ1(ru0UMfss)vC$%g!CnZVGsj?)sJhM2}IX|}`u_&=5zle#0nTv%103#a=*8l(j literal 0 HcmV?d00001 From 3637e81b8dd122ac027486b228c7b007e5b3417e Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 14 Dec 2023 18:50:47 +0100 Subject: [PATCH 08/46] remove blocked tests --- .../r/demo/disperse/disperse_test.gno | 79 ------------------- 1 file changed, 79 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse_test.gno b/examples/gno.land/r/demo/disperse/disperse_test.gno index a13ff733661..e69de29bb2d 100644 --- a/examples/gno.land/r/demo/disperse/disperse_test.gno +++ b/examples/gno.land/r/demo/disperse/disperse_test.gno @@ -1,79 +0,0 @@ -package disperse - -import ( - "gno.land/p/demo/testutils" - "std" - "strings" - "testing" -) - -func TestSendCoin(t *testing.T) { - // Get readonly banker to check balances - bankerTestIssueCoin := std.GetBanker(std.BankerTypeRealmIssue) - //cleanUpRealm(banker) - - // Get Disperse realm address - realmAddr := std.GetOrigPkgAddr() - - // Define test addresses - alice := testutils.TestAddress("alice") - bob := testutils.TestAddress("bob") - charlie := testutils.TestAddress("bob") - - totalSendAmount := std.Coins{{"ugnot", 250}} - //bankerRealmSend := std.GetBanker(std.BankerTypeRealmSend) - - // Mint test GNOT - std.TestIssueCoins(alice, totalSendAmount) - - // Set tx caller to alice - std.TestSetOrigCaller(alice) - - // Send 250ugnot in total to Disperse realm - std.TestSetOrigSend(totalSendAmount, nil) // todo add issue for this - - //Format array for SendCoin function - receiverArr := []string{bob.String(), charlie.String()} - - // Get initial realm balance - //initialBalance := bankerRealmSend.GetCoins(realmAddr) - - // Get random burn address - //zeroAddr := testutils.TestAddress("0x0") - - bankerRealmSend.SendCoins(realmAddr, "g1l9aypkr8xfvs82zeux486ddzec88ty69lue9de", initialBalance) - - // How many tokens to send to bob & charlie - amountArr := []string{"100", "150"} // TODO parse from & to coins properly - - addresses := strings.Join(receiverArr, ",") - amounts := strings.Join(amountArr, ",") - - // Balance of realm should be zero - //if initialRealmBalance := banker.GetCoins(realmAddr).AmountOf("ugnot"); initialRealmBalance != 0 { - // t.Fatal(errBalanceNotZero) - //} - - // Get all balances before sending - //aliceBalBefore := banker.GetCoins(alice).AmountOf("ugnot") - //bobBalBefore := banker.GetCoins(bob).AmountOf("ugnot") - //charlieBalBefore := banker.GetCoins(charlie).AmountOf("ugnot") - - // Send coin - SendCoin(addresses, amounts) - - // Get all balances after sending - //aliceBalAfter := banker.GetCoins(alice).AmountOf("ugnot") - //bobBalAfter := banker.GetCoins(bob).AmountOf("ugnot") - //charlieBalAfter := banker.GetCoins(charlie).AmountOf("ugnot") - - //if aliceBalBefore-aliceBalAfter != totalSendAmount.AmountOf("ugnot") { - // t.Fatal("alice's balance not correct") - //} -} - -//// Remove any existing coin balances for testing purposes -//func cleanUpRealm(banker std.BankerTypeRealmSend) { -// -// -//} From 6cb1b0e6df855aa1ce991170defb4949c6bc64f3 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Sun, 24 Dec 2023 17:37:50 +0100 Subject: [PATCH 09/46] generalize to sendcoin --- examples/gno.land/r/demo/disperse/disperse.gno | 10 +++++----- examples/gno.land/r/demo/disperse/disperse_test.gno | 0 examples/gno.land/r/demo/disperse/errors.gno | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) delete mode 100644 examples/gno.land/r/demo/disperse/disperse_test.gno diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index c6d8af988c2..6406624effc 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -4,10 +4,10 @@ import ( "std" ) -// SendCoin parses receivers and amounts and sends out GNOT +// SendSingleCoin parses receivers and amounts and sends out a single coin // Currently sending slices as arguments with `maketx call` is unsupported, // which is why we parse strings by delimiter here -func SendCoin(addresses, sendAmounts string) { +func SendSingleCoin(addresses, sendAmounts string) { std.AssertOriginCall() // assert the call was by a user coinSent := std.GetOrigSend() // get Coin value & denom sent with call caller := std.GetOrigCaller() // get tx caller @@ -45,10 +45,10 @@ func SendCoin(addresses, sendAmounts string) { totalToSend += val } - // Check if user sent enough GNOT + // Check if user sent enough coin // Possibly not needed, added for error clarity if coinSent.AmountOf(denom) < totalToSend { - panic(errNotEnoughGnot) + panic(errNotEnoughCoin) } for i, val := range parsedAmounts { @@ -59,7 +59,7 @@ func SendCoin(addresses, sendAmounts string) { banker.SendCoins(realmAddr, receiver, send) } - // Return possible leftover GNOT + // Return possible leftover coin leftover := banker.GetCoins(realmAddr).AmountOf(denom) if leftover > 0 { send := std.Coins{{denom, leftover}} diff --git a/examples/gno.land/r/demo/disperse/disperse_test.gno b/examples/gno.land/r/demo/disperse/disperse_test.gno deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index f4388c2a29e..29cb11062b3 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,7 +3,7 @@ package disperse import "errors" var ( - errNotEnoughGnot = errors.New("not enough gnot sent in") + 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") From 39aaf1b2f6d03da3f1d0ab4ba304ab5341a43bd5 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 25 Dec 2023 15:56:11 +0100 Subject: [PATCH 10/46] txtar testing basics --- gno.land/cmd/gnoland/testdata/disperse.txtar | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 gno.land/cmd/gnoland/testdata/disperse.txtar diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar new file mode 100644 index 00000000000..19ac7d0e3d4 --- /dev/null +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -0,0 +1,3 @@ +gnoland start + +gnokey maketx call -pkgpath gno.land/r/demo/disperse -home /Users/sasurai/gnokey -send 700ugnot -func SendSingleCoin -args "g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j,g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp" -args "150,500" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast newhome From eac2b1030975bbba42554e5b32f44f0a784f87bd Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 5 Feb 2024 18:12:22 +0100 Subject: [PATCH 11/46] remove keys db --- gno.land/data/keys.db/CURRENT | 1 - gno.land/data/keys.db/CURRENT.bak | 1 - gno.land/data/keys.db/LOCK | 0 gno.land/data/keys.db/LOG | 52 -------------------------- gno.land/data/keys.db/MANIFEST-000009 | Bin 41 -> 0 bytes 5 files changed, 54 deletions(-) delete mode 100644 gno.land/data/keys.db/CURRENT delete mode 100644 gno.land/data/keys.db/CURRENT.bak delete mode 100644 gno.land/data/keys.db/LOCK delete mode 100644 gno.land/data/keys.db/LOG delete mode 100644 gno.land/data/keys.db/MANIFEST-000009 diff --git a/gno.land/data/keys.db/CURRENT b/gno.land/data/keys.db/CURRENT deleted file mode 100644 index 6ba31a31e7d..00000000000 --- a/gno.land/data/keys.db/CURRENT +++ /dev/null @@ -1 +0,0 @@ -MANIFEST-000009 diff --git a/gno.land/data/keys.db/CURRENT.bak b/gno.land/data/keys.db/CURRENT.bak deleted file mode 100644 index 875cf233554..00000000000 --- a/gno.land/data/keys.db/CURRENT.bak +++ /dev/null @@ -1 +0,0 @@ -MANIFEST-000007 diff --git a/gno.land/data/keys.db/LOCK b/gno.land/data/keys.db/LOCK deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/gno.land/data/keys.db/LOG b/gno.land/data/keys.db/LOG deleted file mode 100644 index d4515eeba13..00000000000 --- a/gno.land/data/keys.db/LOG +++ /dev/null @@ -1,52 +0,0 @@ -=============== Dec 6, 2023 (CET) =============== -14:33:40.760622 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed -14:33:40.768942 db@open opening -14:33:40.769814 version@stat F·[] S·0B[] Sc·[] -14:33:40.775912 db@janitor F·2 G·0 -14:33:40.775949 db@open done T·6.98825ms -14:33:40.776013 db@close closing -14:33:40.776082 db@close done T·65.875µs -=============== Dec 6, 2023 (CET) =============== -14:37:58.429171 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed -14:37:58.430096 version@stat F·[] S·0B[] Sc·[] -14:37:58.430110 db@open opening -14:37:58.430146 journal@recovery F·1 -14:37:58.432543 journal@recovery recovering @1 -14:37:58.433021 version@stat F·[] S·0B[] Sc·[] -14:37:58.438919 db@janitor F·2 G·0 -14:37:58.439117 db@open done T·9.001125ms -14:37:58.439514 db@close closing -14:37:58.439563 db@close done T·48.375µs -=============== Dec 6, 2023 (CET) =============== -14:38:17.182927 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed -14:38:17.183486 version@stat F·[] S·0B[] Sc·[] -14:38:17.183505 db@open opening -14:38:17.183555 journal@recovery F·1 -14:38:17.184133 journal@recovery recovering @2 -14:38:17.185192 version@stat F·[] S·0B[] Sc·[] -14:38:17.195685 db@janitor F·2 G·0 -14:38:17.195711 db@open done T·12.196083ms -14:38:17.195738 db@close closing -14:38:17.198153 db@close done T·2.414583ms -=============== Dec 6, 2023 (CET) =============== -14:58:23.794004 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed -14:58:23.794908 version@stat F·[] S·0B[] Sc·[] -14:58:23.794918 db@open opening -14:58:23.794948 journal@recovery F·1 -14:58:23.795655 journal@recovery recovering @4 -14:58:23.796212 version@stat F·[] S·0B[] Sc·[] -14:58:23.808272 db@janitor F·2 G·0 -14:58:23.808302 db@open done T·13.37775ms -14:58:23.808357 db@close closing -14:58:23.808404 db@close done T·46.208µs -=============== Dec 6, 2023 (CET) =============== -15:01:04.612603 log@legend F·NumFile S·FileSize N·Entry C·BadEntry B·BadBlock Ke·KeyError D·DroppedEntry L·Level Q·SeqNum T·TimeElapsed -15:01:04.613703 version@stat F·[] S·0B[] Sc·[] -15:01:04.613722 db@open opening -15:01:04.613807 journal@recovery F·1 -15:01:04.614355 journal@recovery recovering @6 -15:01:04.615331 version@stat F·[] S·0B[] Sc·[] -15:01:04.624404 db@janitor F·2 G·0 -15:01:04.624440 db@open done T·10.7065ms -15:01:04.624467 db@close closing -15:01:04.624602 db@close done T·135.25µs diff --git a/gno.land/data/keys.db/MANIFEST-000009 b/gno.land/data/keys.db/MANIFEST-000009 deleted file mode 100644 index 3dd087faafcf675e7e2353d1a64ac454a750fb85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 41 wcmZ1(ru0UMfss)vC$%g!CnZVGsj?)sJhM2}IX|}`u_&=5zle#0nTv%103#a=*8l(j From 40ba91832f9acc26d2b68e37f7e98d7ad3cc33bf Mon Sep 17 00:00:00 2001 From: leohhhn Date: Mon, 5 Feb 2024 18:18:23 +0100 Subject: [PATCH 12/46] add tests --- gno.land/cmd/gnoland/testdata/disperse.txtar | 27 +++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar index 19ac7d0e3d4..9732010629c 100644 --- a/gno.land/cmd/gnoland/testdata/disperse.txtar +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -1,3 +1,28 @@ gnoland start -gnokey maketx call -pkgpath gno.land/r/demo/disperse -home /Users/sasurai/gnokey -send 700ugnot -func SendSingleCoin -args "g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j,g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp" -args "150,500" -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast newhome +# Query initial balances +# Sender +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout 'height: 0\ndata: "9999870000000ugnot"' + +# Recipient 1 +gnokey query bank/balances/g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j +stdout 'height: 0\ndata: ""' + +# Recipient 2 +gnokey query bank/balances/g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp +stdout 'height: 0\ndata: ""' + +# Make call to Disperse +gnokey maketx call -pkgpath gno.land/r/demo/disperse -send 700ugnot -func SendSingleCoin -args g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j,g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp -args 150,500 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid tendermint_test test1 + +# Query balances again +# Recipient 1 +gnokey query bank/balances/g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j +stdout 'height: 0\ndata: "150ugnot"' + +# Recipient 2 +gnokey query bank/balances/g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp +stdout 'height: 0\ndata: "500ugnot"' + + From 9079f54b1c97c224afc5ecdd4d010a1b4951e4f2 Mon Sep 17 00:00:00 2001 From: leohhhn Date: Wed, 7 Feb 2024 14:47:26 +0100 Subject: [PATCH 13/46] wip --- examples/gno.land/r/demo/disperse/disperse.gno | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 6406624effc..6e3de9a3606 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -7,12 +7,11 @@ import ( // SendSingleCoin parses receivers and amounts and sends out a single coin // Currently sending slices as arguments with `maketx call` is unsupported, // which is why we parse strings by delimiter here -func SendSingleCoin(addresses, sendAmounts string) { - std.AssertOriginCall() // assert the call was by a user +func SendSingleCoin(addresses string, coins std.Coins) { coinSent := std.GetOrigSend() // get Coin value & denom sent with call - caller := std.GetOrigCaller() // get tx caller + caller := std.GetOrigCaller() // get tx sender - if len(coinSent) != 1 { + if len(coinSent) != 1 && len(coins) != 1 { panic(errMultipleCoinsSent) } @@ -20,7 +19,7 @@ func SendSingleCoin(addresses, sendAmounts string) { denom := coinSent[0].Denom // Get address of Disperse realm - realmAddr := std.GetOrigPkgAddr() + realmAddr := std.CurrentRealm().Addr() // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) From 724656a29b45d92c40b30640eff4f29945ccc94c Mon Sep 17 00:00:00 2001 From: leohhhn Date: Thu, 22 Feb 2024 12:46:33 +0100 Subject: [PATCH 14/46] refactor --- .../gno.land/r/demo/disperse/disperse.gno | 68 +++++++------------ examples/gno.land/r/demo/disperse/errors.gno | 13 ++-- gno.land/cmd/gnoland/testdata/disperse.txtar | 31 ++++----- 3 files changed, 45 insertions(+), 67 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 6e3de9a3606..09624862d86 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -5,18 +5,23 @@ import ( ) // SendSingleCoin parses receivers and amounts and sends out a single coin -// Currently sending slices as arguments with `maketx call` is unsupported, -// which is why we parse strings by delimiter here -func SendSingleCoin(addresses string, coins std.Coins) { - coinSent := std.GetOrigSend() // get Coin value & denom sent with call +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) != 1 && len(coins) != 1 { - panic(errMultipleCoinsSent) + if len(coinSent) != len(coins) { + panic(ErrArgLenAndSentLenMismatch) } - // Extract denom from Coins - denom := coinSent[0].Denom + 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() @@ -24,44 +29,23 @@ func SendSingleCoin(addresses string, coins std.Coins) { // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) - parsedAddresses, err := parseAddresses(addresses) - if err != nil { - panic(err) - } - - parsedAmounts, err := parseAmounts(sendAmounts) - if err != nil { - panic(err) - } - - // Check if arg lengths are good - if len(parsedAddresses) != len(parsedAmounts) { - panic(errNumAddrValMismatch) - } - - var totalToSend int64 - for _, val := range parsedAmounts { - totalToSend += val + // Send coins + for i, _ := range addresses { + banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]}) } - // Check if user sent enough coin - // Possibly not needed, added for error clarity - if coinSent.AmountOf(denom) < totalToSend { - panic(errNotEnoughCoin) + // 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) + } } +} - for i, val := range parsedAmounts { - send := std.Coins{{denom, val}} - receiver := parsedAddresses[i] +func main() { - // Send coin from realm to receiver address - banker.SendCoins(realmAddr, receiver, send) - } + toSend := std.Coins{{"ugnot", 1000}} - // Return possible leftover coin - leftover := banker.GetCoins(realmAddr).AmountOf(denom) - if leftover > 0 { - send := std.Coins{{denom, leftover}} - banker.SendCoins(realmAddr, caller, send) - } } diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 29cb11062b3..a6f3ada91c6 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,10 +3,11 @@ 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") - errMultipleCoinsSent = errors.New("cannot send multiple tokens") - errBalanceNotZero = errors.New("balance needs to be equal to zero") + 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/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar index 9732010629c..435952cfec3 100644 --- a/gno.land/cmd/gnoland/testdata/disperse.txtar +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -1,28 +1,21 @@ gnoland start -# Query initial balances -# Sender -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout 'height: 0\ndata: "9999870000000ugnot"' +gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script.gno -# Recipient 1 -gnokey query bank/balances/g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j -stdout 'height: 0\ndata: ""' +-- script.gno -- +package main -# Recipient 2 -gnokey query bank/balances/g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp -stdout 'height: 0\ndata: ""' +import ( + "gno.land/r/demo/disperse" + "std" +) -# Make call to Disperse -gnokey maketx call -pkgpath gno.land/r/demo/disperse -send 700ugnot -func SendSingleCoin -args g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j,g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp -args 150,500 -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid tendermint_test test1 +func main() { -# Query balances again -# Recipient 1 -gnokey query bank/balances/g1x8uj9wc3wevvn3glvfallt5e5ercfcs520wy4j -stdout 'height: 0\ndata: "150ugnot"' -# Recipient 2 -gnokey query bank/balances/g1uvdmvvj76nfgjhfe69n4lwafsu7lh2m8f3ycfp -stdout 'height: 0\ndata: "500ugnot"' + toSend := std.Coins{} + + +} From 86c1d37129b794376d45244d1ac92ef42e243e16 Mon Sep 17 00:00:00 2001 From: agherasie Date: Tue, 16 Jul 2024 19:50:31 +0200 Subject: [PATCH 15/46] feat: add disperse gnot and token --- .../gno.land/r/demo/disperse/disperse.gno | 84 +++++++++++++++---- examples/gno.land/r/demo/disperse/errors.gno | 2 +- examples/gno.land/r/demo/disperse/gno.mod | 5 ++ examples/gno.land/r/demo/disperse/util.gno | 2 +- gno.land/cmd/gnoland/testdata/disperse.txtar | 69 ++++++++++++--- 5 files changed, 132 insertions(+), 30 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 09624862d86..7075036f10b 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -2,33 +2,31 @@ package disperse import ( "std" + + tokens "gno.land/r/demo/grc20factory" ) -// SendSingleCoin parses receivers and amounts and sends out a single coin -func SendSingleCoin(addresses []std.Address, coins std.Coins) { +// DisperseGnot parses receivers and amounts and sends out gnot +func DisperseGnot(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) + var totalAmount int64 + for _, coin := range coins { + totalAmount += coin.Amount + } + + // Check if total amount of coins sent is bigger or equal to total amount of coins to be sent + if totalAmount > coinSent.AmountOf("ugnot") { + panic(ErrWrongAmount) + } + // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]}) @@ -44,8 +42,58 @@ func SendSingleCoin(addresses []std.Address, coins std.Coins) { } } -func main() { +func DisperseToken(addresses []std.Address, coins std.Coins) { + caller := std.GetOrigCaller() // get tx sender + + var totalAmount uint64 + for _, coin := range coins { + totalAmount += uint64(coin.Amount) + } - toSend := std.Coins{{"ugnot", 1000}} + tokens.TransferFrom(coins[0].Denom, caller, std.CurrentRealm().Addr(), totalAmount) + for i, address := range addresses { + for y, coin := range coins { + if y == i { + tokens.Transfer(coin.Denom, address, uint64(coin.Amount)) + } + } + } +} + +func DisperseTokenString(symbol string, addresses string, amounts string) { + parsedAddresses, err := parseAddresses(addresses) + if err != nil { + panic(err) + } + + parsedAmounts, err := parseAmounts(amounts) + if err != nil { + panic(err) + } + + coins := make(std.Coins, len(parsedAmounts)) + for i, amount := range parsedAmounts { + coins[i] = std.Coin{symbol, amount} + } + + DisperseToken(parsedAddresses, coins) +} + +func DisperseGnotString(addresses string, amounts string) { + parsedAddresses, err := parseAddresses(addresses) + if err != nil { + panic(err) + } + + parsedAmounts, err := parseAmounts(amounts) + if err != nil { + panic(err) + } + + coins := make(std.Coins, len(parsedAmounts)) + for i, amount := range parsedAmounts { + coins[i] = std.Coin{"ugnot", amount} + } + DisperseGnot(parsedAddresses, coins) } diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index a6f3ada91c6..9dff4ae9e31 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -10,4 +10,4 @@ var ( 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") -) +) \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/gno.mod b/examples/gno.land/r/demo/disperse/gno.mod index 06e81884dfa..1cfe338584e 100644 --- a/examples/gno.land/r/demo/disperse/gno.mod +++ b/examples/gno.land/r/demo/disperse/gno.mod @@ -1 +1,6 @@ module gno.land/r/demo/disperse + +require ( + "gno.land/p/demo/ufmt" v0.0.0-latest + "gno.land/r/demo/grc20factory" v0.0.0-latest +) \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index 6ed265890d2..f1a6a854637 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -32,4 +32,4 @@ func parseAmounts(amounts string) ([]int64, error) { } return ret, nil -} +} \ No newline at end of file diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar index 435952cfec3..7663ef410e4 100644 --- a/gno.land/cmd/gnoland/testdata/disperse.txtar +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -1,21 +1,70 @@ +# Test for disperse + +loadpkg gno.land/r/demo/disperse +loadpkg gno.land/r/demo/grc20factory + gnoland start -gnokey maketx run -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 $WORK/script.gno +### Execute disperse gnot + +# Verifying default balance +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout 'height: 0' +stdout 'data: "9999990000000ugnot"' + +# Executing disperse gnot +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 + +# Verifying balances after disperse + +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout 'height: 0' +stdout 'data: "9999988999800ugnot"' + +gnokey query bank/balances/g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0 +stdout 'height: 0' +stdout 'data: "150ugnot"' + +gnokey query bank/balances/g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c +stdout 'height: 0' +stdout 'data: "50ugnot"' + +### Execute disperse gnot (leftover gnot) + +# Execute disperse with 'send' of 200ugnot but total of 199ugnot +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 + +# Verify balance after disperse +gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +stdout 'height: 0' +stdout 'data: "9999987999601ugnot"' --- script.gno -- -package main +### Execute disperse gnot (invalid coins) -import ( - "gno.land/r/demo/disperse" - "std" -) +# Execute disperse with 'send' of 200ugnot but total of 201ugnot +! 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 -func main() { +### Execute disperse token +# Create new token +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 +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 +# Verify balance in new token after mint +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 +stdout '(2000000000 uint64)' - toSend := std.Coins{} +# Approve allowance to smart contract +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 +# Execute disperse token +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 +# Verify balances after disperse +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 +stdout '(1999999800 uint64)' +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 +stdout '(150 uint64)' -} +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 +stdout '(50 uint64)' \ No newline at end of file From ebf397dd12d8e16f3144bfd1ea9caf259f6f21ab Mon Sep 17 00:00:00 2001 From: lennyvongphouthone Date: Mon, 22 Jul 2024 15:30:54 +0200 Subject: [PATCH 16/46] Update: - disperseToken can now support the disperse of multiple tokens - add some panic handling - add tests for disperse multiple tokens and errors --- .../gno.land/r/demo/disperse/disperse.gno | 51 ++++++++++---- examples/gno.land/r/demo/disperse/errors.gno | 12 ++-- examples/gno.land/r/demo/disperse/util.gno | 33 ++++++++- gno.land/cmd/gnoland/testdata/disperse.txtar | 69 +++++++++++++++++-- 4 files changed, 139 insertions(+), 26 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 7075036f10b..4493348c136 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -2,7 +2,7 @@ package disperse import ( "std" - + tokens "gno.land/r/demo/grc20factory" ) @@ -11,6 +11,18 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call caller := std.GetOrigCaller() // get tx sender + // Check if caller correctly sent positif amount of coins + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } + + // Check if number of addresses and coins match + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } + // Get address of Disperse realm realmAddr := std.CurrentRealm().Addr() @@ -21,12 +33,17 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { for _, coin := range coins { totalAmount += coin.Amount } - + // Check if total amount of coins sent is bigger or equal to total amount of coins to be sent if totalAmount > coinSent.AmountOf("ugnot") { panic(ErrWrongAmount) } + // Check if the realm has zero ugnot + if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { + panic(ErrBalanceNotEqToAmountSent) + } + // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]}) @@ -45,12 +62,24 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { func DisperseToken(addresses []std.Address, coins std.Coins) { caller := std.GetOrigCaller() // get tx sender - var totalAmount uint64 + // Check if caller correctly sent positif amount of coins + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } + + // Check if number of addresses and coins match + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } + + // Transfer tokens into the realm for _, coin := range coins { - totalAmount += uint64(coin.Amount) + tokens.TransferFrom(coin.Denom, caller, std.CurrentRealm().Addr(), uint64(coin.Amount)) } - tokens.TransferFrom(coins[0].Denom, caller, std.CurrentRealm().Addr(), totalAmount) + // Disperse tokens for i, address := range addresses { for y, coin := range coins { if y == i { @@ -60,23 +89,18 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } } -func DisperseTokenString(symbol string, addresses string, amounts string) { +func DisperseTokenString(addresses string, tokens string) { parsedAddresses, err := parseAddresses(addresses) if err != nil { panic(err) } - parsedAmounts, err := parseAmounts(amounts) + parseTokens, err := parseTokens(tokens) if err != nil { panic(err) } - coins := make(std.Coins, len(parsedAmounts)) - for i, amount := range parsedAmounts { - coins[i] = std.Coin{symbol, amount} - } - - DisperseToken(parsedAddresses, coins) + DisperseToken(parsedAddresses, parseTokens) } func DisperseGnotString(addresses string, amounts string) { @@ -97,3 +121,4 @@ func DisperseGnotString(addresses string, amounts string) { DisperseGnot(parsedAddresses, coins) } + diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 9dff4ae9e31..e9fa5f00e5f 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,11 +3,11 @@ 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") + 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") + ErrBalanceNotEqToAmountSent = 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") -) \ No newline at end of file +) diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index f1a6a854637..c4c2cb4635b 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -4,6 +4,7 @@ import ( "std" "strconv" "strings" + "unicode" ) func parseAddresses(addresses string) ([]std.Address, error) { @@ -12,7 +13,7 @@ func parseAddresses(addresses string) ([]std.Address, error) { for _, str := range strings.Split(addresses, ",") { addr := std.Address(str) if !addr.IsValid() { - return nil, errInvalidAddress + return nil, ErrInvalidAddress } ret = append(ret, addr) } @@ -20,16 +21,42 @@ func parseAddresses(addresses string) ([]std.Address, error) { return ret, nil } +func splitString(input string) (string, string) { + var pos int + for i, char := range input { + if !unicode.IsDigit(char) { + pos = i + break + } + } + return input[:pos], input[pos:] +} + +func parseTokens(tokens string) ([]std.Coin, error) { + var ret []std.Coin + + for _, token := range strings.Split(tokens, ",") { + amountStr, symbol := splitString(token) + amount, _ := strconv.Atoi(amountStr) + if amount < 0 { + return nil, ErrNegativeCoinAmount + } + ret = append(ret, std.Coin{symbol, int64(amount)}) + } + + 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 + return nil, ErrNegativeCoinAmount } ret = append(ret, int64(amount)) } return ret, nil -} \ No newline at end of file +} diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar index 7663ef410e4..9b7eed15266 100644 --- a/gno.land/cmd/gnoland/testdata/disperse.txtar +++ b/gno.land/cmd/gnoland/testdata/disperse.txtar @@ -1,7 +1,7 @@ # Test for disperse loadpkg gno.land/r/demo/disperse -loadpkg gno.land/r/demo/grc20factory +loadpkg gno.land/r/demo/grc20factory gnoland start @@ -39,13 +39,21 @@ gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 stdout 'height: 0' stdout 'data: "9999987999601ugnot"' -### Execute disperse gnot (invalid coins) +### Execute disperse gnot (errors) # Execute disperse with 'send' of 200ugnot but total of 201ugnot ! 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 +# Execute disperse with negatif coin amount +! 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 "-200" test1 + +# Execute disperse with number of addresses and coin mismatch +! 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" test1 + ### Execute disperse token +## Signle token + # Create new token 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 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 @@ -56,8 +64,9 @@ stdout '(2000000000 uint64)' # Approve allowance to smart contract 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 + # Execute disperse token -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 +gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST,50TEST" test1 # Verify balances after disperse 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 @@ -67,4 +76,56 @@ gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -ga stdout '(150 uint64)' 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 -stdout '(50 uint64)' \ No newline at end of file +stdout '(50 uint64)' + +## Multiple tokens + +# Create new token 1 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "New" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "test1" -args "TEST1" -args 4 -args 1000000000 -args 0 test1 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Mint" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args 1000000000 test1 + +# Create new token 2 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "New" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "test2" -args "TEST2" -args 4 -args 1000000000 -args 0 test1 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Mint" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args 1000000000 test1 + +# Verify balance in new token 1 after mint +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 +stdout '(2000000000 uint64)' + +# Verify balance in new token 2 after mint +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 +stdout '(2000000000 uint64)' + +# Approve allowance token 1 to smart contract +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Approve" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1yryw6qs8h9anvguu4dfdc0u7zh4gvv8vqf59sj" -args "200" test1 + +# Approve allowance token 2 to smart contract +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Approve" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1yryw6qs8h9anvguu4dfdc0u7zh4gvv8vqf59sj" -args "200" test1 + +# Execute disperse token +gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST1,50TEST2" test1 + +# Verify balances after disperse for token 1 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 +stdout '(1999999850 uint64)' + +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0" test1 +stdout '(150 uint64)' + +# Verify balances after disperse for token 2 +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 +stdout '(1999999950 uint64)' + +gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" test1 +stdout '(50 uint64)' + +## Errors + +# Approve allowance to smart contract +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 + +# Panic if amount negatif +! gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "-150TEST,-50TEST" test1 + +# Panic if tokens and addresses mismatch +! gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST" test1 \ No newline at end of file From d1106bc90beb95ade7e18763dc1aacab2c97593d Mon Sep 17 00:00:00 2001 From: agherasie Date: Mon, 22 Jul 2024 16:42:38 +0300 Subject: [PATCH 17/46] chore(examples): disperse fmt and tidy --- .../gno.land/r/demo/disperse/disperse.gno | 51 +++++++++---------- examples/gno.land/r/demo/disperse/gno.mod | 5 +- examples/gno.land/r/demo/disperse/util.gno | 16 +++--- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 4493348c136..260343803e5 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -11,17 +11,17 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call caller := std.GetOrigCaller() // get tx sender - // Check if caller correctly sent positif amount of coins - for _, coin := range coins { - if coin.Amount <= 0 { - panic(ErrNegativeCoinAmount) - } - } - - // Check if number of addresses and coins match - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } + // Check if caller correctly sent positive amount of coins + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } + + // Check if number of addresses and coins match + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } // Get address of Disperse realm realmAddr := std.CurrentRealm().Addr() @@ -39,10 +39,10 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { panic(ErrWrongAmount) } - // Check if the realm has zero ugnot - if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { - panic(ErrBalanceNotEqToAmountSent) - } + // Check if the realm has zero ugnot + if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { + panic(ErrBalanceNotEqToAmountSent) + } // Send coins for i, _ := range addresses { @@ -62,17 +62,17 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { func DisperseToken(addresses []std.Address, coins std.Coins) { caller := std.GetOrigCaller() // get tx sender - // Check if caller correctly sent positif amount of coins - for _, coin := range coins { - if coin.Amount <= 0 { - panic(ErrNegativeCoinAmount) - } - } + // Check if caller correctly sent positive amount of coins + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } - // Check if number of addresses and coins match - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } + // Check if number of addresses and coins match + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } // Transfer tokens into the realm for _, coin := range coins { @@ -121,4 +121,3 @@ func DisperseGnotString(addresses string, amounts string) { DisperseGnot(parsedAddresses, coins) } - diff --git a/examples/gno.land/r/demo/disperse/gno.mod b/examples/gno.land/r/demo/disperse/gno.mod index 1cfe338584e..0ba9c88810a 100644 --- a/examples/gno.land/r/demo/disperse/gno.mod +++ b/examples/gno.land/r/demo/disperse/gno.mod @@ -1,6 +1,3 @@ module gno.land/r/demo/disperse -require ( - "gno.land/p/demo/ufmt" v0.0.0-latest - "gno.land/r/demo/grc20factory" v0.0.0-latest -) \ No newline at end of file +require gno.land/r/demo/grc20factory v0.0.0-latest diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index c4c2cb4635b..aaea41369d2 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -22,14 +22,14 @@ func parseAddresses(addresses string) ([]std.Address, error) { } func splitString(input string) (string, string) { - var pos int - for i, char := range input { - if !unicode.IsDigit(char) { - pos = i - break - } - } - return input[:pos], input[pos:] + var pos int + for i, char := range input { + if !unicode.IsDigit(char) { + pos = i + break + } + } + return input[:pos], input[pos:] } func parseTokens(tokens string) ([]std.Coin, error) { From a542d4d1871cae669c975400eb5a9b435a5faced Mon Sep 17 00:00:00 2001 From: agherasie Date: Mon, 22 Jul 2024 16:58:33 +0300 Subject: [PATCH 18/46] chore(examples): disperse godoc --- examples/gno.land/r/demo/disperse/disperse.gno | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 260343803e5..7b7687b24c2 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -59,6 +59,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { } } +// DisperseToken disperses tokens to multiple addresses func DisperseToken(addresses []std.Address, coins std.Coins) { caller := std.GetOrigCaller() // get tx sender @@ -89,6 +90,8 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } } +// DisperseTokenString receives a string of addresses and a string of tokens +// and parses them to be used in DisperseToken func DisperseTokenString(addresses string, tokens string) { parsedAddresses, err := parseAddresses(addresses) if err != nil { @@ -103,6 +106,8 @@ func DisperseTokenString(addresses string, tokens string) { DisperseToken(parsedAddresses, parseTokens) } +// DisperseGnotString receives a string of addresses and a string of amounts +// and parses them to be used in DisperseGnot func DisperseGnotString(addresses string, amounts string) { parsedAddresses, err := parseAddresses(addresses) if err != nil { From 5157b66306a0d8ca255628e559b755eeb94095ed Mon Sep 17 00:00:00 2001 From: Alex Gherasie <68433935+agherasie@users.noreply.github.com> Date: Tue, 30 Jul 2024 16:22:27 +0300 Subject: [PATCH 19/46] Update examples/gno.land/r/demo/disperse/disperse.gno Co-authored-by: Guilhem Fanton <8671905+gfanton@users.noreply.github.com> --- examples/gno.land/r/demo/disperse/disperse.gno | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 7b7687b24c2..da66a1c6c8f 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -81,11 +81,8 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } // Disperse tokens - for i, address := range addresses { - for y, coin := range coins { - if y == i { - tokens.Transfer(coin.Denom, address, uint64(coin.Amount)) - } + for i := 0; i < len(address); i++ { + tokens.Transfer(coins[i].Denom, address[i], uint64(coins[i].Amount)) } } } From 7437f607c6c942090a6398d46a30d024d9b17b44 Mon Sep 17 00:00:00 2001 From: agherasie Date: Tue, 30 Jul 2024 16:28:48 +0300 Subject: [PATCH 20/46] fix(examples): disperse checker function --- .../gno.land/r/demo/disperse/disperse.gno | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index da66a1c6c8f..df43bc014bf 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -6,6 +6,13 @@ import ( tokens "gno.land/r/demo/grc20factory" ) +// Check if number of addresses and coins match +func checkNumAddrValMatch(addresses []std.Address, coins std.Coins) { + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } +} + // DisperseGnot parses receivers and amounts and sends out gnot func DisperseGnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call @@ -18,10 +25,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { } } - // Check if number of addresses and coins match - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } + checkNumAddrValMatch(addresses, coins) // Get address of Disperse realm realmAddr := std.CurrentRealm().Addr() @@ -70,10 +74,7 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } } - // Check if number of addresses and coins match - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } + checkNumAddrValMatch(addresses, coins) // Transfer tokens into the realm for _, coin := range coins { @@ -81,9 +82,8 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } // Disperse tokens - for i := 0; i < len(address); i++ { - tokens.Transfer(coins[i].Denom, address[i], uint64(coins[i].Amount)) - } + for i := 0; i < len(addresses); i++ { + tokens.Transfer(coins[i].Denom, addresses[i], uint64(coins[i].Amount)) } } From 6418040c7439d6176042aca0211fc61497a5e235 Mon Sep 17 00:00:00 2001 From: agherasie Date: Tue, 30 Jul 2024 18:08:04 +0300 Subject: [PATCH 21/46] feat(examples): disperse file tests --- .../gno.land/r/demo/disperse/disperse.gno | 26 ++-- examples/gno.land/r/demo/disperse/errors.gno | 2 +- .../gno.land/r/demo/disperse/z_0_filetest.gno | 32 +++++ .../gno.land/r/demo/disperse/z_1_filetest.gno | 32 +++++ .../gno.land/r/demo/disperse/z_2_filetest.gno | 25 ++++ .../gno.land/r/demo/disperse/z_3_filetest.gno | 45 ++++++ .../gno.land/r/demo/disperse/z_4_filetest.gno | 48 +++++++ gno.land/cmd/gnoland/testdata/disperse.txtar | 131 ------------------ 8 files changed, 195 insertions(+), 146 deletions(-) create mode 100644 examples/gno.land/r/demo/disperse/z_0_filetest.gno create mode 100644 examples/gno.land/r/demo/disperse/z_1_filetest.gno create mode 100644 examples/gno.land/r/demo/disperse/z_2_filetest.gno create mode 100644 examples/gno.land/r/demo/disperse/z_3_filetest.gno create mode 100644 examples/gno.land/r/demo/disperse/z_4_filetest.gno delete mode 100644 gno.land/cmd/gnoland/testdata/disperse.txtar diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index df43bc014bf..2ed60098794 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -3,6 +3,7 @@ package disperse import ( "std" + "gno.land/p/demo/ufmt" tokens "gno.land/r/demo/grc20factory" ) @@ -13,18 +14,21 @@ func checkNumAddrValMatch(addresses []std.Address, coins std.Coins) { } } -// DisperseGnot parses receivers and amounts and sends out gnot -func DisperseGnot(addresses []std.Address, coins std.Coins) { - coinSent := std.GetOrigSend() // get Coins sent with call - caller := std.GetOrigCaller() // get tx sender - - // Check if caller correctly sent positive amount of coins +// Check if caller correctly sent positive amount of coins +func checkCoinAmount(coins std.Coins) { for _, coin := range coins { if coin.Amount <= 0 { panic(ErrNegativeCoinAmount) } } +} +// DisperseGnot parses receivers and amounts and sends out gnot +func DisperseGnot(addresses []std.Address, coins std.Coins) { + coinSent := std.GetOrigSend() // get Coins sent with call + caller := std.GetOrigCaller() // get tx sender + + checkCoinAmount(coins) checkNumAddrValMatch(addresses, coins) // Get address of Disperse realm @@ -44,7 +48,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { } // Check if the realm has zero ugnot - if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { + if banker.GetCoins(realmAddr).AmountOf("ugnot") < coinSent.AmountOf("ugnot") { panic(ErrBalanceNotEqToAmountSent) } @@ -67,13 +71,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { func DisperseToken(addresses []std.Address, coins std.Coins) { caller := std.GetOrigCaller() // get tx sender - // Check if caller correctly sent positive amount of coins - for _, coin := range coins { - if coin.Amount <= 0 { - panic(ErrNegativeCoinAmount) - } - } - + checkCoinAmount(coins) checkNumAddrValMatch(addresses, coins) // Transfer tokens into the realm diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index e9fa5f00e5f..f525274405d 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -7,7 +7,7 @@ var ( 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") - ErrBalanceNotEqToAmountSent = errors.New("balance needs to be equal to zero") + ErrBalanceNotEqToAmountSent = errors.New("balance needs to be equal to amount sent") 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/z_0_filetest.gno b/examples/gno.land/r/demo/disperse/z_0_filetest.gno new file mode 100644 index 00000000000..4e632869dbf --- /dev/null +++ b/examples/gno.land/r/demo/disperse/z_0_filetest.gno @@ -0,0 +1,32 @@ +// SEND: 200ugnot + +package main + +import ( + "std" + + "gno.land/r/demo/disperse" +) + +func main() { + disperseAddr := std.DerivePkgAddr("gno.land/r/demo/disperse") + mainaddr := std.DerivePkgAddr("main") + + std.TestSetOrigPkgAddr(disperseAddr) + std.TestSetOrigCaller(mainaddr) + + banker := std.GetBanker(std.BankerTypeRealmSend) + + mainbal := banker.GetCoins(mainaddr) + println("main before:", mainbal) + + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 200}}) + disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") + + mainbal = banker.GetCoins(mainaddr) + println("main after:", mainbal) +} + +// Output: +// main before: 200000200ugnot +// main after: 200000000ugnot \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/z_1_filetest.gno b/examples/gno.land/r/demo/disperse/z_1_filetest.gno new file mode 100644 index 00000000000..22829d81c9a --- /dev/null +++ b/examples/gno.land/r/demo/disperse/z_1_filetest.gno @@ -0,0 +1,32 @@ +// SEND: 300ugnot + +package main + +import ( + "std" + + "gno.land/r/demo/disperse" +) + +func main() { + disperseAddr := std.DerivePkgAddr("gno.land/r/demo/disperse") + mainaddr := std.DerivePkgAddr("main") + + std.TestSetOrigPkgAddr(disperseAddr) + std.TestSetOrigCaller(mainaddr) + + banker := std.GetBanker(std.BankerTypeRealmSend) + + mainbal := banker.GetCoins(mainaddr) + println("main before:", mainbal) + + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 300}}) + disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") + + mainbal = banker.GetCoins(mainaddr) + println("main after:", mainbal) +} + +// Output: +// main before: 200000300ugnot +// main after: 200000100ugnot \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/z_2_filetest.gno b/examples/gno.land/r/demo/disperse/z_2_filetest.gno new file mode 100644 index 00000000000..1f6105aaba9 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/z_2_filetest.gno @@ -0,0 +1,25 @@ +// SEND: 300ugnot + +package main + +import ( + "std" + + "gno.land/r/demo/disperse" +) + +func main() { + disperseAddr := std.DerivePkgAddr("gno.land/r/demo/disperse") + mainaddr := std.DerivePkgAddr("main") + + std.TestSetOrigPkgAddr(disperseAddr) + std.TestSetOrigCaller(mainaddr) + + banker := std.GetBanker(std.BankerTypeRealmSend) + + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 100}}) + disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") +} + +// Error: +// balance needs to be equal to amount sent \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/z_3_filetest.gno b/examples/gno.land/r/demo/disperse/z_3_filetest.gno new file mode 100644 index 00000000000..f57db05db82 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/z_3_filetest.gno @@ -0,0 +1,45 @@ +// SEND: 300ugnot + +package main + +import ( + "std" + + "gno.land/r/demo/disperse" + tokens "gno.land/r/demo/grc20factory" +) + +func main() { + disperseAddr := std.DerivePkgAddr("gno.land/r/demo/disperse") + mainaddr := std.DerivePkgAddr("main") + beneficiary1 := std.Address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0") + beneficiary2 := std.Address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c") + + std.TestSetOrigPkgAddr(disperseAddr) + std.TestSetOrigCaller(mainaddr) + + banker := std.GetBanker(std.BankerTypeRealmSend) + + tokens.New("test", "TEST", 4, 0, 0) + tokens.Mint("TEST", mainaddr, 200) + + mainbal := tokens.BalanceOf("TEST", mainaddr) + println("main before:", mainbal) + + tokens.Approve("TEST", disperseAddr, 200) + + disperse.DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") + + mainbal = tokens.BalanceOf("TEST", mainaddr) + println("main after:", mainbal) + ben1bal := tokens.BalanceOf("TEST", beneficiary1) + println("beneficiary1:", ben1bal) + ben2bal := tokens.BalanceOf("TEST", beneficiary2) + println("beneficiary2:", ben2bal) +} + +// Output: +// main before: 200 +// main after: 0 +// beneficiary1: 150 +// beneficiary2: 50 \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/z_4_filetest.gno b/examples/gno.land/r/demo/disperse/z_4_filetest.gno new file mode 100644 index 00000000000..477e552b546 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/z_4_filetest.gno @@ -0,0 +1,48 @@ +// SEND: 300ugnot + +package main + +import ( + "std" + + "gno.land/r/demo/disperse" + tokens "gno.land/r/demo/grc20factory" +) + +func main() { + disperseAddr := std.DerivePkgAddr("gno.land/r/demo/disperse") + mainaddr := std.DerivePkgAddr("main") + beneficiary1 := std.Address("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0") + beneficiary2 := std.Address("g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c") + + std.TestSetOrigPkgAddr(disperseAddr) + std.TestSetOrigCaller(mainaddr) + + banker := std.GetBanker(std.BankerTypeRealmSend) + + tokens.New("test1", "TEST1", 4, 0, 0) + tokens.Mint("TEST1", mainaddr, 200) + tokens.New("test2", "TEST2", 4, 0, 0) + tokens.Mint("TEST2", mainaddr, 200) + + mainbal := tokens.BalanceOf("TEST1", mainaddr) + tokens.BalanceOf("TEST2", mainaddr) + println("main before:", mainbal) + + tokens.Approve("TEST1", disperseAddr, 200) + tokens.Approve("TEST2", disperseAddr, 200) + + disperse.DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "200TEST1,200TEST2") + + mainbal = tokens.BalanceOf("TEST1", mainaddr) + tokens.BalanceOf("TEST2", mainaddr) + println("main after:", mainbal) + ben1bal := tokens.BalanceOf("TEST1", beneficiary1) + tokens.BalanceOf("TEST2", beneficiary1) + println("beneficiary1:", ben1bal) + ben2bal := tokens.BalanceOf("TEST1", beneficiary2) + tokens.BalanceOf("TEST2", beneficiary2) + println("beneficiary2:", ben2bal) +} + +// Output: +// main before: 400 +// main after: 0 +// beneficiary1: 200 +// beneficiary2: 200 \ No newline at end of file diff --git a/gno.land/cmd/gnoland/testdata/disperse.txtar b/gno.land/cmd/gnoland/testdata/disperse.txtar deleted file mode 100644 index 9b7eed15266..00000000000 --- a/gno.land/cmd/gnoland/testdata/disperse.txtar +++ /dev/null @@ -1,131 +0,0 @@ -# Test for disperse - -loadpkg gno.land/r/demo/disperse -loadpkg gno.land/r/demo/grc20factory - -gnoland start - -### Execute disperse gnot - -# Verifying default balance -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout 'height: 0' -stdout 'data: "9999990000000ugnot"' - -# Executing disperse gnot -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 - -# Verifying balances after disperse - -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout 'height: 0' -stdout 'data: "9999988999800ugnot"' - -gnokey query bank/balances/g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0 -stdout 'height: 0' -stdout 'data: "150ugnot"' - -gnokey query bank/balances/g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c -stdout 'height: 0' -stdout 'data: "50ugnot"' - -### Execute disperse gnot (leftover gnot) - -# Execute disperse with 'send' of 200ugnot but total of 199ugnot -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 - -# Verify balance after disperse -gnokey query bank/balances/g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 -stdout 'height: 0' -stdout 'data: "9999987999601ugnot"' - -### Execute disperse gnot (errors) - -# Execute disperse with 'send' of 200ugnot but total of 201ugnot -! 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 - -# Execute disperse with negatif coin amount -! 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 "-200" test1 - -# Execute disperse with number of addresses and coin mismatch -! 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" test1 - -### Execute disperse token - -## Signle token - -# Create new token -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 -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 - -# Verify balance in new token after mint -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 -stdout '(2000000000 uint64)' - -# Approve allowance to smart contract -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 - -# Execute disperse token -gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST,50TEST" test1 - -# Verify balances after disperse -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 -stdout '(1999999800 uint64)' - -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 -stdout '(150 uint64)' - -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 -stdout '(50 uint64)' - -## Multiple tokens - -# Create new token 1 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "New" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "test1" -args "TEST1" -args 4 -args 1000000000 -args 0 test1 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Mint" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args 1000000000 test1 - -# Create new token 2 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "New" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "test2" -args "TEST2" -args 4 -args 1000000000 -args 0 test1 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Mint" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" -args 1000000000 test1 - -# Verify balance in new token 1 after mint -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 -stdout '(2000000000 uint64)' - -# Verify balance in new token 2 after mint -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 -stdout '(2000000000 uint64)' - -# Approve allowance token 1 to smart contract -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Approve" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1yryw6qs8h9anvguu4dfdc0u7zh4gvv8vqf59sj" -args "200" test1 - -# Approve allowance token 2 to smart contract -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "Approve" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1yryw6qs8h9anvguu4dfdc0u7zh4gvv8vqf59sj" -args "200" test1 - -# Execute disperse token -gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST1,50TEST2" test1 - -# Verify balances after disperse for token 1 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 -stdout '(1999999850 uint64)' - -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST1" -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0" test1 -stdout '(150 uint64)' - -# Verify balances after disperse for token 2 -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5" test1 -stdout '(1999999950 uint64)' - -gnokey maketx call -pkgpath "gno.land/r/demo/grc20factory" -func "BalanceOf" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "TEST2" -args "g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" test1 -stdout '(50 uint64)' - -## Errors - -# Approve allowance to smart contract -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 - -# Panic if amount negatif -! gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "-150TEST,-50TEST" test1 - -# Panic if tokens and addresses mismatch -! gnokey maketx call -pkgpath "gno.land/r/demo/disperse" -func "DisperseTokenString" -gas-fee 1000000ugnot -gas-wanted 2000000 -send "" -broadcast -chainid=tendermint_test -args "g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c" -args "150TEST" test1 \ No newline at end of file From c30e1f69e654d71ff1be16f1054351de5992bf0f Mon Sep 17 00:00:00 2001 From: agherasie Date: Tue, 30 Jul 2024 18:08:42 +0300 Subject: [PATCH 22/46] chore(examples): fmt disperse --- examples/gno.land/r/demo/disperse/disperse.gno | 1 - examples/gno.land/r/demo/disperse/z_0_filetest.gno | 8 ++++---- examples/gno.land/r/demo/disperse/z_1_filetest.gno | 8 ++++---- examples/gno.land/r/demo/disperse/z_2_filetest.gno | 6 +++--- examples/gno.land/r/demo/disperse/z_3_filetest.gno | 10 +++++----- examples/gno.land/r/demo/disperse/z_4_filetest.gno | 10 +++++----- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 2ed60098794..378ed4d3c3f 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -3,7 +3,6 @@ package disperse import ( "std" - "gno.land/p/demo/ufmt" tokens "gno.land/r/demo/grc20factory" ) diff --git a/examples/gno.land/r/demo/disperse/z_0_filetest.gno b/examples/gno.land/r/demo/disperse/z_0_filetest.gno index 4e632869dbf..d40136eff9f 100644 --- a/examples/gno.land/r/demo/disperse/z_0_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_0_filetest.gno @@ -14,12 +14,12 @@ func main() { std.TestSetOrigPkgAddr(disperseAddr) std.TestSetOrigCaller(mainaddr) - + banker := std.GetBanker(std.BankerTypeRealmSend) - + mainbal := banker.GetCoins(mainaddr) println("main before:", mainbal) - + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 200}}) disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") @@ -29,4 +29,4 @@ func main() { // Output: // main before: 200000200ugnot -// main after: 200000000ugnot \ No newline at end of file +// main after: 200000000ugnot diff --git a/examples/gno.land/r/demo/disperse/z_1_filetest.gno b/examples/gno.land/r/demo/disperse/z_1_filetest.gno index 22829d81c9a..5d13c09f43e 100644 --- a/examples/gno.land/r/demo/disperse/z_1_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_1_filetest.gno @@ -14,12 +14,12 @@ func main() { std.TestSetOrigPkgAddr(disperseAddr) std.TestSetOrigCaller(mainaddr) - + banker := std.GetBanker(std.BankerTypeRealmSend) - + mainbal := banker.GetCoins(mainaddr) println("main before:", mainbal) - + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 300}}) disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") @@ -29,4 +29,4 @@ func main() { // Output: // main before: 200000300ugnot -// main after: 200000100ugnot \ No newline at end of file +// main after: 200000100ugnot diff --git a/examples/gno.land/r/demo/disperse/z_2_filetest.gno b/examples/gno.land/r/demo/disperse/z_2_filetest.gno index 1f6105aaba9..ffe651b524e 100644 --- a/examples/gno.land/r/demo/disperse/z_2_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_2_filetest.gno @@ -14,12 +14,12 @@ func main() { std.TestSetOrigPkgAddr(disperseAddr) std.TestSetOrigCaller(mainaddr) - + banker := std.GetBanker(std.BankerTypeRealmSend) - + banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 100}}) disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") } // Error: -// balance needs to be equal to amount sent \ No newline at end of file +// balance needs to be equal to amount sent diff --git a/examples/gno.land/r/demo/disperse/z_3_filetest.gno b/examples/gno.land/r/demo/disperse/z_3_filetest.gno index f57db05db82..32cdfe9198e 100644 --- a/examples/gno.land/r/demo/disperse/z_3_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_3_filetest.gno @@ -17,15 +17,15 @@ func main() { std.TestSetOrigPkgAddr(disperseAddr) std.TestSetOrigCaller(mainaddr) - + banker := std.GetBanker(std.BankerTypeRealmSend) - + tokens.New("test", "TEST", 4, 0, 0) tokens.Mint("TEST", mainaddr, 200) - + mainbal := tokens.BalanceOf("TEST", mainaddr) println("main before:", mainbal) - + tokens.Approve("TEST", disperseAddr, 200) disperse.DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") @@ -42,4 +42,4 @@ func main() { // main before: 200 // main after: 0 // beneficiary1: 150 -// beneficiary2: 50 \ No newline at end of file +// beneficiary2: 50 diff --git a/examples/gno.land/r/demo/disperse/z_4_filetest.gno b/examples/gno.land/r/demo/disperse/z_4_filetest.gno index 477e552b546..2b3cda2f1fb 100644 --- a/examples/gno.land/r/demo/disperse/z_4_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_4_filetest.gno @@ -17,17 +17,17 @@ func main() { std.TestSetOrigPkgAddr(disperseAddr) std.TestSetOrigCaller(mainaddr) - + banker := std.GetBanker(std.BankerTypeRealmSend) - + tokens.New("test1", "TEST1", 4, 0, 0) tokens.Mint("TEST1", mainaddr, 200) tokens.New("test2", "TEST2", 4, 0, 0) tokens.Mint("TEST2", mainaddr, 200) - + mainbal := tokens.BalanceOf("TEST1", mainaddr) + tokens.BalanceOf("TEST2", mainaddr) println("main before:", mainbal) - + tokens.Approve("TEST1", disperseAddr, 200) tokens.Approve("TEST2", disperseAddr, 200) @@ -45,4 +45,4 @@ func main() { // main before: 400 // main after: 0 // beneficiary1: 200 -// beneficiary2: 200 \ No newline at end of file +// beneficiary2: 200 From 885d791a07e6907fb07615083a76e9af12e18fd8 Mon Sep 17 00:00:00 2001 From: Alex Gherasie <68433935+agherasie@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:02:59 +0300 Subject: [PATCH 23/46] Update examples/gno.land/r/demo/disperse/disperse.gno Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com> --- examples/gno.land/r/demo/disperse/disperse.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 378ed4d3c3f..94dfdc7db84 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -53,7 +53,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { // Send coins for i, _ := range addresses { - banker.SendCoins(realmAddr, addresses[i], std.Coins{coins[i]}) + banker.SendCoins(realmAddr, addresses[i], std.NewCoins(coins[i])) } // Return possible leftover coins From 86b0671575d26b7d2abe8acb93f2701c87ffd6fe Mon Sep 17 00:00:00 2001 From: Alex Gherasie <68433935+agherasie@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:03:13 +0300 Subject: [PATCH 24/46] Update examples/gno.land/r/demo/disperse/disperse.gno Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com> --- examples/gno.land/r/demo/disperse/disperse.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 94dfdc7db84..1e4a695c94d 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -68,7 +68,7 @@ func DisperseGnot(addresses []std.Address, coins std.Coins) { // DisperseToken disperses tokens to multiple addresses func DisperseToken(addresses []std.Address, coins std.Coins) { - caller := std.GetOrigCaller() // get tx sender + caller := std.PrevRealm().Addr() // get tx sender checkCoinAmount(coins) checkNumAddrValMatch(addresses, coins) From 78ee151fa1b3073df7d0c3799aab7c3dac3875d6 Mon Sep 17 00:00:00 2001 From: Alex Gherasie <68433935+agherasie@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:03:20 +0300 Subject: [PATCH 25/46] Update examples/gno.land/r/demo/disperse/util.gno Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com> --- examples/gno.land/r/demo/disperse/util.gno | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index aaea41369d2..c99517afc69 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -15,6 +15,7 @@ func parseAddresses(addresses string) ([]std.Address, error) { if !addr.IsValid() { return nil, ErrInvalidAddress } + ret = append(ret, addr) } From de01899cca34411b963f2ece1f62922d846739d2 Mon Sep 17 00:00:00 2001 From: Alex Gherasie <68433935+agherasie@users.noreply.github.com> Date: Thu, 8 Aug 2024 00:01:31 +0300 Subject: [PATCH 26/46] Update examples/gno.land/r/demo/disperse/disperse.gno Co-authored-by: Leon Hudak <33522493+leohhhn@users.noreply.github.com> --- examples/gno.land/r/demo/disperse/disperse.gno | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 1e4a695c94d..782779cb393 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -25,7 +25,7 @@ func checkCoinAmount(coins std.Coins) { // DisperseGnot parses receivers and amounts and sends out gnot func DisperseGnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call - caller := std.GetOrigCaller() // get tx sender + caller := std.PrevRealm().Addr() // get tx sender checkCoinAmount(coins) checkNumAddrValMatch(addresses, coins) From 28e029ae96d00f9360ca9b9c53a513bed6adbc66 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:10:42 +0300 Subject: [PATCH 27/46] fix(examples): disperse helper functions at bottom --- .../gno.land/r/demo/disperse/disperse.gno | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 782779cb393..3dbc1576c40 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -6,22 +6,6 @@ import ( tokens "gno.land/r/demo/grc20factory" ) -// Check if number of addresses and coins match -func checkNumAddrValMatch(addresses []std.Address, coins std.Coins) { - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } -} - -// Check if caller correctly sent positive amount of coins -func checkCoinAmount(coins std.Coins) { - for _, coin := range coins { - if coin.Amount <= 0 { - panic(ErrNegativeCoinAmount) - } - } -} - // DisperseGnot parses receivers and amounts and sends out gnot func DisperseGnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call @@ -120,3 +104,19 @@ func DisperseGnotString(addresses string, amounts string) { DisperseGnot(parsedAddresses, coins) } + +// Check if number of addresses and coins match +func checkNumAddrValMatch(addresses []std.Address, coins std.Coins) { + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } +} + +// Check if caller correctly sent positive amount of coins +func checkCoinAmount(coins std.Coins) { + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } +} \ No newline at end of file From d41e44cb3b67eec14f3c489adcbfb5c641396f28 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:13:52 +0300 Subject: [PATCH 28/46] fix(examples): disperse ugnot instead of gnot --- examples/gno.land/r/demo/disperse/disperse.gno | 12 ++++++------ examples/gno.land/r/demo/disperse/z_0_filetest.gno | 2 +- examples/gno.land/r/demo/disperse/z_1_filetest.gno | 2 +- examples/gno.land/r/demo/disperse/z_2_filetest.gno | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 3dbc1576c40..768d6d3a879 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -6,8 +6,8 @@ import ( tokens "gno.land/r/demo/grc20factory" ) -// DisperseGnot parses receivers and amounts and sends out gnot -func DisperseGnot(addresses []std.Address, coins std.Coins) { +// DisperseUgnot parses receivers and amounts and sends out ugnot +func DisperseUgnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call caller := std.PrevRealm().Addr() // get tx sender @@ -84,9 +84,9 @@ func DisperseTokenString(addresses string, tokens string) { DisperseToken(parsedAddresses, parseTokens) } -// DisperseGnotString receives a string of addresses and a string of amounts -// and parses them to be used in DisperseGnot -func DisperseGnotString(addresses string, amounts string) { +// DisperseUgnotString receives a string of addresses and a string of amounts +// and parses them to be used in DisperseUgnot +func DisperseUgnotString(addresses string, amounts string) { parsedAddresses, err := parseAddresses(addresses) if err != nil { panic(err) @@ -102,7 +102,7 @@ func DisperseGnotString(addresses string, amounts string) { coins[i] = std.Coin{"ugnot", amount} } - DisperseGnot(parsedAddresses, coins) + DisperseUgnot(parsedAddresses, coins) } // Check if number of addresses and coins match diff --git a/examples/gno.land/r/demo/disperse/z_0_filetest.gno b/examples/gno.land/r/demo/disperse/z_0_filetest.gno index d40136eff9f..62a34cfdf26 100644 --- a/examples/gno.land/r/demo/disperse/z_0_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_0_filetest.gno @@ -21,7 +21,7 @@ func main() { println("main before:", mainbal) banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 200}}) - disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") + disperse.DisperseUgnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") mainbal = banker.GetCoins(mainaddr) println("main after:", mainbal) diff --git a/examples/gno.land/r/demo/disperse/z_1_filetest.gno b/examples/gno.land/r/demo/disperse/z_1_filetest.gno index 5d13c09f43e..1e042d320f6 100644 --- a/examples/gno.land/r/demo/disperse/z_1_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_1_filetest.gno @@ -21,7 +21,7 @@ func main() { println("main before:", mainbal) banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 300}}) - disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") + disperse.DisperseUgnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") mainbal = banker.GetCoins(mainaddr) println("main after:", mainbal) diff --git a/examples/gno.land/r/demo/disperse/z_2_filetest.gno b/examples/gno.land/r/demo/disperse/z_2_filetest.gno index ffe651b524e..393f70aed12 100644 --- a/examples/gno.land/r/demo/disperse/z_2_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_2_filetest.gno @@ -18,7 +18,7 @@ func main() { banker := std.GetBanker(std.BankerTypeRealmSend) banker.SendCoins(mainaddr, disperseAddr, std.Coins{{"ugnot", 100}}) - disperse.DisperseGnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") + disperse.DisperseUgnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") } // Error: From 7f8aa8485594bb3489678ee59173f6fcd9afeec8 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:15:57 +0300 Subject: [PATCH 29/46] fix(examples): disperse global state realmAddr --- examples/gno.land/r/demo/disperse/disperse.gno | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 768d6d3a879..e26019f8acb 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -6,6 +6,9 @@ import ( tokens "gno.land/r/demo/grc20factory" ) +// Get address of Disperse realm +var realmAddr = std.CurrentRealm().Addr() + // DisperseUgnot parses receivers and amounts and sends out ugnot func DisperseUgnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call @@ -14,9 +17,6 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { checkCoinAmount(coins) checkNumAddrValMatch(addresses, coins) - // Get address of Disperse realm - realmAddr := std.CurrentRealm().Addr() - // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) From cfc2ee9eb60a34897f3b1bbf3f8a961184562f1f Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:17:52 +0300 Subject: [PATCH 30/46] fix(examples): disperse common helper function --- examples/gno.land/r/demo/disperse/disperse.gno | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index e26019f8acb..df467ed2999 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -14,8 +14,7 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() // get Coins sent with call caller := std.PrevRealm().Addr() // get tx sender - checkCoinAmount(coins) - checkNumAddrValMatch(addresses, coins) + checkCoinAmount(addresses, coins) // Get Banker banker := std.GetBanker(std.BankerTypeOrigSend) @@ -54,8 +53,7 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { func DisperseToken(addresses []std.Address, coins std.Coins) { caller := std.PrevRealm().Addr() // get tx sender - checkCoinAmount(coins) - checkNumAddrValMatch(addresses, coins) + checkCoinAmount(addresses, coins) // Transfer tokens into the realm for _, coin := range coins { @@ -105,15 +103,13 @@ func DisperseUgnotString(addresses string, amounts string) { DisperseUgnot(parsedAddresses, coins) } -// Check if number of addresses and coins match -func checkNumAddrValMatch(addresses []std.Address, coins std.Coins) { + +// Check if caller correctly sent coins +func checkCoinAmount(addresses []std.Address, coins std.Coins) { if len(addresses) != len(coins) { panic(ErrNumAddrValMismatch) } -} -// Check if caller correctly sent positive amount of coins -func checkCoinAmount(coins std.Coins) { for _, coin := range coins { if coin.Amount <= 0 { panic(ErrNegativeCoinAmount) From d91b227305b9dbc33b8d2b4bdeb3b716c952546f Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:24:53 +0300 Subject: [PATCH 31/46] feat(examples): disperse doc.gno --- examples/gno.land/r/demo/disperse/doc.gno | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/gno.land/r/demo/disperse/doc.gno diff --git a/examples/gno.land/r/demo/disperse/doc.gno b/examples/gno.land/r/demo/disperse/doc.gno new file mode 100644 index 00000000000..1f19f16d858 --- /dev/null +++ b/examples/gno.land/r/demo/disperse/doc.gno @@ -0,0 +1,19 @@ +// Package disperse provides methods to disperse coins or GRC20 tokens among multiple addresses. +// +// The disperse package is an implementation of an existing service that allows users to send coins or GRC20 tokens to multiple addresses +// on the Ethereum blockchain. +// +// Usage: +// To use disperse, you can either use `DisperseUgnot` to send coins or `DisperseToken` to send GRC20 tokens to multiple addresses. +// +// Example: +// Dispersing 200 coins to two addresses: +// - DisperseUgnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") +// Dispersing 200 worth of a GRC20 token "TEST" to two addresses: +// - DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") +// +// Reference: +// - [the original dispere app](https://disperse.app/) +// - [the original disperse app on etherscan](https://etherscan.io/address/0xd152f549545093347a162dce210e7293f1452150#code) +// - [the gno disperse web app](https://gno-disperse.netlify.app/) +package disperse // import "gno.land/r/demo/disperse" \ No newline at end of file From 5fd031e767718344020c2b2000d043727c847109 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:25:43 +0300 Subject: [PATCH 32/46] fix(examples): disperse err format --- examples/gno.land/r/demo/disperse/errors.gno | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index f525274405d..0f5c1abaffa 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,11 +3,11 @@ 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") - ErrBalanceNotEqToAmountSent = errors.New("balance needs to be equal to amount sent") - ErrArgLenAndSentLenMismatch = errors.New("mismatch between coins sent and args called") - ErrWrongAmount = errors.New("wrong coin amount") + ErrNotEnoughCoin = errors.New("disperse: not enough coin sent in") + ErrNumAddrValMismatch = errors.New("disperse: number of addresses and values to send doesn't match") + ErrInvalidAddress = errors.New("disperse: invalid address") + ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") + ErrBalanceNotEqToAmountSent = errors.New("disperse: balance needs to be equal to amount sent") + ErrArgLenAndSentLenMismatch = errors.New("disperse: mismatch between coins sent and args called") + ErrWrongAmount = errors.New("disperse: wrong coin amount") ) From 042ec411f405c1e18227a7731159b80f5f7ab5b3 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:28:04 +0300 Subject: [PATCH 33/46] fix(examples): disperse grc20 function name --- examples/gno.land/r/demo/disperse/disperse.gno | 12 ++++++------ examples/gno.land/r/demo/disperse/doc.gno | 4 ++-- examples/gno.land/r/demo/disperse/z_2_filetest.gno | 2 +- examples/gno.land/r/demo/disperse/z_3_filetest.gno | 2 +- examples/gno.land/r/demo/disperse/z_4_filetest.gno | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index df467ed2999..0d7d6669816 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -49,8 +49,8 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { } } -// DisperseToken disperses tokens to multiple addresses -func DisperseToken(addresses []std.Address, coins std.Coins) { +// DisperseGRC20 disperses tokens to multiple addresses +func DisperseGRC20(addresses []std.Address, coins std.Coins) { caller := std.PrevRealm().Addr() // get tx sender checkCoinAmount(addresses, coins) @@ -66,9 +66,9 @@ func DisperseToken(addresses []std.Address, coins std.Coins) { } } -// DisperseTokenString receives a string of addresses and a string of tokens -// and parses them to be used in DisperseToken -func DisperseTokenString(addresses string, tokens string) { +// DisperseGRC20String receives a string of addresses and a string of tokens +// and parses them to be used in DisperseGRC20 +func DisperseGRC20String(addresses string, tokens string) { parsedAddresses, err := parseAddresses(addresses) if err != nil { panic(err) @@ -79,7 +79,7 @@ func DisperseTokenString(addresses string, tokens string) { panic(err) } - DisperseToken(parsedAddresses, parseTokens) + DisperseGRC20(parsedAddresses, parseTokens) } // DisperseUgnotString receives a string of addresses and a string of amounts diff --git a/examples/gno.land/r/demo/disperse/doc.gno b/examples/gno.land/r/demo/disperse/doc.gno index 1f19f16d858..b754050e469 100644 --- a/examples/gno.land/r/demo/disperse/doc.gno +++ b/examples/gno.land/r/demo/disperse/doc.gno @@ -4,13 +4,13 @@ // on the Ethereum blockchain. // // Usage: -// To use disperse, you can either use `DisperseUgnot` to send coins or `DisperseToken` to send GRC20 tokens to multiple addresses. +// To use disperse, you can either use `DisperseUgnot` to send coins or `DisperseGRC20` to send GRC20 tokens to multiple addresses. // // Example: // Dispersing 200 coins to two addresses: // - DisperseUgnotString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150,50") // Dispersing 200 worth of a GRC20 token "TEST" to two addresses: -// - DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") +// - DisperseGRC20String("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") // // Reference: // - [the original dispere app](https://disperse.app/) diff --git a/examples/gno.land/r/demo/disperse/z_2_filetest.gno b/examples/gno.land/r/demo/disperse/z_2_filetest.gno index 393f70aed12..772c96e1d00 100644 --- a/examples/gno.land/r/demo/disperse/z_2_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_2_filetest.gno @@ -22,4 +22,4 @@ func main() { } // Error: -// balance needs to be equal to amount sent +// disperse: balance needs to be equal to amount sent diff --git a/examples/gno.land/r/demo/disperse/z_3_filetest.gno b/examples/gno.land/r/demo/disperse/z_3_filetest.gno index 32cdfe9198e..eabed52fb38 100644 --- a/examples/gno.land/r/demo/disperse/z_3_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_3_filetest.gno @@ -28,7 +28,7 @@ func main() { tokens.Approve("TEST", disperseAddr, 200) - disperse.DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") + disperse.DisperseGRC20String("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "150TEST,50TEST") mainbal = tokens.BalanceOf("TEST", mainaddr) println("main after:", mainbal) diff --git a/examples/gno.land/r/demo/disperse/z_4_filetest.gno b/examples/gno.land/r/demo/disperse/z_4_filetest.gno index 2b3cda2f1fb..ebf4bed4473 100644 --- a/examples/gno.land/r/demo/disperse/z_4_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_4_filetest.gno @@ -31,7 +31,7 @@ func main() { tokens.Approve("TEST1", disperseAddr, 200) tokens.Approve("TEST2", disperseAddr, 200) - disperse.DisperseTokenString("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "200TEST1,200TEST2") + disperse.DisperseGRC20String("g1dmt3sa5ucvecxuhf3j6ne5r0e3z4x7h6c03xc0,g1akeqsvhucjt8gf5yupyzjxsjd29wv8fayng37c", "200TEST1,200TEST2") mainbal = tokens.BalanceOf("TEST1", mainaddr) + tokens.BalanceOf("TEST2", mainaddr) println("main after:", mainbal) From 20a428c4d6351433cdd95652fbf356e7548124b0 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:32:19 +0300 Subject: [PATCH 34/46] fix(examples): disperse comments --- examples/gno.land/r/demo/disperse/disperse.gno | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 0d7d6669816..38dad1f51dd 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -11,13 +11,16 @@ var realmAddr = std.CurrentRealm().Addr() // DisperseUgnot parses receivers and amounts and sends out ugnot func DisperseUgnot(addresses []std.Address, coins std.Coins) { - coinSent := std.GetOrigSend() // get Coins sent with call - caller := std.PrevRealm().Addr() // get tx sender + coinSent := std.GetOrigSend() + caller := std.PrevRealm().Addr() + banker := std.GetBanker(std.BankerTypeOrigSend) - checkCoinAmount(addresses, coins) + // Check if caller sent enough coins to match the function call arguments + if banker.GetCoins(realmAddr).AmountOf("ugnot") < coinSent.AmountOf("ugnot") { + panic(ErrBalanceNotEqToAmountSent) + } - // Get Banker - banker := std.GetBanker(std.BankerTypeOrigSend) + checkCoinAmount(addresses, coins) var totalAmount int64 for _, coin := range coins { @@ -29,11 +32,6 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { panic(ErrWrongAmount) } - // Check if the realm has zero ugnot - if banker.GetCoins(realmAddr).AmountOf("ugnot") < coinSent.AmountOf("ugnot") { - panic(ErrBalanceNotEqToAmountSent) - } - // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.NewCoins(coins[i])) From 7928cb1363c1ce1308c2f927c5bb310553982dd7 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:42:46 +0300 Subject: [PATCH 35/46] fix(examples): mistake in comment --- examples/gno.land/r/demo/disperse/disperse.gno | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 38dad1f51dd..041bfe92a06 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -3,6 +3,7 @@ package disperse import ( "std" + "gno.land/p/demo/ufmt" tokens "gno.land/r/demo/grc20factory" ) @@ -15,8 +16,8 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { caller := std.PrevRealm().Addr() banker := std.GetBanker(std.BankerTypeOrigSend) - // Check if caller sent enough coins to match the function call arguments - if banker.GetCoins(realmAddr).AmountOf("ugnot") < coinSent.AmountOf("ugnot") { + // Check if realm balance is equal to amount sent + if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { panic(ErrBalanceNotEqToAmountSent) } From f2fa4973b38192d71c4b87073a02ed69b1bb9052 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:44:59 +0300 Subject: [PATCH 36/46] fix(examples): disperse document leftover functionality --- examples/gno.land/r/demo/disperse/disperse.gno | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 041bfe92a06..d809b6c358c 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -11,6 +11,8 @@ import ( var realmAddr = std.CurrentRealm().Addr() // DisperseUgnot parses receivers and amounts and sends out ugnot +// The function will send out the coins to the addresses and return the leftover coins to the caller +// if there are any to return func DisperseUgnot(addresses []std.Address, coins std.Coins) { coinSent := std.GetOrigSend() caller := std.PrevRealm().Addr() From b70a621ddd02188e406401486afde44450d85051 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 00:48:25 +0300 Subject: [PATCH 37/46] fix(examples): disperse unnecessary error check --- examples/gno.land/r/demo/disperse/disperse.gno | 5 ----- examples/gno.land/r/demo/disperse/errors.gno | 1 - 2 files changed, 6 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index d809b6c358c..d0a66bf6143 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -30,11 +30,6 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { totalAmount += coin.Amount } - // Check if total amount of coins sent is bigger or equal to total amount of coins to be sent - if totalAmount > coinSent.AmountOf("ugnot") { - panic(ErrWrongAmount) - } - // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.NewCoins(coins[i])) diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 0f5c1abaffa..74a4d1bad49 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -9,5 +9,4 @@ var ( ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") ErrBalanceNotEqToAmountSent = errors.New("disperse: balance needs to be equal to amount sent") ErrArgLenAndSentLenMismatch = errors.New("disperse: mismatch between coins sent and args called") - ErrWrongAmount = errors.New("disperse: wrong coin amount") ) From 56d07169fe92cd10363165713edf9f0f88ba4958 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 12:37:40 +0300 Subject: [PATCH 38/46] fix(examples): disperse not using std.Coins for tokens --- .../gno.land/r/demo/disperse/disperse.gno | 45 ++++++++----------- examples/gno.land/r/demo/disperse/util.gno | 12 ++--- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index d0a66bf6143..f7c72d7963f 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -23,7 +23,15 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { panic(ErrBalanceNotEqToAmountSent) } - checkCoinAmount(addresses, coins) + if len(addresses) != len(coins) { + panic(ErrNumAddrValMismatch) + } + + for _, coin := range coins { + if coin.Amount <= 0 { + panic(ErrNegativeCoinAmount) + } + } var totalAmount int64 for _, coin := range coins { @@ -46,19 +54,18 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { } // DisperseGRC20 disperses tokens to multiple addresses -func DisperseGRC20(addresses []std.Address, coins std.Coins) { - caller := std.PrevRealm().Addr() // get tx sender - - checkCoinAmount(addresses, coins) +func DisperseGRC20(addresses []std.Address, amounts []uint64, symbols []string) { + caller := std.PrevRealm().Addr() - // Transfer tokens into the realm - for _, coin := range coins { - tokens.TransferFrom(coin.Denom, caller, std.CurrentRealm().Addr(), uint64(coin.Amount)) + if (len(addresses) != len(amounts)) || (len(amounts) != len(symbols)) { + panic(ErrArgLenAndSentLenMismatch) } - // Disperse tokens for i := 0; i < len(addresses); i++ { - tokens.Transfer(coins[i].Denom, addresses[i], uint64(coins[i].Amount)) + // Transfer tokens into the realm + tokens.TransferFrom(symbols[i], caller, std.CurrentRealm().Addr(), amounts[i]) + // Transfer tokens to the addresses + tokens.Transfer(symbols[i], addresses[i], uint64(amounts[i])) } } @@ -70,12 +77,12 @@ func DisperseGRC20String(addresses string, tokens string) { panic(err) } - parseTokens, err := parseTokens(tokens) + parsedAmounts, parsedSymbols, err := parseTokens(tokens) if err != nil { panic(err) } - DisperseGRC20(parsedAddresses, parseTokens) + DisperseGRC20(parsedAddresses, parsedAmounts, parsedSymbols) } // DisperseUgnotString receives a string of addresses and a string of amounts @@ -98,17 +105,3 @@ func DisperseUgnotString(addresses string, amounts string) { DisperseUgnot(parsedAddresses, coins) } - - -// Check if caller correctly sent coins -func checkCoinAmount(addresses []std.Address, coins std.Coins) { - if len(addresses) != len(coins) { - panic(ErrNumAddrValMismatch) - } - - for _, coin := range coins { - if coin.Amount <= 0 { - panic(ErrNegativeCoinAmount) - } - } -} \ No newline at end of file diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index c99517afc69..f7256f0c2f4 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -33,19 +33,21 @@ func splitString(input string) (string, string) { return input[:pos], input[pos:] } -func parseTokens(tokens string) ([]std.Coin, error) { - var ret []std.Coin +func parseTokens(tokens string) ([]uint64, []string, error) { + var amounts []uint64 + var symbols []string for _, token := range strings.Split(tokens, ",") { amountStr, symbol := splitString(token) amount, _ := strconv.Atoi(amountStr) if amount < 0 { - return nil, ErrNegativeCoinAmount + return nil, nil, ErrNegativeCoinAmount } - ret = append(ret, std.Coin{symbol, int64(amount)}) + amounts = append(amounts, uint64(amount)) + symbols = append(symbols, symbol) } - return ret, nil + return amounts, symbols, nil } func parseAmounts(amounts string) ([]int64, error) { From 73f42ff52c3d0de751e92f2186468672aa1373de Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 12:38:39 +0300 Subject: [PATCH 39/46] chore(examples): fmt --- examples/gno.land/r/demo/disperse/disperse.gno | 1 - examples/gno.land/r/demo/disperse/doc.gno | 4 ++-- examples/gno.land/r/demo/disperse/util.gno | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index f7c72d7963f..9bb68da33f8 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -3,7 +3,6 @@ package disperse import ( "std" - "gno.land/p/demo/ufmt" tokens "gno.land/r/demo/grc20factory" ) diff --git a/examples/gno.land/r/demo/disperse/doc.gno b/examples/gno.land/r/demo/disperse/doc.gno index b754050e469..100aa92cb3d 100644 --- a/examples/gno.land/r/demo/disperse/doc.gno +++ b/examples/gno.land/r/demo/disperse/doc.gno @@ -1,6 +1,6 @@ // Package disperse provides methods to disperse coins or GRC20 tokens among multiple addresses. // -// The disperse package is an implementation of an existing service that allows users to send coins or GRC20 tokens to multiple addresses +// The disperse package is an implementation of an existing service that allows users to send coins or GRC20 tokens to multiple addresses // on the Ethereum blockchain. // // Usage: @@ -16,4 +16,4 @@ // - [the original dispere app](https://disperse.app/) // - [the original disperse app on etherscan](https://etherscan.io/address/0xd152f549545093347a162dce210e7293f1452150#code) // - [the gno disperse web app](https://gno-disperse.netlify.app/) -package disperse // import "gno.land/r/demo/disperse" \ No newline at end of file +package disperse // import "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 index f7256f0c2f4..1f56623dcb5 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -15,7 +15,7 @@ func parseAddresses(addresses string) ([]std.Address, error) { if !addr.IsValid() { return nil, ErrInvalidAddress } - + ret = append(ret, addr) } From 0a87b66f6037f03a688651a62589e878f1af161b Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 12:41:14 +0300 Subject: [PATCH 40/46] chore(examples): disperse new line after block --- examples/gno.land/r/demo/disperse/util.gno | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/gno.land/r/demo/disperse/util.gno b/examples/gno.land/r/demo/disperse/util.gno index 1f56623dcb5..7101522572d 100644 --- a/examples/gno.land/r/demo/disperse/util.gno +++ b/examples/gno.land/r/demo/disperse/util.gno @@ -43,6 +43,7 @@ func parseTokens(tokens string) ([]uint64, []string, error) { if amount < 0 { return nil, nil, ErrNegativeCoinAmount } + amounts = append(amounts, uint64(amount)) symbols = append(symbols, symbol) } @@ -58,6 +59,7 @@ func parseAmounts(amounts string) ([]int64, error) { if amount < 0 { return nil, ErrNegativeCoinAmount } + ret = append(ret, int64(amount)) } From dcd81fe1c62445f8924ea0b07716eb68adb079b9 Mon Sep 17 00:00:00 2001 From: agherasie Date: Thu, 8 Aug 2024 12:42:38 +0300 Subject: [PATCH 41/46] feat(examples): disperse comment on approval --- examples/gno.land/r/demo/disperse/disperse.gno | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 9bb68da33f8..d9151cdb82f 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -53,6 +53,8 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { } // DisperseGRC20 disperses tokens to multiple addresses +// Note that it is necessary to approve the realm to spend the tokens before calling this function +// see the corresponding filetests for examples func DisperseGRC20(addresses []std.Address, amounts []uint64, symbols []string) { caller := std.PrevRealm().Addr() From d05632a7711b9629469fc981cf295631ac0a9a57 Mon Sep 17 00:00:00 2001 From: agherasie Date: Fri, 16 Aug 2024 15:36:07 +0200 Subject: [PATCH 42/46] fix(examples): redundant disperse logic --- examples/gno.land/r/demo/disperse/disperse.gno | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index d9151cdb82f..66a0d2a4532 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -62,11 +62,8 @@ func DisperseGRC20(addresses []std.Address, amounts []uint64, symbols []string) panic(ErrArgLenAndSentLenMismatch) } - for i := 0; i < len(addresses); i++ { - // Transfer tokens into the realm - tokens.TransferFrom(symbols[i], caller, std.CurrentRealm().Addr(), amounts[i]) - // Transfer tokens to the addresses - tokens.Transfer(symbols[i], addresses[i], uint64(amounts[i])) + for i := 0; i < len(addresses); i++ { + tokens.TransferFrom(symbols[i], caller, addresses[i], amounts[i]) } } From 286da8304a9846f54fcf6fcab35b66d433bcc1db Mon Sep 17 00:00:00 2001 From: agherasie Date: Fri, 16 Aug 2024 16:11:01 +0200 Subject: [PATCH 43/46] fix(examples): disperse using NewCoin --- examples/gno.land/r/demo/disperse/disperse.gno | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 66a0d2a4532..ea2f215bfc8 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -46,7 +46,7 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { for _, coin := range coinSent { leftoverAmt := banker.GetCoins(realmAddr).AmountOf(coin.Denom) if leftoverAmt > 0 { - send := std.Coins{{coin.Denom, leftoverAmt}} + send := std.Coins{std.NewCoin(coin.Denom, leftoverAmt)} banker.SendCoins(realmAddr, caller, send) } } @@ -98,7 +98,7 @@ func DisperseUgnotString(addresses string, amounts string) { coins := make(std.Coins, len(parsedAmounts)) for i, amount := range parsedAmounts { - coins[i] = std.Coin{"ugnot", amount} + coins[i] = std.NewCoin("ugnot", amount) } DisperseUgnot(parsedAddresses, coins) From 128a8e87132ac05b4759e8c8369301ea642f9d06 Mon Sep 17 00:00:00 2001 From: agherasie Date: Fri, 16 Aug 2024 17:09:31 +0200 Subject: [PATCH 44/46] feat(examples): disperse check denoms per sent and called --- examples/gno.land/r/demo/disperse/disperse.gno | 9 ++++----- examples/gno.land/r/demo/disperse/errors.gno | 4 ++-- examples/gno.land/r/demo/disperse/z_2_filetest.gno | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index ea2f215bfc8..24e1df46c9e 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -17,11 +17,6 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { caller := std.PrevRealm().Addr() banker := std.GetBanker(std.BankerTypeOrigSend) - // Check if realm balance is equal to amount sent - if banker.GetCoins(realmAddr).AmountOf("ugnot") != coinSent.AmountOf("ugnot") { - panic(ErrBalanceNotEqToAmountSent) - } - if len(addresses) != len(coins) { panic(ErrNumAddrValMismatch) } @@ -30,6 +25,10 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { if coin.Amount <= 0 { panic(ErrNegativeCoinAmount) } + + if banker.GetCoins(realmAddr).AmountOf(coin.Denom) < coin.Amount { + panic(ErrMismatchBetweenSentAndParams) + } } var totalAmount int64 diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 74a4d1bad49..781b6f34973 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -6,7 +6,7 @@ var ( ErrNotEnoughCoin = errors.New("disperse: not enough coin sent in") ErrNumAddrValMismatch = errors.New("disperse: number of addresses and values to send doesn't match") ErrInvalidAddress = errors.New("disperse: invalid address") - ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") - ErrBalanceNotEqToAmountSent = errors.New("disperse: balance needs to be equal to amount sent") + ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") + ErrMismatchBetweenSentAndParams = errors.New("disperse: mismatch between coins sent and params called") ErrArgLenAndSentLenMismatch = errors.New("disperse: mismatch between coins sent and args called") ) diff --git a/examples/gno.land/r/demo/disperse/z_2_filetest.gno b/examples/gno.land/r/demo/disperse/z_2_filetest.gno index 772c96e1d00..163bb2fc1ab 100644 --- a/examples/gno.land/r/demo/disperse/z_2_filetest.gno +++ b/examples/gno.land/r/demo/disperse/z_2_filetest.gno @@ -22,4 +22,4 @@ func main() { } // Error: -// disperse: balance needs to be equal to amount sent +// disperse: mismatch between coins sent and params called From 5b0ef03b0caea3b31ee087e1d016f961910554a6 Mon Sep 17 00:00:00 2001 From: agherasie Date: Fri, 16 Aug 2024 17:10:17 +0200 Subject: [PATCH 45/46] fix(examples): remove unused logic --- examples/gno.land/r/demo/disperse/disperse.gno | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 24e1df46c9e..79af4c8380c 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -29,13 +29,8 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { if banker.GetCoins(realmAddr).AmountOf(coin.Denom) < coin.Amount { panic(ErrMismatchBetweenSentAndParams) } - } - - var totalAmount int64 - for _, coin := range coins { - totalAmount += coin.Amount - } - + } + // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.NewCoins(coins[i])) From a6e970df6a22c6630b29d8ba70fe5ba239b64697 Mon Sep 17 00:00:00 2001 From: agherasie Date: Fri, 16 Aug 2024 17:23:33 +0200 Subject: [PATCH 46/46] chore(examples): fmt disperse --- examples/gno.land/r/demo/disperse/disperse.gno | 8 ++++---- examples/gno.land/r/demo/disperse/errors.gno | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/gno.land/r/demo/disperse/disperse.gno b/examples/gno.land/r/demo/disperse/disperse.gno index 79af4c8380c..0dc833dda95 100644 --- a/examples/gno.land/r/demo/disperse/disperse.gno +++ b/examples/gno.land/r/demo/disperse/disperse.gno @@ -29,8 +29,8 @@ func DisperseUgnot(addresses []std.Address, coins std.Coins) { if banker.GetCoins(realmAddr).AmountOf(coin.Denom) < coin.Amount { panic(ErrMismatchBetweenSentAndParams) } - } - + } + // Send coins for i, _ := range addresses { banker.SendCoins(realmAddr, addresses[i], std.NewCoins(coins[i])) @@ -56,8 +56,8 @@ func DisperseGRC20(addresses []std.Address, amounts []uint64, symbols []string) panic(ErrArgLenAndSentLenMismatch) } - for i := 0; i < len(addresses); i++ { - tokens.TransferFrom(symbols[i], caller, addresses[i], amounts[i]) + for i := 0; i < len(addresses); i++ { + tokens.TransferFrom(symbols[i], caller, addresses[i], amounts[i]) } } diff --git a/examples/gno.land/r/demo/disperse/errors.gno b/examples/gno.land/r/demo/disperse/errors.gno index 781b6f34973..c054e658651 100644 --- a/examples/gno.land/r/demo/disperse/errors.gno +++ b/examples/gno.land/r/demo/disperse/errors.gno @@ -3,10 +3,10 @@ package disperse import "errors" var ( - ErrNotEnoughCoin = errors.New("disperse: not enough coin sent in") - ErrNumAddrValMismatch = errors.New("disperse: number of addresses and values to send doesn't match") - ErrInvalidAddress = errors.New("disperse: invalid address") - ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") + ErrNotEnoughCoin = errors.New("disperse: not enough coin sent in") + ErrNumAddrValMismatch = errors.New("disperse: number of addresses and values to send doesn't match") + ErrInvalidAddress = errors.New("disperse: invalid address") + ErrNegativeCoinAmount = errors.New("disperse: coin amount cannot be negative") ErrMismatchBetweenSentAndParams = errors.New("disperse: mismatch between coins sent and params called") - ErrArgLenAndSentLenMismatch = errors.New("disperse: mismatch between coins sent and args called") + ErrArgLenAndSentLenMismatch = errors.New("disperse: mismatch between coins sent and args called") )