Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjagod1251 committed Aug 4, 2023
0 parents commit 0440403
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
.hypothesis/
.pytest_cache/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
chris
85 changes: 85 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from fastapi import FastAPI, File, UploadFile, HTTPException, BackgroundTasks
import os
import random
import string
import subprocess
import json

app = FastAPI()

def is_supported_language(filename):
# Add your supported languages here (Vyper)
supported_languages = [".vy"]
_, file_extension = os.path.splitext(filename)
return file_extension.lower() in supported_languages

def compile_files(task_id, files, vyper_version):
# Your Vyper compilation logic here
# This is just a dummy example that prints the filenames and Vyper version
print(f"Task ID: {task_id}")
print("Files to compile:")
for file in files:
print(file)
print(f"Vyper version: {vyper_version}")

# Replace this with actual Vyper compilation command using subprocess
# Example command: subprocess.run(["vyper", "--version"]) or subprocess.run(["vyper", "my_contract.vy"])
# Use vyper_version to specify the desired Vyper version in the command

@app.post("/compile/")
async def create_compilation_task(
files: List[UploadFile] = File(...),
vyper_version: str = Query(..., title="Vyper version to use for compilation")
):
supported_files = [file.filename for file in files if is_supported_language(file.filename)]
if not supported_files:
raise HTTPException(status_code=400, detail="Unsupported file format or language")

# Generate a task ID (for demonstration purposes, you can use a more robust method in a real application)
task_id = ''.join(random.choices(string.ascii_letters + string.digits, k=10))

# Run the compilation task in the background using TaskIQ
task_queue.add_task(compile_files, task_id, supported_files, vyper_version)

return {"task_id": task_id}


@app.get("/status/{task_id}")
async def get_task_status(task_id: str):
# Check the status of the task (dummy example, replace with actual status tracking)
# You should maintain the task status in a database or some other persistent storage
# Here, we just return random statuses for illustration purposes
statuses = ["In Progress", "Success", "Error"]
return random.choice(statuses)

@app.get("/exceptions/{task_id}")
async def get_task_exceptions(task_id: str):
# Fetch the exception information for a particular compilation task
# This is just a dummy example, you should handle exceptions and errors appropriately
status = await get_task_status(task_id)
if status != "Error":
raise HTTPException(status_code=400, detail="Task is not completed with Error status")

# Dummy list of compilation errors (replace with actual errors)
compilation_errors = ["Error 1: Compilation failed", "Error 2: Syntax error"]
return {"task_id": task_id, "compilation_errors": compilation_errors}

@app.get("/compiled_artifact/{task_id}")
async def get_compiled_artifact(task_id: str):
# Fetch the compiled artifact data in ethPM v3 format for a particular task
# This is just a dummy example, you should handle the response data accordingly based on the actual compilation result
status = await get_task_status(task_id)
if status != "Success":
raise HTTPException(status_code=400, detail="Task is not completed with Success status")

# Dummy ethPM v3 formatted manifest (replace with actual compiled data)
ethpm_manifest = {
"contract_name": "MyContract",
"abi": [{"name": "myFunction", "inputs": [], "outputs": []}],
"compiler": {
"name": "vyper",
"version": "0.2.10", # Replace with the actual Vyper compiler version used
"settings": {}
}
}
return json.dumps(ethpm_manifest)
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fastapi [all]
pytest
taskiq
53 changes: 53 additions & 0 deletions test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import json
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_create_compilation_task():
# Test create compilation task
response = client.post("/compile/")
assert response.status_code == 422 # Since the files parameter is missing, it should return 422 Unprocessable Entity

# Upload a valid Vyper file
files = {"files": ("my_contract.vy", open("path_to_your_vyper_file/my_contract.vy", "rb"))}
response = client.post("/compile/", files=files, data={"vyper_version": "0.2.10"})
assert response.status_code == 200
data = response.json()
assert "task_id" in data

def test_get_task_status():
# Test get task status
response = client.get("/status/some_invalid_task_id")
assert response.status_code == 400 # Invalid task_id should return 400 Bad Request

response = client.get("/status/some_valid_task_id")
assert response.status_code == 200
data = response.json()
assert data in ["In Progress", "Success", "Error"]

def test_get_task_exceptions():
# Test get task exceptions
response = client.get("/exceptions/some_invalid_task_id")
assert response.status_code == 400 # Invalid task_id should return 400 Bad Request

# Assuming the task_id has an Error status
response = client.get("/exceptions/some_valid_task_id")
assert response.status_code == 200
data = response.json()
assert "task_id" in data
assert "compilation_errors" in data

def test_get_compiled_artifact():
# Test get compiled artifact
response = client.get("/compiled_artifact/some_invalid_task_id")
assert response.status_code == 400 # Invalid task_id should return 400 Bad Request

# Assuming the task_id has a Success status
response = client.get("/compiled_artifact/some_valid_task_id")
assert response.status_code == 200
data = response.json()
assert "contract_name" in data
assert "abi" in data
assert "compiler" in data

0 comments on commit 0440403

Please sign in to comment.