This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Publishing client to sparkswap-desktop: v0.3.7
- Loading branch information
Sparkswap
committed
Jan 13, 2020
1 parent
4965494
commit 737abb3
Showing
39 changed files
with
1,542 additions
and
228 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Asset } from '../global-shared/types' | ||
|
||
export class BalanceError extends Error { | ||
asset: Asset | ||
|
||
constructor (message: string, asset: Asset) { | ||
super(message) | ||
this.asset = asset | ||
} | ||
} | ||
|
||
export class QuantityError extends Error { | ||
asset: Asset | ||
|
||
constructor (message: string, asset: Asset) { | ||
super(message) | ||
this.asset = asset | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
RecurringBuy, | ||
WireRecurringBuy, | ||
Trade, | ||
TradeStatus, | ||
UnsavedRecurringBuy, | ||
WireUnsavedRecurringBuy | ||
} from './types' | ||
import { Amount, UnknownObject } from '../global-shared/types' | ||
|
||
export function deserializeTradeFromWire (wireTrade: UnknownObject): Trade { | ||
return { | ||
id: wireTrade.id as number, | ||
status: wireTrade.status as TradeStatus, | ||
hash: wireTrade.hash as string, | ||
destinationAmount: wireTrade.destinationAmount as Amount, | ||
sourceAmount: wireTrade.sourceAmount as Amount, | ||
startTime: new Date(wireTrade.startTime as string), | ||
endTime: wireTrade.endTime ? new Date(wireTrade.endTime as string) : undefined | ||
} | ||
} | ||
|
||
export function serializeUnsavedRecurringBuyToWire (recurringBuy: UnsavedRecurringBuy): WireUnsavedRecurringBuy { | ||
return { | ||
amount: recurringBuy.amount, | ||
frequency: recurringBuy.frequency, | ||
referenceTime: recurringBuy.referenceTime.toISOString() | ||
} | ||
} | ||
|
||
export function deserializeUnsavedRecurringBuyFromWire (recurringBuy: WireUnsavedRecurringBuy): UnsavedRecurringBuy { | ||
return { | ||
amount: recurringBuy.amount, | ||
frequency: recurringBuy.frequency, | ||
referenceTime: new Date(recurringBuy.referenceTime) | ||
} | ||
} | ||
|
||
export function deserializeRecurringBuyFromWire (wireRecurringBuy: WireRecurringBuy): RecurringBuy { | ||
const partialRecurringBuy = deserializeUnsavedRecurringBuyFromWire(wireRecurringBuy) | ||
return { | ||
id: wireRecurringBuy.id, | ||
...partialRecurringBuy | ||
} | ||
} | ||
|
||
export function serializeRecurringBuyToWire (recurringBuy: RecurringBuy): WireRecurringBuy { | ||
const partialWireRecurringBuy = serializeUnsavedRecurringBuyToWire(recurringBuy) | ||
return { | ||
id: recurringBuy.id, | ||
...partialWireRecurringBuy | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import serverRequest from './server-request' | ||
import { getNextTimeoutDuration, isStartOfInterval, getCronDate } from './time' | ||
|
||
export { | ||
serverRequest | ||
serverRequest, | ||
getNextTimeoutDuration, | ||
isStartOfInterval, | ||
getCronDate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Frequency, TimeUnit } from '../types' | ||
|
||
const MILLISECONDS_PER_MINUTE = 60 * 1000 | ||
const MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60 | ||
const MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * 24 | ||
const MILLISECONDS_PER_WEEK = MILLISECONDS_PER_DAY * 7 | ||
|
||
const EPOCH_DATE = new Date(Date.UTC(1970, 0, 1)) | ||
|
||
function getMinutesSince (start: Date, since = EPOCH_DATE): number { | ||
return Math.floor((start.getTime() - since.getTime()) / MILLISECONDS_PER_MINUTE) | ||
} | ||
|
||
function getHoursSince (start: Date, since = EPOCH_DATE): number { | ||
return Math.floor((start.getTime() - since.getTime()) / MILLISECONDS_PER_HOUR) | ||
} | ||
|
||
function getDaysSince (start: Date, since = EPOCH_DATE): number { | ||
return Math.floor((start.getTime() - since.getTime()) / MILLISECONDS_PER_DAY) | ||
} | ||
|
||
function getWeeksSince (start: Date, since = EPOCH_DATE): number { | ||
return Math.floor((start.getTime() - since.getTime()) / MILLISECONDS_PER_WEEK) | ||
} | ||
|
||
function getMonthsSince (start: Date, since = EPOCH_DATE): number { | ||
const yearsSince = start.getUTCFullYear() - since.getUTCFullYear() | ||
return yearsSince * 12 + start.getUTCMonth() - since.getUTCMonth() | ||
} | ||
|
||
function getNextMinuteMs (start: Date, minutes: number, reference = EPOCH_DATE): number { | ||
return (getMinutesSince(start, reference) + minutes) * MILLISECONDS_PER_MINUTE | ||
} | ||
|
||
function getNextHourMs (start: Date, hours: number, reference = EPOCH_DATE): number { | ||
return (getHoursSince(start, reference) + hours) * MILLISECONDS_PER_HOUR | ||
} | ||
|
||
function getNextDayMs (start: Date, days: number, reference = EPOCH_DATE): number { | ||
return (getDaysSince(start, reference) + days) * MILLISECONDS_PER_DAY | ||
} | ||
|
||
function getNextWeekMs (start: Date, weeks: number, reference = EPOCH_DATE): number { | ||
return (getWeeksSince(start, reference) + weeks) * MILLISECONDS_PER_WEEK | ||
} | ||
|
||
function getNextMonthMs (start: Date, months: number, reference = EPOCH_DATE): number { | ||
const sinceMut = new Date(reference.getTime()) | ||
return sinceMut.setUTCMonth(getMonthsSince(start, reference) + months) - reference.getTime() | ||
} | ||
|
||
const getNextMsFns = { | ||
[TimeUnit.MINUTES]: getNextMinuteMs, | ||
[TimeUnit.HOURS]: getNextHourMs, | ||
[TimeUnit.DAYS]: getNextDayMs, | ||
[TimeUnit.WEEKS]: getNextWeekMs, | ||
[TimeUnit.MONTHS]: getNextMonthMs | ||
} | ||
|
||
export function getNextTimeoutDuration (frequency: Frequency, start = new Date(), reference = EPOCH_DATE): number { | ||
const msFromSinceToNext = getNextMsFns[frequency.unit](start, frequency.interval, reference) | ||
return msFromSinceToNext + reference.getTime() - start.getTime() | ||
} | ||
|
||
export function getCronDate (date: Date): Date { | ||
const cronDate = new Date(date.getTime()) | ||
cronDate.setUTCSeconds(0, 0) | ||
return cronDate | ||
} | ||
|
||
export function isStartOfInterval ({ unit, interval }: Frequency, since = EPOCH_DATE): boolean { | ||
const now = getCronDate(new Date()) | ||
|
||
if (now.getUTCMinutes() !== since.getUTCMinutes()) { | ||
return false | ||
} | ||
|
||
switch (unit) { | ||
case TimeUnit.HOURS: | ||
return getHoursSince(now, since) % interval === 0 | ||
case TimeUnit.DAYS: | ||
return getDaysSince(now, since) % interval === 0 && now.getUTCHours() === since.getUTCHours() | ||
case TimeUnit.WEEKS: | ||
return getWeeksSince(now, since) % interval === 0 && now.getUTCDay() === since.getUTCDay() && | ||
now.getUTCHours() === since.getUTCHours() | ||
case TimeUnit.MONTHS: | ||
return getMonthsSince(now, since) % interval === 0 && now.getUTCDate() === since.getUTCDate() && | ||
now.getUTCHours() === since.getUTCHours() | ||
default: | ||
throw new Error(`Unrecognized time unit ${unit}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/node/data/migrations/0003-create-recurring-buys-table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
CREATE TABLE timeUnit ( | ||
unit VARCHAR(5) PRIMARY KEY NOT NULL | ||
); | ||
|
||
INSERT INTO timeUnit(unit) VALUES('MINUTES'); | ||
INSERT INTO timeUnit(unit) VALUES('HOURS'); | ||
INSERT INTO timeUnit(unit) VALUES('DAYS'); | ||
INSERT INTO timeUnit(unit) VALUES('WEEKS'); | ||
INSERT INTO timeUnit(unit) VALUES('MONTHS'); | ||
|
||
CREATE TABLE recurringBuys ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
amountAsset VARCHAR(5) NOT NULL REFERENCES assets(symbol), | ||
amountUnit VARCHAR(20) NOT NULL REFERENCES units(unit), | ||
amountValue INTEGER NOT NULL, | ||
duration INTEGER NOT NULL, | ||
timeUnit VARCHAR(5) NOT NULL REFERENCES timeUnit(unit), | ||
referenceTime DATETIME NOT NULL | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.