-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web3Call added for uniswap broadcasts
- Loading branch information
Showing
5 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
dapp/src/main/java/com/alphawallet/dapp/listeners/Web3EthCall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.alphawallet.dapp.listeners | ||
|
||
import com.alphawallet.dapp.utils.toJSONObjectOrNull | ||
import okhttp3.* | ||
import okhttp3.MediaType.Companion.toMediaTypeOrNull | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import java.io.IOException | ||
import java.util.concurrent.TimeUnit | ||
|
||
class Web3EthCall(val callback: Callback) { | ||
|
||
interface Callback { | ||
fun onCallSuccessful(callbackId: Int, out: String) | ||
fun onCallFailure(callbackId: Int, throwable: Throwable) | ||
} | ||
|
||
|
||
private val client = OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS) | ||
.callTimeout(20, TimeUnit.SECONDS).build() | ||
|
||
var fromAddress: String = "" | ||
var rpcUrl: String = "" | ||
|
||
fun ethCall(callbackId: Int, toAddress: String, payload: String) { | ||
|
||
val postJsonBody = """ | ||
{"jsonrpc":"2.0","method":"eth_call","params":[{"from": "$fromAddress", "to": "$toAddress", "data": "$payload"}, "latest"], "id":1} | ||
""".trimIndent() | ||
|
||
val body: RequestBody = postJsonBody.toRequestBody("application/json".toMediaTypeOrNull()) | ||
|
||
val request: Request = Request.Builder() | ||
.url(rpcUrl) | ||
.post(body) | ||
.build() | ||
|
||
|
||
client.newCall(request).enqueue(object : okhttp3.Callback { | ||
override fun onFailure(call: Call, e: IOException) { | ||
callback.onCallFailure(callbackId, e) | ||
} | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
|
||
val jsonResponse = response.body?.string() | ||
val json = jsonResponse?.toJSONObjectOrNull() | ||
val result = json?.optString("result") | ||
|
||
if (!response.isSuccessful || result == null) { | ||
callback.onCallFailure( | ||
callbackId, | ||
RuntimeException("Http Response Not Successful or result is null ") | ||
) | ||
return | ||
} | ||
|
||
callback.onCallSuccessful(callbackId, result) | ||
} | ||
}) | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
dapp/src/main/java/com/alphawallet/dapp/utils/JSONExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.alphawallet.dapp.utils | ||
|
||
import org.json.JSONObject | ||
|
||
fun String.toJSONObjectOrNull() : JSONObject? { | ||
|
||
return try { | ||
JSONObject(this) | ||
} catch (t : Throwable) { | ||
null | ||
} | ||
} |