You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documents/CaveatEnforcers.md
+107Lines changed: 107 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,3 +25,110 @@ This redemption may alter the state of other contracts. For example, the balance
25
25
Consider a scenario where D1 includes an array of caveats: one caveat is the `NativeBalanceGteEnforcer`, which verifies that Bob’s balance has increased as a result of the execution attached to D1. The second caveat is the `NativeTokenPaymentEnforcer`, which deducts from Bob’s balance by redeeming D2. If these enforcers are not correctly ordered, they could conflict. For instance, if the `NativeTokenPaymentEnforcer` is executed before the `NativeBalanceGteEnforcer`, Bob’s balance would be reduced first, potentially causing the `NativeBalanceGteEnforcer` to fail its validation of ensuring Bob’s balance exceeds a certain threshold.
26
26
27
27
Because the `NativeTokenPaymentEnforcer` modifies the state of external contracts, it is essential to carefully order enforcers in the delegation to prevent conflicts. The enforcers are designed to protect the execution process, but they do not guarantee a final state after the redemption. This means that even if the `NativeBalanceGteEnforcer` validates Bob’s balance at one point, subsequent enforcers, such as the `NativeTokenPaymentEnforcer`, may modify it later.
28
+
29
+
### ERC20SubscriptionEnforcer
30
+
31
+
A delegate (i.e., redeemer) can use the `ERC20SubscriptionEnforcer` to transfer ERC20 tokens from the delegator's account once every `x` day.
32
+
33
+
Given an initial timestamp from the redeemer, the `next allowed timestamp` is calculated dynamically, checking this value against the current `block.timestamp` to ensure the redemption fits within the next cycle before execution. `ERC20SubscriptionEnforcer` will enforce the following constraints:
34
+
35
+
1. The redeemer can only redeem the subscription token amount once per cycle, preventing duplicate claims in the same cycle
36
+
2. The redeemer can claim missed a cycle. They skip a claim period at a later date.
37
+
38
+
### References
39
+
40
+
Here are a few implementations of subscriptions in the wild that we have taken some inspiration from:
We will use integer division to dynamically determine how many full `x-day` periods have passed since the `elapsed time` while ignoring any remainder(i.e., accumulated extra days/hours toward the current cycle in progress). We `elapsedTime` evaluates to `0`. No full cycle has passed, and the first cycle is in progress.
(meaning **2 full cycles** have passed and the **third cycle** is in progress. Once the 90-day mark is reached, the redeemer can claim tokens for the third cycle).
82
+
83
+
For example:
84
+
85
+
- If an of `elapsedTime = 10 days`, then:
86
+
\[
87
+
\frac{10}{30} = 0
88
+
\]
89
+
(meaning **first full cycles** is in progress).
90
+
91
+
---
92
+
93
+
#### Next valid timestamp
94
+
95
+
We can use a simple calculation with values derived from the previous formula to determine the next timestamp the redeemer is eligible to claim the subscription token amount.
96
+
97
+
Since `periodsPassed` will give us the last completed cycle, we need to `+1` to know where to start the next cycle (i.e., cycle actively in progress).
0 commit comments