Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from web3 import Web3
|
| 3 |
+
from solcx import compile_source, install_solc
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# --- PAGE SETUP ---
|
| 7 |
+
st.set_page_config(page_title="Vivara Forge", page_icon="βοΈ", layout="wide")
|
| 8 |
+
st.title("βοΈ Vivara Forge (Python IDE)")
|
| 9 |
+
st.markdown("### Compile & Deploy Smart Contracts without Remix")
|
| 10 |
+
|
| 11 |
+
# --- SIDEBAR: CONFIGURATION ---
|
| 12 |
+
with st.sidebar:
|
| 13 |
+
st.header("βοΈ Network Settings")
|
| 14 |
+
# Default to Polygon Mainnet, but you can change it
|
| 15 |
+
rpc_url = st.text_input("RPC URL", value="https://polygon-rpc.com")
|
| 16 |
+
chain_id = st.number_input("Chain ID", value=137)
|
| 17 |
+
|
| 18 |
+
# Securely load Private Key from Hugging Face Secrets
|
| 19 |
+
private_key = os.getenv("PRIVATE_KEY")
|
| 20 |
+
|
| 21 |
+
if private_key:
|
| 22 |
+
st.success("π Admin Key Loaded Securely")
|
| 23 |
+
try:
|
| 24 |
+
w3 = Web3(Web3.HTTPProvider(rpc_url))
|
| 25 |
+
account = w3.eth.account.from_key(private_key)
|
| 26 |
+
my_address = account.address
|
| 27 |
+
st.info(f"Wallet: {my_address[:6]}...{my_address[-4:]}")
|
| 28 |
+
balance = w3.eth.get_balance(my_address) / 10**18
|
| 29 |
+
st.write(f"Balance: {balance:.4f} MATIC")
|
| 30 |
+
except:
|
| 31 |
+
st.error("Could not connect to RPC")
|
| 32 |
+
else:
|
| 33 |
+
st.error("β οΈ No PRIVATE_KEY found in Secrets!")
|
| 34 |
+
|
| 35 |
+
# --- MAIN LAYOUT ---
|
| 36 |
+
col1, col2 = st.columns([1.5, 1])
|
| 37 |
+
|
| 38 |
+
with col1:
|
| 39 |
+
st.subheader("π Solidity Editor")
|
| 40 |
+
# Default Code Template (Vivara Protocol)
|
| 41 |
+
default_code = """// SPDX-License-Identifier: MIT
|
| 42 |
+
pragma solidity ^0.8.0;
|
| 43 |
+
|
| 44 |
+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
| 45 |
+
|
| 46 |
+
contract MyToken is ERC20 {
|
| 47 |
+
constructor() ERC20("MyToken", "MTK") {
|
| 48 |
+
_mint(msg.sender, 1000 * 10**18);
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
"""
|
| 52 |
+
code_input = st.text_area("Write your Contract:", value=default_code, height=600)
|
| 53 |
+
|
| 54 |
+
with col2:
|
| 55 |
+
st.subheader("π Compiler & Deployer")
|
| 56 |
+
|
| 57 |
+
# 1. COMPILE STEP
|
| 58 |
+
if st.button("1. COMPILE CONTRACT"):
|
| 59 |
+
with st.spinner("Installing Solidity Compiler..."):
|
| 60 |
+
# Auto-install the right compiler version
|
| 61 |
+
try:
|
| 62 |
+
install_solc('0.8.20')
|
| 63 |
+
st.success("β
Compiler Ready (v0.8.20)")
|
| 64 |
+
except:
|
| 65 |
+
st.warning("Compiler already installed or error.")
|
| 66 |
+
|
| 67 |
+
with st.spinner("Compiling Code..."):
|
| 68 |
+
try:
|
| 69 |
+
# Compile the code from the text area
|
| 70 |
+
compiled_sol = compile_source(
|
| 71 |
+
code_input,
|
| 72 |
+
output_values=['abi', 'bin'],
|
| 73 |
+
solc_version='0.8.20'
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Get the last contract defined in the file
|
| 77 |
+
contract_id, contract_interface = list(compiled_sol.items())[-1]
|
| 78 |
+
|
| 79 |
+
st.session_state['abi'] = contract_interface['abi']
|
| 80 |
+
st.session_state['bytecode'] = contract_interface['bin']
|
| 81 |
+
st.session_state['contract_name'] = contract_id
|
| 82 |
+
|
| 83 |
+
st.success(f"β
Compiled: {contract_id}")
|
| 84 |
+
st.json(st.session_state['abi']) # Show ABI preview
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
st.error(f"Compilation Failed: {e}")
|
| 88 |
+
|
| 89 |
+
st.divider()
|
| 90 |
+
|
| 91 |
+
# 2. DEPLOY STEP
|
| 92 |
+
if 'bytecode' in st.session_state:
|
| 93 |
+
st.write(f"Ready to deploy: **{st.session_state['contract_name']}**")
|
| 94 |
+
|
| 95 |
+
# Constructor Arguments (Simple version: No args)
|
| 96 |
+
st.caption("Note: This simple deployer supports contracts without constructor arguments.")
|
| 97 |
+
|
| 98 |
+
if st.button("2. DEPLOY TO POLYGON"):
|
| 99 |
+
if not private_key:
|
| 100 |
+
st.error("Add PRIVATE_KEY to Secrets first.")
|
| 101 |
+
else:
|
| 102 |
+
try:
|
| 103 |
+
with st.spinner("Deploying..."):
|
| 104 |
+
# Build Contract Object
|
| 105 |
+
Contract = w3.eth.contract(abi=st.session_state['abi'], bytecode=st.session_state['bytecode'])
|
| 106 |
+
|
| 107 |
+
# Build Transaction
|
| 108 |
+
construct_txn = Contract.constructor().build_transaction({
|
| 109 |
+
'from': my_address,
|
| 110 |
+
'nonce': w3.eth.get_transaction_count(my_address),
|
| 111 |
+
'gas': 3000000,
|
| 112 |
+
'gasPrice': w3.to_wei('50', 'gwei'),
|
| 113 |
+
'chainId': int(chain_id)
|
| 114 |
+
})
|
| 115 |
+
|
| 116 |
+
# Sign & Send
|
| 117 |
+
signed = w3.eth.account.sign_transaction(construct_txn, private_key)
|
| 118 |
+
tx_hash = w3.eth.send_raw_transaction(signed.rawTransaction)
|
| 119 |
+
|
| 120 |
+
st.success(f"π Contract Deployed!")
|
| 121 |
+
st.write(f"TX Hash: {w3.to_hex(tx_hash)}")
|
| 122 |
+
st.write("Check PolygonScan to get your address.")
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
st.error(f"Deployment Failed: {e}")
|