Skip to content
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
111 changes: 50 additions & 61 deletions app/components/UI/Predict/controllers/PredictController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,70 +1157,58 @@ export class PredictController extends BaseController<
// Track Predict Action Completed or Failed
const completionDuration = performance.now() - startTime;

if (result.success) {
const { spentAmount, receivedAmount } = result.response;

const cachedBalance =
this.state.balances[providerId]?.[signer.address]?.balance ?? 0;
let realAmountUsd = amountUsd;
let realSharePrice = sharePrice;
try {
if (preview.side === Side.BUY) {
realAmountUsd = parseFloat(spentAmount);
realSharePrice =
parseFloat(spentAmount) / parseFloat(receivedAmount);

// Optimistically update balance
this.update((state) => {
state.balances[providerId] = state.balances[providerId] || {};
state.balances[providerId][signer.address] = {
balance: cachedBalance - realAmountUsd,
// valid for 5 seconds (since it takes some time to reflect balance on-chain)
validUntil: Date.now() + 5000,
};
});
} else {
realAmountUsd = parseFloat(receivedAmount);
realSharePrice =
parseFloat(receivedAmount) / parseFloat(spentAmount);

// Optimistically update balance
this.update((state) => {
state.balances[providerId] = state.balances[providerId] || {};
state.balances[providerId][signer.address] = {
balance: cachedBalance + realAmountUsd,
// valid for 5 seconds (since it takes some time to reflect balance on-chain)
validUntil: Date.now() + 5000,
};
});
}
} catch (_e) {
// If we can't get real share price, continue without it
}

// Track Predict Action Completed (fire and forget)
this.trackPredictOrderEvent({
eventType: PredictEventType.COMPLETED,
amountUsd: realAmountUsd,
analyticsProperties,
providerId,
completionDuration,
sharePrice: realSharePrice,
});
} else {
// Track Predict Action Failed (fire and forget)
this.trackPredictOrderEvent({
eventType: PredictEventType.FAILED,
amountUsd,
analyticsProperties,
providerId,
sharePrice,
completionDuration,
failureReason: result.error || 'Unknown error',
});
if (!result.success) {
throw new Error(result.error);
}

const { spentAmount, receivedAmount } = result.response;

const cachedBalance =
this.state.balances[providerId]?.[signer.address]?.balance ?? 0;
let realAmountUsd = amountUsd;
let realSharePrice = sharePrice;
try {
if (preview.side === Side.BUY) {
realAmountUsd = parseFloat(spentAmount);
realSharePrice = parseFloat(spentAmount) / parseFloat(receivedAmount);

// Optimistically update balance
this.update((state) => {
state.balances[providerId] = state.balances[providerId] || {};
state.balances[providerId][signer.address] = {
balance: cachedBalance - realAmountUsd,
// valid for 5 seconds (since it takes some time to reflect balance on-chain)
validUntil: Date.now() + 5000,
};
});
} else {
realAmountUsd = parseFloat(receivedAmount);
realSharePrice = parseFloat(receivedAmount) / parseFloat(spentAmount);

// Optimistically update balance
this.update((state) => {
state.balances[providerId] = state.balances[providerId] || {};
state.balances[providerId][signer.address] = {
balance: cachedBalance + realAmountUsd,
// valid for 5 seconds (since it takes some time to reflect balance on-chain)
validUntil: Date.now() + 5000,
};
});
}
} catch (_e) {
// If we can't get real share price, continue without it
}

// Track Predict Action Completed (fire and forget)
this.trackPredictOrderEvent({
eventType: PredictEventType.COMPLETED,
amountUsd: realAmountUsd,
analyticsProperties,
providerId,
completionDuration,
sharePrice: realSharePrice,
});

return result as unknown as Result;
} catch (error) {
const completionDuration = performance.now() - startTime;
Expand All @@ -1229,6 +1217,7 @@ export class PredictController extends BaseController<
? error.message
: PREDICT_ERROR_CODES.PLACE_ORDER_FAILED;

// Track Predict Action Failed (fire and forget)
this.trackPredictOrderEvent({
eventType: PredictEventType.FAILED,
amountUsd,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,10 @@ export class PolymarketProvider implements PredictProvider {
if (error.includes(`order couldn't be fully filled`)) {
throw new Error(PREDICT_ERROR_CODES.ORDER_NOT_FULLY_FILLED);
}
if (error.includes(`not available in your region`)) {
if (
error.includes(`not available in your region`) ||
error.includes(`unable to access this provider`)
) {
throw new Error(PREDICT_ERROR_CODES.NOT_ELIGIBLE);
}
throw new Error(error ?? PREDICT_ERROR_CODES.PLACE_ORDER_FAILED);
Expand Down
Loading