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

fix: trade history crash #2445

Merged
merged 1 commit into from
Dec 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion client/src/ui/components/trading/MarketTradingHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const MarketTradingHistory = () => {
{filteredTradeEvents
.sort((a, b) => b.event.eventTime.getTime() - a.event.eventTime.getTime())
.filter((trade) =>
selectedResourceId
trade.event.resourceGiven && trade.event.resourceTaken && selectedResourceId
? trade.event.resourceGiven.resourceId === selectedResourceId ||
trade.event.resourceTaken.resourceId === selectedResourceId
: true,
Expand Down
30 changes: 20 additions & 10 deletions client/src/ui/components/trading/TradeHistoryEvent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export const TradeHistoryRowHeader = () => {

export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => {
const { getAddressNameFromEntity } = useEntitiesUtils();

const resourceTaken = trade.event.resourceTaken;
const resourceGiven = trade.event.resourceGiven;
if (!resourceTaken || !resourceGiven) {
return null;
}

const price = getLordsPricePerResource(trade.event.resourceGiven, trade.event.resourceTaken);
const taker = getAddressNameFromEntity(trade.event.takerId);

Expand All @@ -37,12 +44,12 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => {
<div className={`text-sm my-auto flex flex-row items-center justify-start`}>{taker}</div>
<div className="text-sm my-auto flex flex-row">
<div>{"bought"}</div>
<ResourceIcon resource={ResourcesIds[Number(trade.event.resourceTaken.resourceId)]} size={"sm"} />
<div>{`${currencyIntlFormat(divideByPrecision(trade.event.resourceTaken.amount), 2)} for ${currencyIntlFormat(
divideByPrecision(trade.event.resourceGiven.amount),
<ResourceIcon resource={ResourcesIds[Number(resourceTaken.resourceId)]} size={"sm"} />
<div>{`${currencyIntlFormat(divideByPrecision(resourceTaken.amount), 2)} for ${currencyIntlFormat(
divideByPrecision(resourceGiven.amount),
2,
)}`}</div>
<ResourceIcon resource={ResourcesIds[Number(trade.event.resourceGiven.resourceId)]} size={"sm"} />
<ResourceIcon resource={ResourcesIds[Number(resourceGiven.resourceId)]} size={"sm"} />
</div>
<div className="text-sm my-auto flex flex-row">
{currencyIntlFormat(Number(price), 2)}
Expand All @@ -52,9 +59,7 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => {
resource={
ResourcesIds[
Number(
trade.event.resourceTaken.resourceId === ResourcesIds.Lords
? trade.event.resourceGiven.resourceId
: trade.event.resourceTaken.resourceId,
resourceTaken.resourceId === ResourcesIds.Lords ? resourceGiven.resourceId : resourceTaken.resourceId,
)
]
}
Expand All @@ -66,8 +71,13 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => {
};

const getLordsPricePerResource = (resourceA: Resource, resourceB: Resource): number => {
const lordsResource = resourceA.resourceId === ResourcesIds.Lords ? resourceA : resourceB;
const otherResource = resourceA.resourceId === ResourcesIds.Lords ? resourceB : resourceA;
try {
const lordsResource = resourceA.resourceId === ResourcesIds.Lords ? resourceA : resourceB;
const otherResource = resourceA.resourceId === ResourcesIds.Lords ? resourceB : resourceA;

return Number(lordsResource.amount) / Number(otherResource.amount);
return Number(lordsResource.amount) / Number(otherResource.amount);
} catch (e) {
console.error(e);
return 0;
}
};
Loading