-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add Sardis payment tools for ADK agents #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
795b67a
baacc40
9e391d7
8813b6b
611b73b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Sardis Payment Tools for Google ADK | ||
|
|
||
| Policy-controlled payments for AI agents built with [Google Agent Development Kit](https://google.github.io/adk-docs/). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pip install google-adk-community sardis | ||
| ``` | ||
|
|
||
| ## Setup | ||
|
|
||
| Set your environment variables: | ||
|
|
||
| ```bash | ||
| export SARDIS_API_KEY="sk_..." | ||
| export SARDIS_WALLET_ID="wal_..." | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| from google.adk.agents import Agent | ||
| from google.adk_community.tools.sardis import ( | ||
| sardis_pay, sardis_check_balance, sardis_check_policy, | ||
| ) | ||
|
|
||
| agent = Agent( | ||
| model="gemini-2.0-flash", | ||
| name="payment_agent", | ||
| description="An agent that can make policy-controlled payments", | ||
| tools=[sardis_pay, sardis_check_balance, sardis_check_policy], | ||
| ) | ||
| ``` | ||
|
|
||
| ## Available Tools | ||
|
|
||
| | Tool | Description | | ||
| |------|-------------| | ||
| | `sardis_pay` | Execute a payment with policy guardrails | | ||
| | `sardis_check_balance` | Check wallet balance and spending limits | | ||
| | `sardis_check_policy` | Pre-check if a payment would be allowed | | ||
|
|
||
| ## Features | ||
|
|
||
| - **Policy guardrails** — spending limits, merchant restrictions, category rules | ||
| - **Non-custodial** — MPC wallets, no private keys stored | ||
| - **Multi-chain** — Base, Polygon, Ethereum, Arbitrum, Optimism | ||
| - **Stablecoin native** — USDC, USDT, EURC, PYUSD | ||
| - **Full audit trail** — append-only ledger for compliance | ||
|
|
||
| ## Links | ||
|
|
||
| - [Sardis Documentation](https://sardis.sh/docs) | ||
| - [sardis-adk on PyPI](https://pypi.org/project/sardis-adk/) | ||
| - [GitHub](https://github.com/EfeDurmaz16/sardis) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Sardis payment tools for Google ADK - policy-controlled AI agent payments.""" | ||
|
|
||
| from .sardis_tools import sardis_check_balance | ||
| from .sardis_tools import sardis_check_policy | ||
| from .sardis_tools import sardis_pay | ||
|
|
||
| __all__ = [ | ||
| "sardis_pay", | ||
| "sardis_check_balance", | ||
| "sardis_check_policy", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Sardis payment tools for Google Agent Development Kit. | ||
|
|
||
| These tools enable ADK agents to make policy-controlled payments through | ||
| Sardis non-custodial MPC wallets. | ||
|
|
||
| Installation: | ||
| pip install google-adk-community sardis | ||
|
|
||
| Usage: | ||
| from google.adk.agents import Agent | ||
| from google.adk_community.tools.sardis import ( | ||
| sardis_pay, sardis_check_balance, sardis_check_policy, | ||
| ) | ||
|
|
||
| agent = Agent( | ||
| model="gemini-2.0-flash", | ||
| name="payment_agent", | ||
| tools=[sardis_pay, sardis_check_balance, sardis_check_policy], | ||
| ) | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import os | ||
| from typing import Optional | ||
|
|
||
| logger = logging.getLogger("google_adk." + __name__) | ||
|
|
||
|
|
||
| def sardis_pay( | ||
| recipient: str, | ||
| amount: str, | ||
| token: str = "USDC", | ||
| chain: str = "base", | ||
| memo: Optional[str] = None, | ||
| ) -> dict: | ||
| """Execute a policy-controlled payment via Sardis. | ||
|
|
||
| Args: | ||
| recipient: Destination wallet address or Sardis wallet ID. | ||
| amount: Amount to send as a decimal string (e.g. "10.00"). | ||
| token: Token symbol (USDC, EURC, USDT, PYUSD). Default: USDC. | ||
| chain: Chain to execute on (base, polygon, ethereum, arbitrum, | ||
| optimism). Default: base. | ||
| memo: Optional human-readable memo for the payment. | ||
|
|
||
| Returns: | ||
| A dict with keys: status, transaction_id, tx_hash, amount, token, chain. | ||
| """ | ||
| try: | ||
| from sardis import SardisClient | ||
| except ImportError: | ||
| return { | ||
| "status": "error", | ||
| "error": "sardis package required. Install with: pip install sardis", | ||
| } | ||
|
|
||
| api_key = os.environ.get("SARDIS_API_KEY") | ||
| wallet_id = os.environ.get("SARDIS_WALLET_ID") | ||
| if not api_key: | ||
| return { | ||
| "status": "error", | ||
| "error": "SARDIS_API_KEY environment variable not set.", | ||
| } | ||
| if not wallet_id: | ||
| return { | ||
| "status": "error", | ||
| "error": "SARDIS_WALLET_ID environment variable not set.", | ||
| } | ||
|
|
||
| client = SardisClient(api_key=api_key) | ||
| result = client.payments.send( | ||
| wallet_id, to=recipient, amount=float(amount), token=token, | ||
| purpose=memo or "Payment", | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| return { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| "status": "APPROVED" if result.success else "BLOCKED", | ||
| "transaction_id": getattr(result, "tx_id", ""), | ||
| "message": getattr(result, "message", ""), | ||
| "amount": amount, | ||
| "token": token, | ||
| "chain": chain, | ||
| } | ||
|
|
||
|
|
||
| def sardis_check_balance( | ||
| token: str = "USDC", | ||
| ) -> dict: | ||
| """Check the balance of a Sardis wallet. | ||
|
|
||
| Args: | ||
| token: Token symbol to check (e.g. "USDC"). Default: USDC. | ||
|
|
||
| Returns: | ||
| A dict with keys: balance, remaining, token. | ||
| """ | ||
| try: | ||
| from sardis import SardisClient | ||
| except ImportError: | ||
| return {"status": "error", "error": "sardis package required."} | ||
|
|
||
| api_key = os.environ.get("SARDIS_API_KEY") | ||
| wallet_id = os.environ.get("SARDIS_WALLET_ID") | ||
| if not api_key or not wallet_id: | ||
| return {"status": "error", "error": "SARDIS_API_KEY and SARDIS_WALLET_ID required."} | ||
|
|
||
| client = SardisClient(api_key=api_key) | ||
| balance = client.wallets.get_balance(wallet_id, token=token) | ||
| return { | ||
| "balance": str(balance.balance), | ||
| "remaining": str(balance.remaining), | ||
| "token": token, | ||
| } | ||
|
|
||
|
|
||
| def sardis_check_policy( | ||
| amount: str, | ||
| merchant: str, | ||
| ) -> dict: | ||
| """Check if a payment would be allowed by spending policy. | ||
|
|
||
| Args: | ||
| amount: Amount to check as a decimal string (e.g. "250.00"). | ||
| merchant: Merchant or recipient to check against policy rules. | ||
|
|
||
| Returns: | ||
| A dict with keys: allowed (bool), reason (str). | ||
| """ | ||
| try: | ||
| from sardis import SardisClient | ||
| except ImportError: | ||
| return {"status": "error", "error": "sardis package required."} | ||
|
|
||
| api_key = os.environ.get("SARDIS_API_KEY") | ||
| wallet_id = os.environ.get("SARDIS_WALLET_ID") | ||
| if not api_key or not wallet_id: | ||
| return {"status": "error", "error": "SARDIS_API_KEY and SARDIS_WALLET_ID required."} | ||
|
|
||
| client = SardisClient(api_key=api_key) | ||
| balance = client.wallets.get_balance(wallet_id) | ||
| amt = float(amount) | ||
| if amt > balance.remaining: | ||
| return { | ||
| "allowed": False, | ||
| "reason": f"Amount ${amt} exceeds remaining limit ${balance.remaining}", | ||
| } | ||
| if amt > balance.balance: | ||
| return { | ||
| "allowed": False, | ||
| "reason": f"Amount ${amt} exceeds balance ${balance.balance}", | ||
| } | ||
| return { | ||
| "allowed": True, | ||
| "reason": f"Payment of ${amt} to {merchant} would be allowed", | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Copyright 2025 Google LLC | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current tests only cover the error cases for missing environment variables. Consider adding unit tests that mock the |
||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Tests for Sardis payment tools.""" | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| from google.adk_community.tools.sardis.sardis_tools import ( | ||
| sardis_check_balance, | ||
| sardis_check_policy, | ||
| sardis_pay, | ||
| ) | ||
|
|
||
|
|
||
| def test_sardis_pay_missing_api_key(monkeypatch): | ||
| monkeypatch.delenv("SARDIS_API_KEY", raising=False) | ||
| monkeypatch.delenv("SARDIS_WALLET_ID", raising=False) | ||
| result = sardis_pay(recipient="0xABC", amount="10.00") | ||
| assert result["status"] == "error" | ||
|
|
||
|
|
||
| def test_sardis_check_balance_missing_key(monkeypatch): | ||
| monkeypatch.delenv("SARDIS_API_KEY", raising=False) | ||
| result = sardis_check_balance() | ||
| assert result["status"] == "error" | ||
|
|
||
|
|
||
| def test_sardis_check_policy_missing_key(monkeypatch): | ||
| monkeypatch.delenv("SARDIS_API_KEY", raising=False) | ||
| result = sardis_check_policy(amount="100.00", merchant="openai") | ||
| assert result["status"] == "error" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to catch specific exceptions (like
sardis.SardisError) rather than relying on a broadtry...exceptif the library provides them, or at least logging the error details if an unexpected exception occurs.