aboutsummaryrefslogtreecommitdiffstats
path: root/ui-desktop/src/main/java/market/guess/ui/desktop/controllers/CreateEventDialogController.java
blob: 5649145ca407debbd43d11ed99653a3481b34c57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package market.guess.ui.desktop.controllers;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import market.guess.model.event.CommissionTiming;
import market.guess.model.event.CreateEventRequest;
import market.guess.model.event.MechanismType;
import market.guess.ui.desktop.util.Format;
import market.guess.ui.desktop.util.Views;

public class CreateEventDialogController {
  private Runnable onCancel;
  private Consumer<CreateEventRequest> onCreateRequest;
  private BiConsumer<String, MechanismType> onCreate;
  private MechanismType selectedType = MechanismType.LMSR;

  @FXML private TextField nameField;
  @FXML private TextArea descArea;
  @FXML private Button lmsrBtn;
  @FXML private Button orderBookBtn;
  @FXML private ComboBox<String> feeCollectionCombo;
  @FXML private TextField feePercentField;
  @FXML private Label paramLabel;
  @FXML private TextField paramField;
  @FXML private TextField opt1Field;
  @FXML private TextField opt2Field;
  @FXML private CheckBox mintingCheckBox;
  @FXML private Label infoNoteLabel;

  @FXML
  private void initialize() {
    feeCollectionCombo.getItems().setAll("On purchase", "On close");
    feeCollectionCombo.setValue("On purchase");
    updateTypeSelection();
  }

  public void setOnCancel(Runnable onCancel) {
    this.onCancel = onCancel;
  }

  public void setOnCreate(Consumer<CreateEventRequest> onCreateRequest) {
    this.onCreateRequest = onCreateRequest;
  }

  public void setOnCreate(BiConsumer<String, MechanismType> onCreate) {
    this.onCreate = onCreate;
  }

  public void reset() {
    nameField.setText("Will the summit happen before Q4?");
    descArea.setText(
        "Resolves YES if a joint summit is publicly confirmed and held before October 1st.");
    feeCollectionCombo.setValue("On purchase");
    feePercentField.setText("5");
    opt1Field.setText("YES");
    opt2Field.setText("NO");
    selectedType = MechanismType.LMSR;
    updateTypeSelection();
  }

  private void updateTypeSelection() {
    Views.setSelected(lmsrBtn, selectedType == MechanismType.LMSR);
    Views.setSelected(orderBookBtn, selectedType == MechanismType.ORDER_BOOK);

    boolean isLmsr = selectedType == MechanismType.LMSR;
    paramLabel.setText(isLmsr ? "Liquidity b (integer)" : "Base value d ($)");
    paramField.setText(isLmsr ? "100" : "1.00");
    mintingCheckBox.setVisible(!isLmsr);

    String infoText =
        isLmsr
            ? "Opening this event will move "
                + Format.money(BigDecimal.valueOf(100 * Math.log(2)))
                + " of subsidy from your account into the contract account (b  ln 2)."
            : "Opening this event will buy the initial share inventory from your account into the"
                + " contract account.";
    infoNoteLabel.setText(infoText);
  }

  @FXML
  private void handleSelectLmsr() {
    selectedType = MechanismType.LMSR;
    updateTypeSelection();
  }

  @FXML
  private void handleSelectOrderBook() {
    selectedType = MechanismType.ORDER_BOOK;
    updateTypeSelection();
  }

  @FXML
  private void handleCancel() {
    if (onCancel != null) {
      onCancel.run();
    }
  }

  @FXML
  private void handleCreate() {
    String name = nameField.getText() != null ? nameField.getText().trim() : "";
    String desc = descArea.getText() != null ? descArea.getText().trim() : "";
    MechanismType type = selectedType;
    CommissionTiming timing = "On close".equalsIgnoreCase(feeCollectionCombo.getValue())
        ? CommissionTiming.ON_CLOSE
        : CommissionTiming.ON_PURCHASE;

    int fee = 5;
    try {
      fee = Integer.parseInt(feePercentField.getText().trim());
      if (fee < 0) fee = 0;
      if (fee > 90) fee = 90;
    } catch (Exception ignored) {
    }

    Integer b = null;
    BigDecimal d = null;
    if (type == MechanismType.LMSR) {
      try {
        b = Integer.parseInt(paramField.getText().trim());
      } catch (Exception ignored) {
        b = 100;
      }
    } else {
      try {
        d = new BigDecimal(paramField.getText().trim());
      } catch (Exception ignored) {
        d = BigDecimal.ONE;
      }
    }

    String opt1 = opt1Field.getText() != null && !opt1Field.getText().isBlank() ? opt1Field.getText().trim() : "YES";
    String opt2 = opt2Field.getText() != null && !opt2Field.getText().isBlank() ? opt2Field.getText().trim() : "NO";
    boolean minting = mintingCheckBox.isSelected();

    var request = new CreateEventRequest(
        name,
        desc,
        type,
        fee,
        timing,
        null,
        b,
        d,
        List.of(opt1, opt2),
        minting);

    if (onCreateRequest != null) {
      onCreateRequest.accept(request);
    } else if (onCreate != null) {
      onCreate.accept(name, type);
    }
  }
}