-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/MetaMask/metamask-mobile in…
…to lightdark/actionbuttons
- Loading branch information
Showing
4 changed files
with
215 additions
and
36 deletions.
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
111 changes: 111 additions & 0 deletions
111
app/components/Views/confirmations/SendFlow/Confirm/validation.test.ts
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { BN } from 'ethereumjs-util'; | ||
import { validateTokenTransaction } from './validation'; | ||
import { renderFromWei, hexToBN } from '../../../../../util/number'; | ||
import { | ||
getTicker, | ||
decodeTransferData, | ||
} from '../../../../../util/transactions'; | ||
import { strings } from '../../../../../../locales/i18n'; | ||
|
||
jest.mock('../../../../../util/number', () => ({ | ||
renderFromWei: jest.fn(), | ||
hexToBN: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../../util/transactions', () => ({ | ||
getTicker: jest.fn(), | ||
decodeTransferData: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../../../locales/i18n', () => ({ | ||
strings: jest.fn(), | ||
})); | ||
|
||
describe('validateTokenTransaction', () => { | ||
it('return an error message if weiBalance is less than totalTransactionValue', () => { | ||
const transaction = { data: '0x' }; | ||
const weiBalance = new BN('1000'); | ||
const totalTransactionValue = new BN('2000'); | ||
const contractBalances = { '0x123': '1000' }; | ||
const selectedAsset = { address: '0x123', decimals: '18', symbol: 'TOKEN' }; | ||
const ticker = 'TOKEN'; | ||
|
||
(renderFromWei as jest.Mock).mockReturnValue('1'); | ||
(getTicker as jest.Mock).mockReturnValue('TOKEN'); | ||
(strings as jest.Mock).mockReturnValue('Insufficient amount'); | ||
|
||
const result = validateTokenTransaction( | ||
transaction, | ||
weiBalance, | ||
totalTransactionValue, | ||
contractBalances, | ||
selectedAsset, | ||
ticker, | ||
); | ||
|
||
expect(result).toBe('Insufficient amount'); | ||
expect(renderFromWei).toHaveBeenCalledWith( | ||
totalTransactionValue.sub(weiBalance), | ||
); | ||
expect(getTicker).toHaveBeenCalledWith(ticker); | ||
expect(strings).toHaveBeenCalledWith('transaction.insufficient_amount', { | ||
amount: '1', | ||
tokenSymbol: 'TOKEN', | ||
}); | ||
}); | ||
|
||
it('return an error message if tokenBalance is less than weiInput', () => { | ||
const transaction = { data: '0x' }; | ||
const weiBalance = new BN('2000'); | ||
const totalTransactionValue = new BN('1000'); | ||
const contractBalances = { '0x123': '1000' }; | ||
const selectedAsset = { address: '0x123', decimals: '18', symbol: 'TOKEN' }; | ||
const ticker = 'TOKEN'; | ||
|
||
(decodeTransferData as jest.Mock).mockReturnValue([null, null, '5000']); | ||
(hexToBN as jest.Mock).mockImplementation((value) => new BN(value)); | ||
(strings as jest.Mock).mockReturnValue('Insufficient tokens'); | ||
|
||
const result = validateTokenTransaction( | ||
transaction, | ||
weiBalance, | ||
totalTransactionValue, | ||
contractBalances, | ||
selectedAsset, | ||
ticker, | ||
); | ||
|
||
expect(result).toBe('Insufficient tokens'); | ||
expect(decodeTransferData).toHaveBeenCalledWith( | ||
'transfer', | ||
transaction.data, | ||
); | ||
expect(hexToBN).toHaveBeenCalledWith('5000'); | ||
expect(strings).toHaveBeenCalledWith('transaction.insufficient_tokens', { | ||
token: 'TOKEN', | ||
}); | ||
}); | ||
|
||
it('return undefined if weiBalance and tokenBalance are sufficient', () => { | ||
const transaction = { data: '0x' }; | ||
const weiBalance = new BN('2000'); | ||
const totalTransactionValue = new BN('1000'); | ||
const contractBalances = { '0x123': '5000' }; | ||
const selectedAsset = { address: '0x123', decimals: '18', symbol: 'TOKEN' }; | ||
const ticker = 'TOKEN'; | ||
|
||
(decodeTransferData as jest.Mock).mockReturnValue([null, null, '1000']); | ||
(hexToBN as jest.Mock).mockImplementation((value) => new BN(value)); | ||
|
||
const result = validateTokenTransaction( | ||
transaction, | ||
weiBalance, | ||
totalTransactionValue, | ||
contractBalances, | ||
selectedAsset, | ||
ticker, | ||
); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
60 changes: 60 additions & 0 deletions
60
app/components/Views/confirmations/SendFlow/Confirm/validation.ts
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { renderFromWei, hexToBN } from '../../../../../util/number'; | ||
import { | ||
getTicker, | ||
decodeTransferData, | ||
} from '../../../../../util/transactions'; | ||
import { strings } from '../../../../../../locales/i18n'; | ||
import { BN } from 'ethereumjs-util'; | ||
|
||
interface SelectedAsset { | ||
address: string; | ||
decimals: string; | ||
symbol: string; | ||
} | ||
|
||
export const generateInsufficientBalanceMessage = ( | ||
weiBalance: BN, | ||
transactionValue: BN, | ||
ticker: string, | ||
) => { | ||
const amount = renderFromWei(transactionValue.sub(weiBalance)); | ||
const tokenSymbol = getTicker(ticker); | ||
return strings('transaction.insufficient_amount', { | ||
amount, | ||
tokenSymbol, | ||
}); | ||
}; | ||
|
||
export const validateBalance = (weiBalance: BN, transactionValue: BN) => | ||
!weiBalance.gte(transactionValue); | ||
|
||
export const validateTokenTransaction = ( | ||
transaction: { | ||
data: string; | ||
}, | ||
weiBalance: BN, | ||
totalTransactionValue: BN, | ||
contractBalances: { [key: string]: string }, | ||
selectedAsset: SelectedAsset, | ||
ticker: string, | ||
) => { | ||
if (validateBalance(weiBalance, totalTransactionValue)) { | ||
return generateInsufficientBalanceMessage( | ||
weiBalance, | ||
totalTransactionValue, | ||
ticker, | ||
); | ||
} | ||
|
||
const [, , amount] = decodeTransferData('transfer', transaction.data); | ||
const tokenBalance = hexToBN(contractBalances[selectedAsset.address]); | ||
const weiInput = hexToBN(amount); | ||
|
||
if (!tokenBalance.gte(weiInput)) { | ||
return strings('transaction.insufficient_tokens', { | ||
token: selectedAsset.symbol, | ||
}); | ||
} | ||
|
||
return undefined; | ||
}; |