Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WalletConnect Gaslimit #3379

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions app/src/main/java/com/alphawallet/app/service/GasService.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ public Single<GasEstimate> calculateGasEstimateInternal(byte[] transactionBytes,
updateChainId(chainId);
String finalTxData = txData;

BigInteger useGasLimit = defaultLimit.equals(BigInteger.ZERO) ? EthereumNetworkBase.getMaxGasLimit(chainId) : defaultLimit;

if ((toAddress.equals("") || toAddress.equals(ZERO_ADDRESS)) && txData.length() > 0) //Check gas for constructor
{
return networkRepository.getLastTransactionNonce(web3j, wallet.address)
Expand All @@ -389,7 +391,7 @@ public Single<GasEstimate> calculateGasEstimateInternal(byte[] transactionBytes,
else
{
return networkRepository.getLastTransactionNonce(web3j, wallet.address)
.flatMap(nonce -> ethEstimateGas(chainId, wallet.address, nonce, toAddress, amount, finalTxData))
.flatMap(nonce -> ethEstimateGas(chainId, wallet.address, useGasLimit, nonce, toAddress, amount, finalTxData))
.flatMap(estimate -> handleOutOfGasError(estimate, chainId, toAddress, amount, finalTxData))
.map(estimate -> convertToGasLimit(estimate, defaultLimit));
}
Expand Down Expand Up @@ -427,7 +429,7 @@ private Single<EthEstimateGas> handleOutOfGasError(@NonNull EthEstimateGas estim
{
if (!estimate.hasError() || chainId != 1) return Single.fromCallable(() -> estimate);
else return networkRepository.getLastTransactionNonce(web3j, WHALE_ACCOUNT)
.flatMap(nonce -> ethEstimateGas(chainId, WHALE_ACCOUNT, nonce, toAddress, amount, finalTxData));
.flatMap(nonce -> ethEstimateGas(chainId, WHALE_ACCOUNT, EthereumNetworkBase.getMaxGasLimit(chainId), nonce, toAddress, amount, finalTxData));
}

private BigInteger getLowGasPrice()
Expand All @@ -443,14 +445,14 @@ private Single<EthEstimateGas> ethEstimateGas(String fromAddress, BigInteger non
return Single.fromCallable(() -> web3j.ethEstimateGas(transaction).send());
}

private Single<EthEstimateGas> ethEstimateGas(long chainId, String fromAddress, BigInteger nonce, String toAddress,
private Single<EthEstimateGas> ethEstimateGas(long chainId, String fromAddress, BigInteger limit, BigInteger nonce, String toAddress,
BigInteger amount, String txData)
{
final Transaction transaction = new Transaction (
fromAddress,
nonce,
currentGasPrice,
EthereumNetworkBase.getMaxGasLimit(chainId),
limit,
toAddress,
amount,
txData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.alphawallet.ethereum.EthereumNetworkBase.ARBITRUM_MAIN_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.AURORA_MAINNET_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.AVALANCHE_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.BASE_MAINNET_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.BINANCE_MAIN_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.CLASSIC_ID;
import static com.alphawallet.ethereum.EthereumNetworkBase.CRONOS_MAIN_ID;
Expand Down Expand Up @@ -837,6 +838,7 @@ private void resetTickerUpdate()
put(CRONOS_MAIN_ID, "cronos");
put(ROOTSTOCK_MAINNET_ID, "rootstock");
put(LINEA_ID, "linea");
put(BASE_MAINNET_ID, "base");
}};

// For now, don't use Dexguru unless we obtain API key
Expand Down Expand Up @@ -875,6 +877,7 @@ public void deleteTickers()
put(OKX_ID, "okb");
put(ROOTSTOCK_MAINNET_ID, "rootstock");
put(LINEA_ID, "ethereum");
put(BASE_MAINNET_ID, "base");
}};

public static boolean validateCoinGeckoAPI(Token token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface GasWidgetInterface
BigInteger getPriorityFee();
BigInteger getGasPrice();
void setGasEstimate(BigInteger estimate);
void setGasEstimateExact(BigInteger estimate);
void onDestroy();
boolean checkSufficientGas();
void setupResendSettings(ActionSheetMode mode, BigInteger gasPrice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ public Web3Transaction(
public Web3Transaction(WCEthereumTransaction wcTx, long callbackId, SignType signType)
{
String gasPrice = wcTx.getGasPrice() != null ? wcTx.getGasPrice() : "0";
String gasLimit = wcTx.getGasLimit() != null ? wcTx.getGasLimit() : "0";
//WC2 uses "gas" for gas limit
String gasLimit = wcTx.getGas() != null ? wcTx.getGas() : "0";
String nonce = wcTx.getNonce() != null ? wcTx.getNonce() : "";

this.recipient = TextUtils.isEmpty(wcTx.getTo()) ? Address.EMPTY : new Address(wcTx.getTo());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public ActionSheetDialog(@NonNull Activity activity, Web3Transaction tx, Token t

if (!tx.gasLimit.equals(BigInteger.ZERO))
{
setGasEstimate(new GasEstimate(tx.gasLimit));
gasWidgetInterface.setGasEstimateExact(tx.gasLimit);
functionBar.setPrimaryButtonEnabled(true);
}

updateAmount();
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/alphawallet/app/widget/GasWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ public void setGasEstimate(BigInteger estimate)
estimate = estimate.multiply(BigInteger.valueOf(6)).divide(BigInteger.valueOf(5)); // increase estimate by 20% to be safe
}

setGasEstimateExact(estimate);
}

@Override
public void setGasEstimateExact(BigInteger estimate)
{
//Override custom gas limit
if (customGasLimit.equals(baseLineGasLimit))
{
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/alphawallet/app/widget/GasWidget2.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,17 @@ public long getExpectedTransactionTime()
*/
public void setGasEstimate(BigInteger estimate)
{
if (estimate.longValue() > C.GAS_LIMIT_MIN) //some kind of contract interaction
if (!customGasLimit.equals(estimate) && estimate.longValue() > C.GAS_LIMIT_MIN) //some kind of contract interaction
{
estimate = estimate.multiply(BigInteger.valueOf(6)).divide(BigInteger.valueOf(5)); // increase estimate by 20% to be safe
}

//Override custom gas limit if required
setGasEstimateExact(estimate);
}

@Override
public void setGasEstimateExact(BigInteger estimate)
{
if (customGasLimit.equals(presetGasLimit))
{
customGasLimit = estimate;
Expand Down
Loading