import React, { Component } from 'react'; import { Checkbox, Slider, Switch, Tooltip, Paper, Button , Grid, Typography, TextField, Select, FormHelperText, MenuItem, FormControl, Radio, FormControlLabel, RadioGroup} from "@mui/material" import { LocalizationProvider, TimePicker} from '@mui/lab'; import DateFnsUtils from "@date-io/date-fns"; import { Link } from 'react-router-dom' import getFlags from './getFlags' import LockIcon from '@mui/icons-material/Lock'; import SelfImprovementIcon from '@mui/icons-material/SelfImprovement'; function getCookie(name) { let cookieValue = null; if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } const csrftoken = getCookie('csrftoken'); // pretty numbers function pn(x) { if(x==null){ return(null) } var parts = x.toString().split("."); parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); return parts.join("."); } export default class MakerPage extends Component { defaultCurrency = 1; defaultCurrencyCode = 'USD'; defaultPaymentMethod = "not specified"; defaultPremium = 0; minTradeSats = 20000; maxTradeSats = 800000; maxBondlessSats = 50000; minAmountFraction = 0.2; constructor(props) { super(props); this.state={ is_explicit: false, type: 0, currency: this.defaultCurrency, currencyCode: this.defaultCurrencyCode, payment_method: this.defaultPaymentMethod, premium: 0, satoshis: null, currencies_dict: {"1":"USD"}, showAdvanced: false, allowBondless: false, publicExpiryTime: new Date(0, 0, 0, 23, 59), publicDuration: 23*60*60 + 59*60, enableAmountRange: false, minAmount: null, bondSize: 1, } this.getCurrencyDict() } handleTypeChange=(e)=>{ this.setState({ type: e.target.value, }); } handleCurrencyChange=(e)=>{ this.setState({ currency: e.target.value, currencyCode: this.getCurrencyCode(e.target.value), }); } handleAmountChange=(e)=>{ this.setState({ amount: e.target.value, }); } handleMinAmountChange=(e)=>{ this.setState({ minAmount: e.target.value, }); } handlePaymentMethodChange=(e)=>{ this.setState({ payment_method: e.target.value, badPaymentMethod: e.target.value.length > 35, }); } handlePremiumChange=(e)=>{ if(e.target.value > 999){ var bad_premium = "Must be less than 999%" } if(e.target.value < -100){ var bad_premium = "Must be more than -100%" } this.setState({ premium: e.target.value, badPremium: bad_premium, }); } handleSatoshisChange=(e)=>{ if(e.target.value > this.maxTradeSats){ var bad_sats = "Must be less than " + pn(this.maxTradeSats) } if(e.target.value < this.minTradeSats){ var bad_sats = "Must be more than "+pn(this.minTradeSats) } this.setState({ satoshis: e.target.value, badSatoshis: bad_sats, }); } handleClickRelative=(e)=>{ this.setState({ is_explicit: false, }); this.handlePremiumChange(); } handleClickExplicit=(e)=>{ this.setState({ is_explicit: true, }); this.handleSatoshisChange(); } handleCreateOfferButtonPressed=()=>{ this.state.amount == null ? this.setState({amount: 0}) : null; const requestOptions = { method: 'POST', headers: {'Content-Type':'application/json', 'X-CSRFToken': getCookie('csrftoken')}, body: JSON.stringify({ type: this.state.type, currency: this.state.currency, amount: this.state.amount, payment_method: this.state.payment_method, is_explicit: this.state.is_explicit, premium: this.state.is_explicit ? null: this.state.premium, satoshis: this.state.is_explicit ? this.state.satoshis: null, public_duration: this.state.publicDuration, }), }; fetch("/api/make/",requestOptions) .then((response) => response.json()) .then((data) => (this.setState({badRequest:data.bad_request}) & (data.id ? this.props.history.push('/order/' + data.id) :""))); } getCurrencyDict() { fetch('/static/assets/currencies.json') .then((response) => response.json()) .then((data) => this.setState({ currencies_dict: data })); } getCurrencyCode(val){ return this.state.currencies_dict[val.toString()] } handleBlur = () => { if (this.state.bondSize < 0) { this.setState({bondSize:0}); } else if (this.state.bondSize > 100) { this.setState({bondSize:20}); } }; handleSliderBondSizeChange = (event, newValue) => { this.setState({bondSize: newValue}); }; handleInputBondSizeChange = (event) => { this.setState({bondSize: event.target.value === '' ? 1 : Number(event.target.value)}); }; StandardMakerOptions = () => { return( Buy or Sell Bitcoin? } label="Buy" labelPlacement="Top" /> } label="Sell" labelPlacement="Top" />

Choose a Pricing Method
} label="Relative" labelPlacement="Top" onClick={this.handleClickRelative} /> } label="Explicit" labelPlacement="Top" onClick={this.handleClickExplicit} />
{/* conditional shows either Premium % field or Satoshis field based on pricing method */}
) } handleChangePublicDuration = (date) => { console.log(date) let d = new Date(date), hours = d.getHours(), minutes = d.getMinutes(); var total_secs = hours*60*60 + minutes * 60; this.setState({ changedPublicExpiryTime: true, publicExpiryTime: date, publicDuration: total_secs, badDuration: false, }); } AdvancedMakerOptions = () => { return(
} label="Public Duration (HH:mm)" value={this.state.publicExpiryTime} onChange={this.handleChangePublicDuration} minTime={new Date(0, 0, 0, 0, 10)} maxTime={new Date(0, 0, 0, 23, 59)} />
Amount Range
this.setState({enableAmountRange:!this.state.enableAmountRange})}/> (x+" "+this.state.currencyCode)} marks={this.state.amount == null ? null : [{value: this.state.amount*this.minAmountFraction,label: parseFloat(parseFloat(this.state.amount*this.minAmountFraction).toFixed(4))+" "+ this.state.currencyCode}, {value: this.state.amount,label: this.state.amount+" "+this.state.currencyCode}]} min={this.state.amount*this.minAmountFraction} max={this.state.amount} onChange={this.handleMinAmountChange} />
Fidelity Bond Size
(x+'%')} step={0.5} marks={[{value: 1,label: '1%'},{value: 3,label: '3%'},{value: 5,label: '5%'},{value: 9,label: '9%'},{value: 15,label: '15%'}]} min={1} max={15} onChange={this.handleSliderBondSizeChange} />
Allow bondless taker (info)} control={ this.setState({allowBondless: !this.state.allowBondless})} /> } />
) } render() { return ( {/* ORDER MAKER */}
{/* */} Advanced} labelPlacement="start" control={ this.setState({showAdvanced: !this.state.showAdvanced})}/>} /> {/* */}
{/* conditions to disable the make button */} {(this.state.amount == null || this.state.amount <= 0 || (this.state.is_explicit & (this.state.badSatoshis != null || this.state.satoshis == null)) || (!this.state.is_explicit & this.state.badPremium != null)) ?
: }
{this.state.badRequest ? {this.state.badRequest}
: ""}
Create a BTC {this.state.type==0 ? "buy":"sell"} order for {pn(this.state.amount)} {this.state.currencyCode} {this.state.is_explicit ? " of " + pn(this.state.satoshis) + " Satoshis" : (this.state.premium == 0 ? " at market price" : (this.state.premium > 0 ? " at a " + this.state.premium + "% premium":" at a " + -this.state.premium + "% discount") ) }
); } }