From ed292b3a0ddf4570fc678a0a54543b1c5976540e Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 19 Feb 2026 17:12:57 +0000 Subject: [PATCH] fix: improve contract simulation accuracy - Update supported contract types: TransferContract, CreateSmartContract, TriggerSmartContract - Add 'simulationAccurate' flag to TransactionScanResult - For unsupported transactions: return empty estimated changes instead of blocking - Only block confirm button for accurate simulations with errors - Update tests to reflect new supported contract types Co-authored-by: ulisses.ferreira --- packages/snap/CHANGELOG.md | 5 ++- packages/snap/snap.manifest.json | 2 +- .../clients/security-alerts-api/utils.test.ts | 39 +------------------ .../src/clients/security-alerts-api/utils.ts | 2 +- .../TransactionScanService.ts | 18 +++++++++ .../src/services/transaction-scan/types.ts | 1 + .../ConfirmSignTransaction.tsx | 3 +- .../ConfirmSignTransaction/render.test.tsx | 1 + 8 files changed, 29 insertions(+), 42 deletions(-) diff --git a/packages/snap/CHANGELOG.md b/packages/snap/CHANGELOG.md index 509bc779..6878fe2f 100644 --- a/packages/snap/CHANGELOG.md +++ b/packages/snap/CHANGELOG.md @@ -9,8 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Disable simulation for unsupported transactions ([#186](https://github.com/MetaMask/snap-tron-wallet/pull/186)) - - Supported transactions are those single-contract interaction transactions of the following types: `TransferContract`, `TriggerSmartContract`, `TransferAssetContract`. +- Improve simulation accuracy for contract transactions ([#186](https://github.com/MetaMask/snap-tron-wallet/pull/186)) + - Supported transactions are those single-contract interaction transactions of the following types: `TransferContract`, `CreateSmartContract`, `TriggerSmartContract`. + - Unsupported transactions will show empty estimated changes and allow the user to proceed without blocking the confirmation. ## [1.20.0] diff --git a/packages/snap/snap.manifest.json b/packages/snap/snap.manifest.json index e7adf451..f049406c 100644 --- a/packages/snap/snap.manifest.json +++ b/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snap-tron-wallet.git" }, "source": { - "shasum": "Izxk79sRDjnS3yUNx8TV9c9D7azOJxTwVSchsbEToXU=", + "shasum": "j+g3RqXwctphEFRvdLZ5BnLpW02TivcjJ8FnVegxOm8=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/snap/src/clients/security-alerts-api/utils.test.ts b/packages/snap/src/clients/security-alerts-api/utils.test.ts index 2aba7f1e..72082e51 100644 --- a/packages/snap/src/clients/security-alerts-api/utils.test.ts +++ b/packages/snap/src/clients/security-alerts-api/utils.test.ts @@ -6,46 +6,10 @@ import { isTransactionSupported, SUPPORTED_CONTRACT_TYPES, } from './utils'; -import type { - TransferAssetContractParameter, - TransferContractParameter, -} from '../trongrid/types'; +import type { TransferContractParameter } from '../trongrid/types'; describe('SecurityAlertsApiClient utils', () => { describe('extractScanParametersFromTransactionData', () => { - it('extracts scan parameters from a TransferAssetContractParameter', () => { - const contractInteraction: TransferAssetContractParameter = { - type_url: 'type.googleapis.com/protocol.TransferAssetContract', - value: { - asset_name: 'MyToken', - owner_address: '41a614f803b6fd780986a42c78ec9c7f77e6ded13c', - to_address: '4191bba2f3f6e1c4d5c8e8f5b6a7c8d9e0f1a2b3c4', - amount: 1000000, - }, - }; - const rawData: Types.Transaction['raw_data'] = { - contract: [ - { - type: Types.ContractType.TransferAssetContract, - parameter: contractInteraction, - }, - ], - ref_block_bytes: '', - ref_block_hash: '', - expiration: 0, - timestamp: 0, - }; - - const result = extractScanParametersFromTransactionData(rawData); - - expect(result).toStrictEqual({ - from: 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t', - to: 'TPFmm695uHPTn8wNmQbF8yMiZxfCeUdXkJ', - data: null, - value: 1000000, - }); - }); - it('extracts scan parameters from a TransferContractParameter', () => { const contractInteraction: TransferContractParameter = { type_url: 'type.googleapis.com/protocol.TransferContract', @@ -179,6 +143,7 @@ describe('SecurityAlertsApiClient utils', () => { Types.ContractType.ExchangeInjectContract, Types.ContractType.ExchangeWithdrawContract, Types.ContractType.ExchangeTransactionContract, + Types.ContractType.TransferAssetContract, ])( 'returns false for transactions with unsupported contract types', (contractType) => { diff --git a/packages/snap/src/clients/security-alerts-api/utils.ts b/packages/snap/src/clients/security-alerts-api/utils.ts index 94f4163d..dd26c96b 100644 --- a/packages/snap/src/clients/security-alerts-api/utils.ts +++ b/packages/snap/src/clients/security-alerts-api/utils.ts @@ -5,7 +5,7 @@ import type { SecurityScanPayload } from './types'; export const SUPPORTED_CONTRACT_TYPES: Types.ContractType[] = [ Types.ContractType.TransferContract, - Types.ContractType.TransferAssetContract, + Types.ContractType.CreateSmartContract, Types.ContractType.TriggerSmartContract, ]; diff --git a/packages/snap/src/services/transaction-scan/TransactionScanService.ts b/packages/snap/src/services/transaction-scan/TransactionScanService.ts index 153871d7..8bba7079 100644 --- a/packages/snap/src/services/transaction-scan/TransactionScanService.ts +++ b/packages/snap/src/services/transaction-scan/TransactionScanService.ts @@ -5,6 +5,7 @@ import type { TransactionScanResult, TransactionScanValidation } from './types'; import { ScanStatus, SecurityAlertResponse } from './types'; import type { SecurityAlertsApiClient } from '../../clients/security-alerts-api/SecurityAlertsApiClient'; import type { SecurityAlertSimulationValidationResponse } from '../../clients/security-alerts-api/types'; +import { isTransactionSupported } from '../../clients/security-alerts-api/utils'; import type { SnapClient } from '../../clients/snap/SnapClient'; import type { Network } from '../../constants'; import type { TronKeyringAccount } from '../../entities'; @@ -57,6 +58,22 @@ export class TransactionScanService { options?: string[] | undefined; account?: TronKeyringAccount; }): Promise { + const simulationAccurate = isTransactionSupported(transactionRawData); + + if (!simulationAccurate) { + this.#logger.info( + 'Transaction is not supported for scanning, returning inaccurate simulation result', + ); + + return { + status: 'SUCCESS', + estimatedChanges: { assets: [] }, + validation: { type: null, reason: null }, + error: null, + simulationAccurate: false, + }; + } + try { const result = await this.#securityAlertsApiClient.scanTransaction({ accountAddress, @@ -239,6 +256,7 @@ export class TransactionScanService { message: result.simulation?.error ?? null, } : null, + simulationAccurate: true, }; } } diff --git a/packages/snap/src/services/transaction-scan/types.ts b/packages/snap/src/services/transaction-scan/types.ts index 313ac10e..f7a4bb92 100644 --- a/packages/snap/src/services/transaction-scan/types.ts +++ b/packages/snap/src/services/transaction-scan/types.ts @@ -36,6 +36,7 @@ export type TransactionScanResult = { estimatedChanges: TransactionScanEstimatedChanges; validation: TransactionScanValidation; error: TransactionScanError | null; + simulationAccurate: boolean; }; export enum SecurityAlertResponse { diff --git a/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/ConfirmSignTransaction.tsx b/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/ConfirmSignTransaction.tsx index 37da072d..47f25539 100644 --- a/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/ConfirmSignTransaction.tsx +++ b/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/ConfirmSignTransaction.tsx @@ -43,7 +43,8 @@ export const ConfirmSignTransaction = ({ } = context; const shouldDisableConfirmButton = - scanFetchStatus === 'fetching' || scan?.status === 'ERROR'; + scanFetchStatus === 'fetching' || + (scan?.status === 'ERROR' && scan?.simulationAccurate); const addressCaip10 = account ? `${scope}:${account.address}` : null; diff --git a/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/render.test.tsx b/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/render.test.tsx index 85b0968e..5a7eaa80 100644 --- a/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/render.test.tsx +++ b/packages/snap/src/ui/confirmation/views/ConfirmSignTransaction/render.test.tsx @@ -79,6 +79,7 @@ describe('ConfirmSignTransaction render', () => { reason: null, }, error: null, + simulationAccurate: true, }; let mockSnapClient: jest.Mocked;