@sparkysolutions/agentrail 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/.github/ISSUE_TEMPLATE/drips_wave_task.md +33 -0
- package/.github/workflows/auto-merge.yml +119 -0
- package/.github/workflows/ci.yml +35 -0
- package/BENCHMARKS.md +67 -0
- package/CONTRIBUTING.md +40 -0
- package/DRIPS_WAVE.md +81 -0
- package/FUNDING.json +7 -0
- package/ISSUES_PIPELINE.json +106 -0
- package/LICENSE +21 -0
- package/README.md +145 -2
- package/RUNBOOK.md +95 -0
- package/SCF_ACTIVATION_PROPOSAL.md +137 -0
- package/assets/sorobanagents_avatar.jpg +0 -0
- package/assets/sorobanagents_avatar.png +0 -0
- package/assets/sorobanagents_avatar_elite.png +0 -0
- package/contracts/agentrail/Cargo.lock +1895 -0
- package/contracts/agentrail/Cargo.toml +25 -0
- package/contracts/agentrail/src/lib.rs +296 -0
- package/contracts/agentrail/src/test.rs +442 -0
- package/contracts/agentrail/test_snapshots/test/test_circuit_breaker_pause_and_unpause.1.json +195 -0
- package/contracts/agentrail/test_snapshots/test/test_double_initialize_fails.1.json +123 -0
- package/contracts/agentrail/test_snapshots/test/test_execute_spend_deducts_and_accumulates.1.json +347 -0
- package/contracts/agentrail/test_snapshots/test/test_get_allowance_returns_none_for_unregistered.1.json +61 -0
- package/contracts/agentrail/test_snapshots/test/test_get_version_matches_cargo_toml.1.json +61 -0
- package/contracts/agentrail/test_snapshots/test/test_grant_allowance_negative_amount_fails.1.json +123 -0
- package/contracts/agentrail/test_snapshots/test/test_grant_allowance_success.1.json +250 -0
- package/contracts/agentrail/test_snapshots/test/test_grant_allowance_zero_amount_fails.1.json +123 -0
- package/contracts/agentrail/test_snapshots/test/test_initialize_and_protocol_fee.1.json +154 -0
- package/contracts/agentrail/test_snapshots/test/test_initialize_success.1.json +124 -0
- package/contracts/agentrail/test_snapshots/test/test_initialize_with_excessive_fee_fails.1.json +61 -0
- package/contracts/agentrail/test_snapshots/test/test_max_protocol_fee_spend_boundary.1.json +298 -0
- package/contracts/agentrail/test_snapshots/test/test_multi_agent_allowance_isolation.1.json +426 -0
- package/contracts/agentrail/test_snapshots/test/test_multi_owner_isolation.1.json +426 -0
- package/contracts/agentrail/test_snapshots/test/test_regrant_allowance_updates_limits.1.json +346 -0
- package/contracts/agentrail/test_snapshots/test/test_revoke_allowance_freezes_spending.1.json +292 -0
- package/contracts/agentrail/test_snapshots/test/test_set_protocol_fee_by_admin.1.json +165 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_after_expiry_fails.1.json +250 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_exact_allowance_boundary.1.json +298 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_exceeding_allowance_fails.1.json +250 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_fails_when_circuit_breaker_tripped.1.json +285 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_negative_amount_fails.1.json +250 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_non_existent_allowance_fails.1.json +123 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_on_revoked_allowance_fails.1.json +292 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_with_protocol_fee.1.json +286 -0
- package/contracts/agentrail/test_snapshots/test/test_spend_zero_amount_fails.1.json +250 -0
- package/contracts/agentrail/test_snapshots/test/test_unpause_restores_spend_operations.1.json +370 -0
- package/contracts/agentrail/test_snapshots/test/test_zero_protocol_fee_spend.1.json +298 -0
- package/deployment.testnet.json +15 -0
- package/dist/cli/sandbox.d.ts +41 -0
- package/dist/cli/sandbox.js +82 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +26 -0
- package/dist/lib/micro-stream.d.ts +21 -0
- package/dist/lib/micro-stream.js +41 -0
- package/dist/lib/proof-of-task.d.ts +12 -0
- package/dist/lib/proof-of-task.js +32 -0
- package/dist/lib/scoped-allowance.d.ts +11 -0
- package/dist/lib/scoped-allowance.js +95 -0
- package/dist/lib/ttl-keeper.d.ts +10 -0
- package/dist/lib/ttl-keeper.js +36 -0
- package/dist/sdk/agent-client.d.ts +26 -0
- package/dist/sdk/agent-client.js +53 -0
- package/dist/types.d.ts +72 -0
- package/dist/types.js +5 -0
- package/docs/assets/favicon.png +0 -0
- package/docs/assets/logo.png +0 -0
- package/docs/index.html +840 -0
- package/package.json +38 -4
- package/scripts/demo-agent.ts +52 -0
- package/scripts/deploy-testnet.ts +228 -0
- package/scripts/security-audit.mjs +159 -0
- package/src/cli/sandbox.ts +127 -0
- package/src/index.ts +11 -0
- package/src/lib/micro-stream.ts +52 -0
- package/src/lib/proof-of-task.ts +39 -0
- package/src/lib/scoped-allowance.ts +112 -0
- package/src/lib/ttl-keeper.ts +44 -0
- package/src/sdk/agent-client.ts +66 -0
- package/src/types.ts +78 -0
- package/tests/agent-client.test.ts +62 -0
- package/tests/cli.test.ts +68 -0
- package/tests/micro-stream.test.ts +38 -0
- package/tests/proof-of-task.test.ts +78 -0
- package/tests/scoped-allowance.test.ts +135 -0
- package/tests/security-audit.test.ts +76 -0
- package/tests/ttl-keeper.test.ts +36 -0
- package/tsconfig.json +16 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "agentrail-contract"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
description = "Soroban Smart Contract for AgentRail Scoped Allowances & AI Agent Escrow"
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
crate-type = ["cdylib", "rlib"]
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
soroban-sdk = "27.0.2"
|
|
13
|
+
|
|
14
|
+
[dev-dependencies]
|
|
15
|
+
soroban-sdk = { version = "27.0.2", features = ["testutils"] }
|
|
16
|
+
|
|
17
|
+
[profile.release]
|
|
18
|
+
opt-level = "z"
|
|
19
|
+
overflow-checks = true
|
|
20
|
+
debug = 0
|
|
21
|
+
strip = "symbols"
|
|
22
|
+
debug-assertions = false
|
|
23
|
+
panic = "abort"
|
|
24
|
+
codegen-units = 1
|
|
25
|
+
lto = true
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
#![no_std]
|
|
2
|
+
use soroban_sdk::{
|
|
3
|
+
contract, contracterror, contractimpl, contracttype, symbol_short, Address, Env, String, Symbol,
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
#[contracterror]
|
|
7
|
+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
|
8
|
+
#[repr(u32)]
|
|
9
|
+
pub enum Error {
|
|
10
|
+
AlreadyInitialized = 1,
|
|
11
|
+
NotInitialized = 2,
|
|
12
|
+
Unauthorized = 3,
|
|
13
|
+
AllowanceNotFound = 4,
|
|
14
|
+
AllowanceExpired = 5,
|
|
15
|
+
AllowanceExceeded = 6,
|
|
16
|
+
AllowanceInactive = 7,
|
|
17
|
+
InvalidAmount = 8,
|
|
18
|
+
InvalidFeeBps = 9,
|
|
19
|
+
ContractPaused = 10,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
#[contracttype]
|
|
23
|
+
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
24
|
+
pub struct ScopedAllowance {
|
|
25
|
+
pub owner: Address,
|
|
26
|
+
pub agent: Address,
|
|
27
|
+
pub max_stroops: i128,
|
|
28
|
+
pub spent_stroops: i128,
|
|
29
|
+
pub valid_until: u64,
|
|
30
|
+
pub active: bool,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[contracttype]
|
|
34
|
+
pub enum DataKey {
|
|
35
|
+
Admin,
|
|
36
|
+
Token,
|
|
37
|
+
Treasury,
|
|
38
|
+
ProtocolFeeBps,
|
|
39
|
+
Paused,
|
|
40
|
+
Allowance(Address, Address), // (Owner, Agent)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const ALLOWANCE_EVENT: Symbol = symbol_short!("allowance");
|
|
44
|
+
const SPEND_EVENT: Symbol = symbol_short!("spend");
|
|
45
|
+
const FEE_EVENT: Symbol = symbol_short!("fee");
|
|
46
|
+
const CIRCUIT_EVENT: Symbol = symbol_short!("circuit");
|
|
47
|
+
|
|
48
|
+
/// Maximum protocol fee is 500 bps (5.00%). Default is 20 bps (0.20%).
|
|
49
|
+
pub const MAX_FEE_BPS: u32 = 500;
|
|
50
|
+
pub const DEFAULT_FEE_BPS: u32 = 20;
|
|
51
|
+
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
|
|
52
|
+
|
|
53
|
+
#[contract]
|
|
54
|
+
pub struct AgentRailContract;
|
|
55
|
+
|
|
56
|
+
#[contractimpl]
|
|
57
|
+
impl AgentRailContract {
|
|
58
|
+
/// Return the contract semver string matching Cargo.toml
|
|
59
|
+
pub fn get_version(env: Env) -> String {
|
|
60
|
+
String::from_str(&env, CONTRACT_VERSION)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/// Initialize the AgentRail Vault contract with an admin, settlement token (Circle USDC),
|
|
64
|
+
/// treasury address for protocol monetization, and initial fee basis points.
|
|
65
|
+
pub fn initialize(
|
|
66
|
+
env: Env,
|
|
67
|
+
admin: Address,
|
|
68
|
+
token: Address,
|
|
69
|
+
treasury: Address,
|
|
70
|
+
fee_bps: u32,
|
|
71
|
+
) -> Result<(), Error> {
|
|
72
|
+
if env.storage().instance().has(&DataKey::Admin) {
|
|
73
|
+
return Err(Error::AlreadyInitialized);
|
|
74
|
+
}
|
|
75
|
+
if fee_bps > MAX_FEE_BPS {
|
|
76
|
+
return Err(Error::InvalidFeeBps);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
env.storage().instance().set(&DataKey::Admin, &admin);
|
|
80
|
+
env.storage().instance().set(&DataKey::Token, &token);
|
|
81
|
+
env.storage().instance().set(&DataKey::Treasury, &treasury);
|
|
82
|
+
env.storage().instance().set(&DataKey::ProtocolFeeBps, &fee_bps);
|
|
83
|
+
env.storage().instance().set(&DataKey::Paused, &false);
|
|
84
|
+
Ok(())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/// Emergency Circuit Breaker: Freeze all spend transactions (requires admin auth).
|
|
88
|
+
pub fn pause(env: Env) -> Result<(), Error> {
|
|
89
|
+
let admin: Address = env
|
|
90
|
+
.storage()
|
|
91
|
+
.instance()
|
|
92
|
+
.get(&DataKey::Admin)
|
|
93
|
+
.ok_or(Error::NotInitialized)?;
|
|
94
|
+
admin.require_auth();
|
|
95
|
+
|
|
96
|
+
env.storage().instance().set(&DataKey::Paused, &true);
|
|
97
|
+
env.events()
|
|
98
|
+
.publish((CIRCUIT_EVENT, symbol_short!("pause")), (admin,));
|
|
99
|
+
Ok(())
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// Unfreeze contract and restore spend execution (requires admin auth).
|
|
103
|
+
pub fn unpause(env: Env) -> Result<(), Error> {
|
|
104
|
+
let admin: Address = env
|
|
105
|
+
.storage()
|
|
106
|
+
.instance()
|
|
107
|
+
.get(&DataKey::Admin)
|
|
108
|
+
.ok_or(Error::NotInitialized)?;
|
|
109
|
+
admin.require_auth();
|
|
110
|
+
|
|
111
|
+
env.storage().instance().set(&DataKey::Paused, &false);
|
|
112
|
+
env.events()
|
|
113
|
+
.publish((CIRCUIT_EVENT, symbol_short!("unpause")), (admin,));
|
|
114
|
+
Ok(())
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/// Query current pause circuit breaker state.
|
|
118
|
+
pub fn is_paused(env: Env) -> bool {
|
|
119
|
+
env.storage().instance().get(&DataKey::Paused).unwrap_or(false)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/// Admin configuration to adjust protocol monetization fee & treasury address.
|
|
123
|
+
pub fn set_protocol_fee(
|
|
124
|
+
env: Env,
|
|
125
|
+
treasury: Address,
|
|
126
|
+
fee_bps: u32,
|
|
127
|
+
) -> Result<(), Error> {
|
|
128
|
+
let admin: Address = env
|
|
129
|
+
.storage()
|
|
130
|
+
.instance()
|
|
131
|
+
.get(&DataKey::Admin)
|
|
132
|
+
.ok_or(Error::NotInitialized)?;
|
|
133
|
+
admin.require_auth();
|
|
134
|
+
|
|
135
|
+
if fee_bps > MAX_FEE_BPS {
|
|
136
|
+
return Err(Error::InvalidFeeBps);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
env.storage().instance().set(&DataKey::Treasury, &treasury);
|
|
140
|
+
env.storage().instance().set(&DataKey::ProtocolFeeBps, &fee_bps);
|
|
141
|
+
|
|
142
|
+
env.events()
|
|
143
|
+
.publish((FEE_EVENT, symbol_short!("set")), (treasury, fee_bps));
|
|
144
|
+
|
|
145
|
+
Ok(())
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/// Retrieve the current protocol fee configuration: (treasury_address, fee_bps).
|
|
149
|
+
pub fn get_protocol_fee(env: Env) -> Result<(Address, u32), Error> {
|
|
150
|
+
let treasury: Address = env
|
|
151
|
+
.storage()
|
|
152
|
+
.instance()
|
|
153
|
+
.get(&DataKey::Treasury)
|
|
154
|
+
.ok_or(Error::NotInitialized)?;
|
|
155
|
+
let fee_bps: u32 = env
|
|
156
|
+
.storage()
|
|
157
|
+
.instance()
|
|
158
|
+
.get(&DataKey::ProtocolFeeBps)
|
|
159
|
+
.unwrap_or(DEFAULT_FEE_BPS);
|
|
160
|
+
Ok((treasury, fee_bps))
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/// Grant a scoped allowance from a human owner to an autonomous AI agent keypair.
|
|
164
|
+
pub fn grant_allowance(
|
|
165
|
+
env: Env,
|
|
166
|
+
owner: Address,
|
|
167
|
+
agent: Address,
|
|
168
|
+
max_stroops: i128,
|
|
169
|
+
valid_until: u64,
|
|
170
|
+
) -> Result<(), Error> {
|
|
171
|
+
owner.require_auth();
|
|
172
|
+
|
|
173
|
+
if max_stroops <= 0 {
|
|
174
|
+
return Err(Error::InvalidAmount);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
let allowance = ScopedAllowance {
|
|
178
|
+
owner: owner.clone(),
|
|
179
|
+
agent: agent.clone(),
|
|
180
|
+
max_stroops,
|
|
181
|
+
spent_stroops: 0,
|
|
182
|
+
valid_until,
|
|
183
|
+
active: true,
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
let key = DataKey::Allowance(owner.clone(), agent.clone());
|
|
187
|
+
env.storage().persistent().set(&key, &allowance);
|
|
188
|
+
|
|
189
|
+
// Emit Soroban topic event for indexing
|
|
190
|
+
env.events()
|
|
191
|
+
.publish((ALLOWANCE_EVENT, symbol_short!("grant")), (owner, agent, max_stroops));
|
|
192
|
+
|
|
193
|
+
Ok(())
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/// Execute a micro-spend from an agent's active allowance.
|
|
197
|
+
/// Calculates protocol fee routed to treasury and net amount to destination.
|
|
198
|
+
pub fn execute_spend(
|
|
199
|
+
env: Env,
|
|
200
|
+
owner: Address,
|
|
201
|
+
agent: Address,
|
|
202
|
+
destination: Address,
|
|
203
|
+
amount: i128,
|
|
204
|
+
) -> Result<(), Error> {
|
|
205
|
+
// Enforce Circuit Breaker
|
|
206
|
+
if Self::is_paused(env.clone()) {
|
|
207
|
+
return Err(Error::ContractPaused);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
agent.require_auth();
|
|
211
|
+
|
|
212
|
+
if amount <= 0 {
|
|
213
|
+
return Err(Error::InvalidAmount);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let key = DataKey::Allowance(owner.clone(), agent.clone());
|
|
217
|
+
let mut allowance: ScopedAllowance = env
|
|
218
|
+
.storage()
|
|
219
|
+
.persistent()
|
|
220
|
+
.get(&key)
|
|
221
|
+
.ok_or(Error::AllowanceNotFound)?;
|
|
222
|
+
|
|
223
|
+
if !allowance.active {
|
|
224
|
+
return Err(Error::AllowanceInactive);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let now = env.ledger().timestamp();
|
|
228
|
+
if now > allowance.valid_until {
|
|
229
|
+
return Err(Error::AllowanceExpired);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let new_spent = allowance.spent_stroops.checked_add(amount).ok_or(Error::AllowanceExceeded)?;
|
|
233
|
+
if new_spent > allowance.max_stroops {
|
|
234
|
+
return Err(Error::AllowanceExceeded);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
allowance.spent_stroops = new_spent;
|
|
238
|
+
env.storage().persistent().set(&key, &allowance);
|
|
239
|
+
|
|
240
|
+
// Compute Protocol Fee monetization
|
|
241
|
+
let fee_bps: u32 = env
|
|
242
|
+
.storage()
|
|
243
|
+
.instance()
|
|
244
|
+
.get(&DataKey::ProtocolFeeBps)
|
|
245
|
+
.unwrap_or(0);
|
|
246
|
+
let fee_amount = if fee_bps > 0 {
|
|
247
|
+
(amount * fee_bps as i128) / 10_000
|
|
248
|
+
} else {
|
|
249
|
+
0
|
|
250
|
+
};
|
|
251
|
+
let net_amount = amount - fee_amount;
|
|
252
|
+
|
|
253
|
+
// Emit spend execution event
|
|
254
|
+
env.events()
|
|
255
|
+
.publish((SPEND_EVENT, symbol_short!("exec")), (agent, destination, net_amount));
|
|
256
|
+
|
|
257
|
+
// If fee was deducted, emit fee event for treasury tracking
|
|
258
|
+
if fee_amount > 0 {
|
|
259
|
+
if let Some(treasury) = env.storage().instance().get::<DataKey, Address>(&DataKey::Treasury) {
|
|
260
|
+
env.events()
|
|
261
|
+
.publish((FEE_EVENT, symbol_short!("route")), (treasury, fee_amount));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
Ok(())
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/// Revoke an active allowance immediately (emergency stop).
|
|
269
|
+
pub fn revoke_allowance(env: Env, owner: Address, agent: Address) -> Result<(), Error> {
|
|
270
|
+
owner.require_auth();
|
|
271
|
+
|
|
272
|
+
let key = DataKey::Allowance(owner.clone(), agent.clone());
|
|
273
|
+
let mut allowance: ScopedAllowance = env
|
|
274
|
+
.storage()
|
|
275
|
+
.persistent()
|
|
276
|
+
.get(&key)
|
|
277
|
+
.ok_or(Error::AllowanceNotFound)?;
|
|
278
|
+
|
|
279
|
+
allowance.active = false;
|
|
280
|
+
env.storage().persistent().set(&key, &allowance);
|
|
281
|
+
|
|
282
|
+
env.events()
|
|
283
|
+
.publish((ALLOWANCE_EVENT, symbol_short!("revoke")), (owner, agent));
|
|
284
|
+
|
|
285
|
+
Ok(())
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/// Fetch allowance status
|
|
289
|
+
pub fn get_allowance(env: Env, owner: Address, agent: Address) -> Option<ScopedAllowance> {
|
|
290
|
+
let key = DataKey::Allowance(owner, agent);
|
|
291
|
+
env.storage().persistent().get(&key)
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
#[cfg(test)]
|
|
296
|
+
mod test;
|