roscaslim.gno
3.23 Kb · 101 lines
1// Package rosca is a dependency-free toolkit for building rotating savings
2// and credit associations (ROSCA - "arisan", "tanda", "chit fund", "susus")
3// on Gno.land.
4//
5// It contains no state and no realm capability of its own: the economics,
6// the deterministic payout schedule and the presentation helpers are pure
7// functions, so any realm can import this package and add a savings circle
8// to its own state machine without re-deriving the hard parts.
9//
10// The companion realm gno.land/r/<addr>/kolektif is the reference
11// implementation; it imports exactly these helpers.
12package roscaslim2
13
14import "errors"
15
16// Status is the lifecycle stage of a circle.
17type Status uint8
18
19const (
20 // StatusOpen collects members and collateral. Nothing is paid out.
21 StatusOpen Status = iota
22 // StatusActive is a full circle that is paying round by round.
23 StatusActive
24 // StatusComplete means every payout happened and collateral was returned.
25 StatusComplete
26)
27
28// String renders a status as a short label.
29func (s Status) String() string {
30 switch s {
31 case StatusOpen:
32 return "OPEN"
33 case StatusActive:
34 return "ACTIVE"
35 case StatusComplete:
36 return "COMPLETE"
37 }
38 return "UNKNOWN"
39}
40
41// Limits keep a circle inside numbers the reference realm can render and
42// settle safely. They are advisory: an importing realm may enforce others.
43const (
44 MinSize = 3
45 MaxSize = 50
46 MinContribution = int64(1000) // 0.001 GNOT
47 MaxContribution = int64(100000000000) // 100,000 GNOT
48 MinRoundSpan = int64(60) // 1 minute
49 MaxRoundSpan = int64(2592000) // 30 days
50 MaxNameLen = 64
51 DefaultRoundSpan = int64(604800) // 7 days
52 UgnotPerGnot = int64(1000000)
53)
54
55// Params is the immutable economics of a circle.
56//
57// Contribution is what each member pays every round. Collateral is what a
58// member locks when joining; it is returned at completion and slashed into
59// the penalty fund when the member defaults.
60type Params struct {
61 Contribution int64
62 Collateral int64
63 Size int
64 RoundSpan int64 // seconds per round
65}
66
67// Validate checks the parameters of a circle.
68func (p Params) Validate() error {
69 if p.Size < MinSize || p.Size > MaxSize {
70 return errors.New("size must be between 3 and 50 members")
71 }
72 if p.Contribution < MinContribution || p.Contribution > MaxContribution {
73 return errors.New("contribution out of range")
74 }
75 if p.Collateral != p.Contribution {
76 return errors.New("collateral must equal one round contribution")
77 }
78 if p.RoundSpan < MinRoundSpan || p.RoundSpan > MaxRoundSpan {
79 return errors.New("round span must be between 1 minute and 30 days")
80 }
81 return nil
82}
83
84// Pot is the payout one round produces when every seat pays.
85func (p Params) Pot() int64 {
86 return p.Contribution * int64(p.Size)
87}
88
89// Locked is the capital a circle needs at rest: every seat's collateral.
90func (p Params) Locked() int64 {
91 return p.Collateral * int64(p.Size)
92}
93
94// NetPerMember is the net cash flow of an ideal circle (no defaults) for a
95// member who stays to the end: they pay Size contributions and receive one
96// pot of Size contributions. It is always zero, which is the point of a
97// ROSCA: it is a savings rotation, not a loan.
98func (p Params) NetPerMember() int64 {
99 return p.Pot() - p.Contribution*int64(p.Size)
100}
101