forked from temptemp3/reverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.rsh
258 lines (235 loc) · 6.33 KB
/
interface.rsh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"reach 0.1";
"use strict";
// -----------------------------------------------
// Name: ALGO/ETH/CFX NFT Jam Reverse Auction
// Author: Nicholas Shellabarger
// Version: 0.4.2 - fix cancel, add seller addr
// Requires Reach v0.1.7
// -----------------------------------------------
const SERIAL_VER = 1;
import { min, max } from "@nash-protocol/starter-kit#lite-v0.1.9r1:util.rsh";
const DIST_LENGTH = 10;
// FUNCS
/*
* precision used in fixed point arithmetic
*/
const precision = 1000000; // 10 ^ 6
/*
* calculate price based on seconds elapsed since reference secs
*/
const priceFunc = (startPrice, floorPrice, referenceConcensusSecs, dk) =>
max(
floorPrice,
((diff) => (startPrice <= diff ? floorPrice : startPrice - diff))(
min(
((lastConsensusSecs() - referenceConcensusSecs) * dk) / precision,
startPrice - floorPrice
)
)
);
/*
* caculate percent
* c - cap
* i - part
* p - precision
*/
const percent = (c, i, p) => {
const fD = fx(6)(Pos, i);
const fD2 = fx(6)(Pos, c);
return fxdiv(fD, fD2, p);
};
/*
* calulate payout
* amt - amount to split
* d - distribution [0,10000]
*/
const payout = (rc, amt, d) =>
fxmul(fx(6)(Pos, amt), percent(rc, d, precision)).i.i / precision;
// calculate slope
const calc = (d, d2, p) => {
const fD = fx(6)(Pos, d);
const fD2 = fx(6)(Pos, d2);
return fxdiv(fD, fD2, p);
};
// INTERACTS
const relayInteract = {};
const auctioneerInteract = {
getParams: Fun(
[],
Object({
tokenAmount: UInt, // NFT token amount
rewardAmount: UInt, // 1 ALGO
startPrice: UInt, // 100
floorPrice: UInt, // 1
endSecs: UInt, // 1
addrs: Array(Address, DIST_LENGTH),
distr: Array(UInt, DIST_LENGTH),
royaltyCap: UInt,
})
),
signal: Fun([], Null),
};
export const Event = () => [];
export const Participants = () => [
Participant("Auctioneer", auctioneerInteract),
ParticipantClass("Relay", relayInteract),
];
export const Views = () => [
View({
manager: Address, // Standard View: Manager Address
token: Token,
currentPrice: UInt,
startPrice: UInt,
floorPrice: UInt,
closed: Bool,
endSecs: UInt,
priceChangePerSec: UInt,
seller: Address,
}),
];
export const Api = () => [
API({
touch: Fun([], Null),
acceptOffer: Fun([], Null),
cancel: Fun([], Null),
}),
];
export const App = (map) => {
const [{ amt, ttl, tok0: token }, [addr, _], [Auctioneer, Relay], [v], [a], _] = map;
Auctioneer.only(() => {
const {
tokenAmount,
rewardAmount,
startPrice,
floorPrice,
endSecs,
addrs,
distr,
royaltyCap,
} = declassify(interact.getParams());
assume(tokenAmount > 0);
assume(rewardAmount > 0);
assume(floorPrice > 0);
assume(floorPrice <= startPrice); // fp < sp => auction, fp == sp => sale
assume(endSecs > 0);
assume(distr.sum() <= royaltyCap);
assume(royaltyCap == (10 * floorPrice) / 1000000);
});
Auctioneer.publish(
tokenAmount,
rewardAmount,
startPrice,
floorPrice,
endSecs,
addrs,
distr,
royaltyCap
)
.pay([amt + rewardAmount + SERIAL_VER, [tokenAmount, token]])
.timeout(relativeTime(ttl), () => {
Anybody.publish();
commit();
exit();
});
require(tokenAmount > 0);
require(rewardAmount > 0);
require(floorPrice > 0);
require(floorPrice <= startPrice); // fp < sp => auction, fp == sp => sale
require(endSecs > 0);
require(distr.sum() <= royaltyCap);
require(royaltyCap == (10 * floorPrice) / 1000000);
transfer(amt).to(addr);
v.startPrice.set(startPrice);
v.floorPrice.set(floorPrice);
v.endSecs.set(endSecs);
v.token.set(token);
v.closed.set(false);
v.seller.set(Auctioneer);
Auctioneer.interact.signal();
const referenceConcensusSecs = lastConsensusSecs();
const dk = calc(
startPrice - floorPrice,
endSecs - referenceConcensusSecs,
precision
).i.i;
v.priceChangePerSec.set(dk / precision);
const [keepGoing, currentPrice] = parallelReduce([true, startPrice])
.define(() => {
v.currentPrice.set(currentPrice);
})
.invariant(balance() >= rewardAmount)
.while(keepGoing)
.api(
a.touch,
() => assume(currentPrice >= floorPrice),
() => 0,
(k) => {
require(currentPrice >= floorPrice);
k(null);
return [
true,
priceFunc(startPrice, floorPrice, referenceConcensusSecs, dk),
];
}
)
.api(
a.acceptOffer,
() => assume(true),
() => currentPrice,
(k) => {
require(true);
k(null);
const cent = currentPrice / 100;
const partTake = (currentPrice - cent) / royaltyCap;
const distrTake = distr.slice(0, DIST_LENGTH).sum();
const sellerTake = currentPrice - cent - partTake * distrTake;
transfer(cent).to(addr);
transfer(partTake * distr[0]).to(addrs[0]);
transfer(sellerTake).to(Auctioneer);
transfer([[balance(token), token]]).to(this);
return [false, currentPrice];
}
)
.api(
a.cancel,
() => assume(this === Auctioneer),
() => 0,
(k) => {
require(this === Auctioneer);
k(null);
transfer([[balance(token), token]]).to(this);
return [false, 0];
}
)
.timeout(false);
v.closed.set(true); // Set View Closed
commit();
// REM Auction over
// REM Relay races to reward while distributing proceeds
Relay.publish();
const cent = currentPrice / 100;
const partTake = (currentPrice - cent) / royaltyCap;
const distrTake = distr.slice(1, DIST_LENGTH - 1).sum();
const recvAmount = balance() - partTake * distrTake; // REM includes reward amount
transfer(partTake * distr[1]).to(addrs[1]);
transfer(partTake * distr[2]).to(addrs[2]);
transfer(partTake * distr[3]).to(addrs[3]);
commit();
Relay.publish();
transfer(partTake * distr[4]).to(addrs[4]);
transfer(partTake * distr[5]).to(addrs[5]);
transfer(partTake * distr[6]).to(addrs[6]);
commit();
Relay.only(() => {
const rAddr = this;
});
Relay.publish(rAddr);
transfer(partTake * distr[7]).to(addrs[7]);
transfer(partTake * distr[8]).to(addrs[8]);
transfer(partTake * distr[9]).to(addrs[9]);
transfer(recvAmount).to(rAddr);
transfer([[balance(token), token]]).to(rAddr);
commit();
exit();
};
// -----------------------------------------------