Playground
fees.gno
package cust0dy_core
// Deterministic basis-point fee math. One basis point is 1/100 of a percent.
//
// The marketplace charges two fees, both taken out of the buyer's locked
// payment at settlement time:
//
// seller fee 500 bps (5.0%), deducted from the agreed price
// buyer fee 250 bps (2.5%), added on top of the agreed price
//
// For offers, the maker locks an all-in amount and the buyer fee is waived;
// see the realm's AcceptOffer for the exact rule.
const (
SellerFeeBps int64 = 500
BuyerFeeBps int64 = 250
bpsDenominator int64 = 10000
)
// FeeFor returns amount * bps / 10000, rounded down. It never returns more
// than amount.
func FeeFor(amount, bps int64) int64 {
if amount <= 0 || bps <= 0 {
return 0
}
// Multiply first; a price that overflows int64 when multiplied by 500
// is far outside any sane trade, and the realm rejects it at listing.
fee := amount * bps / bpsDenominator
if fee > amount {
return amount
}
return fee
}
// BuyerTotal is what a buyer must lock to buy at the listed price:
// price + buyer fee.
func BuyerTotal(price int64) int64 {
return price + FeeFor(price, BuyerFeeBps)
}
// SellerPayout is what the seller receives at settlement: price - seller fee.
func SellerPayout(price int64) int64 {
return price - FeeFor(price, SellerFeeBps)
}
// PlatformCut is what the marketplace keeps from a full-price buy:
// the buyer fee plus the seller fee.
func PlatformCut(price int64) int64 {
return FeeFor(price, BuyerFeeBps) + FeeFor(price, SellerFeeBps)
}
// MaxPrice is the largest accepted price. It keeps amount*bps comfortably
// inside int64 and bounds nonsense listings.
const MaxPrice int64 = 1 << 50
gnomod.toml
module = "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cust0dy_core" gno = "" [addpkg] creator = "g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v" height = 1539073 max_deposit = "100000000ugnot"
types.gno
// Package cust0dy_core holds the pure domain logic for Cust0dy: asset
// types and validation, the escrow state machine, and fee math. It has no
// chain imports, so every rule is unit-testable off-chain.
package cust0dy_core
import (
"errors"
"strings"
)
// AssetKind identifies what is being traded on the marketplace.
type AssetKind int
const (
KindDomain AssetKind = iota // Web2 domain names (example.com)
KindSocial // social handles (X, Instagram, ...)
KindGno // Gno-native handles
)
func (k AssetKind) String() string {
switch k {
case KindDomain:
return "domain"
case KindSocial:
return "social"
case KindGno:
return "gno-handle"
default:
return "unknown"
}
}
// ParseKind accepts "domain", "social", "gno" (and a few obvious aliases).
func ParseKind(s string) (AssetKind, error) {
switch strings.ToLower(strings.TrimSpace(s)) {
case "domain", "domains", "d":
return KindDomain, nil
case "social", "handle", "social-handle", "s":
return KindSocial, nil
case "gno", "gno-handle", "gnohandle", "g":
return KindGno, nil
default:
return 0, errors.New("unknown asset kind " + s + " (want domain, social or gno)")
}
}
// ValidateAsset checks that value is plausible for its kind. It cannot prove
// ownership; that is what the off-chain verification flow is for.
func ValidateAsset(k AssetKind, value string) error {
v := strings.TrimSpace(value)
if v == "" {
return errors.New("empty asset value")
}
switch k {
case KindDomain:
return validateDomain(v)
case KindSocial:
return validateHandle(strings.TrimPrefix(v, "@"), 1, 30, "social handle")
case KindGno:
return validateHandle(strings.TrimPrefix(v, "@"), 1, 39, "gno handle")
default:
return errors.New("unknown asset kind")
}
}
func validateDomain(d string) error {
d = strings.ToLower(d)
if len(d) > 253 {
return errors.New("domain too long")
}
labels := strings.Split(d, ".")
if len(labels) < 2 {
return errors.New("domain needs at least one dot, like example.com")
}
for i, l := range labels {
if l == "" {
return errors.New("empty label in domain")
}
if len(l) > 63 {
return errors.New("label too long in domain")
}
if strings.HasPrefix(l, "-") || strings.HasSuffix(l, "-") {
return errors.New("labels cannot start or end with a hyphen")
}
for _, r := range l {
if !((r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r == '-') {
return errors.New("bad character in domain")
}
}
if i == len(labels)-1 && !onlyLetters(l) {
return errors.New("top-level domain must be letters")
}
}
return nil
}
func onlyLetters(s string) bool {
for _, r := range s {
if r < 'a' || r > 'z' {
return false
}
}
return len(s) >= 2
}
func validateHandle(h string, min, max int, what string) error {
if len(h) < min || len(h) > max {
return errors.New(what + " must be 1-" + itoa(max) + " characters")
}
for _, r := range h {
ok := (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') ||
(r >= '0' && r <= '9') || r == '_' || r == '.' || r == '-'
if !ok {
return errors.New("bad character in " + what)
}
}
return nil
}
func itoa(n int) string {
if n == 0 {
return "0"
}
digits := ""
for n > 0 {
digits = string(rune('0'+n%10)) + digits
n /= 10
}
return digits
}
// ListingState is the lifecycle of one marketplace listing.
type ListingState int
const (
ListingOpen ListingState = iota
ListingSold
ListingCancelled
)
func (s ListingState) String() string {
switch s {
case ListingOpen:
return "open"
case ListingSold:
return "sold"
case ListingCancelled:
return "cancelled"
default:
return "unknown"
}
}
// State is the escrow state machine. Every trade moves through these states;
// terminal states never change again.
type State int
const (
StateFunded State = iota
StateTransferSubmitted
StateCompleted
StateDisputed
StateResolvedSeller
StateResolvedBuyer
StateRefunded
)
func (s State) String() string {
switch s {
case StateFunded:
return "funded"
case StateTransferSubmitted:
return "transfer-submitted"
case StateCompleted:
return "completed"
case StateDisputed:
return "disputed"
case StateResolvedSeller:
return "resolved-seller"
case StateResolvedBuyer:
return "resolved-buyer"
case StateRefunded:
return "refunded"
default:
return "unknown"
}
}
// Terminal reports whether the escrow can no longer change state.
func (s State) Terminal() bool {
switch s {
case StateCompleted, StateResolvedSeller, StateResolvedBuyer, StateRefunded:
return true
default:
return false
}
}
// CanBuyerConfirm: the buyer may confirm receipt once the seller has
// submitted the transfer (or while still merely funded; confirming early is
// the buyer's own risk).
func CanBuyerConfirm(s State) bool {
return s == StateFunded || s == StateTransferSubmitted
}
// CanTimeoutRefund: the automatic refund guard applies only while the seller
// has never submitted a transfer. Once a transfer is on the table, the
// resolution path is buyer confirmation or a dispute.
func CanTimeoutRefund(s State) bool {
return s == StateFunded
}
// CanDispute: either party may open a dispute while the trade is live.
func CanDispute(s State) bool {
return s == StateFunded || s == StateTransferSubmitted
}