Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: p/demo/subscription #1025

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions examples/gno.land/p/demo/subscription/subscription.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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()

// TODO implement
good := false
if !good {
panic("you need to pay a subscription of " + rs.amount)
}
}

// TODO: other subscription models
11 changes: 11 additions & 0 deletions examples/gno.land/p/demo/subscription/subscription_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package subscription

import "testing"

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()
}