mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
CW-1103:Token Validation Issues (#2327)
* feat(token_validation): Improve flow for adding new tokens across wallets This change: - Implements check to see if a token is already added, preventing duplicates - Triggers dialog warning if its a duplicate token - Takes EVM Chains contract adddress case-insensitivity when making checks for potential scams. * refactor(token_validation): Modify token management flow This change: - Removes duplicate token check during token addition in EVMChainWalletBase. - Introduces a flag to indicate if a token is being edited - Adjusts token addition validation to bypass checks when editing an existing token. * Update lib/src/screens/dashboard/edit_token_page.dart --------- Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
parent
4434ad7363
commit
af89603b81
37 changed files with 127 additions and 5 deletions
|
@ -199,8 +199,16 @@ class CWEthereum extends Ethereum {
|
|||
}
|
||||
|
||||
@override
|
||||
List<String> getDefaultTokenContractAddresses() =>
|
||||
DefaultEthereumErc20Tokens().initialErc20Tokens.map((e) => e.contractAddress).toList();
|
||||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultEthereumErc20Tokens().initialErc20Tokens.map((e) => e.contractAddress).toList();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final ethereumWallet = wallet as EthereumWallet;
|
||||
return ethereumWallet.erc20Currencies.any((element) => element.contractAddress.toLowerCase() == contractAddress.toLowerCase());
|
||||
}
|
||||
|
||||
Future<PendingTransaction> createTokenApproval(WalletBase wallet, BigInt amount, String spender,
|
||||
CryptoCurrency token, TransactionPriority priority) =>
|
||||
|
|
|
@ -216,4 +216,10 @@ class CWPolygon extends Polygon {
|
|||
.initialPolygonErc20Tokens
|
||||
.map((e) => e.contractAddress)
|
||||
.toList();
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final polygonWallet = wallet as PolygonWallet;
|
||||
return polygonWallet.erc20Currencies.any((element) => element.contractAddress.toLowerCase() == contractAddress.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -155,4 +155,10 @@ class CWSolana extends Solana {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultSPLTokens().initialSPLTokens.map((e) => e.mintAddress).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final solanaWallet = wallet as SolanaWallet;
|
||||
return solanaWallet.splTokenCurrencies.any((element) => element.mintAddress == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -73,6 +73,7 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
|||
|
||||
bool _showDisclaimer = false;
|
||||
bool _disclaimerChecked = false;
|
||||
bool isEditingToken = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
|
@ -88,6 +89,8 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
|||
_tokenSymbolController.text = widget.token!.title;
|
||||
_tokenDecimalController.text = widget.token!.decimals.toString();
|
||||
_tokenIconPathController.text = widget.token?.iconPath ?? '';
|
||||
|
||||
isEditingToken = true;
|
||||
}
|
||||
|
||||
if (widget.initialContractAddress != null) {
|
||||
|
@ -200,6 +203,25 @@ class _EditTokenPageBodyState extends State<EditTokenPageBody> {
|
|||
onPressed: () async {
|
||||
if (_formKey.currentState!.validate() &&
|
||||
(!_showDisclaimer || _disclaimerChecked)) {
|
||||
final isTokenAlreadyAdded = isEditingToken
|
||||
? false
|
||||
: await widget.homeSettingsViewModel
|
||||
.checkIfTokenIsAlreadyAdded(_contractAddressController.text);
|
||||
if (isTokenAlreadyAdded) {
|
||||
showPopUp<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertWithOneAction(
|
||||
alertTitle: S.current.warning,
|
||||
alertContent: S.of(context).token_already_exists,
|
||||
buttonText: S.of(context).ok,
|
||||
buttonAction: () => Navigator.of(dialogContext).pop(),
|
||||
);
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final isWhitelisted = await widget.homeSettingsViewModel
|
||||
.checkIfTokenIsWhitelisted(_contractAddressController.text);
|
||||
|
||||
|
|
|
@ -138,4 +138,10 @@ class CWTron extends Tron {
|
|||
List<String> getDefaultTokenContractAddresses() {
|
||||
return DefaultTronTokens().initialTronTokens.map((e) => e.contractAddress).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final tronWallet = wallet as TronWallet;
|
||||
return tronWallet.tronTokenCurrencies.any((element) => element.contractAddress == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,6 +126,31 @@ abstract class HomeSettingsViewModelBase with Store {
|
|||
}
|
||||
}
|
||||
|
||||
@action
|
||||
bool checkIfTokenIsAlreadyAdded(String contractAddress) {
|
||||
if (_balanceViewModel.wallet.type == WalletType.ethereum) {
|
||||
return ethereum!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.polygon) {
|
||||
return polygon!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.solana) {
|
||||
return solana!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.tron) {
|
||||
return tron!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
if (_balanceViewModel.wallet.type == WalletType.zano) {
|
||||
return zano!.isTokenAlreadyAdded(_balanceViewModel.wallet, contractAddress);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@action
|
||||
Future<void> deleteToken(CryptoCurrency token) async {
|
||||
try {
|
||||
|
@ -297,7 +322,7 @@ abstract class HomeSettingsViewModelBase with Store {
|
|||
required bool isEthereum,
|
||||
}) async {
|
||||
final uri = Uri.https(
|
||||
"api.etherscan.io",
|
||||
"api.etherscan.io",
|
||||
"/v2/api",
|
||||
{
|
||||
"chainid": isEthereum ? "1" : "137",
|
||||
|
|
|
@ -136,4 +136,10 @@ class CWZano extends Zano {
|
|||
Map<String, List<int>> debugCallLength() {
|
||||
return api.debugCallLength();
|
||||
}
|
||||
|
||||
@override
|
||||
bool isTokenAlreadyAdded(WalletBase wallet, String contractAddress) {
|
||||
final zanoWallet = wallet as ZanoWallet;
|
||||
return zanoWallet.zanoAssets.values.any((element) => element?.assetId == contractAddress);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue