Skip to content

Commit

Permalink
Merge pull request #253 from csfloat/feature/cancel-unconfirmed-offer…
Browse files Browse the repository at this point in the history
…s-for-sales

Auto-Cancel Unconfirmed Trade Offers for CSFloat Sales
  • Loading branch information
Step7750 authored Sep 18, 2024
2 parents e0de1e6 + 420aa24 commit 360987e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/alarms/csfloat_trade_pings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Trade} from '../types/float_market';
import {FetchPendingTrades} from '../bridge/handlers/fetch_pending_trades';
import {pingTradeHistory} from './trade_history';
import {pingCancelTrades, pingSentTradeOffers} from './trade_offer';
import {cancelUnconfirmedTradeOffers, pingCancelTrades, pingSentTradeOffers} from './trade_offer';
import {HasPermissions} from '../bridge/handlers/has_permissions';
import {PingExtensionStatus} from '../bridge/handlers/ping_extension_status';
import {AccessToken, getAccessToken} from './access_token';
Expand Down Expand Up @@ -72,6 +72,12 @@ interface UpdateErrors {
async function pingUpdates(pendingTrades: Trade[]): Promise<UpdateErrors> {
const errors: UpdateErrors = {};

try {
await cancelUnconfirmedTradeOffers(pendingTrades);
} catch (e) {
console.error(`failed to cancel unconfirmed trade offers`, e);
}

try {
await pingTradeHistory(pendingTrades);
} catch (e) {
Expand Down
53 changes: 53 additions & 0 deletions src/lib/alarms/trade_offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {OfferStatus, TradeOfferStatus, TradeOffersType} from '../bridge/handlers
import {clearAccessTokenFromStorage, getAccessToken} from './access_token';
import {AnnotateOffer} from '../bridge/handlers/annotate_offer';
import {PingCancelTrade} from '../bridge/handlers/ping_cancel_trade';
import {CancelTradeOffer} from '../bridge/handlers/cancel_trade_offer';
import {FetchSteamUser} from '../bridge/handlers/fetch_steam_user';

export async function pingSentTradeOffers(pendingTrades: Trade[]) {
const {offers, type} = await getSentTradeOffers();
Expand Down Expand Up @@ -94,6 +96,57 @@ export async function pingCancelTrades(pendingTrades: Trade[]) {
}
}
}

// cancelUnconfirmedTradeOffers related to sales on CSFloat that haven't been confirmed for a while
// Helps prevent the user from sending a trade offer _way after_ the sale has already failed
export async function cancelUnconfirmedTradeOffers(pendingTrades: Trade[]) {
const offerIDsToCancel = [
...new Set(
pendingTrades
.filter(
(e) =>
e.steam_offer.state === TradeOfferState.CreatedNeedsConfirmation &&
new Date(e.steam_offer.sent_at).getTime() < Date.now() - 60 * 60 * 1000
)
.map((e) => e.steam_offer.id)
),
];

if (offerIDsToCancel.length === 0) {
return;
}

const resp = await getSentTradeOffers();

const offersIDsStillNeedsConfirmation = offerIDsToCancel.filter((id) => {
const sentOffer = resp.offers.find((offer) => offer.offer_id === id);
if (!sentOffer) {
return false;
}

return sentOffer.state === TradeOfferState.CreatedNeedsConfirmation;
});

if (offersIDsStillNeedsConfirmation.length === 0) {
return;
}

const steamUser = await FetchSteamUser.handleRequest({}, {});
if (!steamUser.sessionID) {
// Can't cancel offers without a session
return;
}

for (const offerID of offersIDsStillNeedsConfirmation) {
try {
await CancelTradeOffer.handleRequest({trade_offer_id: offerID, session_id: steamUser.sessionID}, {});
} catch (e: any) {
console.error(`failed to cancel needs confirmation trade, returning early: ${e.toString()}`);
return;
}
}
}

async function getEnglishSentTradeOffersHTML(): Promise<string> {
const resp = await fetch(`https://steamcommunity.com/id/me/tradeoffers/sent`, {
credentials: 'include',
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/float_market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export enum TradeState {
export interface SteamOffer {
id: string;
state: TradeOfferState;
sent_at: string;
}

export interface Trade {
Expand Down

0 comments on commit 360987e

Please sign in to comment.