// Package portbook is the on-chain Solidity -> Gno Migration Registry & Request Board // powered by Smelt (https://smeltcli.xyz). package portbook import ( "chain" "chain/runtime" "strconv" "strings" "gno.land/p/nt/avl/v0" ) // PortEntry records a Solidity contract transpiled and deployed to Gno.land. type PortEntry struct { ID uint64 Name string Category string SourceURL string SolHash string RealmPath string Mode string // "strict", "idiomatic", or "ai-assisted" CleanFuncs int TotalFuncs int Warns int Holes int Summary string Author address CreatedAt int64 Upvotes uint64 } // PortRequest records a community request to port a Solidity contract to Gno.land. type PortRequest struct { ID uint64 ContractName string Category string SourceURL string Notes string Requester address CreatedAt int64 Votes uint64 FulfilledPortID uint64 } var ( admin address nextPortID uint64 nextReqID uint64 totalVotes uint64 openReqCount uint64 portsByID *avl.Tree // key: idKey(id) -> *PortEntry requestsByID *avl.Tree // key: idKey(id) -> *PortRequest portUpvoters *avl.Tree // key: idKey(id) + ":" + string(addr) -> true reqVoters *avl.Tree // key: idKey(id) + ":" + string(addr) -> true contributors *avl.Tree // key: string(addr) -> true ) func init(cur realm) { admin = cur.Previous().Address() portsByID = avl.NewTree() requestsByID = avl.NewTree() portUpvoters = avl.NewTree() reqVoters = avl.NewTree() contributors = avl.NewTree() } // RegisterPort publishes a Solidity -> Gno migration record on-chain. func RegisterPort( cur realm, name string, category string, sourceURL string, solHash string, realmPath string, mode string, cleanFuncs int, totalFuncs int, warns int, holes int, summary string, ) uint64 { name = strings.TrimSpace(name) category = normalizeCategory(category) sourceURL = strings.TrimSpace(sourceURL) solHash = strings.TrimSpace(solHash) realmPath = strings.TrimSpace(realmPath) mode = normalizeMode(mode) summary = strings.TrimSpace(summary) if name == "" { panic("portbook: name is required") } if !strings.HasPrefix(realmPath, "gno.land/r/") && !strings.HasPrefix(realmPath, "gno.land/p/") { panic("portbook: realmPath must start with gno.land/r/ or gno.land/p/") } if cleanFuncs < 0 || totalFuncs < 0 || cleanFuncs > totalFuncs { panic("portbook: invalid cleanFuncs/totalFuncs counts") } if warns < 0 || holes < 0 { panic("portbook: warns and holes must be non-negative") } caller := cur.Previous().Address() nextPortID++ entry := &PortEntry{ ID: nextPortID, Name: name, Category: category, SourceURL: sourceURL, SolHash: solHash, RealmPath: realmPath, Mode: mode, CleanFuncs: cleanFuncs, TotalFuncs: totalFuncs, Warns: warns, Holes: holes, Summary: summary, Author: caller, CreatedAt: runtime.ChainHeight(), Upvotes: 0, } portsByID.Set(idKey(entry.ID), entry) contributors.Set(string(caller), true) chain.Emit( "PortRegistered", "id", strconv.FormatUint(entry.ID, 10), "name", entry.Name, "realmPath", entry.RealmPath, "mode", entry.Mode, "author", string(caller), ) return entry.ID } // UpvotePort casts a single upvote per address on a registered port. func UpvotePort(cur realm, portID uint64) { raw := portsByID.Get(idKey(portID)) if raw == nil { panic("portbook: port not found") } caller := cur.Previous().Address() voteKey := idKey(portID) + ":" + string(caller) if portUpvoters.Has(voteKey) { panic("portbook: already upvoted this port") } entry := raw.(*PortEntry) entry.Upvotes++ totalVotes++ portUpvoters.Set(voteKey, true) contributors.Set(string(caller), true) chain.Emit( "PortUpvoted", "portID", strconv.FormatUint(portID, 10), "voter", string(caller), "upvotes", strconv.FormatUint(entry.Upvotes, 10), ) } // RequestPort submits a Solidity contract that the community wants ported to Gno.land. // Automatically casts the requester's initial vote. func RequestPort( cur realm, contractName string, category string, sourceURL string, notes string, ) uint64 { contractName = strings.TrimSpace(contractName) category = normalizeCategory(category) sourceURL = strings.TrimSpace(sourceURL) notes = strings.TrimSpace(notes) if contractName == "" { panic("portbook: contractName is required") } caller := cur.Previous().Address() nextReqID++ req := &PortRequest{ ID: nextReqID, ContractName: contractName, Category: category, SourceURL: sourceURL, Notes: notes, Requester: caller, CreatedAt: runtime.ChainHeight(), Votes: 1, FulfilledPortID: 0, } requestsByID.Set(idKey(req.ID), req) reqVoters.Set(idKey(req.ID)+":"+string(caller), true) contributors.Set(string(caller), true) openReqCount++ totalVotes++ chain.Emit( "PortRequested", "id", strconv.FormatUint(req.ID, 10), "contractName", req.ContractName, "category", req.Category, "requester", string(caller), ) return req.ID } // VoteRequest casts a single vote per address on an open port request. func VoteRequest(cur realm, requestID uint64) { raw := requestsByID.Get(idKey(requestID)) if raw == nil { panic("portbook: request not found") } req := raw.(*PortRequest) if req.FulfilledPortID != 0 { panic("portbook: request is already fulfilled") } caller := cur.Previous().Address() voteKey := idKey(requestID) + ":" + string(caller) if reqVoters.Has(voteKey) { panic("portbook: already voted on this request") } req.Votes++ totalVotes++ reqVoters.Set(voteKey, true) contributors.Set(string(caller), true) chain.Emit( "RequestVoted", "requestID", strconv.FormatUint(requestID, 10), "voter", string(caller), "votes", strconv.FormatUint(req.Votes, 10), ) } // FulfillRequest links an open community request to a registered PortEntry. // Callable by the port author, the requester, or the realm admin. func FulfillRequest(cur realm, requestID uint64, portID uint64) { rawReq := requestsByID.Get(idKey(requestID)) if rawReq == nil { panic("portbook: request not found") } rawPort := portsByID.Get(idKey(portID)) if rawPort == nil { panic("portbook: port not found") } req := rawReq.(*PortRequest) if req.FulfilledPortID != 0 { panic("portbook: request already fulfilled") } port := rawPort.(*PortEntry) caller := cur.Previous().Address() if caller != port.Author && caller != req.Requester && caller != admin { panic("portbook: only port author, requester, or admin can fulfill") } req.FulfilledPortID = portID if openReqCount > 0 { openReqCount-- } chain.Emit( "RequestFulfilled", "requestID", strconv.FormatUint(requestID, 10), "portID", strconv.FormatUint(portID, 10), "by", string(caller), ) } // Stats returns aggregate registry counters for indexers, tests, and cross-realm reads. func Stats() (ports uint64, requests uint64, openRequests uint64, votes uint64, uniqueContributors int) { return nextPortID, nextReqID, openReqCount, totalVotes, contributors.Size() } // GetPort returns a registered port by ID, or nil if it does not exist. func GetPort(id uint64) *PortEntry { raw := portsByID.Get(idKey(id)) if raw == nil { return nil } return raw.(*PortEntry) } // GetRequest returns a community port request by ID, or nil if it does not exist. func GetRequest(id uint64) *PortRequest { raw := requestsByID.Get(idKey(id)) if raw == nil { return nil } return raw.(*PortRequest) } // DeterministicPct returns the integer percentage (0..100) of clean deterministic functions. func (p *PortEntry) DeterministicPct() int { if p.TotalFuncs == 0 { if p.Holes == 0 { return 100 } return 0 } pct := (p.CleanFuncs * 100) / p.TotalFuncs if p.Holes > 0 && pct == 100 { return 99 } return pct } func idKey(id uint64) string { s := strconv.FormatUint(id, 10) if len(s) >= 20 { return s } return strings.Repeat("0", 20-len(s)) + s } func normalizeMode(mode string) string { switch strings.ToLower(strings.TrimSpace(mode)) { case "", "strict", "deterministic": return "strict" case "idiomatic": return "idiomatic" case "ai-assisted", "hybrid", "assist": return "ai-assisted" default: panic("portbook: mode must be 'strict', 'idiomatic', or 'ai-assisted'") } } func normalizeCategory(cat string) string { cat = strings.TrimSpace(cat) if cat == "" { return "Utility" } return cat }