APEX-Core-Node / main.go
UPNISO's picture
Create main.go
a37c2ac verified
Raw History Blame Contribute Delete
2.5 kB
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
)
// --- CONFIGURATION ---
const (
MaxSupply = 10000000.0
Port = ":8080" // Internal Port (Python talks to this)
)
// --- DATA STRUCTURES (Cosmos/Bitcoin Style) ---
type Block struct {
Index int
Timestamp string
Data string
Hash string
PrevHash string
}
type Wallet struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
}
type LedgerState struct {
Supply float64 `json:"current_supply"`
Wallets map[string]float64 `json:"wallets"`
Chain []Block `json:"chain"`
Mutex sync.Mutex
}
// Global State
var ApexState = LedgerState{
Supply: 0,
Wallets: make(map[string]float64),
Chain: []Block{},
}
// --- HANDLERS ---
// POST: Mint Coin (Only called by AI Sentinel)
func mintHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
return
}
var req struct {
Address string `json:"address"`
Amount float64 `json:"amount"`
}
json.NewDecoder(r.Body).Decode(&req)
ApexState.Mutex.Lock()
defer ApexState.Mutex.Unlock()
// 10M CAP ENFORCEMENT
if ApexState.Supply+req.Amount > MaxSupply {
http.Error(w, "Max Supply Reached", http.StatusForbidden)
return
}
ApexState.Wallets[req.Address] += req.Amount
ApexState.Supply += req.Amount
// Append to Chain (Simplified)
newBlock := Block{
Index: len(ApexState.Chain) + 1,
Data: fmt.Sprintf("MINT %f TO %s", req.Amount, req.Address),
}
ApexState.Chain = append(ApexState.Chain, newBlock)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "success",
"new_balance": ApexState.Wallets[req.Address],
"total_supply": ApexState.Supply,
})
}
// POST: Get Balance
func balanceHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost { // Using POST as requested
http.Error(w, "Use POST", http.StatusMethodNotAllowed)
return
}
var req struct {
Address string `json:"address"`
}
json.NewDecoder(r.Body).Decode(&req)
ApexState.Mutex.Lock()
bal := ApexState.Wallets[req.Address]
ApexState.Mutex.Unlock()
json.NewEncoder(w).Encode(map[string]float64{"balance": bal})
}
func main() {
http.HandleFunc("/mint", mintHandler)
http.HandleFunc("/balance", balanceHandler)
fmt.Println("APEX Go Ledger running on internal port 8080...")
log.Fatal(http.ListenAndServe(Port, nil))
}