Skip to content

Commit

Permalink
Currency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Apr 9, 2021
1 parent 929a29d commit b6140a6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
29 changes: 18 additions & 11 deletions app/src/steam/market.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import axios from 'axios';
import { ItemPrice, Item } from '../transactions/types';
import Currency from './currency';

const { parse: cleanPrice } = Currency.DEFAULT;
import Currency, { ICurrency } from './currency';

const priceOverview = 'http://steamcommunity.com/market/priceoverview';

export async function getItemPrice(item: Item): Promise<ItemPrice> {
export async function getItemPrice(
item: Item,
currency: ICurrency | undefined = Currency.USD
): Promise<ItemPrice> {
const { appid } = item;
const params = {
appid,
currency: Currency.DEFAULT.currencyId,
currency: currency.currencyId,
market_hash_name: getItemName(item)
};
const { data } = await axios.get(priceOverview, { params });
return parseData(data);
return parseData(data, currency);
}

export function getItemName({ market_name, market_hash_name, name }: Item) {
Expand All @@ -24,14 +25,20 @@ export function getItemName({ market_name, market_hash_name, name }: Item) {
return name;
}

function parseData({ success, lowest_price, median_price }: any): ItemPrice {
function parseData(
{ success, lowest_price, median_price }: any,
{ parse }: ICurrency
): ItemPrice {
return {
success,
lowest_price: cleanPrice(lowest_price),
median_price: median_price ? cleanPrice(median_price) : undefined
lowest_price: parse(lowest_price),
median_price: median_price ? parse(median_price) : undefined
};
}

export async function getAllItemsPrice(items: Item[]): Promise<ItemPrice[]> {
return Promise.all(items.map((item) => getItemPrice(item)));
export async function getAllItemsPrice(
items: Item[],
currency?: ICurrency
): Promise<ItemPrice[]> {
return Promise.all(items.map((item) => getItemPrice(item, currency)));
}
24 changes: 14 additions & 10 deletions app/src/transactions/logic/calculatePrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@ export default async function middleware(
next: NextFunction
) {
const { processor, itemsToReceive, itemsToGive } = context;
const { options } = processor.account;
context.receiveItemsPrices = await getAllItemsPrice(
itemsToReceive,
processor.account.options.status.currency
);

context.receiveItemsPrices = await getAllItemsPrice(itemsToReceive);

if (
context.receiveItemsPrices.some((item) =>
isTrash(item, processor.account.options)
)
) {
if (context.receiveItemsPrices.some((item) => isTrash(item, options))) {
processor.decline(context, Reason.TRASH);
}

context.receivePrice = reducePrices(context.receiveItemsPrices);

context.giveItemsPrices = await getAllItemsPrice(itemsToGive);
context.giveItemsPrices = await getAllItemsPrice(
itemsToGive,
options.status.currency
);
context.givePrice = reducePrices(context.giveItemsPrices);

context.profit = context.receivePrice - context.givePrice;

return next();
}

export function isTrash(item: ItemPrice, options: AccountOptions): boolean {
return calculatePrice(item) <= options.trading.trashLimit;
export function isTrash(item: ItemPrice, { trading }: AccountOptions): boolean {
return calculatePrice(item) <= trading.trashLimit;
}

export function calculatePrice({
Expand Down
2 changes: 0 additions & 2 deletions app/src/transactions/logic/checkOverpay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { Reason } from '../processor';
import { OfferContext } from '../types';

export default function middleware(context: OfferContext, next: NextFunction) {
context.profit = context.receivePrice - context.givePrice;

const { processor, profit } = context;
const { trading } = context.processor.account.options;

Expand Down
3 changes: 2 additions & 1 deletion app/src/transactions/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function createContext(offer: Offer, processor: TradeProcessor): OfferContext {
givePrice: 0,
receiveItemsPrices: [],
receivePrice: 0,
profit: 0
// @ts-ignore
profit: 'Not calculated'
};
}

Expand Down
4 changes: 3 additions & 1 deletion web/src/components/accounts/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default function Account({ account }: any) {
const [{ login, status }, setOptions] = useState<AccountOptions>(account);

useEffect(() => {
socket.on('updateAccount', setOptions);
socket.on('updateAccount', (acc: AccountOptions) => {
if (acc.login.username === login.username) setOptions(acc);
});

getAccount(login.username)
.then(({ data }) => data.response)
Expand Down
1 change: 1 addition & 0 deletions web/src/components/trades/trade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ItemSet } from './item';
import { Trade as ITrade } from './util';
import { ArrowLeftRight } from 'react-bootstrap-icons';

// TODO: Display the account that received the trade and display the profit currency
export default function Trade({ trade }: { trade: ITrade }) {
const title = `Profit: ${trade.profit} - Partner: ${trade.partner} - Trade Id: ${trade.offerId}`;
return (
Expand Down

0 comments on commit b6140a6

Please sign in to comment.