forked from x402-foundation/x402
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall_networks.py
More file actions
140 lines (113 loc) · 3.89 KB
/
Copy pathall_networks.py
File metadata and controls
140 lines (113 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
"""All Networks Server Example.
Demonstrates how to create a server that supports all available networks with
optional chain configuration via environment variables.
New chain support should be added here in alphabetic order by network prefix
(e.g., "eip155" before "solana" before "tvm").
"""
import os
import sys
from dotenv import load_dotenv
from fastapi import FastAPI
from pydantic import BaseModel
from x402.http import FacilitatorConfig, HTTPFacilitatorClient, PaymentOption
from x402.http.middleware.fastapi import PaymentMiddlewareASGI
from x402.http.types import RouteConfig
from x402.mechanisms.evm.exact import ExactEvmServerScheme
from x402.mechanisms.svm.exact import ExactSvmServerScheme
from x402.mechanisms.tvm import TVM_TESTNET
from x402.mechanisms.tvm.exact import ExactTvmServerScheme
from x402.schemas import Network
from x402.server import x402ResourceServer
load_dotenv()
# Configuration - optional per network
EVM_ADDRESS = os.getenv("EVM_ADDRESS")
SVM_ADDRESS = os.getenv("SVM_ADDRESS")
TVM_ADDRESS = os.getenv("TVM_ADDRESS")
# Validate at least one address is provided
if not EVM_ADDRESS and not SVM_ADDRESS and not TVM_ADDRESS:
print("❌ At least one of EVM_ADDRESS, SVM_ADDRESS, or TVM_ADDRESS is required")
sys.exit(1)
# Network configuration
EVM_NETWORK: Network = "eip155:84532" # Base Sepolia
SVM_NETWORK: Network = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1" # Solana Devnet
TVM_NETWORK: Network = os.getenv("TVM_NETWORK", TVM_TESTNET) # TON testnet by default
FACILITATOR_URL = os.getenv("FACILITATOR_URL", "https://x402.org/facilitator")
# Response schemas
class WeatherReport(BaseModel):
weather: str
temperature: int
class WeatherResponse(BaseModel):
report: WeatherReport
# App
app = FastAPI(
title="All Networks Server",
description="x402 server supporting EVM, SVM, and TVM networks",
version="2.0.0",
)
# Build accepts array dynamically based on configured addresses
accepts: list[PaymentOption] = []
if EVM_ADDRESS:
accepts.append(
PaymentOption(
scheme="exact",
pay_to=EVM_ADDRESS,
price="$0.001",
network=EVM_NETWORK,
)
)
if SVM_ADDRESS:
accepts.append(
PaymentOption(
scheme="exact",
pay_to=SVM_ADDRESS,
price="$0.001",
network=SVM_NETWORK,
)
)
if TVM_ADDRESS:
accepts.append(
PaymentOption(
scheme="exact",
pay_to=TVM_ADDRESS,
price="$0.001",
network=TVM_NETWORK,
)
)
# x402 Middleware
facilitator = HTTPFacilitatorClient(FacilitatorConfig(url=FACILITATOR_URL))
server = x402ResourceServer(facilitator)
# Register schemes dynamically based on configured addresses
if EVM_ADDRESS:
server.register(EVM_NETWORK, ExactEvmServerScheme())
if SVM_ADDRESS:
server.register(SVM_NETWORK, ExactSvmServerScheme())
if TVM_ADDRESS:
server.register(TVM_NETWORK, ExactTvmServerScheme())
routes = {
"GET /weather": RouteConfig(
accepts=accepts,
mime_type="application/json",
description="Weather report",
),
}
app.add_middleware(PaymentMiddlewareASGI, routes=routes, server=server)
# Routes
@app.get("/health")
async def health_check() -> dict[str, str]:
return {"status": "ok"}
@app.get("/weather")
async def get_weather() -> WeatherResponse:
return WeatherResponse(report=WeatherReport(weather="sunny", temperature=70))
if __name__ == "__main__":
import uvicorn
port = int(os.getenv("PORT", "4021"))
print(f"🚀 All Networks Server listening on http://localhost:{port}")
if EVM_ADDRESS:
print(f" EVM: {EVM_ADDRESS} on {EVM_NETWORK}")
if SVM_ADDRESS:
print(f" SVM: {SVM_ADDRESS} on {SVM_NETWORK}")
if TVM_ADDRESS:
print(f" TVM: {TVM_ADDRESS} on {TVM_NETWORK}")
print(f" Facilitator: {FACILITATOR_URL}")
print()
uvicorn.run(app, host="0.0.0.0", port=port)