-
Notifications
You must be signed in to change notification settings - Fork 0
/
aa-lib.oscript
202 lines (154 loc) · 7.94 KB
/
aa-lib.oscript
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
getters: `{
$check_aa = ($aa) => {
require(is_aa($aa), "not an AA");
};
$get_reserve = ($aa, $yes_amount, $no_amount, $draw_amount) => {
// $check_aa($aa);
$coef = var[$aa]['coef'] OTHERWISE 1;
$r = ceil($coef * sqrt($yes_amount * $yes_amount + $no_amount * $no_amount + $draw_amount * $draw_amount));
$r
};
$get_exchange_result = ($aa, $yes_amount, $no_amount, $draw_amount) => {
// $check_aa($aa);
$params = definition[$aa][1].params;
$supply_yes = var[$aa]['supply_yes'] OTHERWISE 0;
$supply_no = var[$aa]['supply_no'] OTHERWISE 0;
$supply_draw = var[$aa]['supply_draw'] OTHERWISE 0;
$allow_draw = $params.allow_draw OTHERWISE false;
$current_reserve = var[$aa]['reserve'] OTHERWISE 0;
$coef = var[$aa]['coef'] OTHERWISE 1;
$issue_fee = exists($params.issue_fee) ? $params.issue_fee : 0.01;
$redeem_fee = exists($params.redeem_fee) ? $params.redeem_fee : 0.02;
$arb_profit_tax = exists($params.arb_profit_tax) ? $params.arb_profit_tax : 0.9;
$new_supply_yes = $supply_yes + ($yes_amount ? $yes_amount : 0);
$new_supply_no = $supply_no + ($no_amount ? $no_amount : 0);
$new_supply_draw = $supply_draw + ($draw_amount ? $draw_amount : 0);
$new_reserve = $get_reserve($aa, $new_supply_yes, $new_supply_no, $new_supply_draw);
$reserve_delta = $new_reserve - $current_reserve;
require($new_supply_yes >= 0 AND $new_supply_no >= 0 AND $new_supply_draw >= 0, "too many tokens to redeem?");
require($allow_draw OR $draw_amount == 0, "draw_asset doesn't exist");
$reserve_needed = $reserve_delta > 0 ? $reserve_delta : 0; // reserve needed to buy tokens, not including fees
$gross_payout = $reserve_delta < 0 ? abs($reserve_delta) : 0; // for redemptions, before fees
require($gross_payout <= $current_reserve, "too much reserve to redeem?");
$new_den = sqrt($new_supply_yes * $new_supply_yes + $new_supply_no * $new_supply_no + $new_supply_draw * $new_supply_draw);
// calc arb profit fee
if (($supply_yes + $supply_no + $supply_draw) != 0) {
$old_den = sqrt($supply_yes*$supply_yes + $supply_no*$supply_no + $supply_draw*$supply_draw);
$old_yes_price = $coef * ($supply_yes / $old_den);
$old_no_price = $coef * ($supply_no / $old_den);
$old_draw_price = $coef * ($supply_draw / $old_den);
$new_yes_price = $coef * ($new_supply_yes / $new_den);
$new_no_price = $coef * ($new_supply_no / $new_den);
$new_draw_price = $coef * ($new_supply_draw / $new_den);
$yes_arb_profit_tax = (abs(($old_yes_price - $new_yes_price) * $yes_amount) / 2) * $arb_profit_tax;
$no_arb_profit_tax = (abs(($old_no_price - $new_no_price) * $no_amount) / 2) * $arb_profit_tax;
$draw_arb_profit_tax = $allow_draw ? (abs(($old_draw_price - $new_draw_price) * $draw_amount) / 2) * $arb_profit_tax : 0;
} else {
$yes_arb_profit_tax = 0;
$no_arb_profit_tax = 0;
$draw_arb_profit_tax = 0;
}
$total_arb_profit_tax = $yes_arb_profit_tax + $no_arb_profit_tax + $draw_arb_profit_tax;
require($yes_arb_profit_tax >= 0 AND $no_arb_profit_tax >= 0 AND $draw_arb_profit_tax >= 0, "negative arb profit tax");
$fee = ceil($reserve_needed * $issue_fee + $gross_payout * $redeem_fee + $total_arb_profit_tax);
$next_coef = $coef * (($new_reserve + $fee) / $new_reserve);
{
reserve_needed: $reserve_needed,
new_reserve: $new_reserve + $fee,
gross_payout: $gross_payout,
fee: $fee,
arb_profit_tax: $total_arb_profit_tax,
next_coef: $next_coef,
old_coef: $coef,
yes_price: $next_coef * ($new_supply_yes / $new_den),
no_price: $next_coef * ($new_supply_no / $new_den),
draw_price: $next_coef * ($new_supply_draw / $new_den),
}
};
// sending $reserve_amount to buy tokens of specific $type (yes, no, or draw)
$get_result_for_buying_by_type = ($aa, $type, $reserve_amount) => {
// $check_aa($aa);
$params = definition[$aa][1].params;
$reserve_asset = $params.reserve_asset OTHERWISE 'base';
$issue_fee = exists($params.issue_fee) ? $params.issue_fee : 0.01;
$arb_profit_tax = exists($params.arb_profit_tax) ? $params.arb_profit_tax : 0.9;
require($type == 'yes' OR $type == 'no' OR $type == 'draw', "unknown type");
require($params.allow_draw OR $type != 'draw', "draw token does not exist");
require($reserve_amount - ($reserve_asset == 'base' ? 10000 : 0) > 0, "negative amount");
$coef = var[$aa]['coef'] OTHERWISE 1;
$supply_yes = var[$aa]['supply_yes'] OTHERWISE 0;
$supply_no = var[$aa]['supply_no'] OTHERWISE 0;
$supply_draw = var[$aa]['supply_draw'] OTHERWISE 0;
$network_fee = ($reserve_asset == 'base' ? 10000 : 0);
$gross_reserve_delta = $reserve_amount - $network_fee; // gross, because it includes the fee and tax
$fee = ceil($gross_reserve_delta * $issue_fee);
$old_reserve = var[$aa]['reserve'];
$new_reserve = $old_reserve + $gross_reserve_delta;
$reserve_without_fee = $new_reserve - $fee;
$ratio = ($reserve_without_fee * $reserve_without_fee) / ($coef * $coef);
$supply_yes_squared = $supply_yes * $supply_yes;
$supply_no_squared = $supply_no * $supply_no;
$supply_draw_squared = $supply_draw * $supply_draw;
if ($type == 'yes') {
$prepare_calc = $ratio - $supply_no_squared - $supply_draw_squared;
} else if ($type == 'no') {
$prepare_calc = $ratio - $supply_yes_squared - $supply_draw_squared;
} else {
$prepare_calc = $ratio - $supply_yes_squared - $supply_no_squared;
}
$supply = $type == 'yes' ? $supply_yes : $type == 'no' ? $supply_no : $supply_draw;
$amount = floor(sqrt($prepare_calc) - $supply);
$new_supply = $supply + $amount;
$new_supply_squared = $new_supply * $new_supply;
$new_den = sqrt(($type == 'yes' ? $new_supply_squared : $supply_yes_squared) + ($type == 'no' ? $new_supply_squared : $supply_no_squared) + ($type == 'draw' ? $new_supply_squared : $supply_draw_squared));
if (($supply_yes + $supply_no + $supply_draw) != 0) {
$old_den = sqrt($supply_yes_squared + $supply_no_squared + $supply_draw_squared);
$old_price = $coef * ($supply / $old_den);
$new_price = $coef * ($new_supply / $new_den);
$arb_profit_tax_amount = ((abs($old_price - $new_price) * $amount) / 2) * $arb_profit_tax;
$fee_with_arb_profit_tax = $fee + $arb_profit_tax_amount;
$reserve_without_tax_and_fee = $new_reserve - $fee_with_arb_profit_tax;
$new_ratio = ($reserve_without_tax_and_fee * $reserve_without_tax_and_fee) / ($coef * $coef);
if ($type == 'yes') {
$prepare_calc_2 = $new_ratio - $supply_no_squared - $supply_draw_squared;
} else if ($type == 'no') {
$prepare_calc_2 = $new_ratio - $supply_yes_squared - $supply_draw_squared;
} else {
$prepare_calc_2 = $new_ratio - $supply_yes_squared - $supply_no_squared;
}
$token_amount = floor(sqrt($prepare_calc_2) - $supply);
} else {
$token_amount = $amount;
$fee_with_arb_profit_tax = $fee;
}
// for stats
$next_coef = $coef * $new_reserve / ($new_reserve - $fee_with_arb_profit_tax);
$supply_by_type = $supply + $token_amount;
$supply_by_type_squared = $supply_by_type * $supply_by_type;
$den = sqrt(($type == 'yes' ? $supply_by_type_squared : $supply_yes_squared) + ($type == 'no' ? $supply_by_type_squared : $supply_no_squared) + ($type == 'draw' ? $supply_by_type_squared : $supply_draw_squared));
$yes_price = $next_coef * (($type == 'yes' ? $supply_by_type : $supply_yes) / $den);
$no_price = $next_coef * (($type == 'no' ? $supply_by_type : $supply_no) / $den);
$draw_price = $params.allow_draw ? $next_coef * (($type == 'draw' ? $supply_by_type : $supply_draw) / $den) : 0;
{
fee: $fee_with_arb_profit_tax,
gross_payout: 0,
old_coef: $coef,
reserve_needed: $gross_reserve_delta - $fee_with_arb_profit_tax,
new_reserve: $new_reserve,
arb_profit_tax: $arb_profit_tax_amount,
next_coef: $next_coef,
yes_price: $yes_price,
no_price: $no_price,
draw_price: $draw_price,
amount: $token_amount
}
};
}`,
messages: [{
app: 'state',
state: `{
bounce("library only");
}`
}]
}