Skip to content

Commit

Permalink
- update to use Infura gas provider if available.
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesSmartCell committed Feb 10, 2024
1 parent 2a31ab1 commit 550e6b2
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ android {
def DEFUALT_WALLETCONNECT_PROJECT_ID = "\"40c6071febfd93f4fe485c232a8a4cd9\""
def DEFAULT_AURORA_API_KEY = "\"HFDDY5BNKGXBB82DE2G8S64C3C41B76PYI\""; //Put your Aurorascan.dev API key here - this one will rate limit as it is common

buildConfigField 'int', 'DB_VERSION', '53'
buildConfigField 'int', 'DB_VERSION', '54'

buildConfigField "String", XInfuraAPI, DEFAULT_INFURA_API_KEY
buildConfigField "String", "WALLETCONNECT_PROJECT_ID", DEFUALT_WALLETCONNECT_PROJECT_ID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.alphawallet.app.entity;

import android.content.Intent;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;

import com.alphawallet.app.web3.entity.Web3Transaction;

Expand Down Expand Up @@ -58,4 +61,14 @@ default void setSignOnly()
default void setCurrentGasIndex(ActivityResult result)
{
}

default ActivityResultLauncher<Intent> gasSelectLauncher()
{
return null;

Check warning on line 67 in app/src/main/java/com/alphawallet/app/entity/ActionSheetInterface.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/entity/ActionSheetInterface.java#L67

Added line #L67 was not covered by tests
}

default void gasEstimateReady()
{

}

Check warning on line 73 in app/src/main/java/com/alphawallet/app/entity/ActionSheetInterface.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/entity/ActionSheetInterface.java#L73

Added line #L73 was not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ else if (!realmData.hasField("attestation"))

oldVersion = 53;
}

if (oldVersion == 53)
{
RealmObjectSchema realmData = schema.get("Realm1559Gas");
if (realmData != null) schema.remove("Realm1559Gas");
schema.create("Realm1559Gas")
.addField("chainId", long.class, FieldAttribute.PRIMARY_KEY)
.addField("timeStamp", long.class)
.addField("resultData", String.class);

oldVersion = 54;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,6 @@ public static NetworkInfo getNetwork(long chainId)
return networkMap.get(chainId);
}

// fetches the last transaction nonce; if it's identical to the last used one then increment by one
// to ensure we don't get transaction replacement
@Override
public Single<BigInteger> getLastTransactionNonce(Web3j web3j, String walletAddress)
{
Expand All @@ -889,7 +887,7 @@ public Single<BigInteger> getLastTransactionNonce(Web3j web3j, String walletAddr
try
{
EthGetTransactionCount ethGetTransactionCount = web3j
.ethGetTransactionCount(walletAddress, DefaultBlockParameterName.LATEST)
.ethGetTransactionCount(walletAddress, DefaultBlockParameterName.PENDING)

Check warning on line 890 in app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/repository/EthereumNetworkBase.java#L890

Added line #L890 was not covered by tests
.send();
return ethGetTransactionCount.getTransactionCount();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.alphawallet.app.entity.Transaction;
import com.alphawallet.app.entity.TransactionMeta;
import com.alphawallet.app.entity.Wallet;
import com.alphawallet.app.repository.entity.Realm1559Gas;
import com.alphawallet.app.repository.entity.RealmAuxData;
import com.alphawallet.app.repository.entity.RealmNFTAsset;
import com.alphawallet.app.repository.entity.RealmToken;
Expand Down Expand Up @@ -295,6 +296,7 @@ public Single<Boolean> deleteAllForWallet(String currentAddress)
r.where(RealmAuxData.class).findAll().deleteAllFromRealm();
r.where(RealmNFTAsset.class).findAll().deleteAllFromRealm();
r.where(RealmTransfer.class).findAll().deleteAllFromRealm();
r.where(Realm1559Gas.class).findAll().deleteAllFromRealm();
});
instance.refresh();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public class Realm1559Gas extends RealmObject
public Map<Integer, EIP1559FeeOracleResult> getResult()
{
Type entry = new TypeToken<Map<Integer, EIP1559FeeOracleResult>>() {}.getType();
return new Gson().fromJson(resultData, entry);
return new Gson().fromJson(getResultData(), entry);
}

public String getResultData()
{
return resultData;
}

public void setResultData(Map<Integer, EIP1559FeeOracleResult> result, long ts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,14 @@ private boolean updateEIP1559Realm(final Map<Integer, EIP1559FeeOracleResult> re
Realm1559Gas rgs = r.where(Realm1559Gas.class)
.equalTo("chainId", chainId)
.findFirst();

if (rgs == null)
{
rgs = r.createObject(Realm1559Gas.class, chainId);
}

rgs.setResultData(result, System.currentTimeMillis());
r.insertOrUpdate(rgs);
//r.insertOrUpdate(rgs);
});
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ public interface GasWidgetInterface
void setupResendSettings(ActionSheetMode mode, BigInteger gasPrice);
void setCurrentGasIndex(int gasSelectionIndex, BigInteger maxFeePerGas, BigInteger maxPriorityFee, BigDecimal customGasLimit, long expectedTxTime, long customNonce);
long getExpectedTransactionTime();
default boolean gasPriceReady(long gasEstimateTime)
{
return gasEstimateTime > (System.currentTimeMillis() - 30 * 1000);
}

boolean gasPriceReady();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

Expand Down Expand Up @@ -89,7 +90,6 @@ public class ActionSheetDialog extends ActionSheet implements StandardFunctionIn
private boolean use1559Transactions = false;
private Transaction transaction;
private final WalletType walletType;
private Disposable disposable;

public ActionSheetDialog(@NonNull Activity activity, Web3Transaction tx, Token t,
String destName, String destAddress, TokensService ts,
Expand Down Expand Up @@ -329,14 +329,14 @@ private GasWidgetInterface setupGasWidget()

if (use1559Transactions)
{
gasWidget.setupWidget(tokensService, token, candidateTransaction, actionSheetCallback.gasSelectLauncher());
gasWidget.setupWidget(tokensService, token, candidateTransaction, this);

Check warning on line 332 in app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java#L332

Added line #L332 was not covered by tests
return gasWidget;
}
else
{
gasWidget.setVisibility(View.GONE);
gasWidgetLegacy.setVisibility(View.VISIBLE);
gasWidgetLegacy.setupWidget(tokensService, token, candidateTransaction, this, actionSheetCallback.gasSelectLauncher());
gasWidgetLegacy.setupWidget(tokensService, token, candidateTransaction, this, this);

Check warning on line 339 in app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java#L339

Added line #L339 was not covered by tests
return gasWidgetLegacy;
}
}
Expand Down Expand Up @@ -434,6 +434,13 @@ public void updateAmount()
@SuppressWarnings("checkstyle:MissingSwitchDefault")
public void handleClick(String action, int id)
{
//first ensure gas estimate is up to date
if (gasEstimateOutOfDate())
{
functionBar.setPrimaryButtonWaiting();
return;

Check warning on line 441 in app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java#L440-L441

Added lines #L440 - L441 were not covered by tests
}

if (walletType == WalletType.HARDWARE)
{
//TODO: Hardware - Maybe flick a toast to tell user to apply card
Expand Down Expand Up @@ -477,6 +484,23 @@ public void handleClick(String action, int id)
}
}

@Override
public void gasEstimateReady()
{
functionBar.setPrimaryButtonEnabled(true);
}

Check warning on line 491 in app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java#L490-L491

Added lines #L490 - L491 were not covered by tests

@Override
public ActivityResultLauncher<Intent> gasSelectLauncher()
{
return actionSheetCallback.gasSelectLauncher();

Check warning on line 496 in app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/ActionSheetDialog.java#L496

Added line #L496 was not covered by tests
}

private boolean gasEstimateOutOfDate()
{
return !gasWidget.gasPriceReady();
}

private BigDecimal getTransactionAmount()
{
BigDecimal txAmount;
Expand Down
55 changes: 43 additions & 12 deletions app/src/main/java/com/alphawallet/app/widget/GasWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;

import com.alphawallet.app.BuildConfig;
import com.alphawallet.app.C;
import com.alphawallet.app.R;
import com.alphawallet.app.entity.ActionSheetInterface;
import com.alphawallet.app.entity.GasPriceSpread;
import com.alphawallet.app.entity.StandardFunctionInterface;
import com.alphawallet.app.entity.TXSpeed;
Expand Down Expand Up @@ -70,6 +69,8 @@ public class GasWidget extends LinearLayout implements Runnable, GasWidgetInterf
private long customNonce = -1;
private boolean isSendingAll;
private BigInteger resendGasPrice = BigInteger.ZERO;
long gasEstimateTime = 0;

Check warning on line 72 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L72

Added line #L72 was not covered by tests
private ActionSheetInterface actionSheetInterface;

public GasWidget(Context ctx, AttributeSet attrs)
{
Expand All @@ -85,7 +86,7 @@ public GasWidget(Context ctx, AttributeSet attrs)
//For legacy transaction, either we are sending all or the chain doesn't support EIP1559
//Since these chains are not so well used, we will compromise and send at the standard gas rate
//That is - not allow selection of gas price
public void setupWidget(TokensService svs, Token t, Web3Transaction tx, StandardFunctionInterface sfi, ActivityResultLauncher<Intent> gasSelectLauncher)
public void setupWidget(TokensService svs, Token t, Web3Transaction tx, StandardFunctionInterface sfi, ActionSheetInterface actionSheetIf)
{
tokensService = svs;
token = t;
Expand All @@ -96,6 +97,7 @@ public void setupWidget(TokensService svs, Token t, Web3Transaction tx, Standard
isSendingAll = isSendingAll(tx);
initialGasPrice = tx.gasPrice;
customNonce = tx.nonce;
actionSheetInterface = actionSheetIf;

Check warning on line 100 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L100

Added line #L100 was not covered by tests

if (tx.gasLimit.equals(BigInteger.ZERO)) //dapp didn't specify a limit, use default limits until node returns an estimate (see setGasEstimate())
{
Expand Down Expand Up @@ -133,22 +135,27 @@ public void setupWidget(TokensService svs, Token t, Web3Transaction tx, Standard
intent.putExtra(C.EXTRA_NONCE, customNonce);
intent.putExtra(C.EXTRA_1559_TX, false);
intent.putExtra(C.EXTRA_MIN_GAS_PRICE, resendGasPrice.longValue());
gasSelectLauncher.launch(intent);
actionSheetInterface.gasSelectLauncher().launch(intent);

Check warning on line 138 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L138

Added line #L138 was not covered by tests
});
}
}

private void setupGasSpeeds(Web3Transaction w3tx)
{
RealmGasSpread rgs = getGasQuery().findFirst();
if (rgs != null)
{
initGasSpeeds(rgs);
}
else
try (Realm realm = tokensService.getTickerRealmInstance())

Check warning on line 145 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L145

Added line #L145 was not covered by tests
{
// Couldn't get current gas. Add a blank custom gas speed node
gasSpread = new GasPriceSpread(getContext(), w3tx.gasPrice);
RealmGasSpread gasReturn = realm.where(RealmGasSpread.class)
.equalTo("chainId", token.tokenInfo.chainId).findFirst();

Check warning on line 148 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L147-L148

Added lines #L147 - L148 were not covered by tests

if (gasReturn != null)
{
initGasSpeeds(gasReturn);

Check warning on line 152 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L152

Added line #L152 was not covered by tests
}
else
{
// Couldn't get current gas. Add a blank custom gas speed node
gasSpread = new GasPriceSpread(getContext(), w3tx.gasPrice);

Check warning on line 157 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L157

Added line #L157 was not covered by tests
}
}

if (w3tx.gasPrice.compareTo(BigInteger.ZERO) > 0)
Expand Down Expand Up @@ -301,6 +308,8 @@ private void initGasSpeeds(RealmGasSpread rgs)

TextView editTxt = findViewById(R.id.edit_text);

gasEstimateTime = rgs.getTimeStamp();

Check warning on line 311 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L311

Added line #L311 was not covered by tests

if (gasSpread.hasLockedGas() && editTxt.getVisibility() == View.VISIBLE)
{
findViewById(R.id.edit_text).setVisibility(View.GONE);
Expand Down Expand Up @@ -383,6 +392,16 @@ public void run()
}
checkSufficientGas();
manageWarnings();

if (gasPriceReady(gasEstimateTime))
{
actionSheetInterface.gasEstimateReady();
setGasReadyStatus(true);

Check warning on line 399 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L398-L399

Added lines #L398 - L399 were not covered by tests
}
else
{
setGasReadyStatus(false);

Check warning on line 403 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L403

Added line #L403 was not covered by tests
}
}

@Override
Expand All @@ -399,6 +418,12 @@ public BigInteger getGasPrice(BigInteger defaultPrice)
}
}

@Override
public boolean gasPriceReady()
{
return gasPriceReady(gasEstimateTime);

Check warning on line 424 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L424

Added line #L424 was not covered by tests
}

@Override
public BigInteger getValue()
{
Expand Down Expand Up @@ -447,6 +472,12 @@ else if (dGasPrice > 2.0 * upperBound)
}
}

private void setGasReadyStatus(boolean ready)
{
findViewById(R.id.view_spacer).setVisibility(ready ? View.VISIBLE : View.GONE);
findViewById(R.id.gas_fetch_wait).setVisibility(ready ? View.GONE : View.VISIBLE);
}

Check warning on line 479 in app/src/main/java/com/alphawallet/app/widget/GasWidget.java

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/com/alphawallet/app/widget/GasWidget.java#L479

Added line #L479 was not covered by tests

private void showCustomSpeedWarning(boolean high)
{
if (currentGasSpeedIndex != TXSpeed.CUSTOM)
Expand Down
Loading

0 comments on commit 550e6b2

Please sign in to comment.