Skip to content

Commit 9b5f4af

Browse files
committed
Prevents Sending Redundant Trade Pings as Seller
Re-affirming the state of a trade offer or trade history as a seller doesn't provide value server-side. Will extend to buyers as well in the future, but it requires a couple server-side changes in the works.
1 parent e89bda6 commit 9b5f4af

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

src/lib/alarms/csfloat_trade_pings.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function pingTradeStatus(expectedSteamID?: string) {
4949
let errors;
5050

5151
if (pendingTrades.length > 0) {
52-
errors = await pingUpdates(pendingTrades);
52+
errors = await pingUpdates(pendingTrades, access?.steam_id);
5353
}
5454

5555
// Ping status of ext + permissions
@@ -75,7 +75,7 @@ interface UpdateErrors {
7575
rollback_trades_error?: string;
7676
}
7777

78-
async function pingUpdates(pendingTrades: SlimTrade[]): Promise<UpdateErrors> {
78+
async function pingUpdates(pendingTrades: SlimTrade[], steamID?: string|null): Promise<UpdateErrors> {
7979
const errors: UpdateErrors = {};
8080

8181
try {
@@ -93,14 +93,14 @@ async function pingUpdates(pendingTrades: SlimTrade[]): Promise<UpdateErrors> {
9393

9494
let tradeHistory: TradeHistoryStatus[] = [];
9595
try {
96-
tradeHistory = await pingTradeHistory(pendingTrades);
96+
tradeHistory = await pingTradeHistory(pendingTrades, steamID);
9797
} catch (e) {
9898
console.error('failed to ping trade history', e);
9999
errors.history_error = (e as any).toString();
100100
}
101101

102102
try {
103-
await pingSentTradeOffers(pendingTrades);
103+
await pingSentTradeOffers(pendingTrades, steamID);
104104
} catch (e) {
105105
console.error('failed to ping sent trade offer state', e);
106106
errors.trade_offer_error = (e as any).toString();

src/lib/alarms/trade_history.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
1-
import {SlimTrade, Trade} from '../types/float_market';
1+
import {SlimTrade} from '../types/float_market';
22
import {TradeHistoryStatus, TradeHistoryType} from '../bridge/handlers/trade_history_status';
3-
import {AppId, TradeStatus} from '../types/steam_constants';
3+
import {AppId, TradeOfferState, TradeStatus} from '../types/steam_constants';
44
import {clearAccessTokenFromStorage, getAccessToken} from './access_token';
55

6-
export async function pingTradeHistory(pendingTrades: SlimTrade[]): Promise<TradeHistoryStatus[]> {
6+
export async function pingTradeHistory(pendingTrades: SlimTrade[], steamID?: string|null): Promise<TradeHistoryStatus[]> {
77
const {history, type} = await getTradeHistory();
88

99
// premature optimization in case it's 100 trades
1010
const assetsToFind = pendingTrades.reduce(
1111
(acc, e) => {
12-
acc[e.contract.item.asset_id] = true;
12+
acc[e.contract.item.asset_id] = e;
1313
return acc;
1414
},
15-
{} as {[key: string]: boolean}
15+
{} as {[key: string]: SlimTrade}
1616
);
1717

1818
// We only want to send history that is relevant to verifying trades on CSFloat
1919
const historyForCSFloat = history.filter((e) => {
2020
const received_ids = e.received_assets.map((e) => e.asset_id);
2121
const given_ids = e.given_assets.map((e) => e.asset_id);
22-
return !![...received_ids, ...given_ids].find((e) => {
22+
23+
const foundSlimTrades = [...received_ids, ...given_ids].map((e) => {
2324
return assetsToFind[e];
24-
});
25+
}).filter(e => !!e);
26+
if (!foundSlimTrades || foundSlimTrades.length === 0) {
27+
return false;
28+
}
29+
30+
// Have we already reported this status as a seller? If so, we can skip doing it again
31+
if (foundSlimTrades.every((t) => t.steam_offer?.state === TradeOfferState.Accepted && t.seller_id === steamID)) {
32+
return false;
33+
}
34+
35+
return true;
2536
});
2637

2738
if (historyForCSFloat.length === 0) {

src/lib/alarms/trade_offer.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,33 @@ import {HasPermissions} from '../bridge/handlers/has_permissions';
1111
import {convertSteamID32To64} from '../utils/userinfo';
1212
import {TradeHistoryStatus} from '../bridge/handlers/trade_history_status';
1313

14-
export async function pingSentTradeOffers(pendingTrades: SlimTrade[]) {
14+
export async function pingSentTradeOffers(pendingTrades: SlimTrade[], steamID?: string|null) {
1515
const {offers, type} = await getSentTradeOffers();
1616

1717
const offersToFind = pendingTrades.reduce(
1818
(acc, e) => {
19-
acc[e.steam_offer.id] = true;
19+
if (!acc[e.steam_offer.id]) {
20+
acc[e.steam_offer.id] = [];
21+
}
22+
acc[e.steam_offer.id].push(e);
2023
return acc;
2124
},
22-
{} as {[key: string]: boolean}
25+
{} as {[key: string]: SlimTrade[]}
2326
);
2427

2528
// We only want to send offers that are relevant to verifying trades on CSFloat
2629
const offersForCSFloat = offers.filter((e) => {
27-
return !!offersToFind[e.offer_id];
30+
const slimTrades = offersToFind[e.offer_id];
31+
if (!slimTrades || slimTrades.length === 0) {
32+
return false;
33+
}
34+
35+
if (slimTrades.every(t => t.steam_offer?.state === e.state && t.seller_id === steamID)) {
36+
// We're pushing the same trade offer state update as a seller, this has no effect server-side, skip
37+
return false;
38+
}
39+
40+
return true;
2841
});
2942

3043
if (offersForCSFloat.length > 0) {

0 commit comments

Comments
 (0)