Skip to content

Commit

Permalink
Optimise buy/sell transaction creation
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD committed Aug 2, 2021
1 parent 8651ee2 commit 3416006
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 419 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ The appication is a stock management API. It can be used to track a user's holdi

## High Level Architecture

![sdasdas](https://drive.google.com/uc?export=view&id=1c23xrNTzgfmtXKUVgtGEV69HihnONv7T)
![sdasdas](https://drive.google.com/uc?export=view&id=1XzheJISS4bxO0l-EFbM_qW0rLLCVtvTj)

#### User

- User schema contains information about the user like the funds he has to buy shares, his total returns etc.
- If some shares are sold then the funds received by selling are not immediately added to the user's account as there are still chances of the transaction being modified or deleted, so the funds are stored in another identifier called lockedFunds, with a lockin period, and are unlocked on the next trading day to be used for purchase. Another identifier lockedTill helps us keep a track of the the lockin time.
- User schema contains information about the user like his userName, mobile, funds he has to buy shares etc.

#### Holdings

- Represent a user's holdings of a particular share. It contains information about the number of shares of a particular stock held by the user, the average price, also the number of shares that are locked.
- Like in case of user's funds the shares that a user buys are also locked till the time the transaction can be deleted or updated(till the next trading day). After that time the shares are automatically moved to users active share tally.
- Represent a user's holdings of a particular share. It contains information about the number of shares of a particular stock held by the user, the average price, the total returns from that share etc.
- In case we add a new buy or sell transaction, then we take the values present in holding object, like averagePrice, and shareCount for calculating their new values, but if we update or delete a transaction, the holdings of a user are recreated from the history of transactions.

#### Transactions

- Stores all the trades that are done by the user, a trade can be of 2 types `BUY` or `SELL`.
- Each transaction contains details like the number of shares that were exchanged, the price at which they were shared, the average price of the share for the user when the transaction was made etc.
- A transaction also contains an identifier name unlockedTill, which represents the time till which the transaction is unlocked i.e. the time till which it can be deleted or updated, after this no such operation is possible on the transaction.
- Each transaction contains details like the number of shares that were exchanged, the price at which they were shared.
- A transaction can be created, deleted and updated, given the fact that it upholds the consistency of the system, in cases of conflicts if problems like double spending occurs and the transaction deletion/updation is terminated and the user is shown correcponding error.

#### Security

Expand Down
35 changes: 35 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@types/morgan": "^1.9.3",
"@types/swagger-ui-express": "^4.1.3",
"bluebird": "^3.7.2",
"chai": "^4.3.4",
Expand All @@ -28,6 +29,7 @@
"mocha": "^9.0.3",
"mongodb": "^4.0.1",
"mongoose": "^5.13.3",
"morgan": "^1.10.0",
"source-map-support": "^0.5.19",
"swagger-ui-dist": "^4.0.0-beta.2",
"swagger-ui-express": "^4.1.6",
Expand Down
16 changes: 10 additions & 6 deletions src/controllers/transaction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ class TransactionController {
averagePrice: holding.averagePrice,
}).save();
try {
const { averagePrice, totalReturns, shareCount } =
await TransactionService.calculateHoldings(userId, "");
const averagePrice =
(holding.shareCount * holding.averagePrice +
transaction.shareCount * transaction.exchangePrice) /
(holding.shareCount + transaction.shareCount);
await Holding.findByIdAndUpdate(holding._id, {
$set: { averagePrice, shareCount, totalReturns },
$set: { averagePrice },
$inc: { shareCount: transaction.shareCount },
});
await User.findByIdAndUpdate(userId, {
$inc: {
Expand Down Expand Up @@ -143,10 +146,11 @@ class TransactionController {
averagePrice: holding.averagePrice,
}).save();
try {
const { averagePrice, totalReturns, shareCount } =
await TransactionService.calculateHoldings(userId, "");
const totalReturns =
(transaction.exchangePrice - holding.averagePrice) *
transaction.shareCount;
await Holding.findByIdAndUpdate(holding._id, {
$set: { averagePrice, shareCount, totalReturns },
$inc: { shareCount: -transaction.shareCount, totalReturns },
});
await User.findByIdAndUpdate(userId, {
$inc: {
Expand Down
3 changes: 2 additions & 1 deletion src/middlewares/http.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Locals from "../providers/Locals";
import swaggerUIDist from "swagger-ui-dist";
import YAML from "yamljs";
import swaggerUi from "swagger-ui-express";
import morgan from "morgan";

class Http {
public static mount(_express: Application): Application {
Expand Down Expand Up @@ -44,7 +45,7 @@ class Http {
],
})
);

_express.use(morgan("dev"));
return _express;
}
}
Expand Down
Loading

0 comments on commit 3416006

Please sign in to comment.