add ERC20 token display

This commit is contained in:
Michele Balistreri 2019-11-25 18:49:05 +03:00
parent c226ffa741
commit 3cfd782336
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
3 changed files with 40 additions and 5 deletions

View File

@ -2,4 +2,4 @@ package im.status.keycard.connect.data
import java.math.BigInteger
fun BigInteger.toTransferredAmount(decimals : Int = 18) : String = this.toBigDecimal().movePointLeft(decimals).toPlainString().trimEnd('0')
fun BigInteger.toTransferredAmount(decimals : Int = 18): String = this.toBigDecimal().movePointLeft(decimals).toPlainString().trimEnd('0').trimEnd('.')

View File

@ -1,13 +1,29 @@
package im.status.keycard.connect.net
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import okhttp3.OkHttpClient
import okhttp3.Request
import org.kethereum.extensions.maybeHexToBigInteger
import org.kethereum.rpc.HttpEthereumRPC
import org.kethereum.rpc.model.StringResultResponse
import java.io.IOException
import java.lang.Exception
import java.lang.reflect.Type
import java.math.BigInteger
class EthereumRPC(endpointURL: String) {
private var endpoint = HttpEthereumRPC(endpointURL)
private val ethplorerClient = OkHttpClient().newBuilder().build()
private val ethplorerJSONAdapter: JsonAdapter<Map<String, Any>>
init {
val moshi = Moshi.Builder().build()
val type: Type = Types.newParameterizedType(Map::class.java, String::class.java, Any::class.java)
ethplorerJSONAdapter = moshi.adapter<Map<String, Any>>(type)
}
fun changeEndpoint(endpointURL: String) {
endpoint = HttpEthereumRPC(endpointURL)
@ -32,4 +48,15 @@ class EthereumRPC(endpointURL: String) {
fun ethSendRawTransaction(rawTx: String): String {
return valueOrThrow(endpoint.sendRawTransaction(rawTx)) { it }
}
fun ethplorerGetTokenInfo(address: String): Map<String, Any>? {
//TODO: add a personalized API key
try {
val request = Request.Builder().url("https://api.ethplorer.io/getTokenInfo/${address}?apiKey=freekey").build()
val response = ethplorerClient.newCall(request).execute().body.use { it?.string() } ?: return null
return ethplorerJSONAdapter.fromJson(response)
} catch(e: Exception) {
return null
}
}
}

View File

@ -158,10 +158,18 @@ class WalletConnect(var bip32Path: String, var chainID: Long) : ExportKeyCommand
val intent = Intent(Registry.mainActivity, SignTransactionActivity::class.java).apply {
if (tx.isTokenTransfer()) {
//TODO: use currency name and decimal places to show amount
putExtra(SIGN_TX_AMOUNT, tx.getTokenTransferValue().toString(10))
putExtra(SIGN_TX_CURRENCY, tx.to?.hex)
putExtra(SIGN_TX_TO, tx.getTokenTransferTo().hex)
val tokenInfo = Registry.ethereumRPC.ethplorerGetTokenInfo(tx.to!!.hex)
if (tokenInfo != null) {
val decimals = (tokenInfo["decimals"] as? String)?.toInt() ?: 1
putExtra(SIGN_TX_AMOUNT, tx.getTokenTransferValue().toTransferredAmount(decimals))
putExtra(SIGN_TX_CURRENCY, "${tokenInfo["name"]} (${tokenInfo["symbol"]})" )
putExtra(SIGN_TX_TO, tx.getTokenTransferTo().hex)
} else {
putExtra(SIGN_TX_AMOUNT, tx.getTokenTransferValue().toTransferredAmount(1))
putExtra(SIGN_TX_CURRENCY, tx.to?.hex)
putExtra(SIGN_TX_TO, tx.getTokenTransferTo().hex)
}
} else {
putExtra(SIGN_TX_AMOUNT, tx.value?.toTransferredAmount())
putExtra(SIGN_TX_CURRENCY, "ETH")