Search Apps Documentation Source Content File Folder Download Copy Actions Download

lifetime.gno

2.02 Kb ยท 82 lines
 1package lifetime
 2
 3import (
 4	"chain/banker"
 5	"chain/runtime"
 6
 7	"gno.land/p/nt/avl"
 8	"gno.land/p/nt/ownable"
 9)
10
11// LifetimeSubscription represents a subscription that requires only a one-time payment.
12// It grants permanent access to a service or product.
13type LifetimeSubscription struct {
14	ownable.Ownable
15	amount int64
16	subs   *avl.Tree // chain.Address -> bool
17}
18
19// NewLifetimeSubscription creates and returns a new lifetime subscription.
20func NewLifetimeSubscription(amount int64) *LifetimeSubscription {
21	return &LifetimeSubscription{
22		Ownable: *ownable.New(),
23		amount:  amount,
24		subs:    avl.NewTree(),
25	}
26}
27
28// processSubscription handles the subscription process for a given receiver.
29func (ls *LifetimeSubscription) processSubscription(receiver address) error {
30	amount := banker.OriginSend()
31
32	if amount.AmountOf("ugnot") != ls.amount {
33		return ErrAmt
34	}
35
36	_, exists := ls.subs.Get(receiver.String())
37
38	if exists {
39		return ErrAlreadySub
40	}
41
42	ls.subs.Set(receiver.String(), true)
43
44	return nil
45}
46
47// Subscribe processes the payment for a lifetime subscription.
48func (ls *LifetimeSubscription) Subscribe() error {
49	caller := runtime.CurrentRealm().Address()
50	return ls.processSubscription(caller)
51}
52
53// GiftSubscription allows the caller to pay for a lifetime subscription for another user.
54func (ls *LifetimeSubscription) GiftSubscription(receiver address) error {
55	return ls.processSubscription(receiver)
56}
57
58// HasValidSubscription checks if the given address has an active lifetime subscription.
59func (ls *LifetimeSubscription) HasValidSubscription(addr address) error {
60	_, exists := ls.subs.Get(addr.String())
61
62	if !exists {
63		return ErrNoSub
64	}
65
66	return nil
67}
68
69// UpdateAmount allows the owner of the LifetimeSubscription contract to update the subscription price.
70func (ls *LifetimeSubscription) UpdateAmount(newAmount int64) error {
71	if !ls.OwnedByCurrent() {
72		return ErrNotAuthorized
73	}
74
75	ls.amount = newAmount
76	return nil
77}
78
79// GetAmount returns the current subscription price.
80func (ls *LifetimeSubscription) GetAmount() int64 {
81	return ls.amount
82}