unslept 0.1.0__tar.gz
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.
- unslept-0.1.0/.gitignore +9 -0
- unslept-0.1.0/LICENSE +21 -0
- unslept-0.1.0/PKG-INFO +52 -0
- unslept-0.1.0/README.md +36 -0
- unslept-0.1.0/pyproject.toml +23 -0
- unslept-0.1.0/tests/test_client.py +141 -0
- unslept-0.1.0/unslept/__init__.py +20 -0
- unslept-0.1.0/unslept/abi/AgentAccount.json +1106 -0
- unslept-0.1.0/unslept/abi/SettlementRouter.json +605 -0
- unslept-0.1.0/unslept/abi/UnsleptBuffer.json +1969 -0
- unslept-0.1.0/unslept/client.py +314 -0
- unslept-0.1.0/unslept/langchain_tool.py +49 -0
- unslept-0.1.0/unslept/web3_chain.py +104 -0
- unslept-0.1.0/unslept/x402.py +68 -0
unslept-0.1.0/.gitignore
ADDED
unslept-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Unslept contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
unslept-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: unslept
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Pay x402 paywalls from an Unslept agent account on Robinhood Chain.
|
|
5
|
+
Project-URL: Homepage, https://unslept-o29g.vercel.app/docs
|
|
6
|
+
Project-URL: Source, https://github.com/Unsleptxyz/Unslept
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Python: >=3.10
|
|
10
|
+
Provides-Extra: langchain
|
|
11
|
+
Requires-Dist: langchain-core>=0.3; extra == 'langchain'
|
|
12
|
+
Provides-Extra: web3
|
|
13
|
+
Requires-Dist: eth-account>=0.13; extra == 'web3'
|
|
14
|
+
Requires-Dist: web3<8,>=7; extra == 'web3'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# unslept
|
|
18
|
+
|
|
19
|
+
Pay x402 paywalls from an Unslept agent account on Robinhood Chain, from Python.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
pip install "unslept[web3]" # add [langchain] for the LangChain tool
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import os
|
|
27
|
+
from eth_account import Account
|
|
28
|
+
from unslept import Unslept
|
|
29
|
+
|
|
30
|
+
unslept = Unslept(
|
|
31
|
+
agent_account=os.environ["UNSLEPT_AGENT_ACCOUNT"],
|
|
32
|
+
signer=Account.from_key(os.environ["UNSLEPT_SIGNER_KEY"]),
|
|
33
|
+
network="robinhood-chain",
|
|
34
|
+
)
|
|
35
|
+
res = unslept.fetch("https://example.com/paid-resource")
|
|
36
|
+
print(res.status, res.text())
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
LangChain: `from unslept.langchain_tool import unslept_tool` and add `unslept_tool(unslept)` to your agent's tools.
|
|
40
|
+
|
|
41
|
+
## Configuration
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
# Robinhood Chain mainnet. Addresses are published in contracts/deployments/4663.json.
|
|
45
|
+
UNSLEPT_CHAIN_ID=4663
|
|
46
|
+
UNSLEPT_RPC_URL=https://rpc.mainnet.chain.robinhood.com
|
|
47
|
+
UNSLEPT_USDG=0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168
|
|
48
|
+
UNSLEPT_ROUTER=0x1FeAB3B8f88FB0667B686fDCB0206194e88c984b
|
|
49
|
+
UNSLEPT_BUFFERS=0xcD853fb30eCe3134Cf21e2f47B9e23eaf965F40D
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Unslept is experimental software on a guarded, unaudited mainnet launch. Yield is variable and not guaranteed. Give each agent a small daily cap.
|
unslept-0.1.0/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# unslept
|
|
2
|
+
|
|
3
|
+
Pay x402 paywalls from an Unslept agent account on Robinhood Chain, from Python.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pip install "unslept[web3]" # add [langchain] for the LangChain tool
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import os
|
|
11
|
+
from eth_account import Account
|
|
12
|
+
from unslept import Unslept
|
|
13
|
+
|
|
14
|
+
unslept = Unslept(
|
|
15
|
+
agent_account=os.environ["UNSLEPT_AGENT_ACCOUNT"],
|
|
16
|
+
signer=Account.from_key(os.environ["UNSLEPT_SIGNER_KEY"]),
|
|
17
|
+
network="robinhood-chain",
|
|
18
|
+
)
|
|
19
|
+
res = unslept.fetch("https://example.com/paid-resource")
|
|
20
|
+
print(res.status, res.text())
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
LangChain: `from unslept.langchain_tool import unslept_tool` and add `unslept_tool(unslept)` to your agent's tools.
|
|
24
|
+
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
# Robinhood Chain mainnet. Addresses are published in contracts/deployments/4663.json.
|
|
29
|
+
UNSLEPT_CHAIN_ID=4663
|
|
30
|
+
UNSLEPT_RPC_URL=https://rpc.mainnet.chain.robinhood.com
|
|
31
|
+
UNSLEPT_USDG=0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168
|
|
32
|
+
UNSLEPT_ROUTER=0x1FeAB3B8f88FB0667B686fDCB0206194e88c984b
|
|
33
|
+
UNSLEPT_BUFFERS=0xcD853fb30eCe3134Cf21e2f47B9e23eaf965F40D
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Unslept is experimental software on a guarded, unaudited mainnet launch. Yield is variable and not guaranteed. Give each agent a small daily cap.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.24"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "unslept"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Pay x402 paywalls from an Unslept agent account on Robinhood Chain."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
dependencies = []
|
|
13
|
+
|
|
14
|
+
[project.urls]
|
|
15
|
+
Homepage = "https://unslept-o29g.vercel.app/docs"
|
|
16
|
+
Source = "https://github.com/Unsleptxyz/Unslept"
|
|
17
|
+
|
|
18
|
+
[project.optional-dependencies]
|
|
19
|
+
web3 = ["web3>=7,<8", "eth-account>=0.13"]
|
|
20
|
+
langchain = ["langchain-core>=0.3"]
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["unslept"]
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from unslept import Policy, SettlementMode, Unslept, UnsleptError, decode_header, encode_header
|
|
5
|
+
from unslept.client import AccountState, HttpResponse, SettlementResult
|
|
6
|
+
from unslept.langchain_tool import pay_x402_request
|
|
7
|
+
|
|
8
|
+
USDG = "0x00000000000000000000000000000000000000aa"
|
|
9
|
+
PAYEE = "0x00000000000000000000000000000000000000bb"
|
|
10
|
+
AGENT = "0x00000000000000000000000000000000000000cc"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class FakeChain:
|
|
14
|
+
chain_id = 4242
|
|
15
|
+
usdg = USDG
|
|
16
|
+
|
|
17
|
+
def __init__(self):
|
|
18
|
+
self.calls = []
|
|
19
|
+
self.allowed = {PAYEE}
|
|
20
|
+
self.state = AccountState(Policy(5_000_000, 20_000_000), 20_000_000, False, False, 0, 100_000_000, 0)
|
|
21
|
+
|
|
22
|
+
def is_payee_allowed(self, payee, domain):
|
|
23
|
+
return payee in self.allowed or domain in self.allowed
|
|
24
|
+
|
|
25
|
+
def get_account_state(self):
|
|
26
|
+
return self.state
|
|
27
|
+
|
|
28
|
+
def request_payment(self, payee, amount, payment_id, domain, endpoint, mode):
|
|
29
|
+
self.calls.append((payee, amount, payment_id, domain, endpoint, mode))
|
|
30
|
+
return SettlementResult("0x" + "11" * 32, amount // 1000, USDG)
|
|
31
|
+
|
|
32
|
+
def set_policy(self, policy):
|
|
33
|
+
return "0x" + "22" * 32
|
|
34
|
+
|
|
35
|
+
def get_payments(self, limit=None):
|
|
36
|
+
return []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def paywall(price, schemes=("unslept-direct",)):
|
|
40
|
+
seen = []
|
|
41
|
+
|
|
42
|
+
def transport(method, url, headers, body):
|
|
43
|
+
seen.append(dict(headers))
|
|
44
|
+
if "PAYMENT-SIGNATURE" not in headers:
|
|
45
|
+
pr = {"x402Version": 2, "resource": {"url": url}, "accepts": [
|
|
46
|
+
{"scheme": s, "network": "eip155:4242", "amount": price, "asset": USDG, "payTo": PAYEE, "maxTimeoutSeconds": 60,
|
|
47
|
+
"extra": {"name": "Global Dollar", "version": "2"} if s == "exact" else {"paymentId": "0x" + "ab" * 32}}
|
|
48
|
+
for s in schemes]}
|
|
49
|
+
return HttpResponse(402, {"PAYMENT-REQUIRED": encode_header(pr)}, b"{}")
|
|
50
|
+
sr = encode_header({"success": True, "transaction": "0x" + "11" * 32, "network": "eip155:4242"})
|
|
51
|
+
return HttpResponse(200, {"PAYMENT-RESPONSE": sr}, b'{"ok":true}')
|
|
52
|
+
|
|
53
|
+
return transport, seen
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class ClientTest(unittest.TestCase):
|
|
57
|
+
def test_pays_and_retries(self):
|
|
58
|
+
chain = FakeChain()
|
|
59
|
+
t, seen = paywall("1000000")
|
|
60
|
+
u = Unslept(AGENT, chain=chain, transport=t)
|
|
61
|
+
r = u.fetch("https://api.example.com/data")
|
|
62
|
+
self.assertEqual(r.status, 200)
|
|
63
|
+
self.assertEqual(chain.calls[0][2], "0x" + "ab" * 32)
|
|
64
|
+
self.assertEqual(chain.calls[0][5], SettlementMode.DIRECT)
|
|
65
|
+
sent = decode_header(seen[1]["PAYMENT-SIGNATURE"])
|
|
66
|
+
self.assertEqual(sent["x402Version"], 2)
|
|
67
|
+
self.assertEqual(sent["payload"]["agentAccount"], AGENT)
|
|
68
|
+
p = u.local_payments()[0]
|
|
69
|
+
self.assertEqual((p.endpoint, p.amount, p.payee), ("https://api.example.com/data", 1_000_000, PAYEE))
|
|
70
|
+
|
|
71
|
+
def test_per_call_cap(self):
|
|
72
|
+
chain = FakeChain()
|
|
73
|
+
u = Unslept(AGENT, chain=chain, transport=paywall("6000000")[0])
|
|
74
|
+
with self.assertRaises(UnsleptError) as cm:
|
|
75
|
+
u.fetch("https://api.example.com/data")
|
|
76
|
+
self.assertEqual(cm.exception.code, "PER_CALL_CAP")
|
|
77
|
+
self.assertIn("6 USDG is above the per-call cap of 5 USDG", str(cm.exception))
|
|
78
|
+
self.assertEqual(chain.calls, [])
|
|
79
|
+
|
|
80
|
+
def test_empty_wallet(self):
|
|
81
|
+
chain = FakeChain()
|
|
82
|
+
chain.state.parked = 0
|
|
83
|
+
u = Unslept(AGENT, chain=chain, transport=paywall("1000000")[0])
|
|
84
|
+
with self.assertRaises(UnsleptError) as cm:
|
|
85
|
+
u.fetch("https://api.example.com/data")
|
|
86
|
+
self.assertEqual(cm.exception.code, "INSUFFICIENT_COLLATERAL")
|
|
87
|
+
|
|
88
|
+
def test_allowlist_and_daily_cap(self):
|
|
89
|
+
chain = FakeChain()
|
|
90
|
+
chain.allowed = set()
|
|
91
|
+
u = Unslept(AGENT, chain=chain, transport=paywall("1000000")[0])
|
|
92
|
+
with self.assertRaises(UnsleptError) as cm:
|
|
93
|
+
u.fetch("https://api.example.com/data")
|
|
94
|
+
self.assertEqual(cm.exception.code, "PAYEE_NOT_ALLOWED")
|
|
95
|
+
chain.allowed = {"api.example.com"}
|
|
96
|
+
chain.state.remaining_today = 10
|
|
97
|
+
with self.assertRaises(UnsleptError) as cm:
|
|
98
|
+
u.fetch("https://api.example.com/data")
|
|
99
|
+
self.assertEqual(cm.exception.code, "DAILY_CAP")
|
|
100
|
+
|
|
101
|
+
def test_exact_scheme_signs_eip3009(self):
|
|
102
|
+
chain = FakeChain()
|
|
103
|
+
captured = {}
|
|
104
|
+
|
|
105
|
+
class S:
|
|
106
|
+
address = "0x00000000000000000000000000000000000000dd"
|
|
107
|
+
|
|
108
|
+
def sign_typed_data(self, domain, types, primary_type, message):
|
|
109
|
+
captured.update(domain=domain, primary=primary_type, message=message)
|
|
110
|
+
return "0x" + "33" * 65
|
|
111
|
+
|
|
112
|
+
t, seen = paywall("2500000", ("exact",))
|
|
113
|
+
Unslept(AGENT, signer=S(), chain=chain, transport=t).fetch("https://api.example.com/data")
|
|
114
|
+
self.assertEqual(chain.calls[0][5], SettlementMode.EXACT)
|
|
115
|
+
self.assertEqual(captured["primary"], "TransferWithAuthorization")
|
|
116
|
+
self.assertEqual(captured["domain"]["chainId"], 4242)
|
|
117
|
+
self.assertEqual(captured["message"]["to"], PAYEE)
|
|
118
|
+
self.assertEqual(captured["message"]["nonce"], chain.calls[0][2])
|
|
119
|
+
sent = decode_header(seen[1]["PAYMENT-SIGNATURE"])
|
|
120
|
+
self.assertEqual(sent["payload"]["authorization"]["value"], "2500000")
|
|
121
|
+
|
|
122
|
+
def test_low_balance_and_tool(self):
|
|
123
|
+
chain = FakeChain()
|
|
124
|
+
chain.state.parked = 1_500_000
|
|
125
|
+
u = Unslept(AGENT, chain=chain, transport=paywall("1000000")[0])
|
|
126
|
+
fired = []
|
|
127
|
+
u.on_low_balance(fired.append, threshold=2_000_000)
|
|
128
|
+
out = json.loads(pay_x402_request(u, "https://api.example.com/data", max_amount_usdg="2"))
|
|
129
|
+
self.assertEqual(out["status"], 200)
|
|
130
|
+
self.assertEqual(out["payment"]["amount"], "1 USDG")
|
|
131
|
+
self.assertEqual(len(fired), 1)
|
|
132
|
+
over = json.loads(pay_x402_request(u, "https://api.example.com/data", max_amount_usdg="0.5"))
|
|
133
|
+
self.assertEqual(over["code"], "MAX_AMOUNT")
|
|
134
|
+
|
|
135
|
+
def test_bad_config(self):
|
|
136
|
+
with self.assertRaises(UnsleptError):
|
|
137
|
+
Unslept("nope")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
if __name__ == "__main__":
|
|
141
|
+
unittest.main()
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Unslept: pay x402 paywalls from an agent account whose idle balance keeps working on Robinhood Chain."""
|
|
2
|
+
from .client import (
|
|
3
|
+
AccountState,
|
|
4
|
+
Balances,
|
|
5
|
+
HttpResponse,
|
|
6
|
+
Payment,
|
|
7
|
+
Policy,
|
|
8
|
+
SettlementMode,
|
|
9
|
+
SettlementResult,
|
|
10
|
+
Unslept,
|
|
11
|
+
UnsleptError,
|
|
12
|
+
fmt_usdg,
|
|
13
|
+
)
|
|
14
|
+
from .x402 import SCHEME_EXACT, SCHEME_UNSLEPT, X402FormatError, decode_header, encode_header
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"AccountState", "Balances", "HttpResponse", "Payment", "Policy", "SettlementMode", "SettlementResult", "Unslept",
|
|
18
|
+
"UnsleptError", "fmt_usdg", "SCHEME_EXACT", "SCHEME_UNSLEPT", "X402FormatError", "decode_header", "encode_header",
|
|
19
|
+
]
|
|
20
|
+
__version__ = "0.1.0"
|