-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
640 lines (552 loc) · 23.4 KB
/
app.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
from fastapi import FastAPI, Depends, HTTPException, WebSocket, BackgroundTasks, status
from fastapi.middleware.cors import CORSMiddleware
import requests
import asyncio
from sqlalchemy.orm import Session
import uuid
import os
import secrets
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jose import JWTError, jwt
from passlib.context import CryptContext
from pydantic import BaseModel
from datetime import datetime, timedelta
from sqlalchemy import or_
from xenon import create_wallet, import_wallet, get_gas_to_usdc, get_wallet_balances, send_base_eth
from schema import WalletImportRequest, CreateBusiness, Token, TokenData, UserInDB, LoginBusiness, BusinessOut, CreateCheckoutRequest, InitiateCheckout, WithdrawRequest
from database import SessionLocal, engine, get_db
from models import Base, Business, Wallet, Payment, Transaction, Analytics
import monitor
import api
from concurrent.futures import ThreadPoolExecutor, TimeoutError
from dotenv import load_dotenv
load_dotenv()
# Initialize ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=5)
app = FastAPI(title="LianFlow API")
app.include_router(api.router, prefix="/api/v1", tags=["API V1"])
# Enable CORS for frontend communication
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Update with specific domains in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# # JWT Configuration
SECRET_KEY = os.getenv("SECRET_KEY")
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60*24
# Password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# OAuth2 scheme
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
# Create tables
Base.metadata.create_all(bind=engine)
FRONTEND_URL = os.getenv("FRONTEND_URL")
def transacts(data, db: Session = Depends(get_db)):
print("transacts")
sender_address = data["sender"]
reciever_address = data["recv"]
amount = data["amount"]
email = data["email"]
payment_id = data["payment_id"]
print("move")
# payment = db.query(Payment).filter(Payment.payment_id == payment_id).first()
result = monitor.monitor_transactions(sender_address, reciever_address, amount)
print('done')
if result != False:
# Prepare transaction data
print("Success")
receipt = result["receipt"]
gas_used = receipt['gasUsed']
gas_price = receipt['effectiveGasPrice']
gas_fee = (gas_used * gas_price) / 10**18 # Convert from Wei to native token
print(gas_fee)
transaction_data = {
"transaction_hash": result["tx_hash"],
"status": 1, # 1 for successful
"receipt": receipt,
"amount": amount,
"gas_fee": gas_fee,
"blockNumber": receipt['blockNumber']
}
# Update payment and create transaction
update_payment_and_create_transaction(payment_id, transaction_data, db)
# # Prepare webhook data
# webhook_data = {
# "receipt": result,
# "email": email,
# "payment_id": payment_id,
# "status": "Successful",
# "amount": amount,
# "sender": sender_address,
# "receiver": reciever_address,
# "transaction_hash": result["tx_hash"],
# "user_id": payment.user_id,
# "gas_fee": gas_fee
# }
# response = requests.post(url, json=webhook_data)
# if response.status_code == 200:
# print(response.json())
# else:
# print(f"Failed to send webhook. Status code: {response.status_code}, Response: {response.text}")
else:
transaction_data = {
"transaction_hash": "Failed",
"status": 0, # 0 for failed
"receipt": {"from": sender_address, "to": reciever_address},
"amount": amount,
"gas_fee": 0,
"blockNumber": None
}
update_payment_and_create_transaction(payment_id, transaction_data, db)
def update_payment_and_create_transaction(payment_id, transaction_data, db: Session = Depends(get_db)):
"""
Update the payment table and create a new row in the Transactions table.
Args:
payment_id: UUID of the payment
transaction_data: Dict containing:
- transaction_hash: str
- status: int (1 for success, 0 for failure)
- receipt: dict with 'from' and 'to' addresses
- amount: float
- gas_fee: float
- blockNumber: int or None
"""
# Update the payment table
payment = db.query(Payment).filter(Payment.payment_id == payment_id).first()
if not payment:
raise HTTPException(status_code=404, detail="No Payment with this ID")
# Update payment status
payment.transaction_hash = transaction_data["transaction_hash"]
payment.status = "Successful" if transaction_data["status"] == 1 else "Failed"
db.commit()
db.refresh(payment)
# Create a new row in the Transactions table
new_transaction = Transaction(
transaction_id=str(uuid.uuid4()),
payment_id=payment_id,
from_address=transaction_data["receipt"]["from"],
to_address=transaction_data["receipt"]["to"],
amount=transaction_data["amount"],
gas_fee=transaction_data["gas_fee"],
status="Successful" if transaction_data["status"] == 1 else "Failed",
block_number=transaction_data["blockNumber"],
transaction_hash=transaction_data["transaction_hash"]
)
try:
db.add(new_transaction)
db.commit()
db.refresh(new_transaction)
except Exception as e:
db.rollback()
raise HTTPException(
status_code=500,
detail=f"Failed to create transaction record: {str(e)}"
)
return {
"message": "Payment and Transaction updated successfully",
"payment_status": payment.status,
"transaction_id": new_transaction.transaction_id
}
# Utility Functions
def generate_api_key():
"""Generate a secure API key."""
return secrets.token_urlsafe(32)
def to_dict(model):
"""Convert SQLAlchemy model instance to dictionary."""
return {column.name: getattr(model, column.name) for column in model.__table__.columns}
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password):
return pwd_context.hash(password)
def get_user(db, email: str):
user = db.query(Business).filter(Business.email == email).first()
if user:
return UserInDB(**to_dict(user))
raise HTTPException(
status_code=401,
detail="Account not found",
headers={"WWW-Authenticate": "Bearer"},
)
def authenticate_user(db, email: str, password: str):
user = get_user(db, email)
if not user or not verify_password(password, user.password_hash):
raise HTTPException(
status_code=401,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Bearer"},
)
return user
def create_access_token(data: dict, expires_delta: timedelta | None = None):
to_encode = data.copy()
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=15))
to_encode.update({"exp": expire})
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
# Dependency
async def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
token_data = TokenData(**payload)
email = token_data.sub
if email is None:
raise credentials_exception
except JWTError:
raise credentials_exception
user = db.query(Business).filter(Business.email == email).first()
if user is None:
raise credentials_exception
return user
# Routes
@app.get("/")
def read_root():
return {"message": "Welcome to LianFlow - A NeoX Crypto Payment Gateway for businesses!"}
@app.head("/")
def uptimer():
return {"status": "ok"}
@app.post("/users/signup", tags=["Auth"])
def create_user(body: CreateBusiness, db: Session = Depends(get_db)):
user = db.query(Business).filter(Business.email == body.email).first()
if user is not None:
raise HTTPException(
status_code=450,
detail="Business with this email already exists"
)
hashed_password = get_password_hash(body.password)
api_key = generate_api_key()
user = Business(
user_id=str(uuid.uuid4()),
email=body.email,
business_name=body.business_name,
password_hash=hashed_password,
api_key=api_key
)
db.add(user)
db.commit()
db.refresh(user)
address, private_key = create_wallet()
wallet = Wallet(wallet_id=str(uuid.uuid4()), user_id=user.user_id, address=address, private_key=private_key)
db.add(wallet)
db.commit()
db.refresh(wallet)
return {
"user_id": user.user_id,
"api_key": api_key,
"access_token": create_access_token(data={"sub": user.email}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
}
@app.post("/login", response_model=Token, tags=["Auth"])
async def login(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):
usr = db.query(Business).filter(Business.business_name == form_data.username).first()
user = authenticate_user(db, usr.email, form_data.password)
if not user:
raise HTTPException(
status_code=401,
detail="Email or Password incorrect",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = create_access_token(data={"sub": user.email},
expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
return {"access_token": access_token, "token_type": "bearer"}
@app.post('/users/token', summary="Create access and refresh tokens for user", response_model=Token, tags=["Auth"])
async def login_token(data: LoginBusiness, db: Session = Depends(get_db)):
result = db.query(Business).filter(Business.email == data.email).first()
user = result
if user is None:
raise HTTPException(
status_code=451,
detail="Incorrect email or password"
)
hashed_pass = user.password_hash
if not verify_password(data.password, hashed_pass):
raise HTTPException(
status_code=451,
detail="Incorrect email or password"
)
return {
"access_token": create_access_token(data={"sub": user.email}, expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)),
"token_type": "bearer"
}
@app.post("/wallet/create", response_model=dict, tags=["Wallet"])
def create_new_wallet(db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to create a new crypto wallet.
"""
# Check if business already has a wallet
existing_wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
if existing_wallet:
raise HTTPException(
status_code=400,
detail="Business already has a wallet"
)
address, private_key = create_wallet()
wallet = Wallet(user_id=business.user_id, address=address, private_key=private_key)
wallet_id = wallet.wallet_id
db.add(wallet)
db.commit()
db.refresh(wallet)
wallet = {
"wallet_address": address,
"private_key": private_key
}
return {"message": "Wallet created successfully", "wallet": wallet, "wallet_id":wallet_id}
@app.post("/wallet/import", response_model=dict, tags=["Wallet"])
def import_existing_wallet(body: WalletImportRequest, db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to import an existing wallet using a private key.
"""
try:
existing_wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
if existing_wallet:
raise HTTPException(
status_code=400,
detail="Business already has a wallet"
)
wallet = import_wallet(body.private_key)
wallet = Wallet(user_id=business.user_id, address=wallet["wallet_address"], private_key=body.private_key)
db.add(wallet)
db.commit()
db.refresh(wallet)
return {"message": "Wallet imported successfully", "wallet": wallet}
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/wallet/balance", response_model=dict, tags=["Wallet"])
def get_wallet_balance(db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to get wallet balances for the business's wallet.
"""
wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
if not wallet:
raise HTTPException(status_code=404, detail="No wallet found for this business")
balances = get_wallet_balances(wallet.address)
return {
"wallet_address": wallet.address,
"balances": balances
}
@app.get("/dashboard", tags=["Business"])
async def dashboard_details(db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
data = {}
wallets = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
payments = db.query(Payment).filter(Payment.user_id == business.user_id).all()
if wallets == None:
return {"message": "Create wallet", "status": 430}
else:
transactions = (
db.query(Transaction)
.filter(
or_(
Transaction.to_address == wallets.address,
Transaction.from_address == wallets.address
)
)
.order_by(Transaction.created_at.desc())
).all()
balances = get_wallet_balances(wallets.address)
data["balances"] = balances
data["business"] = business
data["num_of_payments"] = len(payments)
data["num_of_transactions"] = len(transactions)
data["wallets"] = wallets
data["transactions"] = sorted(payments, key=lambda x: x.created_at, reverse=True)[:3]
successful_transactions = [t for t in transactions if t.status == "Successful"]
data["transaction_volume"] = sum(float(t.amount) for t in successful_transactions)
return data
@app.get("/me", tags=["Business"])
async def business_details(business: BusinessOut = Depends(get_current_user)):
return business
@app.post("/checkout/create", response_model=dict, tags=["Payment"])
def checkout_create(body: CreateCheckoutRequest, db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to initiate payment to business.
"""
business_wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
if not business_wallet:
raise HTTPException(status_code=404, detail="No wallet related to business")
reciever_address = business_wallet.address
amount = body.amount
payment = Payment(payment_id=str(uuid.uuid4()), user_id=business.user_id, receiver_address=reciever_address, amount=amount)
db.add(payment)
db.commit()
db.refresh(payment)
url = f"{FRONTEND_URL}/checkout/{payment.payment_id}"
post_data = {
"payment_id": payment.payment_id,
"url": url
}
return post_data
@app.get("/get/{paymentId}", response_model=dict, tags=["Payment"])
def get_payment(paymentId: str, db: Session = Depends(get_db),):
"""
API endpoint to get Payment Id details.
"""
payment = db.query(Payment).filter(Payment.payment_id == paymentId).first()
if not payment:
raise HTTPException(status_code=404, detail="No Payment with this ID")
amount = payment.amount
total_amount = get_gas_to_usdc(amount)
post_data = to_dict(payment)
post_data["business_name"] = payment.business.business_name
post_data["business_id"] = payment.user_id
post_data["gas_amount"] = amount
post_data["total_amount"] = total_amount
return post_data
@app.post("/checkout/initiate", response_model=dict, tags=["Payment"])
def initiate_checkout_payment(Data: InitiateCheckout, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
"""
API endpoint to Initiate Checkout Payment.
"""
payment = db.query(Payment).filter(Payment.payment_id == Data.payment_id).first()
if not payment:
raise HTTPException(status_code=404, detail="No Payment with this ID")
payment.data = Data.data
payment.sender_address = Data.sender_address
db.commit()
db.refresh(payment)
amount = payment.amount
total_amount = get_gas_to_usdc(amount)
post_data = {
"sender": Data.sender_address,
"recv": payment.receiver_address,
"amount": payment.amount,
"email": Data.data,
"payment_id": Data.payment_id
}
future = executor.submit(transacts, post_data, db)
print(future)
data = {
"payment_id": payment.payment_id,
"business_name": payment.business.business_name,
"gas_amount": payment.amount,
"total_amount": total_amount,
"merchant_address":payment.receiver_address
}
return data
@app.get("/regenerate-api-key", tags=["Business"])
async def regenerate_api_key(db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
Regenerate API key for the authenticated business.
"""
try:
# Generate new API key
new_api_key = generate_api_key()
# Update the business record
business_record = db.query(Business).filter(Business.user_id == business.user_id).first()
business_record.api_key = new_api_key
db.commit()
db.refresh(business_record)
return {
"message": "API key regenerated successfully",
"api_key": new_api_key
}
except Exception as e:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to regenerate API key"
)
@app.get("/transactions/")
@app.get("/transactions/{num}")
def get_transactions(num: str = None, db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to get Transaction to a wallet address.
"""
wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
transaction_query = (
db.query(Transaction)
.filter(
or_(
Transaction.to_address == wallet.address,
Transaction.from_address == wallet.address
)
)
.order_by(Transaction.created_at.desc())
)
if num:
transaction_query = transaction_query.limit(int(num))
transactions = transaction_query.all()
if not transactions:
pass
# raise HTTPException(status_code=404, detail="No Transaction to this wallet address")
post_data = {"transacts":[to_dict(transaction) for transaction in transactions]}
return post_data
@app.get("/payments/")
@app.get("/payments/{num}")
def get_payments(num: str = None, db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to get Transaction to a wallet address.
"""
wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
payment_query = (
db.query(Payment)
.filter(
or_(
Payment.receiver_address == wallet.address,
Payment.sender_address == wallet.address
)
)
.order_by(Payment.created_at.desc())
)
if num:
payment_query = payment_query.limit(int(num))
payments = payment_query.all()
if not payments:
pass
# raise HTTPException(status_code=404, detail="No Transaction to this wallet address")
post_data = {"payments":[to_dict(payment) for payment in payments]}
return post_data
@app.get("/payment/status/{paymentId}", response_model=dict)
def payment_status(paymentId: str, db: Session = Depends(get_db)):
"""
API endpoint to get Payment Id details.
"""
payment = db.query(Payment).filter(Payment.payment_id == paymentId).first()
if not payment:
raise HTTPException(status_code=404, detail="No Payment with this ID")
post_data = {
"payment_id": payment.payment_id,
"status": payment.status,
"transaction_hash": payment.transaction_hash
}
return post_data
def withdraw_neox(wallet, amount, receiver_address, db: Session = get_db()):
result = send_base_eth(wallet.address, wallet.private_key, receiver_address, amount)
receipt = result
gas_used = receipt['gasUsed']
gas_price = receipt['effectiveGasPrice']
gas_fee = (gas_used * gas_price) / 10**18 # Convert from Wei to native token
payment = Payment(payment_id=str(uuid.uuid4()), user_id=wallet.user_id, receiver_address=receiver_address, amount=amount, sender_address=wallet.address, status="Successful", data="Withdrawal", transaction_hash=result["transactionHash"])
db.add(payment)
db.commit()
db.refresh(payment)
transaction = Transaction(transaction_id=str(uuid.uuid4()), payment_id=payment.payment_id, from_address=result["from"], to_address=result["to"], amount=amount, gas_fee=gas_fee, status="Successful" if result["status"] == 1 else "Failed", block_number=result["blockNumber"], transaction_hash=result["transactionHash"])
db.add(transaction)
db.commit()
db.refresh(transaction)
return True
@app.post("/withdraw")
def withdraw(body: WithdrawRequest, background_tasks: BackgroundTasks, db: Session = Depends(get_db), business: BusinessOut = Depends(get_current_user)):
"""
API endpoint to withdraw funds from the business's wallet.
"""
# try:
wallet = db.query(Wallet).filter(Wallet.user_id == business.user_id).first()
if wallet.address == body.receiver_address:
raise HTTPException(status_code=402, detail="Cannot Withdraw to your Business Address")
if not wallet:
raise HTTPException(status_code=404, detail="No wallet found for this business")
balance = get_wallet_balances(wallet.address)
if body.amount > balance["GAS"]:
raise HTTPException(status_code=402, detail="Insufficient balance")
amount = body.amount
receiver_address = body.receiver_address
background_tasks.add_task(withdraw_neox, wallet, amount, receiver_address, db)
# except Exception as e:
# raise HTTPException(status_code=400, detail=f"Failed to send transaction, {e}")
return {"message": "Withdrawal Initiated successfully"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("app:app", host="127.0.0.1", port=5000, reload=True, workers=2)