mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-14 21:14:23 +00:00
Do not allow custom tokens to be added with the same address (#1770)
* Do not allow custom tokens to be added with the same address * prettify
This commit is contained in:
parent
0e7a2ef2e8
commit
039aab09a2
@ -16,8 +16,13 @@ interface IGenerateSymbolLookup {
|
|||||||
[tokenSymbol: string]: boolean;
|
[tokenSymbol: string]: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface IGenerateAddressLookup {
|
||||||
|
[address: string]: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
interface State {
|
interface State {
|
||||||
tokenSymbolLookup: IGenerateSymbolLookup;
|
tokenSymbolLookup: IGenerateSymbolLookup;
|
||||||
|
tokenAddressLookup: IGenerateAddressLookup;
|
||||||
address: string;
|
address: string;
|
||||||
symbol: string;
|
symbol: string;
|
||||||
decimal: string;
|
decimal: string;
|
||||||
@ -25,20 +30,13 @@ interface State {
|
|||||||
|
|
||||||
export default class AddCustomTokenForm extends React.PureComponent<Props, State> {
|
export default class AddCustomTokenForm extends React.PureComponent<Props, State> {
|
||||||
public state: State = {
|
public state: State = {
|
||||||
tokenSymbolLookup: {},
|
tokenSymbolLookup: this.generateSymbolLookup(),
|
||||||
|
tokenAddressLookup: this.generateAddressMap(),
|
||||||
address: '',
|
address: '',
|
||||||
symbol: '',
|
symbol: '',
|
||||||
decimal: ''
|
decimal: ''
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(props: Props) {
|
|
||||||
super(props);
|
|
||||||
this.state = {
|
|
||||||
...this.state,
|
|
||||||
tokenSymbolLookup: this.generateSymbolLookup(props.allTokens)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public render() {
|
public render() {
|
||||||
const { address, symbol, decimal } = this.state;
|
const { address, symbol, decimal } = this.state;
|
||||||
const errors = this.getErrors();
|
const errors = this.getErrors();
|
||||||
@ -76,7 +74,7 @@ export default class AddCustomTokenForm extends React.PureComponent<Props, State
|
|||||||
value={field.value}
|
value={field.value}
|
||||||
onChange={this.onFieldChange}
|
onChange={this.onFieldChange}
|
||||||
/>
|
/>
|
||||||
{typeof errors[field.name] === 'string' && (
|
{errors[field.name] && (
|
||||||
<div className="AddCustom-field-error">{errors[field.name]}</div>
|
<div className="AddCustom-field-error">{errors[field.name]}</div>
|
||||||
)}
|
)}
|
||||||
</label>
|
</label>
|
||||||
@ -106,14 +104,19 @@ export default class AddCustomTokenForm extends React.PureComponent<Props, State
|
|||||||
|
|
||||||
public getErrors() {
|
public getErrors() {
|
||||||
const { address, symbol, decimal } = this.state;
|
const { address, symbol, decimal } = this.state;
|
||||||
const errors: { [key: string]: boolean | string } = {};
|
const errors: { [key: string]: string } = {};
|
||||||
|
|
||||||
// Formatting errors
|
// Formatting errors
|
||||||
if (decimal && !isPositiveIntegerOrZero(Number(decimal))) {
|
if (decimal && !isPositiveIntegerOrZero(Number(decimal))) {
|
||||||
errors.decimal = true;
|
errors.decimal = 'Invalid decimal';
|
||||||
}
|
}
|
||||||
if (address && !isValidETHAddress(address)) {
|
if (address) {
|
||||||
errors.address = true;
|
if (!isValidETHAddress(address)) {
|
||||||
|
errors.address = 'Not a valid address';
|
||||||
|
}
|
||||||
|
if (this.state.tokenAddressLookup[address]) {
|
||||||
|
errors.address = 'A token with this address already exists';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message errors
|
// Message errors
|
||||||
@ -146,13 +149,19 @@ export default class AddCustomTokenForm extends React.PureComponent<Props, State
|
|||||||
this.props.onSave({ address, symbol, decimal: parseInt(decimal, 10) });
|
this.props.onSave({ address, symbol, decimal: parseInt(decimal, 10) });
|
||||||
};
|
};
|
||||||
|
|
||||||
private generateSymbolLookup(tokens: Token[]) {
|
private generateSymbolLookup() {
|
||||||
return tokens.reduce(
|
return this.tknArrToMap('symbol');
|
||||||
(prev, tk) => {
|
}
|
||||||
prev[tk.symbol] = true;
|
|
||||||
return prev;
|
private generateAddressMap() {
|
||||||
},
|
return this.tknArrToMap('address');
|
||||||
{} as IGenerateSymbolLookup
|
}
|
||||||
);
|
|
||||||
|
private tknArrToMap(key: Exclude<keyof Token, 'error'>) {
|
||||||
|
const tokens = this.props.allTokens;
|
||||||
|
return tokens.reduce<{ [k: string]: boolean }>((prev, tk) => {
|
||||||
|
prev[tk[key]] = true;
|
||||||
|
return prev;
|
||||||
|
}, {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user