-
Notifications
You must be signed in to change notification settings - Fork 40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| 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, 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, | ||
| steam_offer: {...pendingTrade.steam_offer, state: TradeOfferState.Accepted}, | ||
| }; | ||
|
|
||
| expect(findFailedTrades([trade], [tradeHistory])).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| const pendingTrade = { | ||
| 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; | ||
|
|
||
| const tradeHistory: TradeHistoryStatus = { | ||
| 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, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 && | ||
| 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}`); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.