Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zadanie) #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 53 additions & 44 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 35 additions & 6 deletions src/marketPlace.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { seller } from "./constant";
import User from "./user";

export class MarketPlace {

Expand All @@ -9,6 +10,31 @@ export class MarketPlace {
this.underlyingCurrency = _underlyingCurrency;
// цена главной валюты (в побочной)
this.price = _price;
// ордерbook
this.orderbook = [];
this.userbook = {};
this.userCounter = 0;
}

methodCreateUser (balance, currencyType) {
const user = new User(balance, currencyType); // TODO Инициализация с нулем
this.userCounter++;
this.userbook[this.userCounter] = user;
return this.userCounter;
}

methodMarketPlaceDeposit (userId, amount, currencyType) {
if (!this.userbook[userId]) {
throw new Error('Нет такого юзера');
}
this.userbook[userId].methodDeposit(amount, currencyType);
}

methodMarketPlaceWithDraw (userId, amount, currencyType) {
if (!this.userbook[userId]) {
throw new Error('Нет такого юзера');
}
this.userbook[userId].methodWithDraw(amount, currencyType);
}

methodTrade (user1,user2) {
Expand Down Expand Up @@ -50,11 +76,14 @@ export class MarketPlace {
if (_buyer.balance < buyerNeedPay || _seller.balance < sellerNeedPay) {
throw new Error('Мало денег, жидло!');
}

// проводим транзакции
_seller.balance[this.underlyingCurrency] = _seller.balance[this.underlyingCurrency] - sellerNeedPay;
_seller.balance[this.priceCurrency] = _seller.balance[this.priceCurrency] + buyerNeedPay;
_buyer.balance[this.underlyingCurrency] = _buyer.balance[this.underlyingCurrency] + sellerNeedPay;
_buyer.balance[this.priceCurrency] = _buyer.balance[this.priceCurrency] - buyerNeedPay;
else {
// проводим транзакции
_seller.balance[this.underlyingCurrency] = _seller.balance[this.underlyingCurrency] - sellerNeedPay;
_seller.balance[this.priceCurrency] = _seller.balance[this.priceCurrency] + buyerNeedPay;
_buyer.balance[this.underlyingCurrency] = _buyer.balance[this.underlyingCurrency] + sellerNeedPay;
_buyer.balance[this.priceCurrency] = _buyer.balance[this.priceCurrency] - buyerNeedPay;
orderStatus = "closed";
// или может проверить балансы, а потом закрывать? как в тесте
}
}
}
28 changes: 26 additions & 2 deletions src/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export class User {
export default class User {
// balance = {
// [usd]: null,
// [rub]: null,
Expand All @@ -17,7 +17,7 @@ export class User {
// };

constructor(_balance, _currencyType) {
if (!this.balance) {
if (!this.balance) { // this для контекста класса (включая методы)
this.balance = {};
}
this.balance[_currencyType] = _balance;
Expand Down Expand Up @@ -48,9 +48,33 @@ export class User {
this.order.orderType = _orderType;
this.order.underlyingType = _underlyingType;
this.order.size = _size;

this.order.currentDate = new Date();
const dd = String(this.order.currentDate.getDate());
const mm = String(this.order.currentDate.getMonth());
const yyyy = String(this.order.currentDate.getFullYear());
const hr = String(this.order.currentDate.getHours());
const min = String(this.order.currentDate.getMinutes());

this.order.currentDate = dd + '/' + mm + '/' + yyyy + ' ' + hr + ':' + min;

this.order.orderStatus = "open";
}

getBalanceInCurrency (currencyType) {
return this.balance[currencyType];
}
}

// клиент создает ордер
// клиент отправляет ордер на рынок
// рынок обновляет ордербук (проверяем весь)
// рынок ищет пару
// если нашел проводит транзакцию
// поиск по филду в ордербуке
//
// проверить:
// 1.Ордер(филды)
// 2. Если есть пара, у них меняются балансы
// 3. Все ордеры есть в ордербуке
// 4. 1 рынок, 5 юзеров, по 3 ордера (10 = open, 5 = closed)
4 changes: 2 additions & 2 deletions test/marketPlace.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { seller, buyer, usd, rub } from "../src/constant";
import { User } from "../src/user";
import User from "../src/user";
import { MarketPlace } from "../src/marketPlace";
import assert from "assert";

Expand All @@ -15,7 +15,7 @@ describe('marketPlace', ()=> {

});

it('Should make a deal with balance calculation (methodTrade) POSITIVE', ()=> {
it('Should make a deal with balance calculation (methodTrade) *POSITIVE*', ()=> {

const priceCurrency = rub;
const price = 30;
Expand Down
2 changes: 1 addition & 1 deletion test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('user', () => {
assert.equal(user.balance[usd], 50);
});

it ('should create user object and cancel the withdrawal operation (methodWithDraw)', () => {
it ('should create user object and cancel the withdrawal operation (methodWithDraw) *NEGATIVE*', () => {
const user = new User (100, rub);
try {
user.methodWithDraw(200, rub);
Expand Down