-
Notifications
You must be signed in to change notification settings - Fork 39
Adds Reporting for Steam Failed Trades #411
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import {describe, expect, it} from 'vitest'; | ||
| import {TradeHistoryStatus} from '../bridge/handlers/trade_history_status'; | ||
| import {SlimTrade, TradeState} from '../types/float_market'; | ||
| import {TradeOfferState, TradeStatus} from '../types/steam_constants'; | ||
| import {findFailedTrades} from './failed_trade'; | ||
|
|
||
| const steamAssetID = '3899876543210123456'; | ||
| const otherPartyID = '76561198000000000'; | ||
|
|
||
| describe('failed Steam trades', () => { | ||
| it('matches failed history to a relevant pending trade', () => { | ||
| const matches = findFailedTrades([pendingTrade()], [tradeHistory()]); | ||
|
|
||
| expect(matches).toHaveLength(1); | ||
| expect(matches[0].csfloatTrade.id).toBe('csfloat-trade-id'); | ||
| expect(matches[0].steamTrade.status).toBe(TradeStatus.Failed); | ||
| }); | ||
|
|
||
| it('does not match an already recorded failed Steam trade', () => { | ||
| const trade = pendingTrade(); | ||
| trade.steam_trade_failed_id = 'steam-trade-id'; | ||
|
|
||
| expect(findFailedTrades([trade], [tradeHistory()])).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not match a trade whose CSFloat offer is accepted', () => { | ||
| const trade = pendingTrade(); | ||
| trade.steam_offer.state = TradeOfferState.Accepted; | ||
|
|
||
| expect(findFailedTrades([trade], [tradeHistory()])).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| function pendingTrade(): SlimTrade { | ||
| return { | ||
| id: 'csfloat-trade-id', | ||
| state: TradeState.PENDING, | ||
| seller_id: otherPartyID, | ||
| buyer_id: '76561198111111111', | ||
| contract: { | ||
| item: { | ||
| asset_id: steamAssetID, | ||
| market_hash_name: 'AK-47 | Redline', | ||
| }, | ||
| }, | ||
| steam_offer: {state: TradeOfferState.Active}, | ||
| } as SlimTrade; | ||
| } | ||
|
|
||
| function tradeHistory(): TradeHistoryStatus { | ||
| return { | ||
| trade_id: 'steam-trade-id', | ||
| status: TradeStatus.Failed, | ||
| other_party_url: `https://steamcommunity.com/profiles/${otherPartyID}`, | ||
| other_party_id: otherPartyID, | ||
| received_assets: [{asset_id: steamAssetID}], | ||
| given_assets: [], | ||
| time_init: 123, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import {TradeHistoryStatus} from '../bridge/handlers/trade_history_status'; | ||
| import {StorageKey} from '../storage/keys'; | ||
| import {gStore} from '../storage/store'; | ||
| import {SlimTrade, TradeState} from '../types/float_market'; | ||
| import {TradeOfferState, TradeStatus} from '../types/steam_constants'; | ||
| import {reportTradeError} from './error_report'; | ||
| import {isBackgroundNotaryRollbackEnabled, proveTradesInBackground} from './notary'; | ||
|
|
||
| interface FailedTradeInfo { | ||
| steamTrade: TradeHistoryStatus; | ||
| csfloatTrade: SlimTrade; | ||
| } | ||
|
|
||
| export function findFailedTrades(pendingTrades: SlimTrade[], tradeHistory: TradeHistoryStatus[]): FailedTradeInfo[] { | ||
| const results: FailedTradeInfo[] = []; | ||
|
|
||
| for (const trade of tradeHistory) { | ||
| if (trade.status !== TradeStatus.Failed) { | ||
| continue; | ||
| } | ||
|
|
||
| const receivedIDs = trade.received_assets.map((asset) => asset.asset_id); | ||
| const givenIDs = trade.given_assets.map((asset) => asset.asset_id); | ||
| const assetIDs = [...receivedIDs, ...givenIDs]; | ||
|
|
||
| const csfloatTrade = pendingTrades.find( | ||
| (pendingTrade) => | ||
| pendingTrade.state === TradeState.PENDING && | ||
| pendingTrade.steam_offer?.state === TradeOfferState.Active && | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Active offer blocks failed matchingMedium Severity
Reviewed by Cursor Bugbot for commit 9a9cf8c. Configure here. |
||
| pendingTrade.steam_trade_failed_id !== trade.trade_id && | ||
| assetIDs.includes(pendingTrade.contract.item.asset_id) && | ||
| (trade.other_party_id === pendingTrade.seller_id || trade.other_party_id === pendingTrade.buyer_id) | ||
| ); | ||
| if (!csfloatTrade) { | ||
| continue; | ||
| } | ||
|
|
||
| results.push({steamTrade: trade, csfloatTrade}); | ||
| } | ||
|
|
||
| return results; | ||
| } | ||
|
|
||
| export async function pingFailedTrades(pendingTrades: SlimTrade[], tradeHistory: TradeHistoryStatus[]) { | ||
| if (!pendingTrades?.length || !tradeHistory?.length) { | ||
| return; | ||
| } | ||
|
|
||
| const failedTrades = findFailedTrades(pendingTrades, tradeHistory); | ||
| if (failedTrades.length === 0 || !(await isBackgroundNotaryRollbackEnabled())) { | ||
| return; | ||
| } | ||
|
|
||
| const lastFailure = await gStore.getWithStorage<number>( | ||
| chrome.storage.local, | ||
| StorageKey.LAST_NOTARY_BG_PROOF_FAILURE | ||
| ); | ||
| if (lastFailure && lastFailure > Date.now() - 60 * 60 * 1000) { | ||
| console.log('skipping failed-trade notary proof, last failure was less than 60 minutes ago'); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await proveTradesInBackground(failedTrades.map((failedTrade) => failedTrade.steamTrade)); | ||
| console.log(`proved ${failedTrades.length} failed trade(s) via notary`); | ||
| } catch (e) { | ||
| console.error('failed-trade notary proving failed', e); | ||
| await gStore.setWithStorage(chrome.storage.local, StorageKey.LAST_NOTARY_BG_PROOF_FAILURE, Date.now()); | ||
| reportTradeError(failedTrades[0].csfloatTrade.id, `background extension failed-trade notary failed: ${e}`); | ||
| } | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do these need to be functions? Feels like
pendingTradeandtradeHistorymight be better suited as static variables