Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 4, 2023
1 parent 1cc9b6c commit 48afd4f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
42 changes: 41 additions & 1 deletion examples/gno.land/p/demo/subscription/subscription.gno
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
8 changes: 7 additions & 1 deletion examples/gno.land/p/demo/subscription/subscription_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
5 changes: 5 additions & 0 deletions examples/gno.land/r/demo/art/millipede/millipede.gno
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package millipede

import (
"std"
"strconv"
"strings"

Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions examples/gno.land/r/demo/art/millipede/millipede_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 48afd4f

Please sign in to comment.