Skip to content

Commit

Permalink
Web3Call added for uniswap broadcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
sharish committed Jun 12, 2021
1 parent 51c1301 commit 0665e7f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 22 deletions.
22 changes: 9 additions & 13 deletions app/src/main/java/com/alphawallet/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package com.alphawallet

import android.os.Bundle
import android.view.KeyEvent
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import android.webkit.WebView
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.alphawallet.dapp.Web3View
import com.alphawallet.dapp.listeners.DappRequestListener
import com.alphawallet.dapp.model.DappMessage
Expand All @@ -24,16 +17,16 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setContentView(R.layout.activity_dapp)
setSupportActionBar(findViewById(R.id.toolbar))

searchBar = findViewById(R.id.urlBar)
webView = findViewById(R.id.webview)
webView.setChainId(1)
webView.setRpcUrl("https://mainnet.infura.io/v3/da3717f25f824cc1baa32d812386d93f")
webView.setWalletAddress("0x615e8391d3f17fa4e29824b29ceeade2c3adcf4e")
webView.setWalletAddress("0xfc43f5f9dd45258b3aff31bdbe6561d97e8b71de")

searchBar.setOnEditorActionListener{ _, actionId, _ ->
searchBar.setOnEditorActionListener { _, actionId, _ ->

if (actionId == EditorInfo.IME_ACTION_DONE) {
webView.loadUrl(searchBar.text.toString().trim())
Expand All @@ -44,13 +37,16 @@ class MainActivity : AppCompatActivity() {

webView.setDappListener(object : DappRequestListener {
override fun onTransactionSignRequest(transaction: DappTransaction) {
Toast.makeText(this@MainActivity, "Tx(${transaction.toAddress}, ${transaction.value}", Toast.LENGTH_LONG).show()
Toast.makeText(
this@MainActivity,
"Tx(${transaction.toAddress}, ${transaction.value}",
Toast.LENGTH_LONG
).show()
}

override fun onMessageSignRequest(message: DappMessage) {
Toast.makeText(this@MainActivity, "Tx(${message.message}", Toast.LENGTH_LONG).show()
}

})
}
}
File renamed without changes.
32 changes: 23 additions & 9 deletions dapp/src/main/java/com/alphawallet/dapp/Web3View.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

import android.annotation.SuppressLint;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alphawallet.dapp.listeners.DappRequestListener;
import com.alphawallet.dapp.listeners.SignCallbackJSInterface;
import com.alphawallet.dapp.listeners.UrlLoadListener;
import com.alphawallet.dapp.listeners.Web3EthCall;
import com.alphawallet.dapp.model.DappMessage;
import com.alphawallet.dapp.model.DappTransaction;
import com.alphawallet.dapp.utils.ResourceExtensionsKt;
Expand All @@ -31,6 +31,19 @@ public class Web3View extends WebView {

private DappRequestListener outerListener = null;

private final Web3EthCall web3EthCaller = new Web3EthCall(new Web3EthCall.Callback() {

@Override
public void onCallFailure(int callbackId, @NotNull Throwable throwable) {
onCallFunctionError(callbackId, "");
}

@Override
public void onCallSuccessful(int callbackId, @NotNull String result) {
onCallFunctionSuccessful(callbackId, result);
}
});

public Web3View(@NonNull Context context) {
super(context);
}
Expand Down Expand Up @@ -75,6 +88,7 @@ public void init() {

public void setWalletAddress(@NonNull String address) {
jsInjectorClient.setWalletAddress(address);
web3EthCaller.setFromAddress(address);
}

public void setChainId(int chainId) {
Expand All @@ -83,6 +97,7 @@ public void setChainId(int chainId) {

public void setRpcUrl(@NonNull String rpcUrl) {
jsInjectorClient.setRpcUrl(rpcUrl);
web3EthCaller.setRpcUrl(rpcUrl);
}

public void setWebLoadCallback(UrlLoadListener listener) {
Expand Down Expand Up @@ -125,25 +140,24 @@ private void callbackToJS(long callbackId, String function, String param) {
private final DappRequestListener innerListener = new DappRequestListener() {
@Override
public void onEthCall(int callbackId, @NotNull String recipient, @NotNull String payload) {
if(outerListener == null) return;
outerListener.onEthCall(callbackId, recipient, payload);
web3EthCaller.ethCall(callbackId, recipient, payload);
}

@Override
public void onSignTypedMessageRequest(int callbackId, @NotNull String payload) {
if(outerListener == null) return;
if (outerListener == null) return;
outerListener.onSignTypedMessageRequest(callbackId, payload);
}

@Override
public void onTransactionSignRequest(@NotNull DappTransaction transaction) {
if(outerListener == null) return;
if (outerListener == null) return;
outerListener.onTransactionSignRequest(transaction);
}

@Override
public void onMessageSignRequest(@NotNull DappMessage message) {
if(outerListener == null) return;
if (outerListener == null) return;
outerListener.onMessageSignRequest(message);
}
};
Expand Down
62 changes: 62 additions & 0 deletions dapp/src/main/java/com/alphawallet/dapp/listeners/Web3EthCall.kt
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 dapp/src/main/java/com/alphawallet/dapp/utils/JSONExtensions.kt
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
}
}

0 comments on commit 0665e7f

Please sign in to comment.