Skip to content

Commit 9bea31b

Browse files
authored
Merge pull request #5586 from EdgeApp/matthew/get-token-details
Use `getTokenDetails` in `EditTokenScene`
2 parents 39bbb41 + a739dd6 commit 9bea31b

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- added: Add eCash.
66
- added: Thorchain TCY stake and unstake
77
- added: Add Fantom to Sonic bridge
8+
- added: Use `getTokenDetails` to auto-complete fields in `EditTokenScene`
89
- added: Toast notifications for PIN changes.
910
- added: Scam warning to camera access request modal
1011
- changed: Auto launch QR scanner for multi-out payments if previously used

src/components/scenes/EditTokenScene.tsx

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ function EditTokenSceneComponent(props: Props) {
7070
return out
7171
})
7272

73+
// Keep track of whether we auto-completed a token:
74+
const [didAutoCompleteToken, setDidAutoCompleteToken] = React.useState<boolean>(false)
75+
const isAutoCompleteTokenLoading = React.useRef<boolean>(false)
76+
7377
const handleDelete = useHandler(async () => {
7478
if (tokenId == null) return
7579
await Airship.show<'ok' | 'cancel' | undefined>(bridge => (
@@ -187,10 +191,72 @@ function EditTokenSceneComponent(props: Props) {
187191
}
188192
})
189193

194+
const autoCompleteToken = async (searchString: string) => {
195+
if (
196+
// Ignore autocomplete if it's already loading
197+
isAutoCompleteTokenLoading.current ||
198+
// and ggnore autocomplete if the scene was initialized with any of the token details prefilled,
199+
route.params.currencyCode != null ||
200+
route.params.displayName != null ||
201+
route.params.multiplier != null ||
202+
route.params.networkLocation != null
203+
) {
204+
return
205+
}
206+
207+
isAutoCompleteTokenLoading.current = true
208+
const [token] = await wallet.currencyConfig.getTokenDetails({ contractAddress: searchString }).catch(() => [])
209+
isAutoCompleteTokenLoading.current = false
210+
211+
if (token != null) {
212+
setCurrencyCode(token.currencyCode)
213+
setDisplayName(token.displayName)
214+
setDecimalPlaces((token.denominations[0].multiplier.length - 1).toString())
215+
setDidAutoCompleteToken(true)
216+
} else if (token == null && didAutoCompleteToken) {
217+
setCurrencyCode('')
218+
setDisplayName('')
219+
setDecimalPlaces('18')
220+
setDidAutoCompleteToken(false)
221+
}
222+
}
223+
224+
const renderCustomTokenTemplateRows = () => {
225+
return customTokenTemplate
226+
.sort((a, b) => (a.key === 'contractAddress' ? -1 : 1))
227+
.map(item => {
228+
if (item.type === 'nativeAmount') return null
229+
return (
230+
<FilledTextInput
231+
key={item.key}
232+
aroundRem={0.5}
233+
autoCapitalize="none"
234+
autoCorrect={false}
235+
autoFocus={false}
236+
placeholder={translateDescription(item.displayName)}
237+
keyboardType={item.type === 'number' ? 'numeric' : 'default'}
238+
value={location.get(item.key) ?? ''}
239+
onChangeText={value => {
240+
setLocation(location => {
241+
const out = new Map(location)
242+
out.set(item.key, value.replace(/\s/g, ''))
243+
return out
244+
})
245+
246+
if (item.key === 'contractAddress') {
247+
autoCompleteToken(value).catch(() => {})
248+
}
249+
}}
250+
/>
251+
)
252+
})
253+
}
254+
190255
return (
191256
<SceneWrapper avoidKeyboard>
192257
<SceneHeader title={tokenId == null ? lstrings.title_add_token : lstrings.title_edit_token} underline />
193258
<ScrollView style={styles.scroll} contentContainerStyle={styles.scrollContainer} scrollIndicatorInsets={SCROLL_INDICATOR_INSET_FIX}>
259+
{renderCustomTokenTemplateRows()}
194260
<FilledTextInput
195261
aroundRem={0.5}
196262
autoCapitalize="characters"
@@ -209,28 +275,6 @@ function EditTokenSceneComponent(props: Props) {
209275
value={displayName}
210276
onChangeText={setDisplayName}
211277
/>
212-
{customTokenTemplate.map(item => {
213-
if (item.type === 'nativeAmount') return null
214-
return (
215-
<FilledTextInput
216-
key={item.key}
217-
aroundRem={0.5}
218-
autoCapitalize="none"
219-
autoCorrect={false}
220-
autoFocus={false}
221-
placeholder={translateDescription(item.displayName)}
222-
keyboardType={item.type === 'number' ? 'numeric' : 'default'}
223-
value={location.get(item.key) ?? ''}
224-
onChangeText={value =>
225-
setLocation(location => {
226-
const out = new Map(location)
227-
out.set(item.key, value.replace(/\s/g, ''))
228-
return out
229-
})
230-
}
231-
/>
232-
)
233-
})}
234278
<FilledTextInput
235279
aroundRem={0.5}
236280
autoCorrect={false}

0 commit comments

Comments
 (0)