-
Notifications
You must be signed in to change notification settings - Fork 5k
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
fix: Change visibility of AmountRow
in contract interaction
#29131
Merged
OGPoyraz
merged 7 commits into
main
from
3783-changes-to-when-we-display-the-amount-row
Dec 12, 2024
+300
−21
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ec7a44
Fix visibility of AmountRow in contract interaction
OGPoyraz 84fbd8b
Fix lint
OGPoyraz c6e332c
Remove unnecessary export
OGPoyraz 81347ef
Merge branch 'main' into 3783-changes-to-when-we-display-the-amount-row
OGPoyraz 9a6e27a
Add more human readable test values
OGPoyraz 7b160db
Fix lint
OGPoyraz fcc975e
Add utility test
OGPoyraz 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 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 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 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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { TransactionMeta } from '@metamask/transaction-controller'; | ||
import type { Hex } from '@metamask/utils'; | ||
import { remove0x } from '@metamask/utils'; | ||
import { BN } from 'bn.js'; | ||
import { DecodedTransactionDataResponse } from '../../../../../../shared/types/transaction-decode'; | ||
import { | ||
BackgroundColor, | ||
TextColor, | ||
} from '../../../../../helpers/constants/design-system'; | ||
|
||
const VALUE_COMPARISON_PERCENT_THRESHOLD = 5; | ||
|
||
export function getIsRevokeSetApprovalForAll( | ||
value: DecodedTransactionDataResponse | undefined, | ||
): boolean { | ||
|
@@ -27,3 +33,80 @@ export const getAmountColors = (credit?: boolean, debit?: boolean) => { | |
} | ||
return { color, backgroundColor }; | ||
}; | ||
|
||
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. Perhaps it would be good to add unit tests to these util methods |
||
/** | ||
* Calculate the absolute percentage change between two values. | ||
* | ||
* @param originalValue - The first value. | ||
* @param newValue - The second value. | ||
* @returns The percentage change from the first value to the second value. | ||
* If the original value is zero and the new value is not, returns 100. | ||
*/ | ||
export function getPercentageChange( | ||
originalValue: InstanceType<typeof BN>, | ||
newValue: InstanceType<typeof BN>, | ||
): number { | ||
const precisionFactor = new BN(10).pow(new BN(18)); | ||
const originalValuePrecision = originalValue.mul(precisionFactor); | ||
const newValuePrecision = newValue.mul(precisionFactor); | ||
|
||
const difference = newValuePrecision.sub(originalValuePrecision); | ||
|
||
if (difference.isZero()) { | ||
return 0; | ||
} | ||
|
||
if (originalValuePrecision.isZero() && !newValuePrecision.isZero()) { | ||
return 100; | ||
} | ||
|
||
return difference.muln(100).div(originalValuePrecision).abs().toNumber(); | ||
} | ||
|
||
/** | ||
* Determine if the percentage change between two values is within a threshold. | ||
* | ||
* @param originalValue - The original value. | ||
* @param newValue - The new value. | ||
* @param newNegative - Whether the new value is negative. | ||
* @returns Whether the percentage change between the two values is within a threshold. | ||
*/ | ||
function percentageChangeWithinThreshold( | ||
originalValue: Hex, | ||
newValue: Hex, | ||
newNegative?: boolean, | ||
): boolean { | ||
const originalValueBN = new BN(remove0x(originalValue), 'hex'); | ||
let newValueBN = new BN(remove0x(newValue), 'hex'); | ||
|
||
if (newNegative) { | ||
newValueBN = newValueBN.neg(); | ||
} | ||
|
||
return ( | ||
getPercentageChange(originalValueBN, newValueBN) <= | ||
VALUE_COMPARISON_PERCENT_THRESHOLD | ||
); | ||
} | ||
|
||
/** | ||
* Determine if a transaction has a value and simulation native balance mismatch. | ||
* | ||
* @param transactionMeta - The transaction metadata. | ||
* @returns Whether the transaction has a value and simulation native balance mismatch. | ||
*/ | ||
export function hasValueAndNativeBalanceMismatch( | ||
transactionMeta: TransactionMeta, | ||
): boolean { | ||
const value = transactionMeta?.txParams?.value ?? '0x0'; | ||
const nativeBalanceChange = | ||
transactionMeta?.simulationData?.nativeBalanceChange; | ||
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. It will be nice to test cover util methods also |
||
const simulatedNativeBalanceDifference = | ||
nativeBalanceChange?.difference ?? '0x0'; | ||
|
||
return !percentageChangeWithinThreshold( | ||
value as Hex, | ||
simulatedNativeBalanceDifference, | ||
nativeBalanceChange?.isDecrease === false, | ||
); | ||
} |
Oops, something went wrong.
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.
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.
Can you use decimal test number constants and convert them to hex using a util, so the numbers are human readable?
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.
Make sense, done in 9a6e27a