This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
526 lines (475 loc) · 37 KB
/
index.html
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<!doctype html>
<html lang="us">
<head>
<meta charset="utf-8">
<title>DeltaDollar: EthContract-for-Difference</title>
<link href="jquery-ui.css" rel="stylesheet">
<style>
body {
font: 100% "Trebuchet MS", sans-serif;
margin: 1em;
}
.slider {
margin-bottom: 1em;
}
._account {
font-family: Monospace;
}
._denom {
font-size: small;
border: 1px #ccc solid;
padding: 0px 3px;
border-radius: 8px;
font-weight: normal;
margin-left: 3px;
}
._balance {
font-weight: bold;
}
._grand ._denom { background-color: #ffe3e3; }
._grand._balance { color: #400; }
._ether ._denom { background-color: #fff3e5; }
._ether._balance { color: #420; }
._finney ._denom { background-color: #ffe; }
._finney._balance { color: #440; }
._szabo ._denom { background-color: #efe; }
._szabo._balance { color: #040; }
._wei ._denom { background-color: #eff; }
._wei._balance { color: #044; }
.account.default {
background-color: #ffd;
}
input.balance {
width: 5ex;
text-align: right;
}
input.adjustment {
width: 10ex;
text-align: right;
}
#accounts, #div {
}
select {
width: 17ex;
}
#markettype span, #order-review {
/*color: #666;*/
}
._usdamount {
font-weight: bold;
}
.them {
font-size: small;
color: #666;
}
span._adjustment {
color: #888;
}
span._adjustmentamount._positive {
font-weight: bold;
color: #6a6;
}
span._adjustmentamount._negative {
font-weight: bold;
color: #a66;
}
span._adjustmentamount._zero {
font-weight: bold;
color: #44b;
}
span.endtime {
font-size: small;
color: #888;
}
span.label {
color: #888;
}
ul {
padding: 0;
}
li {
list-style-type: none;
padding: 0.5ex;
border-bottom: 1px solid #eee;
}
span.event {
font-weight: bold;
font-size: small;
float: left;
width: 12ex
}
li._deposit {
background: #f0fff0;
border-left: 0.5ex solid #060;
}
li._deposit span.event { color: #060; }
li._order, li._match {
background: #fff8f0;
border-left: 0.5ex solid #630;
}
li._order span.event, li._match span.event { color: #630; }
li._cancel {
background: #f8fff0;
border-left: 0.5ex solid #360;
}
li._cancel span.event { color: #360; }
li._finalize {
background: #f0fff8;
border-left: 0.5ex solid #063;
}
li._finalize span.event { color: #063; }
li._withdraw {
background: #fff0f0;
border-left: 0.5ex solid #600;
}
li._withdraw span.event { color: #600; }
li._stable {
background: #f0ffff;
border-left: 0.5ex solid #066;
}
li._leveraged {
background: #fff4ff;
border-left: 0.5ex solid #606;
}
li._stable span.event {
color: #066;
font-weight: bold;
}
li._leveraged span.event {
color: #606;
font-weight: bold;
}
li button {
float: right;
}
.ui-selectmenu-button { vertical-align : middle; }
</style>
</head>
<body>
<h3>EthCore DeltaDollar: Ethereum Contract-for-Difference</h3>
<div>
<div style="float: right; width: 50%">
<h4>Funding History</h4>
<ul id="fundinglog">
</ul>
<h4>Orders Awaiting Fulfillment</h4>
<ul id="allorders">
</ul>
<h4>Agreements</h4>
<ul id="alldeals">
</ul>
</div>
<div style="width: 49%">
<h4>Accounts</h4>
<div id="accounts" class="accordion"></div>
</div>
<h4>Funding</h4>
<div id="transfer">
<input class="balance" style="width: 5ex" value="1"/>
<select class="denominations"></select>
<button id="deposit">Deposit</button>
<button id="withdraw">Withdraw</button>
</div>
<h4>Market</h4>
<div id="market">
<div id="markettype">
<input type="radio" id="stable" name="markettype" checked="checked"><label for="stable">Stable ($)</label>
<input type="radio" id="leveraged" name="markettype"><label for="leveraged">Leveraged (ether+)</label>
<span class="label" style="margin-left: 1em">Best:</span> <span id="best"></span><span class="label" style="margin-left: 1em">Price: </span>1 ETH = <span id="price"></span>
</div>
<div id="order" style="margin-top: 1em">
<input class="balance" value="1" id="stake"/>
<select class="denominations"></select>
<input class="adjustment" value="0" id="adjustment"/>% <span class="label">(market: <span id="marketadjustment"></span>)</span>
</div>
<div id="order-review">
Assuming an immediate match and same exit price, you would receive <span id="adjustedstake"></span> = <span id="dollarequivalent"></span>.
<div>
<button id="neworder">Place Order</button>
</div>
</div>
<script src="external/jquery/jquery.js"></script>
<script src="jquery-ui.js"></script>
<script src="web3.min.js"></script>
<script>!function(e){"use strict";function n(e){function E(e,n){var t,r,i,o,u,s,f=this;if(!(f instanceof E))return j&&L(26,"constructor call without new",e),new E(e,n);if(null!=n&&H(n,2,64,M,"base")){if(n=0|n,s=e+"",10==n)return f=new E(e instanceof E?e:s),U(f,P+f.e+1,k);if((o="number"==typeof e)&&0*e!=0||!new RegExp("^-?"+(t="["+N.slice(0,n)+"]+")+"(?:\\."+t+")?$",37>n?"i":"").test(s))return h(f,s,o,n);o?(f.s=0>1/e?(s=s.slice(1),-1):1,j&&s.replace(/^0\.0*|\./,"").length>15&&L(M,v,e),o=!1):f.s=45===s.charCodeAt(0)?(s=s.slice(1),-1):1,s=D(s,10,n,f.s)}else{if(e instanceof E)return f.s=e.s,f.e=e.e,f.c=(e=e.c)?e.slice():e,void(M=0);if((o="number"==typeof e)&&0*e==0){if(f.s=0>1/e?(e=-e,-1):1,e===~~e){for(r=0,i=e;i>=10;i/=10,r++);return f.e=r,f.c=[e],void(M=0)}s=e+""}else{if(!g.test(s=e+""))return h(f,s,o);f.s=45===s.charCodeAt(0)?(s=s.slice(1),-1):1}}for((r=s.indexOf("."))>-1&&(s=s.replace(".","")),(i=s.search(/e/i))>0?(0>r&&(r=i),r+=+s.slice(i+1),s=s.substring(0,i)):0>r&&(r=s.length),i=0;48===s.charCodeAt(i);i++);for(u=s.length;48===s.charCodeAt(--u););if(s=s.slice(i,u+1))if(u=s.length,o&&j&&u>15&&(e>y||e!==d(e))&&L(M,v,f.s*e),r=r-i-1,r>z)f.c=f.e=null;else if(G>r)f.c=[f.e=0];else{if(f.e=r,f.c=[],i=(r+1)%O,0>r&&(i+=O),u>i){for(i&&f.c.push(+s.slice(0,i)),u-=O;u>i;)f.c.push(+s.slice(i,i+=O));s=s.slice(i),i=O-s.length}else i-=u;for(;i--;s+="0");f.c.push(+s)}else f.c=[f.e=0];M=0}function D(e,n,t,i){var o,u,f,c,a,h,g,p=e.indexOf("."),d=P,m=k;for(37>t&&(e=e.toLowerCase()),p>=0&&(f=J,J=0,e=e.replace(".",""),g=new E(t),a=g.pow(e.length-p),J=f,g.c=s(l(r(a.c),a.e),10,n),g.e=g.c.length),h=s(e,t,n),u=f=h.length;0==h[--f];h.pop());if(!h[0])return"0";if(0>p?--u:(a.c=h,a.e=u,a.s=i,a=C(a,g,d,m,n),h=a.c,c=a.r,u=a.e),o=u+d+1,p=h[o],f=n/2,c=c||0>o||null!=h[o+1],c=4>m?(null!=p||c)&&(0==m||m==(a.s<0?3:2)):p>f||p==f&&(4==m||c||6==m&&1&h[o-1]||m==(a.s<0?8:7)),1>o||!h[0])e=c?l("1",-d):"0";else{if(h.length=o,c)for(--n;++h[--o]>n;)h[o]=0,o||(++u,h.unshift(1));for(f=h.length;!h[--f];);for(p=0,e="";f>=p;e+=N.charAt(h[p++]));e=l(e,u)}return e}function F(e,n,t,i){var o,u,s,c,a;if(t=null!=t&&H(t,0,8,i,w)?0|t:k,!e.c)return e.toString();if(o=e.c[0],s=e.e,null==n)a=r(e.c),a=19==i||24==i&&B>=s?f(a,s):l(a,s);else if(e=U(new E(e),n,t),u=e.e,a=r(e.c),c=a.length,19==i||24==i&&(u>=n||B>=u)){for(;n>c;a+="0",c++);a=f(a,u)}else if(n-=s,a=l(a,u),u+1>c){if(--n>0)for(a+=".";n--;a+="0");}else if(n+=u-c,n>0)for(u+1==c&&(a+=".");n--;a+="0");return e.s<0&&o?"-"+a:a}function _(e,n){var t,r,i=0;for(u(e[0])&&(e=e[0]),t=new E(e[0]);++i<e.length;){if(r=new E(e[i]),!r.s){t=r;break}n.call(t,r)&&(t=r)}return t}function x(e,n,t,r,i){return(n>e||e>t||e!=c(e))&&L(r,(i||"decimal places")+(n>e||e>t?" out of range":" not an integer"),e),!0}function I(e,n,t){for(var r=1,i=n.length;!n[--i];n.pop());for(i=n[0];i>=10;i/=10,r++);return(t=r+t*O-1)>z?e.c=e.e=null:G>t?e.c=[e.e=0]:(e.e=t,e.c=n),e}function L(e,n,t){var r=new Error(["new BigNumber","cmp","config","div","divToInt","eq","gt","gte","lt","lte","minus","mod","plus","precision","random","round","shift","times","toDigits","toExponential","toFixed","toFormat","toFraction","pow","toPrecision","toString","BigNumber"][e]+"() "+n+": "+t);throw r.name="BigNumber Error",M=0,r}function U(e,n,t,r){var i,o,u,s,f,l,c,a=e.c,h=S;if(a){e:{for(i=1,s=a[0];s>=10;s/=10,i++);if(o=n-i,0>o)o+=O,u=n,f=a[l=0],c=f/h[i-u-1]%10|0;else if(l=p((o+1)/O),l>=a.length){if(!r)break e;for(;a.length<=l;a.push(0));f=c=0,i=1,o%=O,u=o-O+1}else{for(f=s=a[l],i=1;s>=10;s/=10,i++);o%=O,u=o-O+i,c=0>u?0:f/h[i-u-1]%10|0}if(r=r||0>n||null!=a[l+1]||(0>u?f:f%h[i-u-1]),r=4>t?(c||r)&&(0==t||t==(e.s<0?3:2)):c>5||5==c&&(4==t||r||6==t&&(o>0?u>0?f/h[i-u]:0:a[l-1])%10&1||t==(e.s<0?8:7)),1>n||!a[0])return a.length=0,r?(n-=e.e+1,a[0]=h[(O-n%O)%O],e.e=-n||0):a[0]=e.e=0,e;if(0==o?(a.length=l,s=1,l--):(a.length=l+1,s=h[O-o],a[l]=u>0?d(f/h[i-u]%h[u])*s:0),r)for(;;){if(0==l){for(o=1,u=a[0];u>=10;u/=10,o++);for(u=a[0]+=s,s=1;u>=10;u/=10,s++);o!=s&&(e.e++,a[0]==b&&(a[0]=1));break}if(a[l]+=s,a[l]!=b)break;a[l--]=0,s=1}for(o=a.length;0===a[--o];a.pop());}e.e>z?e.c=e.e=null:e.e<G&&(e.c=[e.e=0])}return e}var C,M=0,T=E.prototype,q=new E(1),P=20,k=4,B=-7,$=21,G=-1e7,z=1e7,j=!0,H=x,V=!1,W=1,J=100,X={decimalSeparator:".",groupSeparator:",",groupSize:3,secondaryGroupSize:0,fractionGroupSeparator:" ",fractionGroupSize:0};return E.another=n,E.ROUND_UP=0,E.ROUND_DOWN=1,E.ROUND_CEIL=2,E.ROUND_FLOOR=3,E.ROUND_HALF_UP=4,E.ROUND_HALF_DOWN=5,E.ROUND_HALF_EVEN=6,E.ROUND_HALF_CEIL=7,E.ROUND_HALF_FLOOR=8,E.EUCLID=9,E.config=function(){var e,n,t=0,r={},i=arguments,s=i[0],f=s&&"object"==typeof s?function(){return s.hasOwnProperty(n)?null!=(e=s[n]):void 0}:function(){return i.length>t?null!=(e=i[t++]):void 0};return f(n="DECIMAL_PLACES")&&H(e,0,A,2,n)&&(P=0|e),r[n]=P,f(n="ROUNDING_MODE")&&H(e,0,8,2,n)&&(k=0|e),r[n]=k,f(n="EXPONENTIAL_AT")&&(u(e)?H(e[0],-A,0,2,n)&&H(e[1],0,A,2,n)&&(B=0|e[0],$=0|e[1]):H(e,-A,A,2,n)&&(B=-($=0|(0>e?-e:e)))),r[n]=[B,$],f(n="RANGE")&&(u(e)?H(e[0],-A,-1,2,n)&&H(e[1],1,A,2,n)&&(G=0|e[0],z=0|e[1]):H(e,-A,A,2,n)&&(0|e?G=-(z=0|(0>e?-e:e)):j&&L(2,n+" cannot be zero",e))),r[n]=[G,z],f(n="ERRORS")&&(e===!!e||1===e||0===e?(M=0,H=(j=!!e)?x:o):j&&L(2,n+m,e)),r[n]=j,f(n="CRYPTO")&&(e===!!e||1===e||0===e?(V=!(!e||!a),e&&!V&&j&&L(2,"crypto unavailable",a)):j&&L(2,n+m,e)),r[n]=V,f(n="MODULO_MODE")&&H(e,0,9,2,n)&&(W=0|e),r[n]=W,f(n="POW_PRECISION")&&H(e,0,A,2,n)&&(J=0|e),r[n]=J,f(n="FORMAT")&&("object"==typeof e?X=e:j&&L(2,n+" not an object",e)),r[n]=X,r},E.max=function(){return _(arguments,T.lt)},E.min=function(){return _(arguments,T.gt)},E.random=function(){var e=9007199254740992,n=Math.random()*e&2097151?function(){return d(Math.random()*e)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)};return function(e){var t,r,i,o,u,s=0,f=[],l=new E(q);if(e=null!=e&&H(e,0,A,14)?0|e:P,o=p(e/O),V)if(a&&a.getRandomValues){for(t=a.getRandomValues(new Uint32Array(o*=2));o>s;)u=131072*t[s]+(t[s+1]>>>11),u>=9e15?(r=a.getRandomValues(new Uint32Array(2)),t[s]=r[0],t[s+1]=r[1]):(f.push(u%1e14),s+=2);s=o/2}else if(a&&a.randomBytes){for(t=a.randomBytes(o*=7);o>s;)u=281474976710656*(31&t[s])+1099511627776*t[s+1]+4294967296*t[s+2]+16777216*t[s+3]+(t[s+4]<<16)+(t[s+5]<<8)+t[s+6],u>=9e15?a.randomBytes(7).copy(t,s):(f.push(u%1e14),s+=7);s=o/7}else j&&L(14,"crypto unavailable",a);if(!s)for(;o>s;)u=n(),9e15>u&&(f[s++]=u%1e14);for(o=f[--s],e%=O,o&&e&&(u=S[O-e],f[s]=d(o/u)*u);0===f[s];f.pop(),s--);if(0>s)f=[i=0];else{for(i=-1;0===f[0];f.shift(),i-=O);for(s=1,u=f[0];u>=10;u/=10,s++);O>s&&(i-=O-s)}return l.e=i,l.c=f,l}}(),C=function(){function e(e,n,t){var r,i,o,u,s=0,f=e.length,l=n%R,c=n/R|0;for(e=e.slice();f--;)o=e[f]%R,u=e[f]/R|0,r=c*o+u*l,i=l*o+r%R*R+s,s=(i/t|0)+(r/R|0)+c*u,e[f]=i%t;return s&&e.unshift(s),e}function n(e,n,t,r){var i,o;if(t!=r)o=t>r?1:-1;else for(i=o=0;t>i;i++)if(e[i]!=n[i]){o=e[i]>n[i]?1:-1;break}return o}function r(e,n,t,r){for(var i=0;t--;)e[t]-=i,i=e[t]<n[t]?1:0,e[t]=i*r+e[t]-n[t];for(;!e[0]&&e.length>1;e.shift());}return function(i,o,u,s,f){var l,c,a,h,g,p,m,w,v,N,y,S,R,A,D,F,_,x=i.s==o.s?1:-1,I=i.c,L=o.c;if(!(I&&I[0]&&L&&L[0]))return new E(i.s&&o.s&&(I?!L||I[0]!=L[0]:L)?I&&0==I[0]||!L?0*x:x/0:NaN);for(w=new E(x),v=w.c=[],c=i.e-o.e,x=u+c+1,f||(f=b,c=t(i.e/O)-t(o.e/O),x=x/O|0),a=0;L[a]==(I[a]||0);a++);if(L[a]>(I[a]||0)&&c--,0>x)v.push(1),h=!0;else{for(A=I.length,F=L.length,a=0,x+=2,g=d(f/(L[0]+1)),g>1&&(L=e(L,g,f),I=e(I,g,f),F=L.length,A=I.length),R=F,N=I.slice(0,F),y=N.length;F>y;N[y++]=0);_=L.slice(),_.unshift(0),D=L[0],L[1]>=f/2&&D++;do{if(g=0,l=n(L,N,F,y),0>l){if(S=N[0],F!=y&&(S=S*f+(N[1]||0)),g=d(S/D),g>1)for(g>=f&&(g=f-1),p=e(L,g,f),m=p.length,y=N.length;1==n(p,N,m,y);)g--,r(p,m>F?_:L,m,f),m=p.length,l=1;else 0==g&&(l=g=1),p=L.slice(),m=p.length;if(y>m&&p.unshift(0),r(N,p,y,f),y=N.length,-1==l)for(;n(L,N,F,y)<1;)g++,r(N,y>F?_:L,y,f),y=N.length}else 0===l&&(g++,N=[0]);v[a++]=g,N[0]?N[y++]=I[R]||0:(N=[I[R]],y=1)}while((R++<A||null!=N[0])&&x--);h=null!=N[0],v[0]||v.shift()}if(f==b){for(a=1,x=v[0];x>=10;x/=10,a++);U(w,u+(w.e=a+c*O-1)+1,s,h)}else w.e=c,w.r=+h;return w}}(),h=function(){var e=/^(-?)0([xbo])(?=\w[\w.]*$)/i,n=/^([^.]+)\.$/,t=/^\.([^.]+)$/,r=/^-?(Infinity|NaN)$/,i=/^\s*\+(?=[\w.])|^\s+|\s+$/g;return function(o,u,s,f){var l,c=s?u:u.replace(i,"");if(r.test(c))o.s=isNaN(c)?null:0>c?-1:1;else{if(!s&&(c=c.replace(e,function(e,n,t){return l="x"==(t=t.toLowerCase())?16:"b"==t?2:8,f&&f!=l?e:n}),f&&(l=f,c=c.replace(n,"$1").replace(t,"0.$1")),u!=c))return new E(c,l);j&&L(M,"not a"+(f?" base "+f:"")+" number",u),o.s=null}o.c=o.e=null,M=0}}(),T.absoluteValue=T.abs=function(){var e=new E(this);return e.s<0&&(e.s=1),e},T.ceil=function(){return U(new E(this),this.e+1,2)},T.comparedTo=T.cmp=function(e,n){return M=1,i(this,new E(e,n))},T.decimalPlaces=T.dp=function(){var e,n,r=this.c;if(!r)return null;if(e=((n=r.length-1)-t(this.e/O))*O,n=r[n])for(;n%10==0;n/=10,e--);return 0>e&&(e=0),e},T.dividedBy=T.div=function(e,n){return M=3,C(this,new E(e,n),P,k)},T.dividedToIntegerBy=T.divToInt=function(e,n){return M=4,C(this,new E(e,n),0,1)},T.equals=T.eq=function(e,n){return M=5,0===i(this,new E(e,n))},T.floor=function(){return U(new E(this),this.e+1,3)},T.greaterThan=T.gt=function(e,n){return M=6,i(this,new E(e,n))>0},T.greaterThanOrEqualTo=T.gte=function(e,n){return M=7,1===(n=i(this,new E(e,n)))||0===n},T.isFinite=function(){return!!this.c},T.isInteger=T.isInt=function(){return!!this.c&&t(this.e/O)>this.c.length-2},T.isNaN=function(){return!this.s},T.isNegative=T.isNeg=function(){return this.s<0},T.isZero=function(){return!!this.c&&0==this.c[0]},T.lessThan=T.lt=function(e,n){return M=8,i(this,new E(e,n))<0},T.lessThanOrEqualTo=T.lte=function(e,n){return M=9,-1===(n=i(this,new E(e,n)))||0===n},T.minus=T.sub=function(e,n){var r,i,o,u,s=this,f=s.s;if(M=10,e=new E(e,n),n=e.s,!f||!n)return new E(NaN);if(f!=n)return e.s=-n,s.plus(e);var l=s.e/O,c=e.e/O,a=s.c,h=e.c;if(!l||!c){if(!a||!h)return a?(e.s=-n,e):new E(h?s:NaN);if(!a[0]||!h[0])return h[0]?(e.s=-n,e):new E(a[0]?s:3==k?-0:0)}if(l=t(l),c=t(c),a=a.slice(),f=l-c){for((u=0>f)?(f=-f,o=a):(c=l,o=h),o.reverse(),n=f;n--;o.push(0));o.reverse()}else for(i=(u=(f=a.length)<(n=h.length))?f:n,f=n=0;i>n;n++)if(a[n]!=h[n]){u=a[n]<h[n];break}if(u&&(o=a,a=h,h=o,e.s=-e.s),n=(i=h.length)-(r=a.length),n>0)for(;n--;a[r++]=0);for(n=b-1;i>f;){if(a[--i]<h[i]){for(r=i;r&&!a[--r];a[r]=n);--a[r],a[i]+=b}a[i]-=h[i]}for(;0==a[0];a.shift(),--c);return a[0]?I(e,a,c):(e.s=3==k?-1:1,e.c=[e.e=0],e)},T.modulo=T.mod=function(e,n){var t,r,i=this;return M=11,e=new E(e,n),!i.c||!e.s||e.c&&!e.c[0]?new E(NaN):!e.c||i.c&&!i.c[0]?new E(i):(9==W?(r=e.s,e.s=1,t=C(i,e,0,3),e.s=r,t.s*=r):t=C(i,e,0,W),i.minus(t.times(e)))},T.negated=T.neg=function(){var e=new E(this);return e.s=-e.s||null,e},T.plus=T.add=function(e,n){var r,i=this,o=i.s;if(M=12,e=new E(e,n),n=e.s,!o||!n)return new E(NaN);if(o!=n)return e.s=-n,i.minus(e);var u=i.e/O,s=e.e/O,f=i.c,l=e.c;if(!u||!s){if(!f||!l)return new E(o/0);if(!f[0]||!l[0])return l[0]?e:new E(f[0]?i:0*o)}if(u=t(u),s=t(s),f=f.slice(),o=u-s){for(o>0?(s=u,r=l):(o=-o,r=f),r.reverse();o--;r.push(0));r.reverse()}for(o=f.length,n=l.length,0>o-n&&(r=l,l=f,f=r,n=o),o=0;n;)o=(f[--n]=f[n]+l[n]+o)/b|0,f[n]%=b;return o&&(f.unshift(o),++s),I(e,f,s)},T.precision=T.sd=function(e){var n,t,r=this,i=r.c;if(null!=e&&e!==!!e&&1!==e&&0!==e&&(j&&L(13,"argument"+m,e),e!=!!e&&(e=null)),!i)return null;if(t=i.length-1,n=t*O+1,t=i[t]){for(;t%10==0;t/=10,n--);for(t=i[0];t>=10;t/=10,n++);}return e&&r.e+1>n&&(n=r.e+1),n},T.round=function(e,n){var t=new E(this);return(null==e||H(e,0,A,15))&&U(t,~~e+this.e+1,null!=n&&H(n,0,8,15,w)?0|n:k),t},T.shift=function(e){var n=this;return H(e,-y,y,16,"argument")?n.times("1e"+c(e)):new E(n.c&&n.c[0]&&(-y>e||e>y)?n.s*(0>e?0:1/0):n)},T.squareRoot=T.sqrt=function(){var e,n,i,o,u,s=this,f=s.c,l=s.s,c=s.e,a=P+4,h=new E("0.5");if(1!==l||!f||!f[0])return new E(!l||0>l&&(!f||f[0])?NaN:f?s:1/0);if(l=Math.sqrt(+s),0==l||l==1/0?(n=r(f),(n.length+c)%2==0&&(n+="0"),l=Math.sqrt(n),c=t((c+1)/2)-(0>c||c%2),l==1/0?n="1e"+c:(n=l.toExponential(),n=n.slice(0,n.indexOf("e")+1)+c),i=new E(n)):i=new E(l+""),i.c[0])for(c=i.e,l=c+a,3>l&&(l=0);;)if(u=i,i=h.times(u.plus(C(s,u,a,1))),r(u.c).slice(0,l)===(n=r(i.c)).slice(0,l)){if(i.e<c&&--l,n=n.slice(l-3,l+1),"9999"!=n&&(o||"4999"!=n)){(!+n||!+n.slice(1)&&"5"==n.charAt(0))&&(U(i,i.e+P+2,1),e=!i.times(i).eq(s));break}if(!o&&(U(u,u.e+P+2,0),u.times(u).eq(s))){i=u;break}a+=4,l+=4,o=1}return U(i,i.e+P+1,k,e)},T.times=T.mul=function(e,n){var r,i,o,u,s,f,l,c,a,h,g,p,d,m,w,v=this,N=v.c,y=(M=17,e=new E(e,n)).c;if(!(N&&y&&N[0]&&y[0]))return!v.s||!e.s||N&&!N[0]&&!y||y&&!y[0]&&!N?e.c=e.e=e.s=null:(e.s*=v.s,N&&y?(e.c=[0],e.e=0):e.c=e.e=null),e;for(i=t(v.e/O)+t(e.e/O),e.s*=v.s,l=N.length,h=y.length,h>l&&(d=N,N=y,y=d,o=l,l=h,h=o),o=l+h,d=[];o--;d.push(0));for(m=b,w=R,o=h;--o>=0;){for(r=0,g=y[o]%w,p=y[o]/w|0,s=l,u=o+s;u>o;)c=N[--s]%w,a=N[s]/w|0,f=p*c+a*g,c=g*c+f%w*w+d[u]+r,r=(c/m|0)+(f/w|0)+p*a,d[u--]=c%m;d[u]=r}return r?++i:d.shift(),I(e,d,i)},T.toDigits=function(e,n){var t=new E(this);return e=null!=e&&H(e,1,A,18,"precision")?0|e:null,n=null!=n&&H(n,0,8,18,w)?0|n:k,e?U(t,e,n):t},T.toExponential=function(e,n){return F(this,null!=e&&H(e,0,A,19)?~~e+1:null,n,19)},T.toFixed=function(e,n){return F(this,null!=e&&H(e,0,A,20)?~~e+this.e+1:null,n,20)},T.toFormat=function(e,n){var t=F(this,null!=e&&H(e,0,A,21)?~~e+this.e+1:null,n,21);if(this.c){var r,i=t.split("."),o=+X.groupSize,u=+X.secondaryGroupSize,s=X.groupSeparator,f=i[0],l=i[1],c=this.s<0,a=c?f.slice(1):f,h=a.length;if(u&&(r=o,o=u,u=r,h-=r),o>0&&h>0){for(r=h%o||o,f=a.substr(0,r);h>r;r+=o)f+=s+a.substr(r,o);u>0&&(f+=s+a.slice(r)),c&&(f="-"+f)}t=l?f+X.decimalSeparator+((u=+X.fractionGroupSize)?l.replace(new RegExp("\\d{"+u+"}\\B","g"),"$&"+X.fractionGroupSeparator):l):f}return t},T.toFraction=function(e){var n,t,i,o,u,s,f,l,c,a=j,h=this,g=h.c,p=new E(q),d=t=new E(q),m=f=new E(q);if(null!=e&&(j=!1,s=new E(e),j=a,(!(a=s.isInt())||s.lt(q))&&(j&&L(22,"max denominator "+(a?"out of range":"not an integer"),e),e=!a&&s.c&&U(s,s.e+1,1).gte(q)?s:null)),!g)return h.toString();for(c=r(g),o=p.e=c.length-h.e-1,p.c[0]=S[(u=o%O)<0?O+u:u],e=!e||s.cmp(p)>0?o>0?p:d:s,u=z,z=1/0,s=new E(c),f.c[0]=0;l=C(s,p,0,1),i=t.plus(l.times(m)),1!=i.cmp(e);)t=m,m=i,d=f.plus(l.times(i=d)),f=i,p=s.minus(l.times(i=p)),s=i;return i=C(e.minus(t),m,0,1),f=f.plus(i.times(d)),t=t.plus(i.times(m)),f.s=d.s=h.s,o*=2,n=C(d,m,o,k).minus(h).abs().cmp(C(f,t,o,k).minus(h).abs())<1?[d.toString(),m.toString()]:[f.toString(),t.toString()],z=u,n},T.toNumber=function(){return+this},T.toPower=T.pow=function(e,n){var t,r,i,o=d(0>e?-e:+e),u=this;if(null!=n&&(M=23,n=new E(n)),!H(e,-y,y,23,"exponent")&&(!isFinite(e)||o>y&&(e/=0)||parseFloat(e)!=e&&!(e=NaN))||0==e)return t=Math.pow(+u,e),new E(n?t%n:t);for(n?e>1&&u.gt(q)&&u.isInt()&&n.gt(q)&&n.isInt()?u=u.mod(n):(i=n,n=null):J&&(t=p(J/O+2)),r=new E(q);;){if(o%2){if(r=r.times(u),!r.c)break;t?r.c.length>t&&(r.c.length=t):n&&(r=r.mod(n))}if(o=d(o/2),!o)break;u=u.times(u),t?u.c&&u.c.length>t&&(u.c.length=t):n&&(u=u.mod(n))}return n?r:(0>e&&(r=q.div(r)),i?r.mod(i):t?U(r,J,k):r)},T.toPrecision=function(e,n){return F(this,null!=e&&H(e,1,A,24,"precision")?0|e:null,n,24)},T.toString=function(e){var n,t=this,i=t.s,o=t.e;return null===o?i?(n="Infinity",0>i&&(n="-"+n)):n="NaN":(n=r(t.c),n=null!=e&&H(e,2,64,25,"base")?D(l(n,o),0|e,10,i):B>=o||o>=$?f(n,o):l(n,o),0>i&&t.c[0]&&(n="-"+n)),n},T.truncated=T.trunc=function(){return U(new E(this),this.e+1,1)},T.valueOf=T.toJSON=function(){var e,n=this,t=n.e;return null===t?n.toString():(e=r(n.c),e=B>=t||t>=$?f(e,t):l(e,t),n.s<0?"-"+e:e)},null!=e&&E.config(e),E}function t(e){var n=0|e;return e>0||e===n?n:n-1}function r(e){for(var n,t,r=1,i=e.length,o=e[0]+"";i>r;){for(n=e[r++]+"",t=O-n.length;t--;n="0"+n);o+=n}for(i=o.length;48===o.charCodeAt(--i););return o.slice(0,i+1||1)}function i(e,n){var t,r,i=e.c,o=n.c,u=e.s,s=n.s,f=e.e,l=n.e;if(!u||!s)return null;if(t=i&&!i[0],r=o&&!o[0],t||r)return t?r?0:-s:u;if(u!=s)return u;if(t=0>u,r=f==l,!i||!o)return r?0:!i^t?1:-1;if(!r)return f>l^t?1:-1;for(s=(f=i.length)<(l=o.length)?f:l,u=0;s>u;u++)if(i[u]!=o[u])return i[u]>o[u]^t?1:-1;return f==l?0:f>l^t?1:-1}function o(e,n,t){return(e=c(e))>=n&&t>=e}function u(e){return"[object Array]"==Object.prototype.toString.call(e)}function s(e,n,t){for(var r,i,o=[0],u=0,s=e.length;s>u;){for(i=o.length;i--;o[i]*=n);for(o[r=0]+=N.indexOf(e.charAt(u++));r<o.length;r++)o[r]>t-1&&(null==o[r+1]&&(o[r+1]=0),o[r+1]+=o[r]/t|0,o[r]%=t)}return o.reverse()}function f(e,n){return(e.length>1?e.charAt(0)+"."+e.slice(1):e)+(0>n?"e":"e+")+n}function l(e,n){var t,r;if(0>n){for(r="0.";++n;r+="0");e=r+e}else if(t=e.length,++n>t){for(r="0",n-=t;--n;r+="0");e+=r}else t>n&&(e=e.slice(0,n)+"."+e.slice(n));return e}function c(e){return e=parseFloat(e),0>e?p(e):d(e)}var a,h,g=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,p=Math.ceil,d=Math.floor,m=" not a boolean or binary digit",w="rounding mode",v="number type has more than 15 significant digits",N="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_",b=1e14,O=14,y=9007199254740991,S=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],R=1e7,A=1e9;if("undefined"!=typeof crypto&&(a=crypto),"function"==typeof define&&define.amd)define(function(){return n()});else if("undefined"!=typeof module&&module.exports){if(module.exports=n(),!a)try{a=require("crypto")}catch(E){}}else e||(e="undefined"!=typeof self?self:Function("return this")()),e.BigNumber=n()}(this);
</script>
<script>
if (typeof web3 === 'undefined') {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.eth.defaultAccount = web3.eth.accounts[0];
}
var theCFD;
if (typeof contracts === 'undefined') {
var cfdContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"finalize","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"value","type":"uint256"}],"name":"withdraw","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"cancel","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_is_stable","type":"bool"},{"name":"_stake","type":"uint256"}],"name":"best_adjustment_for","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[],"name":"oracle","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"is_stable","type":"bool"},{"name":"adjustment","type":"uint256"},{"name":"stake","type":"uint256"}],"name":"order","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"_who","type":"address"}],"name":"balance_of","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"_id","type":"uint256"}],"name":"deal_details","outputs":[{"name":"stable","type":"address"},{"name":"leveraged","type":"address"},{"name":"strike","type":"uint256"},{"name":"stake","type":"uint256"},{"name":"end_time","type":"uint256"}],"type":"function"},{"constant":true,"inputs":[{"name":"_is_stable","type":"bool"}],"name":"best_adjustment","outputs":[{"name":"","type":"uint256"}],"type":"function"},{"inputs":[{"name":"_oracle","type":"address"},{"name":"_period","type":"uint256"}],"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"who","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Withdraw","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":true,"name":"who","type":"address"},{"indexed":true,"name":"is_stable","type":"bool"},{"indexed":false,"name":"adjustment","type":"uint256"},{"indexed":false,"name":"stake","type":"uint256"}],"name":"OrderPlaced","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":true,"name":"stable","type":"address"},{"indexed":true,"name":"leveraged","type":"address"},{"indexed":false,"name":"deal","type":"uint256"},{"indexed":false,"name":"strike","type":"uint256"},{"indexed":false,"name":"stake","type":"uint256"}],"name":"OrderMatched","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":true,"name":"who","type":"address"},{"indexed":false,"name":"stake","type":"uint256"}],"name":"OrderCancelled","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":true,"name":"stable","type":"address"},{"indexed":true,"name":"leveraged","type":"address"},{"indexed":false,"name":"price","type":"uint256"}],"name":"DealFinalized","type":"event"}]);
theCFD = cfdContract.at("0xb0f2fee53c6779896a148b0132a565f51d47b8d5");
}
else
theCFD = contracts['CFD'].contract;
var oracleContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"get_timestamp","outputs":[{"name":"","type":"uint32"}],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint224"}],"type":"function"},{"constant":true,"inputs":[],"name":"data","outputs":[{"name":"timestamp","type":"uint32"},{"name":"value","type":"uint224"}],"type":"function"},{"constant":false,"inputs":[{"name":"_new","type":"address"}],"name":"set_owner","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint224"}],"name":"note","outputs":[],"type":"function"},{"inputs":[],"type":"constructor"}]);
var theOracle = oracleContract.at(theCFD.oracle());
var denominations = [ "wei", "Kwei", "Mwei", "Gwei", "szabo", "finney", "ether", "grand", "Mether", "Gether", "Tether", "Pether", "Eether", "Zether", "Yether", "Nether", "Dether", "Vether", "Uether" ];
function niceAccount(a) {
var a = a.substr(0, 8) + "..." + a.substr(36);
return '<span class="_account">' + a + '</span>';
}
function niceBalance(a) {
var i = 0;
while (a >= 1000 && i < denominations.length - 1) {
a /= 1000;
i++;
}
var a = Math.round(a * 1000) / 1000;
return '<span class="_balance _' + denominations[i] + '">' + a + '<span class="_denom">' + denominations[i] + '</span></span>';
}
function niceAdjustment(a) {
var adj = a.div("10000000").sub("100");
return '<span class="_adjustment"><span class="_adjustmentamount ' + (adj > 0 ? '_positive' : adj < 0 ? '_negative' : '_zero') + '">' + (adj > 0 ? '+' : '') + adj + '</span>%</span>';
}
function niceUSD(a) {
var adj = a.div("1000000000") + '';
if (adj.indexOf('.') == -1)
adj += ".00";
adj = adj.replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
return '<span class="_usd">$<span class="_usdamount">' + adj + '</span></span>';
}
function initAccounts() {
var items = [];
for (i in web3.eth.accounts) {
var a = web3.eth.accounts[i];
var usage = '<div id="account' + a + '"><div id="wallet">Wallet: ' + niceBalance(web3.eth.getBalance(a)) + '</div><div id="hot">Hot: ' + niceBalance(theCFD.balance_of(a)) + '</div></div>';
items.push('<h3>' + a + '</h3>' + usage);
}
$('#accounts').append(items.join(''));
$('#accounts').accordion('refresh');
var activeIndex = web3.eth.accounts.indexOf(web3.eth.defaultAccount);
$('#accounts').accordion('option', 'active', +activeIndex)
$('#accounts').on("accordionactivate", function( event, ui ) { web3.eth.defaultAccount = web3.eth.accounts[$('#accounts').accordion('option', 'active')]; });
}
function refreshAccounts() {
for (i in web3.eth.accounts) {
var a = web3.eth.accounts[i];
$('div#account' + a + " #wallet").html('Wallet: ' + niceBalance(web3.eth.getBalance(a)));
$('div#account' + a + " #hot").html('Hot: ' + niceBalance(theCFD.balance_of(a)));
}
}
function refreshMarket() {
var adj = theCFD.best_adjustment($('#stable').is(':checked'));
if (adj != 0)
$('#best').html(niceAdjustment(adj));
else
$('#best').html('<span style="color: #800">orderbook empty</span>');
$('#price').html(niceUSD(theOracle.get()));
refreshOrder();
}
function orderAdjustment() {
return new BigNumber(Math.round($('#order #adjustment').val() * 10000000)).add('1000000000');
}
function refreshOrder() {
var price = theOracle.get(); // billionths of $.
var adjust = orderAdjustment(); // billionths
var stake = amount('#order'); // wei
var isStable = $('#stable').is(':checked');
var needAdjust = theCFD.best_adjustment_for(!isStable, stake);
var stakevalue;
if (isStable)
stakevalue = stake.mul(adjust).div(1000000000);
else
stakevalue = stake.mul(2) - stake.mul(adjust).div(1000000000);
$('#adjustedstake').html(niceBalance(stakevalue));
$('#dollarequivalent').html(niceUSD(price.mul(stakevalue).div(1000000000000000000)));
if (needAdjust != 0)
$('#marketadjustment').html(niceAdjustment(needAdjust));
else
$('#marketadjustment').html('<span style="color: #800">insufficient liquidity</span>');
}
function calculateReturn(price, deal) {
var stableGets = deal.strike.mul(deal.stake).div(price);
var leveragedGets = deal.stake.mul(2).sub(stableGets);
return [stableGets, leveragedGets];
}
function refreshLog() {
/* event Deposit(address indexed who, uint value);
event Withdraw(address indexed who, uint value);
event OrderPlaced(uint indexed id, address indexed who, bool indexed is_stable, uint adjustment, uint stake);
event OrderMatched(uint indexed id, address indexed stable, address indexed leveraged, uint deal, uint strike, uint stake);
event OrderCancelled(uint indexed id, address indexed who, uint stake);
event DealFinalized(uint indexed id, address indexed stable, address indexed leveraged, uint price);
*/
var currentPrice = theOracle.get();
theCFD.allEvents({fromBlock: 0, toBlock: 'pending'}).get(function(error, logs) {
logs.sort(function(a, b){ return a.blockNumber < b.blockNumber || (a.blockNumber == b.blockNumber && a.transactionIndex < b.transactionIndex) ? -1 : 1; });
var orders = {};
var deals = {};
var funding = [];
var accounts = web3.eth.accounts.reduce(function(x, y){x[y] = true; return x}, {});
for (i in logs) {
var l = logs[i];
if (l.event == 'Deposit' && accounts[l.args.who]) {
funding.push({event: 'deposit', when: l.blockNumber, who: l.args.who, value: l.args.value})
}
else if (l.event == 'Withdraw' && accounts[l.args.who]) {
funding.push({event: 'withdraw', when: l.blockNumber, who: l.args.who, value: l.args.value})
}
else if (l.event == 'OrderPlaced' && accounts[l.args.who]) {
funding.push({event: 'order', when: l.blockNumber, who: l.args.who, value: l.args.stake})
orders[l.args.id] = l.args;
}
else if (l.event == 'OrderMatched') {
var a = l.args.is_stable ? l.args.stable : l.args.leveraged;
if (accounts[a]) {
funding.push({event: 'match', when: l.blockNumber, who: a, value: l.args.stake})
}
}
}
for (i in logs) {
var l = logs[i];
if (l.event == 'OrderMatched') {
if (orders[l.args.id].stake) {
orders[l.args.id].stake = orders[l.args.id].stake.sub(l.args.stake);
if (orders[l.args.id].stake == 0)
delete orders[l.args.id];
}
if (accounts[l.args.stable] || accounts[l.args.leveraged]) {
deals[l.args.deal] = l.args;
deals[l.args.deal].endOfTerm = new Date(((l.blockHash ? web3.eth.getBlock(l.blockHash).timestamp : (Date.now() / 1000 | 0)) + +theCFD.period()) * 1000);
}
}
if (l.event == 'OrderCancelled' && accounts[l.args.who]) {
funding.push({event: 'cancel', when: l.blockNumber, who: l.args.who, value: l.args.stake})
delete orders[l.args.id];
}
}
for (i in logs) {
var l = logs[i];
if (l.event == 'DealFinalized') {
deals[l.args.id].price = l.args.price;
if (accounts[l.args.stable])
funding.push({event: 'finalize', when: l.blockNumber, who: l.args.stable, value: calculateReturn(l.args.price, deals[l.args.id])[0]})
if (accounts[l.args.leveraged])
funding.push({event: 'finalize', when: l.blockNumber, who: l.args.leveraged, value: calculateReturn(l.args.price, deals[l.args.id])[1]})
}
}
var items = funding.reduce(function(items, f) {
items.push('<li class="funding _' + f.event + '"><span class="event" style="float: left;">' + f.event + '</span>\
<span class="who" style="float: right; width: 20ex">' + niceAccount(f.who) + '</span>\
<span class="value">' + niceBalance(f.value) + '</span>\
</li>');
return items;
}, []);
$('#fundinglog').html(items.join(''));
items = [];
var os = [];
for (var k in orders)
os.push(orders[k]);
for (id in os.sort(function(a, b){ return a.adjustment - b.adjustment; })) {
var o = os[id];
var key = (o.is_stable ? "stable" : "leveraged");
items.push('<li class="order _' + key + '">\
<span class="event _' + key + '">' + key + '</span>\
<span class="who" style="float: right; width: 20ex">' + niceAccount(o.who) + '</span>\
<span class="stake">' + niceBalance(o.stake) + '</span>\
@<span class="adjustment">' + niceAdjustment(o.adjustment) + '</span>\
<button onclick="cancelOrder(\'' + o.who + '\', ' + id + ')">cancel</button>\
</li>');
}
$('#allorders').html(items.join(''));
items = [];
for (id in deals) {
var d = deals[id];
var weStable = accounts[d.stable];
var us = weStable ? d.stable : d.leveraged;
var them = weStable ? d.leveraged : d.stable;
var ret = calculateReturn(d.price || currentPrice, d);
var weGet = ret[weStable ? 0 : 1];
var canFinalize = !d.price && (web3.eth.getBlock('latest').timestamp * 1000 >= +d.endOfTerm || ret[0].gt(d.stake.mul(2)));
var key = (weStable ? "stable" : "leveraged");
items.push('<li class="deal _' + key + '"><div>\
<span class="event">' + key + '</span>\
<span class="who" style="float: right; width: 20ex">' + niceAccount(us) + '</span>\
<span class="stake">' + niceBalance(d.stake) + '</span> (now\
<span class="out">' + niceBalance(weGet) + '</span>,\
strike <span class="strike">' + niceUSD(d.strike) + '</span>)\
<button ' + (canFinalize ? '' : 'disabled="disabled" ') + 'onclick="finalizeDeal(' + d.deal + ')">finalize</button>\
</div>\
<div>'+(d.price ?
'<span class="endtime">Deal finalized.</span>' :
'<span class="endtime">Term ends: ' + (d.price ? "ended" : d.endOfTerm) + '</span>') + '\
<span class="them" style="float: right; width: 20ex">' + niceAccount(them) + '</span>\
</div></li>');
}
$('#alldeals').html(items.join(''));
})
}
function cancelOrder(who, id) {
console.log("cancelling " + id + " from " + who);
theCFD.cancel(id, {from: who, gas: 1000000});
refreshLog();
refreshAccounts();
}
function finalizeDeal(id) {
console.log("finalizing " + id);
theCFD.finalize(id, {gas: 1000000});
refreshLog();
refreshAccounts();
}
function refresh() {
refreshAccounts();
refreshMarket();
refreshOrder();
refreshLog();
}
function initDenominations() {
var items = denominations.map(function(d, i) { return '<option value="' + i + '">' + d + '</option>'; });
$('.denominations').append(items.join(''));
$('.denominations').val("6");
$('.denominations').selectmenu("refresh");
}
function initUI() {
$('.accordion').accordion();
$('.balance').spinner({min: 1, max: 10000, step: 1, start: 1, numberFormat: 'C'});
$('.adjustment').spinner({min: -100, max: 100, step: 0.000001, page: 0.1, start: 0, numberFormat: 'n'})
$('button').button();
$('select').selectmenu();
$('#markettype').buttonset().click(function() {
refreshMarket();
});
$('#deposit').click(function() {
var a = amount('#transfer');
web3.eth.sendTransaction({"to": theCFD.address, "gas": 1000000, "value": a});
refreshAccounts();
refreshLog();
});
$('#withdraw').click(function() {
var a = amount('#transfer');
theCFD.withdraw(a, {"gas": 1000000})
refreshAccounts();
refreshLog();
});
$('#neworder').click(function() {
theCFD.order($('#stable').is(':checked'), orderAdjustment(), amount('#order'), {"gas": 2000000});
refreshMarket();
refreshLog();
});
$('#adjustment, #stake').on("spinstop", function(){refreshOrder()});
$('#order .denominations').on("selectmenuselect", function(){refreshOrder()});
}
function amount(x) {
return (new BigNumber($(x + " .balance").val())).mul((new BigNumber(10)).toPower($(x + " .denominations").val() * 3));
}
function initEvents() {
}
function init() {
initUI();
initDenominations();
initEvents();
initAccounts();
refresh();
web3.eth.filter('latest', function(error, result) {
refresh();
});
}
$(document).ready(init);
</script>
</body>
</html>