Home /Modules/advantage/business-exchange.sleela

business-exchange.sleela

lib/advantage/medium-results/business-exchange.sleela · 140 line(s) · sleela

<< lib/advantage/medium-results/business-exchange.sleela
   SLeeLa Standard Library Definition.
   Definition: MEDIUM-RESULT (Business). Demonstrates connecting, controlling,
   and executing a trade over open exchange in the USA using the advantage
   connect medium (SLNetConnector, SLClockConnector, SLSqlConnector,
   SLMoneyConnector) together with the network subject (SLMarketScout,
   SLNegotiationRound, SLCashflow). A small-business wholesale buy/sell. >>
#sleela 3.0
material;
artifact;
pure;
gasoline;
<< THEME: Business. A US small business connects to an exchange venue, checks
   the national clock before trading, scouts quotes, negotiates a clearing
   price, settles through a money source, and records the trade to its database.
   Anchors (hand-computed): best ask 90, best bid 110, spread 20, alpha 0.5 ->
   P* 100; 50 units buy 90 sell 100 -> in 4500 out 5000 profit 500; money
   captured 5000 cents per unit -> net settle math shown in receipt. Every
   external call that needs a host adapter returns code 9. >>
class BusinessExchange {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  String metaVersion = "1.0.0";
  int metaId = 10660;
  String metaAuthor = "Kiro Agent";
  String metaDate = "2026-11-08";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "Advantage";
  String metaCorporation = "SLeeLa project";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "National (United States)";
  String metaBirthDate = "2026-11-08";
  int metaScienceRating = 7;
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  boolean near(double a, double b, double tol) {
    double d = a - b;
    if (d < 0.0) { d = 0.0 - d; }
    return d < tol;
  }

  void main() {
    print("== medium-result: BUSINESS (US open exchange) ==");

    < 1. CONNECT: time discipline against a national clock, then the venue >
    SLClockConnector clock = new SLClockConnector();
    clock.init();
    clock.configure("time.nist.gov");
    clock.openLink();
    int cSync = clock.sync();
    clock.acceptSync(5.0, 2);

    SLNetConnector venue = new SLNetConnector();
    venue.init();
    venue.configure("exchange.us", 443);
    int vOpen = venue.openLink();
    int vProbe = venue.probe();
    venue.acceptProbe(12.0);

    < 2. CONTROL: scout quotes and negotiate a fair price >
    SLMarketScout scout = new SLMarketScout();
    scout.forGood("wholesale-widget");
    scout.addQuote("VenueA", 85.0, 90.0);
    scout.addQuote("VenueB", 95.0, 98.0);
    scout.addQuote("VenueC", 110.0, 115.0);
    double ask = scout.bestAsk();
    double bid = scout.bestBid();

    SLNegotiationRound neg = new SLNegotiationRound();
    neg.configure(ask, bid, 0.5);
    double price = neg.clearingPrice();

    < 3. EXECUTE: cash-flow receipt + settle through a money source >
    SLCashflow cf = new SLCashflow();
    cf.configure(50.0, ask, price, 7.0);
    cf.setFrictions(0.0, 0.0);

    SLMoneyConnector money = new SLMoneyConnector();
    money.init();
    money.configure("bank", "alias:smb-operating", "USD");
    money.openLink();
    int mAuth = money.authorize(5000);
    money.acceptAuthorization(5000);
    int mCap = money.capture(5000);

    < 4. RECORD: persist the trade to the business database >
    SLSqlConnector db = new SLSqlConnector();
    db.init();
    db.configure("db.us-business", 3306, "app", true, "ledger");
    db.openLink();
    int dExec = db.execute("INSERT INTO trades(good,price) VALUES('wholesale-widget',100)");

    print("  clock : " + clock.describe());
    print("  venue : " + venue.describe());
    print("  deal  : " + neg.describe());
    print("  money : " + cf.receipt("wholesale-widget") + " | " + money.receipt());
    print("  db    : " + db.describe());

    print("");
    print("== validation ==");
    boolean okConnect = (vOpen == 0) && venue.isReachable() && clock.isSynced();
    boolean okControl = near(ask, 90.0, 0.00001) && near(bid, 110.0, 0.00001)
                     && near(price, 100.0, 0.00001);
    boolean okExecute = near(cf.moneyIn(), 4500.0, 0.00001)
                     && near(cf.cashOut(), 5000.0, 0.00001)
                     && near(cf.profit(), 500.0, 0.00001)
                     && (mCap == 0) && (money.settledCents() == 5000);
    boolean okNative = (cSync == 9) && (vProbe == 9) && (dExec == 9);
    print("  connect (clock+venue) ? " + okConnect);
    print("  control (scout+deal)  ? " + okControl);
    print("  execute (cash+settle) ? " + okExecute);
    print("  native boundary (9)   ? " + okNative);
    if (okConnect) { if (okControl) { if (okExecute) { if (okNative) {
      print("ALL CHECKS PASSED");
    } } } }
  }
}