Skip to content

Commit

Permalink
Implement GTC Orders (#481)
Browse files Browse the repository at this point in the history
This PR makes all orders Good 'Till Cancelled (GTC) orders. If you place an order it will stay pending and keep re-broadcasting the order every 10 minutes until it either matches or the user cancels it.
  • Loading branch information
lukechilds authored and sindresorhus committed Aug 27, 2018
1 parent 75395bb commit 0040d04
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
4 changes: 3 additions & 1 deletion app/locales/en-US/swap.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
"matched": "Matched",
"reverted": "Reverted",
"pending": "Pending",
"unmatched": "Unmatched"
"open": "Open",
"unmatched": "Unmatched",
"cancelled": "Cancelled"
},
"statusInformation": {
"reverted": "The swap was reverted due to connectivity issues."
Expand Down
1 change: 1 addition & 0 deletions app/renderer/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export default class Api {

return this.request({
method: opts.type,
gtc: 1,
base: opts.baseCurrency,
rel: opts.quoteCurrency,
basevolume: opts.amount,
Expand Down
8 changes: 3 additions & 5 deletions app/renderer/components/SwapList.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const SwapHeader = props => (
</div>
);

const SwapItem = ({style, swap}) => (
const SwapItem = ({style, swap, showCancel}) => (
<div className={`row ${swap.orderType}`} style={style}>
<div className="timestamp">{formatDate(swap.timeStarted, 'HH:mm DD/MM/YY')}</div>
<div className="pairs">{swap.baseCurrency}/{swap.quoteCurrency}</div>
Expand All @@ -93,13 +93,11 @@ const SwapItem = ({style, swap}) => (
<div className="status__icon" data-status={swap.status}>{swap.statusFormatted}</div>
</div>
<div className="buttons">
{/* Disabled until marketmaker v2
See: https://github.com/atomiclabs/hyperdex/issues/262#issuecomment-396587751showCancel
&&
{showCancel && (
<div className="cancel">
<CancelButton swap={swap}/>
</div>
*/}
)}
<div className="view">
<SwapDetails swap={swap}/>
</div>
Expand Down
20 changes: 17 additions & 3 deletions app/renderer/components/SwapList.scss
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,17 @@
}
}

@media (min-width: 1260px) {
@media (min-width: 1280px) {
.row {
grid-template-areas: 'timestamp pairs base-amount quote-amount status buttons';
grid-template-columns: 16% 14% 23% 20%;
grid-template-columns: 14% 0% 23% 23%;
justify-content: unset;
white-space: unset;

.pairs {
display: none;
}

.base-amount {
justify-self: start;
}
Expand All @@ -132,7 +136,7 @@
}

.status {
justify-self: center;
justify-self: end;
}

.buttons {
Expand All @@ -141,6 +145,16 @@
}
}

@media (min-width: 1480px) {
.row {
grid-template-columns: 14% 15% 21% 21%;

.pairs {
display: block;
}
}
}

.timestamp {
color: var(--text-color);
}
Expand Down
21 changes: 14 additions & 7 deletions app/renderer/swap-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import cryptoPouch from 'crypto-pouch';
import Emittery from 'emittery';
import PQueue from 'p-queue';
import roundTo from 'round-to';
import {subDays, isPast, addMinutes} from 'date-fns';
import {subDays, isAfter} from 'date-fns';
import appContainer from 'containers/App';
import {appTimeStarted} from '../constants';
import swapTransactions from './swap-transactions';
import {translate} from './translate';

Expand Down Expand Up @@ -227,14 +228,13 @@ class SwapDB {
}
});

// Treat swaps pending for more than 5 minutes as failed.
// https://github.com/jl777/SuperNET/issues/775#issuecomment-397557568
const timedOut = swap.status === 'pending' && isPast(addMinutes(swap.timeStarted, 5));
if (timedOut) {
// Show open orders from previous session as cancelled
const cancelled = swap.status === 'pending' && isAfter(appTimeStarted, swap.timeStarted);
if (cancelled) {
swap.status = 'failed';
swap.error = {
code: undefined,
message: t('timedOut'),
message: undefined,
};
}

Expand All @@ -252,15 +252,22 @@ class SwapDB {
}

if (swap.status === 'failed') {
if (swap.error.code === -9999 || timedOut) {
if (swap.error.code === -9999) {
swap.statusFormatted = t('status.unmatched').toLowerCase();
}
if (swap.error.code === -9998 || cancelled) {
swap.statusFormatted = t('status.cancelled').toLowerCase();
}
if (swap.transactions.find(tx => tx.stage === 'alicereclaim')) {
swap.statusFormatted = t('status.reverted').toLowerCase();
swap.statusInformation = t('statusInformation.reverted');
}
}

if (swap.status === 'pending') {
swap.statusFormatted = t('status.open').toLowerCase();
}

return swap;
}

Expand Down
4 changes: 2 additions & 2 deletions app/renderer/views/Exchange/Swaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const TabView = ({component}) => (
);

const All = () => (
<SwapList swaps={exchangeContainer.state.swapHistory} limit={swapLimit}/>
<SwapList swaps={exchangeContainer.state.swapHistory} limit={swapLimit} showCancel/>
);

const Split = () => {
Expand All @@ -45,7 +45,7 @@ const Split = () => {
);

return (
<SwapList swaps={filteredData} limit={swapLimit}/>
<SwapList swaps={filteredData} limit={swapLimit} showCancel/>
);
};

Expand Down
4 changes: 4 additions & 0 deletions app/renderer/views/Trades.scss
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
justify-content: unset;
white-space: unset;

.pairs {
display: block;
}

.base-amount {
justify-self: start;
}
Expand Down

0 comments on commit 0040d04

Please sign in to comment.