-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
206 lines (180 loc) · 6.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from 'react';
import Button from 'components/Button';
import Input from 'components/Input';
import CustomScroll from 'react-custom-scroll';
import TronWeb from 'tronweb';
import { BigNumber } from 'bignumber.js';
import { connect } from 'react-redux';
import { PopupAPI } from '@tronlink/lib/api';
import {
FormattedMessage,
FormattedNumber
} from 'react-intl';
import './TokensPage.scss';
class TokensPage extends React.Component {
state = {
address: '',
isLoading: false,
isValid: false,
error: false
};
constructor(props) {
super();
this.onChange = this.onChange.bind(this);
this.addSmartToken = this.addSmartToken.bind(this);
}
async addSmartToken() {
const {
address,
isLoading,
isValid
} = this.state;
const { smart } = this.props.tokens;
if(isLoading || !isValid)
return;
this.setState({
isLoading: true,
error: false
});
const token = await PopupAPI.getSmartToken(address);
if(!token) {
return this.setState({
isLoading: false,
error: 'ERRORS.INVALID_TOKEN'
});
}
if(Object.values(smart).some(({ name, symbol }) => token.name === name || token.symbol === symbol)) {
return this.setState({
isLoading: false,
error: 'ERRORS.TOKEN_ADDED'
});
}
await PopupAPI.addSmartToken(address, token.name, token.symbol, token.decimals);
this.setState({
isLoading: false,
error: false,
isValid: false,
address: ''
});
}
onChange(address) {
address = address.trim();
const { smart } = this.props.tokens;
const error = Object.keys(smart).includes(address) ? 'ERRORS.TOKEN_ADDED' : false;
let isValid = !error && address.length === 34;
try {
isValid = isValid && TronWeb.isAddress(address);
} catch {}
this.setState({
address,
isValid,
error
});
}
renderTokens() {
const {
smart,
basic
} = this.props.tokens;
return (
<div className='tokenList'>
<CustomScroll heightRelativeToParent='100%'>
<FormattedMessage
id='TOKENS.SMART'
children={ value => (
<div className='tokenType'>
{ value }
</div>
)}
/>
<div className='tokenGroup smartTokens'>
{ Object.entries(smart).map(([ address, token ]) => {
const BN = BigNumber.clone({
DECIMAL_PLACES: token.decimals,
ROUNDING_MODE: Math.min(8, token.decimals)
});
const amount = new BN(token.balance)
.shiftedBy(-token.decimals)
.toString();
return (
<div className='token smartToken' key={ address }>
<FormattedNumber
value={ amount }
maximumFractionDigits={ token.decimals }
children={ amount => (
<span className='tokenAmount mono'>
{ amount }
</span>
)}
/>
<span className='tokenSymbol'>
{ token.symbol }
</span>
</div>
);
}) }
</div>
<FormattedMessage id='TOKENS.BASIC' children={ value => (
<div className='tokenType'>
{ value }
</div>
)}
/>
<div className='tokenGroup basicTokens'>
{ Object.entries(basic).map(([ tokenName, amount ]) => (
<div className='token basicToken' key={ tokenName }>
<FormattedNumber
value={ amount }
maximumFractionDigits={ 0 }
children={ amount => (
<span className='tokenAmount mono'>
{ amount }
</span>
)}
/>
<span className='tokenSymbol'>
{ tokenName }
</span>
</div>
)) }
</div>
</CustomScroll>
</div>
);
}
render() {
const {
address,
isLoading,
isValid,
error
} = this.state;
return (
<div className='tokensPage'>
<div className='inputGroup'>
<div className='tooltipContainer'>
<Input
placeholder='INPUT.NEW_TOKEN'
value={ address }
isDisabled={ isLoading }
onChange={ this.onChange }
onEnter={ this.addSmartToken }
tabIndex={ 1 }
/>
{ error ? <FormattedMessage id={ error } /> : '' }
</div>
<Button
id='BUTTON.ADD_TOKEN'
isLoading={ isLoading }
isValid={ isValid }
onClick={ this.addSmartToken }
/>
</div>
{ this.renderTokens() }
</div>
);
}
}
export default connect(state => ({
tokens: state.accounts.selected.tokens
}))(TokensPage);