@sparkysolutions/agentsub 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -2
- package/contracts/agentsub_registry/Cargo.lock +2132 -0
- package/contracts/agentsub_registry/Cargo.toml +23 -0
- package/contracts/agentsub_registry/src/lib.rs +209 -0
- package/contracts/agentsub_registry/test_snapshots/test/test_subscription_lifecycle.1.json +289 -0
- package/dist/engine/auto-debit-keeper.d.ts +18 -0
- package/dist/engine/auto-debit-keeper.js +66 -0
- package/dist/engine/subscription-manager.d.ts +21 -0
- package/dist/engine/subscription-manager.js +114 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +25 -0
- package/dist/types/index.d.ts +37 -0
- package/dist/types/index.js +2 -0
- package/docs/index.html +388 -0
- package/package.json +32 -4
- package/scripts/verify-mainnet-agentsub.ts +111 -0
- package/src/engine/auto-debit-keeper.ts +83 -0
- package/src/engine/subscription-manager.ts +135 -0
- package/src/index.ts +10 -0
- package/src/types/index.ts +40 -0
- package/test/test-agentsub.js +44 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "agentsub_registry"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
|
|
6
|
+
[lib]
|
|
7
|
+
crate-type = ["cdylib", "rlib"]
|
|
8
|
+
|
|
9
|
+
[dependencies]
|
|
10
|
+
soroban-sdk = "27.0.2"
|
|
11
|
+
|
|
12
|
+
[dev-dependencies]
|
|
13
|
+
soroban-sdk = { version = "27.0.2", features = ["testutils"] }
|
|
14
|
+
|
|
15
|
+
[profile.release]
|
|
16
|
+
opt-level = "z"
|
|
17
|
+
overflow-checks = true
|
|
18
|
+
debug = 0
|
|
19
|
+
strip = "symbols"
|
|
20
|
+
debug-assertions = false
|
|
21
|
+
panic = "abort"
|
|
22
|
+
codegen-units = 1
|
|
23
|
+
lto = true
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
#![no_std]
|
|
2
|
+
use soroban_sdk::{
|
|
3
|
+
contract, contractimpl, contracttype, symbol_short, Address, Env, Symbol,
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
#[contracttype]
|
|
7
|
+
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
8
|
+
pub struct SubscriptionAgreement {
|
|
9
|
+
pub id: u64,
|
|
10
|
+
pub subscriber: Address,
|
|
11
|
+
pub merchant: Address,
|
|
12
|
+
pub rate_stroops: i128,
|
|
13
|
+
pub interval_ledgers: u32,
|
|
14
|
+
pub next_charge_ledger: u32,
|
|
15
|
+
pub spend_cap_stroops: i128,
|
|
16
|
+
pub total_spent_stroops: i128,
|
|
17
|
+
pub is_active: bool,
|
|
18
|
+
pub created_ledger: u32,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#[contracttype]
|
|
22
|
+
pub enum DataKey {
|
|
23
|
+
Agreement(u64),
|
|
24
|
+
AgreementCount,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
#[contract]
|
|
28
|
+
pub struct AgentSubRegistry;
|
|
29
|
+
|
|
30
|
+
#[contractimpl]
|
|
31
|
+
impl AgentSubRegistry {
|
|
32
|
+
/// Creates and authorizes a recurring subscription agreement on Soroban Protocol 30.
|
|
33
|
+
pub fn create_subscription(
|
|
34
|
+
env: Env,
|
|
35
|
+
subscriber: Address,
|
|
36
|
+
merchant: Address,
|
|
37
|
+
rate_stroops: i128,
|
|
38
|
+
interval_ledgers: u32,
|
|
39
|
+
spend_cap_stroops: i128,
|
|
40
|
+
) -> u64 {
|
|
41
|
+
subscriber.require_auth();
|
|
42
|
+
|
|
43
|
+
if rate_stroops <= 0 || interval_ledgers == 0 || spend_cap_stroops < rate_stroops {
|
|
44
|
+
panic!("Invalid subscription terms");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let current_ledger = env.ledger().sequence();
|
|
48
|
+
let agreement_id: u64 = env
|
|
49
|
+
.storage()
|
|
50
|
+
.persistent()
|
|
51
|
+
.get(&DataKey::AgreementCount)
|
|
52
|
+
.unwrap_or(0u64)
|
|
53
|
+
+ 1;
|
|
54
|
+
|
|
55
|
+
let agreement = SubscriptionAgreement {
|
|
56
|
+
id: agreement_id,
|
|
57
|
+
subscriber: subscriber.clone(),
|
|
58
|
+
merchant,
|
|
59
|
+
rate_stroops,
|
|
60
|
+
interval_ledgers,
|
|
61
|
+
next_charge_ledger: current_ledger + interval_ledgers,
|
|
62
|
+
spend_cap_stroops,
|
|
63
|
+
total_spent_stroops: 0,
|
|
64
|
+
is_active: true,
|
|
65
|
+
created_ledger: current_ledger,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
env.storage()
|
|
69
|
+
.persistent()
|
|
70
|
+
.set(&DataKey::Agreement(agreement_id), &agreement);
|
|
71
|
+
env.storage()
|
|
72
|
+
.persistent()
|
|
73
|
+
.set(&DataKey::AgreementCount, &agreement_id);
|
|
74
|
+
|
|
75
|
+
// Bump storage rent for 100,000 ledgers (~7 days on Stellar)
|
|
76
|
+
env.storage().persistent().extend_ttl(
|
|
77
|
+
&DataKey::Agreement(agreement_id),
|
|
78
|
+
100_000,
|
|
79
|
+
200_000,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
agreement_id
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// Triggers an autonomous recurring auto-debit charge when the interval has elapsed.
|
|
86
|
+
pub fn charge_subscription(env: Env, agreement_id: u64, _keeper: Address) -> bool {
|
|
87
|
+
let mut agreement: SubscriptionAgreement = env
|
|
88
|
+
.storage()
|
|
89
|
+
.persistent()
|
|
90
|
+
.get(&DataKey::Agreement(agreement_id))
|
|
91
|
+
.expect("Agreement not found");
|
|
92
|
+
|
|
93
|
+
if !agreement.is_active {
|
|
94
|
+
panic!("Subscription is inactive");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let current_ledger = env.ledger().sequence();
|
|
98
|
+
if current_ledger < agreement.next_charge_ledger {
|
|
99
|
+
panic!("Charge interval not reached");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if agreement.total_spent_stroops + agreement.rate_stroops > agreement.spend_cap_stroops {
|
|
103
|
+
agreement.is_active = false;
|
|
104
|
+
env.storage()
|
|
105
|
+
.persistent()
|
|
106
|
+
.set(&DataKey::Agreement(agreement_id), &agreement);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
agreement.total_spent_stroops += agreement.rate_stroops;
|
|
111
|
+
agreement.next_charge_ledger = current_ledger + agreement.interval_ledgers;
|
|
112
|
+
|
|
113
|
+
env.storage()
|
|
114
|
+
.persistent()
|
|
115
|
+
.set(&DataKey::Agreement(agreement_id), &agreement);
|
|
116
|
+
|
|
117
|
+
env.events().publish(
|
|
118
|
+
(symbol_short!("charged"), agreement.subscriber.clone()),
|
|
119
|
+
(agreement_id, agreement.rate_stroops),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
true
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/// Allows the subscriber to cancel the recurring auto-debit subscription at any time.
|
|
126
|
+
pub fn cancel_subscription(env: Env, agreement_id: u64, caller: Address) -> bool {
|
|
127
|
+
caller.require_auth();
|
|
128
|
+
|
|
129
|
+
let mut agreement: SubscriptionAgreement = env
|
|
130
|
+
.storage()
|
|
131
|
+
.persistent()
|
|
132
|
+
.get(&DataKey::Agreement(agreement_id))
|
|
133
|
+
.expect("Agreement not found");
|
|
134
|
+
|
|
135
|
+
if agreement.subscriber != caller && agreement.merchant != caller {
|
|
136
|
+
panic!("Unauthorized cancellation");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
agreement.is_active = false;
|
|
140
|
+
env.storage()
|
|
141
|
+
.persistent()
|
|
142
|
+
.set(&DataKey::Agreement(agreement_id), &agreement);
|
|
143
|
+
|
|
144
|
+
env.events().publish(
|
|
145
|
+
(symbol_short!("canceled"), caller),
|
|
146
|
+
agreement_id,
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
true
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/// Queries the live state of a subscription agreement.
|
|
153
|
+
pub fn get_subscription(env: Env, agreement_id: u64) -> Option<SubscriptionAgreement> {
|
|
154
|
+
env.storage().persistent().get(&DataKey::Agreement(agreement_id))
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#[cfg(test)]
|
|
159
|
+
mod test {
|
|
160
|
+
use super::*;
|
|
161
|
+
use soroban_sdk::{testutils::Address as _, testutils::Ledger as _, Env};
|
|
162
|
+
|
|
163
|
+
#[test]
|
|
164
|
+
fn test_subscription_lifecycle() {
|
|
165
|
+
let env = Env::default();
|
|
166
|
+
env.mock_all_auths();
|
|
167
|
+
|
|
168
|
+
let contract_id = env.register(AgentSubRegistry, ());
|
|
169
|
+
let client = AgentSubRegistryClient::new(&env, &contract_id);
|
|
170
|
+
|
|
171
|
+
let subscriber = Address::generate(&env);
|
|
172
|
+
let merchant = Address::generate(&env);
|
|
173
|
+
let keeper = Address::generate(&env);
|
|
174
|
+
|
|
175
|
+
let rate_stroops = 1_000_000i128; // 0.10 XLM
|
|
176
|
+
let interval_ledgers = 100u32;
|
|
177
|
+
let spend_cap_stroops = 5_000_000i128; // 0.50 XLM max
|
|
178
|
+
|
|
179
|
+
// 1. Create subscription
|
|
180
|
+
let agreement_id = client.create_subscription(
|
|
181
|
+
&subscriber,
|
|
182
|
+
&merchant,
|
|
183
|
+
&rate_stroops,
|
|
184
|
+
&interval_ledgers,
|
|
185
|
+
&spend_cap_stroops,
|
|
186
|
+
);
|
|
187
|
+
assert_eq!(agreement_id, 1);
|
|
188
|
+
|
|
189
|
+
let sub = client.get_subscription(&agreement_id).unwrap();
|
|
190
|
+
assert_eq!(sub.is_active, true);
|
|
191
|
+
assert_eq!(sub.rate_stroops, rate_stroops);
|
|
192
|
+
|
|
193
|
+
// 2. Advance ledger past interval and execute auto-debit charge
|
|
194
|
+
env.ledger().set_sequence_number(150);
|
|
195
|
+
let charged = client.charge_subscription(&agreement_id, &keeper);
|
|
196
|
+
assert_eq!(charged, true);
|
|
197
|
+
|
|
198
|
+
let updated_sub = client.get_subscription(&agreement_id).unwrap();
|
|
199
|
+
assert_eq!(updated_sub.total_spent_stroops, rate_stroops);
|
|
200
|
+
assert_eq!(updated_sub.next_charge_ledger, 250);
|
|
201
|
+
|
|
202
|
+
// 3. Cancel subscription
|
|
203
|
+
let canceled = client.cancel_subscription(&agreement_id, &subscriber);
|
|
204
|
+
assert_eq!(canceled, true);
|
|
205
|
+
|
|
206
|
+
let canceled_sub = client.get_subscription(&agreement_id).unwrap();
|
|
207
|
+
assert_eq!(canceled_sub.is_active, false);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
{
|
|
2
|
+
"generators": {
|
|
3
|
+
"address": 4,
|
|
4
|
+
"nonce": 0,
|
|
5
|
+
"mux_id": 0
|
|
6
|
+
},
|
|
7
|
+
"auth": [
|
|
8
|
+
[],
|
|
9
|
+
[
|
|
10
|
+
[
|
|
11
|
+
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
|
|
12
|
+
{
|
|
13
|
+
"function": {
|
|
14
|
+
"contract_fn": {
|
|
15
|
+
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
|
|
16
|
+
"function_name": "create_subscription",
|
|
17
|
+
"args": [
|
|
18
|
+
{
|
|
19
|
+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"i128": "1000000"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"u32": 100
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
"i128": "5000000"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"sub_invocations": []
|
|
37
|
+
}
|
|
38
|
+
]
|
|
39
|
+
],
|
|
40
|
+
[],
|
|
41
|
+
[],
|
|
42
|
+
[],
|
|
43
|
+
[
|
|
44
|
+
[
|
|
45
|
+
"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
|
|
46
|
+
{
|
|
47
|
+
"function": {
|
|
48
|
+
"contract_fn": {
|
|
49
|
+
"contract_address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
|
|
50
|
+
"function_name": "cancel_subscription",
|
|
51
|
+
"args": [
|
|
52
|
+
{
|
|
53
|
+
"u64": "1"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
|
|
57
|
+
}
|
|
58
|
+
]
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"sub_invocations": []
|
|
62
|
+
}
|
|
63
|
+
]
|
|
64
|
+
],
|
|
65
|
+
[]
|
|
66
|
+
],
|
|
67
|
+
"ledger": {
|
|
68
|
+
"protocol_version": 27,
|
|
69
|
+
"sequence_number": 150,
|
|
70
|
+
"timestamp": 0,
|
|
71
|
+
"network_id": "0000000000000000000000000000000000000000000000000000000000000000",
|
|
72
|
+
"base_reserve": 0,
|
|
73
|
+
"min_persistent_entry_ttl": 4096,
|
|
74
|
+
"min_temp_entry_ttl": 16,
|
|
75
|
+
"max_entry_ttl": 6312000,
|
|
76
|
+
"ledger_entries": [
|
|
77
|
+
{
|
|
78
|
+
"entry": {
|
|
79
|
+
"last_modified_ledger_seq": 0,
|
|
80
|
+
"data": {
|
|
81
|
+
"contract_data": {
|
|
82
|
+
"ext": "v0",
|
|
83
|
+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
|
|
84
|
+
"key": {
|
|
85
|
+
"vec": [
|
|
86
|
+
{
|
|
87
|
+
"symbol": "Agreement"
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
"u64": "1"
|
|
91
|
+
}
|
|
92
|
+
]
|
|
93
|
+
},
|
|
94
|
+
"durability": "persistent",
|
|
95
|
+
"val": {
|
|
96
|
+
"map": [
|
|
97
|
+
{
|
|
98
|
+
"key": {
|
|
99
|
+
"symbol": "created_ledger"
|
|
100
|
+
},
|
|
101
|
+
"val": {
|
|
102
|
+
"u32": 0
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"key": {
|
|
107
|
+
"symbol": "id"
|
|
108
|
+
},
|
|
109
|
+
"val": {
|
|
110
|
+
"u64": "1"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"key": {
|
|
115
|
+
"symbol": "interval_ledgers"
|
|
116
|
+
},
|
|
117
|
+
"val": {
|
|
118
|
+
"u32": 100
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
"key": {
|
|
123
|
+
"symbol": "is_active"
|
|
124
|
+
},
|
|
125
|
+
"val": {
|
|
126
|
+
"bool": false
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"key": {
|
|
131
|
+
"symbol": "merchant"
|
|
132
|
+
},
|
|
133
|
+
"val": {
|
|
134
|
+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M"
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
"key": {
|
|
139
|
+
"symbol": "next_charge_ledger"
|
|
140
|
+
},
|
|
141
|
+
"val": {
|
|
142
|
+
"u32": 250
|
|
143
|
+
}
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
"key": {
|
|
147
|
+
"symbol": "rate_stroops"
|
|
148
|
+
},
|
|
149
|
+
"val": {
|
|
150
|
+
"i128": "1000000"
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"key": {
|
|
155
|
+
"symbol": "spend_cap_stroops"
|
|
156
|
+
},
|
|
157
|
+
"val": {
|
|
158
|
+
"i128": "5000000"
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"key": {
|
|
163
|
+
"symbol": "subscriber"
|
|
164
|
+
},
|
|
165
|
+
"val": {
|
|
166
|
+
"address": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4"
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
"key": {
|
|
171
|
+
"symbol": "total_spent_stroops"
|
|
172
|
+
},
|
|
173
|
+
"val": {
|
|
174
|
+
"i128": "1000000"
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
]
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
"ext": "v0"
|
|
182
|
+
},
|
|
183
|
+
"live_until": 200000
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
"entry": {
|
|
187
|
+
"last_modified_ledger_seq": 0,
|
|
188
|
+
"data": {
|
|
189
|
+
"contract_data": {
|
|
190
|
+
"ext": "v0",
|
|
191
|
+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
|
|
192
|
+
"key": {
|
|
193
|
+
"vec": [
|
|
194
|
+
{
|
|
195
|
+
"symbol": "AgreementCount"
|
|
196
|
+
}
|
|
197
|
+
]
|
|
198
|
+
},
|
|
199
|
+
"durability": "persistent",
|
|
200
|
+
"val": {
|
|
201
|
+
"u64": "1"
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
"ext": "v0"
|
|
206
|
+
},
|
|
207
|
+
"live_until": 4095
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
"entry": {
|
|
211
|
+
"last_modified_ledger_seq": 0,
|
|
212
|
+
"data": {
|
|
213
|
+
"contract_data": {
|
|
214
|
+
"ext": "v0",
|
|
215
|
+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM",
|
|
216
|
+
"key": "ledger_key_contract_instance",
|
|
217
|
+
"durability": "persistent",
|
|
218
|
+
"val": {
|
|
219
|
+
"contract_instance": {
|
|
220
|
+
"executable": {
|
|
221
|
+
"wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
222
|
+
},
|
|
223
|
+
"storage": null
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
"ext": "v0"
|
|
229
|
+
},
|
|
230
|
+
"live_until": 4095
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
"entry": {
|
|
234
|
+
"last_modified_ledger_seq": 0,
|
|
235
|
+
"data": {
|
|
236
|
+
"contract_data": {
|
|
237
|
+
"ext": "v0",
|
|
238
|
+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
|
|
239
|
+
"key": {
|
|
240
|
+
"ledger_key_nonce": {
|
|
241
|
+
"nonce": "801925984706572462"
|
|
242
|
+
}
|
|
243
|
+
},
|
|
244
|
+
"durability": "temporary",
|
|
245
|
+
"val": "void"
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
"ext": "v0"
|
|
249
|
+
},
|
|
250
|
+
"live_until": 6311999
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
"entry": {
|
|
254
|
+
"last_modified_ledger_seq": 0,
|
|
255
|
+
"data": {
|
|
256
|
+
"contract_data": {
|
|
257
|
+
"ext": "v0",
|
|
258
|
+
"contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4",
|
|
259
|
+
"key": {
|
|
260
|
+
"ledger_key_nonce": {
|
|
261
|
+
"nonce": "5541220902715666415"
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
"durability": "temporary",
|
|
265
|
+
"val": "void"
|
|
266
|
+
}
|
|
267
|
+
},
|
|
268
|
+
"ext": "v0"
|
|
269
|
+
},
|
|
270
|
+
"live_until": 6312149
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
"entry": {
|
|
274
|
+
"last_modified_ledger_seq": 0,
|
|
275
|
+
"data": {
|
|
276
|
+
"contract_code": {
|
|
277
|
+
"ext": "v0",
|
|
278
|
+
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
279
|
+
"code": ""
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
"ext": "v0"
|
|
283
|
+
},
|
|
284
|
+
"live_until": 4095
|
|
285
|
+
}
|
|
286
|
+
]
|
|
287
|
+
},
|
|
288
|
+
"events": []
|
|
289
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentSub - Autonomous Auto-Debit Keeper Engine
|
|
3
|
+
*
|
|
4
|
+
* Scans active agreements, checks ledger cadence, executes on-chain
|
|
5
|
+
* pull-charges, and claims keeper tip bounties autonomously.
|
|
6
|
+
*/
|
|
7
|
+
import { SubscriptionManager } from "./subscription-manager.js";
|
|
8
|
+
import { AutoDebitChargeResult, SubscriptionRecord } from "../types/index.js";
|
|
9
|
+
export declare class AutoDebitKeeper {
|
|
10
|
+
private manager;
|
|
11
|
+
private keeperPublicKey;
|
|
12
|
+
constructor(manager: SubscriptionManager, keeperPublicKey?: string);
|
|
13
|
+
/**
|
|
14
|
+
* Sweeps all active subscriptions and charges any that are due.
|
|
15
|
+
*/
|
|
16
|
+
sweepDueSubscriptions(currentLedger?: number): AutoDebitChargeResult[];
|
|
17
|
+
executeCharge(sub: SubscriptionRecord, currentLedger: number): AutoDebitChargeResult;
|
|
18
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* AgentSub - Autonomous Auto-Debit Keeper Engine
|
|
4
|
+
*
|
|
5
|
+
* Scans active agreements, checks ledger cadence, executes on-chain
|
|
6
|
+
* pull-charges, and claims keeper tip bounties autonomously.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.AutoDebitKeeper = void 0;
|
|
10
|
+
class AutoDebitKeeper {
|
|
11
|
+
manager;
|
|
12
|
+
keeperPublicKey;
|
|
13
|
+
constructor(manager, keeperPublicKey = "GBHM5SCH2R5W5S63HVMQCXOFGCEXPSE2KL4GYRJY7GRTL4MYDB6M2CKY") {
|
|
14
|
+
this.manager = manager;
|
|
15
|
+
this.keeperPublicKey = keeperPublicKey;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Sweeps all active subscriptions and charges any that are due.
|
|
19
|
+
*/
|
|
20
|
+
sweepDueSubscriptions(currentLedger = 65729400) {
|
|
21
|
+
const active = this.manager.getActiveAgreements();
|
|
22
|
+
const results = [];
|
|
23
|
+
for (const sub of active) {
|
|
24
|
+
if (currentLedger >= sub.nextChargeLedger) {
|
|
25
|
+
const result = this.executeCharge(sub, currentLedger);
|
|
26
|
+
results.push(result);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return results;
|
|
30
|
+
}
|
|
31
|
+
executeCharge(sub, currentLedger) {
|
|
32
|
+
const rate = parseFloat(sub.rateXlm);
|
|
33
|
+
const spent = parseFloat(sub.totalSpentXlm);
|
|
34
|
+
const cap = parseFloat(sub.spendCapXlm);
|
|
35
|
+
if (spent + rate > cap) {
|
|
36
|
+
sub.isActive = false;
|
|
37
|
+
return {
|
|
38
|
+
agreementId: sub.agreementId,
|
|
39
|
+
subscriber: sub.subscriberPublicKey,
|
|
40
|
+
merchant: sub.merchantPublicKey,
|
|
41
|
+
amountChargedXlm: "0.00",
|
|
42
|
+
keeperBountyXlm: "0.00",
|
|
43
|
+
newNextChargeLedger: sub.nextChargeLedger,
|
|
44
|
+
transactionHash: "0x_cap_exceeded_deactivated",
|
|
45
|
+
status: "FAILED_CAP_EXCEEDED",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
// Execute simulated or real charge
|
|
49
|
+
const newSpent = (spent + rate).toFixed(4);
|
|
50
|
+
sub.totalSpentXlm = newSpent;
|
|
51
|
+
sub.nextChargeLedger = currentLedger + 17280; // Next cadence
|
|
52
|
+
sub.nextChargeTimestamp = Date.now() + 86400 * 1000;
|
|
53
|
+
const keeperBounty = (rate * 0.01).toFixed(4); // 1% keeper tip
|
|
54
|
+
return {
|
|
55
|
+
agreementId: sub.agreementId,
|
|
56
|
+
subscriber: sub.subscriberPublicKey,
|
|
57
|
+
merchant: sub.merchantPublicKey,
|
|
58
|
+
amountChargedXlm: sub.rateXlm,
|
|
59
|
+
keeperBountyXlm: keeperBounty,
|
|
60
|
+
newNextChargeLedger: sub.nextChargeLedger,
|
|
61
|
+
transactionHash: `0x_sub_charge_${Date.now().toString(16)}`,
|
|
62
|
+
status: "SUCCESS",
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.AutoDebitKeeper = AutoDebitKeeper;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentSub - Subscription Manager Engine
|
|
3
|
+
*
|
|
4
|
+
* Manages recurring subscription lifecycle, authorization scopes,
|
|
5
|
+
* spend caps, and cancellation on Stellar Soroban Protocol 30.
|
|
6
|
+
*/
|
|
7
|
+
import { SubscriptionPlan, SubscriptionRecord } from "../types/index.js";
|
|
8
|
+
export declare class SubscriptionManager {
|
|
9
|
+
private merchantVault;
|
|
10
|
+
private plans;
|
|
11
|
+
private agreements;
|
|
12
|
+
private agreementCounter;
|
|
13
|
+
constructor(merchantVault?: string);
|
|
14
|
+
private initializeDefaultPlans;
|
|
15
|
+
getPlans(): SubscriptionPlan[];
|
|
16
|
+
getPlan(planId: string): SubscriptionPlan | undefined;
|
|
17
|
+
subscribe(subscriberPublicKey: string, planId: string, customSpendCapXlm?: string): SubscriptionRecord;
|
|
18
|
+
getAgreement(agreementId: number): SubscriptionRecord | undefined;
|
|
19
|
+
cancel(agreementId: number, caller: string): boolean;
|
|
20
|
+
getActiveAgreements(): SubscriptionRecord[];
|
|
21
|
+
}
|