From 48afd4f4b0b59284bdf49b5fb0cccb16d00fc857 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Fri, 4 Aug 2023 12:41:14 +0200 Subject: [PATCH] chore: fixup --- .../p/demo/subscription/subscription.gno | 42 ++++++++++++++++++- .../p/demo/subscription/subscription_test.gno | 8 +++- .../r/demo/art/millipede/millipede.gno | 5 +++ .../r/demo/art/millipede/millipede_test.gno | 11 +++++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/examples/gno.land/p/demo/subscription/subscription.gno b/examples/gno.land/p/demo/subscription/subscription.gno index ae70981dae0..c3a0a0e22ba 100644 --- a/examples/gno.land/p/demo/subscription/subscription.gno +++ b/examples/gno.land/p/demo/subscription/subscription.gno @@ -1,11 +1,51 @@ -package subscription +// Package subscription provides a basic Gno library for managing recurring +// subscriptions. +// The library allows users to create and manage different subscription models +// to control access to specific features or resources. +// +// Example Usage: +// +// import "gno.land/p/demo/subscription" +// +// // Duration of 30 days and amount of 1000 units subscription.New +// var subs = subscription.NewRecurringSubscription(time.Hour*24*30, 1000) +// +// func SomeHandler() { +// subs.CheckOrigCaller() +// +// // Proceed with handling the request that requires a subscription +// // ... +// } +package subscription // import "gno.land/p/demo/subscription" +// RecurringSubscription represents a recurring subscription model with a given +// duration and amount. type RecurringSubscription struct { duration time.Duration amount int64 subs *avl.Tree // std.Address -> time.Time } +// NewRecurringSubscription creates a new instance of RecurringSubscription with +// the specified duration and amount. +// +// The subs parameter is an AVL tree that holds the subscription status for each +// user (std.Address) with their expiration time (time.Time). +func NewRecurringSubscription(duration time.Duration, amount int64) *RecurringSubscription { + return &RecurringSubscription{ + duration: duration, + amount: amount, + subs: avl.NewTree(), + } +} + +// CheckOrigCaller checks the subscription status of the original caller. +// +// If the original caller does not have an active subscription or the +// subscription has expired, it panics with an error message indicating the +// required payment amount. +// +// This function should be called for each request that requires a subscription. func (rs *RecurringSubscription) CheckOrigCaller() { send := std.GetOrigSend() caller := std.GetOrigCaller() diff --git a/examples/gno.land/p/demo/subscription/subscription_test.gno b/examples/gno.land/p/demo/subscription/subscription_test.gno index 891a6d9d194..10c3ca4eec3 100644 --- a/examples/gno.land/p/demo/subscription/subscription_test.gno +++ b/examples/gno.land/p/demo/subscription/subscription_test.gno @@ -2,4 +2,10 @@ package subscription import "testing" -func TestPackage(t *testing.T) {} +func TestPackage(t *testing.T) { + // Example: Duration of 30 days and amount of 1000 ugno + sub := subscription.NewRecurringSubscription(time.Hour*24*30, 1000) + // simulate send with std.TestSetOrigSend + // simulate orig caller with std.TestSetOrigCaller + sub.CheckOrigCaller() +} diff --git a/examples/gno.land/r/demo/art/millipede/millipede.gno b/examples/gno.land/r/demo/art/millipede/millipede.gno index dbffd8b3fbb..f35838fa7c1 100644 --- a/examples/gno.land/r/demo/art/millipede/millipede.gno +++ b/examples/gno.land/r/demo/art/millipede/millipede.gno @@ -1,6 +1,7 @@ package millipede import ( + "std" "strconv" "strings" @@ -26,6 +27,10 @@ func Draw(size int) string { return b.String() } +func Foo() { + println(std.GetOrigCaller(), std.GetHeight()) +} + func Render(path string) string { size := defaultSize diff --git a/examples/gno.land/r/demo/art/millipede/millipede_test.gno b/examples/gno.land/r/demo/art/millipede/millipede_test.gno index 38914707b4a..c72dbc9aed1 100644 --- a/examples/gno.land/r/demo/art/millipede/millipede_test.gno +++ b/examples/gno.land/r/demo/art/millipede/millipede_test.gno @@ -46,6 +46,17 @@ func TestRender(t *testing.T) { ╚═(███)═╝ ` + "```\n[3](/r/art/millpede:3)< >[5](/r/art/millipede:5)", }, + { + path: "5", + expected: "```" + ` + ╚⊙ ⊙╝ + ╚═(███)═╝ + ╚═(███)═╝ +╚═(███)═╝ + ╚═(███)═╝ + ╚═(███)═╝ +` + "```\n[4](/r/art/millpede:4)< >[6](/r/art/millipede:6)", + }, } for _, tc := range cases {