98 lines
2.6 KiB
JavaScript
98 lines
2.6 KiB
JavaScript
import Serializable from "../../util/Serializable.js";
|
|
import SerializableHelper from "../../util/SerializableHelper.js";
|
|
import TradingPair from "../assets/TradingPair.js";
|
|
import TransactionPhase from "./TransactionPhase.js";
|
|
import TransactionSettings from "./TransactionSettings.js";
|
|
|
|
export default class Transaction extends Serializable {
|
|
/**
|
|
* @type {string} worker or strategy that created the order
|
|
*/
|
|
initiator = "";
|
|
|
|
/**
|
|
* @type {TradingPair} tradingpair
|
|
*/
|
|
symbol = null;
|
|
|
|
/**
|
|
* @type {string} unique order id
|
|
*/
|
|
id = "";
|
|
|
|
/**
|
|
* @type {TransactionSettings} buy settings
|
|
*/
|
|
buySettings = null;
|
|
|
|
/**
|
|
* @type {TransactionSettings} buy settings
|
|
*/
|
|
sellSettings = null;
|
|
|
|
/**
|
|
* @type {number} set only if finished: (sellQuantity * sellPrice) - (buyQuantity * buyPrice)
|
|
*/
|
|
result = 0;
|
|
|
|
/**
|
|
* @type {string} see TransactionPhase for more details and transition graph
|
|
*/
|
|
phase = TransactionPhase.INITIAL;
|
|
|
|
/**
|
|
* @type {number}
|
|
*/
|
|
timestamp = 0;
|
|
|
|
/**
|
|
* @param {string} initiator
|
|
* @param {TradingPair} tradingPair
|
|
*/
|
|
constructor(initiator, tradingPair){
|
|
super();
|
|
this.initiator = initiator;
|
|
this.symbol = tradingPair;
|
|
this.id = this.generateId();
|
|
this.timestamp = Date.now();
|
|
}
|
|
|
|
generateId(){
|
|
return "x-U5D79M5B-LS-" + Date.now() + "-" + Math.floor(Math.random() * 8999999 + 1000000);
|
|
}
|
|
|
|
/**
|
|
* @returns {string} string representation of this object
|
|
*/
|
|
toJson() {
|
|
const obj = JSON.parse(super.toJson());
|
|
obj.initiator = this.initiator;
|
|
obj.id = this.id;
|
|
obj.result = this.result;
|
|
obj.phase = this.phase;
|
|
obj.symbol = SerializableHelper.serialize(this.symbol);
|
|
obj.buySettings = SerializableHelper.serialize(this.buySettings);
|
|
obj.sellSettings = SerializableHelper.serialize(this.sellSettings);
|
|
|
|
return JSON.stringify(obj);
|
|
}
|
|
|
|
/**
|
|
* @param {string} objString
|
|
* @returns {Transaction}
|
|
*/
|
|
static fromJson(objString) {
|
|
const obj = new Transaction();
|
|
const tmpObj = JSON.parse(objString);
|
|
|
|
obj.initiator = tmpObj.initiator;
|
|
obj.id = tmpObj.id;
|
|
obj.result = tmpObj.result;
|
|
obj.phase = tmpObj.phase;
|
|
obj.symbol = SerializableHelper.deserialize(tmpObj.symbol);
|
|
obj.buySettings = SerializableHelper.deserialize(tmpObj.buySettings);
|
|
obj.sellSettings = SerializableHelper.deserialize(tmpObj.sellSettings);
|
|
|
|
return obj;
|
|
}
|
|
} |