fix: custom token validation for non-token addresses

When contract addresses that are not ERC-20 or ERC-721 were input, the token would be allowed to be added and would crash the app.

In addition, when an ERC-20 contract was deployed without a name and symbol, “Invalid ERC-20 address” would appear.

This PR adds error checking from the token detail lookup and reports the error back to the user in the modal. This prevents non-ERC-20/721 contracts from being able to be added to the app and prevents a crash.
This commit is contained in:
Eric Mastro 2021-03-25 17:19:05 +11:00 committed by Iuri Matias
parent 0abaa61067
commit 2336aa4e6f
3 changed files with 27 additions and 16 deletions

View File

@ -1,5 +1,5 @@
import # nim libs
tables, json
strformat, tables, json
import # vendor libs
NimQml
@ -21,18 +21,24 @@ type
address: string
const getTokenDetailsTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[GetTokenDetailsTaskArg](argEncoded)
try:
let
arg = decode[GetTokenDetailsTaskArg](argEncoded)
tkn = newErc20Contract(getCurrentNetwork(), arg.address.parseAddress)
decimals = tkn.tokenDecimals()
let output = %* {
output = %* {
"address": arg.address,
"name": tkn.tokenName(),
"symbol": tkn.tokenSymbol(),
"decimals": (if decimals == 0: "" else: $decimals)
}
arg.finish(output)
except Exception as e:
let output = %* {
"address": arg.address,
"error": fmt"{e.msg}. Is this an ERC-20 or ERC-721 contract?",
}
arg.finish(output)
proc getTokenDetails[T](self: T, slot: string, address: string) =
let arg = GetTokenDetailsTaskArg(

View File

@ -67,7 +67,11 @@ ModalPopup {
target: walletModel.customTokenList
onTokenDetailsWereResolved: {
const jsonObj = JSON.parse(tokenDetails)
if(jsonObj.name === "" || jsonObj.symbol === "" || jsonObj.decimals === ""){
if (jsonObj.error) {
validationError = jsonObj.error
return
}
if (jsonObj.name === "" && jsonObj.symbol === "" && jsonObj.decimals === "") {
//% "Invalid ERC20 address"
validationError = qsTrId("invalid-erc20-address")
return;

View File

@ -179,7 +179,8 @@ Item {
font.pixelSize: 12
height: 16
color: Style.current.danger
width: parent.width
width: inputRectangle.width
wrapMode: TextEdit.Wrap
}
}