From 20446ea7895ac30d5405706e60dd5bf15fe9ed25 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Fri, 4 Aug 2023 12:46:31 +0200 Subject: [PATCH] chore: fixup --- .../p/demo/subscription/subscription.gno | 42 ++++++++++++++++++- .../p/demo/subscription/subscription_test.gno | 8 +++- 2 files changed, 48 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() +}