Skip to content

Commit

Permalink
fix gas fee errors
Browse files Browse the repository at this point in the history
  • Loading branch information
code-z2 committed Oct 11, 2024
1 parent 1229fde commit 707023c
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 114 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.1.5

* improve gas fees estimation
* reduce copywith trigger in gas settings
* change position of custom gas overrides to post sponsor useroperation
* default to p256 precompile over contract verifiers.
* revert to direct balance checking

## 0.1.4

* add surpport for web3_signers v0.1+
Expand Down
51 changes: 14 additions & 37 deletions example/lib/providers/wallet_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class WalletProvider extends ChangeNotifier {
EthereumAddress.fromHex("0xf5bb7f874d8e3f41821175c0aa9910d30d10e193");

final salt = Uint256.zero;
static const rpc =
"https://api.pimlico.io/v2/84532/rpc?apikey=pim_NuuL4a9tBdyfoogF5LtP5A";
static const rpc = "https://api.pimlico.io/v2/84532/rpc?apikey=API_KEY";

WalletProvider()
: _chain = Chains.getChain(Network.baseTestnet)
Expand Down Expand Up @@ -67,8 +66,6 @@ class WalletProvider extends ChangeNotifier {
Future<void> createEOAWallet() async {
final signer = EOAWallet.createWallet(
WordLength.word_12, const SignatureOptions(prefix: [0]));
log("signer: ${signer.getAddress()}");

final SmartWalletFactory walletFactory = SmartWalletFactory(_chain, signer);

try {
Expand All @@ -87,14 +84,11 @@ class WalletProvider extends ChangeNotifier {

final signer = PrivateKeySigner.create(privateKey, "123456", random,
options: const SignatureOptions(prefix: [0]));
log("signer: ${signer.getAddress()}");
log("pk: ${hexlify(privateKey.privateKey)}");

final SmartWalletFactory walletFactory = SmartWalletFactory(_chain, signer);

try {
_wallet = await walletFactory.createAlchemyLightAccount(salt);
log("pk wallet created ${_wallet?.address.hex} ");
log("wallet created ${_wallet?.address.hex} ");
} catch (e) {
_errorMessage = e.toString();
notifyListeners();
Expand All @@ -106,40 +100,26 @@ class WalletProvider extends ChangeNotifier {
_chain.accountFactory = Constants.safeProxyFactoryAddress;

final signer = EOAWallet.createWallet();
log("signer: ${signer.getAddress()}");

final SmartWalletFactory walletFactory = SmartWalletFactory(_chain, signer);

try {
_wallet = await walletFactory.createSafeAccount(salt);
log("safe created ${_wallet?.address.hex} ");
log("wallet created ${_wallet?.address.hex} ");
} catch (e) {
log("something happened: $e");
}
}

Future<void> mintNFt() async {
// pimlico requires us to get the gasfees from their bundler.
// that cannot be built into the sdk so we modify the internal fees manually
if (_chain.entrypoint.version == 0.7) {
_wallet?.gasSettings = GasSettings(
gasMultiplierPercentage: 5,
userDefinedMaxFeePerGas: BigInt.parse("0x524e1909"),
userDefinedMaxPriorityFeePerGas: BigInt.parse("0x52412100"));
}
// mints nft
log(DateTime.timestamp().toString());
// simple tx
final tx1 = await _wallet?.sendTransaction(
nft,
Contract.encodeFunctionCall("safeMint", nft,
ContractAbis.get("ERC721_SafeMint"), [_wallet?.address]));
await tx1?.wait();

await sendBatchedTransaction();
}

Future<void> sendBatchedTransaction() async {
final tx = await _wallet?.sendBatchedTransaction([
// batch tx
final tx2 = await _wallet?.sendBatchedTransaction([
erc20,
erc20
], [
Expand All @@ -152,21 +132,18 @@ class WalletProvider extends ChangeNotifier {
erc20, deployer, w3d.EtherAmount.fromInt(w3d.EtherUnit.ether, 20))
]);

await tx?.wait();
await tx2?.wait();
}

Future<void> sendTransaction(String recipient, String amount) async {
if (_wallet != null) {
final etherAmount = w3d.EtherAmount.fromBigInt(w3d.EtherUnit.wei,
BigInt.from(double.parse(amount) * math.pow(10, 18)));
final etherAmount = w3d.EtherAmount.fromBigInt(w3d.EtherUnit.wei,
BigInt.from(double.parse(amount) * math.pow(10, 18)));

final response =
await _wallet?.send(EthereumAddress.fromHex(recipient), etherAmount);
final receipt = await response?.wait();
final response = await _wallet?.send(
EthereumAddress.fromHex("0xF5bB7F874D8e3f41821175c0Aa9910d30d10e193"),
etherAmount);
final receipt = await response?.wait();

log("Transaction receipt Hash: ${receipt?.userOpHash}");
} else {
log("No wallet available to send transaction");
}
log("Transaction receipt Hash: ${receipt?.userOpHash}");
}
}
1 change: 0 additions & 1 deletion example/lib/screens/home/home_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:qr_flutter/qr_flutter.dart';
Expand Down
44 changes: 16 additions & 28 deletions lib/src/4337/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,26 @@ class JsonRPCProvider implements JsonRPCProviderBase {
}

@override
Future<Map<String, EtherAmount>> getEip1559GasPrice() async {
final fee = await rpc.send<String>("eth_maxPriorityFeePerGas");
final tip = Uint256.fromHex(fee);
final mul = Uint256(BigInt.from(100 * 13));
final buffer = tip / mul;
final maxPriorityFeePerGas = tip + buffer;
final maxFeePerGas = maxPriorityFeePerGas;
return {
'maxFeePerGas': EtherAmount.fromBigInt(EtherUnit.wei, maxFeePerGas.value),
'maxPriorityFeePerGas':
EtherAmount.fromBigInt(EtherUnit.wei, maxPriorityFeePerGas.value)
};
}

@override
Future<Map<String, EtherAmount>> getGasPrice() async {
Future<Fee> getGasPrice([GasEstimation rate = GasEstimation.normal]) async {
try {
return await getEip1559GasPrice();
final [low, medium, high] = await getGasInEIP1559(rpc.url);
switch (rate) {
case GasEstimation.low:
return low;
case GasEstimation.normal:
return medium;
case GasEstimation.high:
return high;
}
} catch (e) {
final value = await getLegacyGasPrice();
return {
'maxFeePerGas': value,
'maxPriorityFeePerGas': value,
};
final data = await rpc.send<String>('eth_gasPrice');
final gasPrice = hexToInt(data);
return Fee(
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
estimatedGas: gasPrice);
}
}

@override
Future<EtherAmount> getLegacyGasPrice() async {
final data = await rpc.send<String>('eth_gasPrice');
return EtherAmount.fromBigInt(EtherUnit.wei, hexToInt(data));
}
}

class RPCBase extends JsonRPC {
Expand Down
8 changes: 3 additions & 5 deletions lib/src/4337/userop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,13 @@ class UserOperation implements UserOperationBase {
}

@override
UserOperation updateOpGas(
UserOperationGas? opGas, Map<String, dynamic>? feePerGas) {
UserOperation updateOpGas([UserOperationGas? opGas, Fee? fee]) {
return copyWith(
callGasLimit: opGas?.callGasLimit,
verificationGasLimit: opGas?.verificationGasLimit,
preVerificationGas: opGas?.preVerificationGas,
maxFeePerGas: (feePerGas?["maxFeePerGas"] as EtherAmount?)?.getInWei,
maxPriorityFeePerGas:
(feePerGas?["maxPriorityFeePerGas"] as EtherAmount?)?.getInWei,
maxFeePerGas: fee?.maxFeePerGas,
maxPriorityFeePerGas: fee?.maxPriorityFeePerGas,
);
}

Expand Down
18 changes: 7 additions & 11 deletions lib/src/4337/wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
op = await plugin<Paymaster>('paymaster').intercept(op);
}

op = applyCustomGasSettings(op);
// Validate the user operation
op.validate(op.nonce > BigInt.zero, initCode);

Expand Down Expand Up @@ -209,14 +210,11 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
.then((value) => Uint256(value[0]))
.catchError((e) => throw NonceError(e.toString(), _walletAddress)));

/// Returns the balance for the Smart Wallet address from the entrypoint.
/// Returns the balance for the Smart Wallet address.
///
/// If an error occurs during the balance retrieval process, a [FetchBalanceError] exception is thrown.
Future<EtherAmount> _getBalance() => plugin<Contract>("contract")
.read(_chain.entrypoint.address, ContractAbis.get('getBalance'),
"balanceOf",
params: [_walletAddress])
.then((value) => EtherAmount.fromBigInt(EtherUnit.wei, value[0]))
.getBalance(_walletAddress)
.catchError((e) => throw FetchBalanceError(e.toString(), _walletAddress));

/// Updates the user operation with the latest nonce and gas prices.
Expand All @@ -234,23 +232,21 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
initCode: responses[0] > Uint256.zero ? Uint8List(0) : null,
signature: dummySignature);

return _updateUserOperationGas(op, feePerGas: responses[1]);
return _updateUserOperationGas(op, responses[1]);
});

/// Updates the gas information for the user operation.
///
/// [op] is the user operation to update.
/// [feePerGas] is an optional map containing the gas prices.
/// [fee] is an optional Fee containing the gas prices.
///
/// Returns a [Future] that resolves to the updated [UserOperation] object.
///
/// If an error occurs during the gas estimation process, a [GasEstimationError] exception is thrown.
Future<UserOperation> _updateUserOperationGas(UserOperation op,
{Map<String, EtherAmount>? feePerGas}) =>
Future<UserOperation> _updateUserOperationGas(UserOperation op, Fee fee) =>
plugin<BundlerProviderBase>('bundler')
.estimateUserOperationGas(
op.toMap(_chain.entrypoint.version), _chain.entrypoint)
.then((opGas) => op.updateOpGas(opGas, feePerGas))
.then((op) => applyCustomGasSettings(op))
.then((opGas) => op.updateOpGas(opGas, fee))
.catchError((e) => throw GasEstimationError(e.toString(), op));
}
2 changes: 1 addition & 1 deletion lib/src/common/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Constants {
static final EthereumAddress safeMultiSendaddress =
EthereumAddress.fromHex("0x38869bf66a61cF6bDB996A6aE40D5853Fd43B526");
static final EthereumAddress p256VerifierAddress =
EthereumAddress.fromHex("0x445a0683e494ea0c5AF3E83c5159fBE47Cf9e765");
EthereumAddress.fromHex("0x0000000000000000000000000000000000000100");

Constants._();
}
2 changes: 2 additions & 0 deletions lib/src/common/mixins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ mixin _GasSettings {
UserOperation applyCustomGasSettings(UserOperation op) {
final multiplier = _gasParams.gasMultiplierPercentage / 100 + 1;

if (multiplier == 1) return op;

return op.copyWith(
callGasLimit: BigInt.from(op.callGasLimit.toDouble() * multiplier),
verificationGasLimit:
Expand Down
1 change: 1 addition & 0 deletions lib/src/interfaces/interfaces.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:typed_data';

import 'package:eip1559/eip1559.dart';
import 'package:web3_signers/web3_signers.dart' show PassKeyPair, Uint256;
import 'package:web3dart/web3dart.dart';

Expand Down
36 changes: 11 additions & 25 deletions lib/src/interfaces/json_rpc_provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
part of 'interfaces.dart';

enum GasEstimation {
low(rate: 0),
normal(rate: 1),
high(rate: 2);

final int rate;

const GasEstimation({required this.rate});
}

/// Abstract base class for interacting with an Bundler RPC provider.
///
/// Implementations of this class are expected to provide functionality for specifically interacting
Expand Down Expand Up @@ -59,18 +69,6 @@ abstract class JsonRPCProviderBase {
bool isContainFullObj = true,
});

/// Asynchronously retrieves the EIP-1559 gas prices, including `maxFeePerGas` and `maxPriorityFeePerGas`.
///
/// Returns:
/// A [Future] that completes with a [Map] containing the gas prices in [EtherAmount].
///
/// Example:
/// ```dart
/// var gasPrices = await getEip1559GasPrice();
/// ```
/// This method uses an ethereum jsonRPC to fetch EIP-1559 gas prices from the Ethereum node.
Future<Map<String, EtherAmount>> getEip1559GasPrice();

/// Asynchronously retrieves the gas prices, supporting both EIP-1559 and legacy gas models.
///
/// Returns:
Expand All @@ -81,17 +79,5 @@ abstract class JsonRPCProviderBase {
/// var gasPrices = await getGasPrice();
/// ```
/// This method first attempts to fetch EIP-1559 gas prices and falls back to legacy gas prices if it fails.
Future<Map<String, EtherAmount>> getGasPrice();

/// Asynchronously retrieves the legacy gas price from the Ethereum node.
///
/// Returns:
/// A [Future] that completes with an [EtherAmount] representing the legacy gas price in [Wei].
///
/// Example:
/// ```dart
/// var legacyGasPrice = await getLegacyGasPrice();
/// ```
/// This method uses an ethereum jsonRPC to fetch the legacy gas price from the Ethereum node.
Future<EtherAmount> getLegacyGasPrice();
Future<Fee> getGasPrice([GasEstimation rate = GasEstimation.normal]);
}
5 changes: 2 additions & 3 deletions lib/src/interfaces/user_operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ abstract class UserOperationBase {
///
/// Parameters:
/// - `opGas`: Optional parameter of type [UserOperationGas] for specifying gas-related information.
/// - `feePerGas`: Optional parameter of type [Map<String, EtherAmount>] for specifying maxFeePerGas and maxPriorityFeePerGas.
/// - `fee`: Optional parameter of type [Fee] for specifying maxFeePerGas and maxPriorityFeePerGas.
///
/// Returns:
/// A [UserOperation] instance created from the provided map.
Expand All @@ -85,8 +85,7 @@ abstract class UserOperationBase {
/// // Other parameters can be updated as needed.
/// );
/// ```
UserOperation updateOpGas(
UserOperationGas? opGas, Map<String, EtherAmount>? feePerGas);
UserOperation updateOpGas([UserOperationGas? opGas, Fee? fee]);

/// Validates the user operation fields for accuracy
///
Expand Down
2 changes: 1 addition & 1 deletion lib/variance_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';

import 'package:eip1559/eip1559.dart';
import 'package:http/http.dart' as http;
import 'package:web3_signers/web3_signers.dart';
import 'package:web3dart/crypto.dart';
Expand All @@ -13,7 +14,6 @@ import 'package:web3dart/web3dart.dart';
import 'src/abis/abis.dart';
import 'src/interfaces/interfaces.dart';

export 'package:web3_signers/web3_signers.dart' show Logger;
export 'src/abis/abis.dart' show ContractAbis;

part 'src/4337/chains.dart';
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ packages:
source: hosted
version: "2.3.6"
eip1559:
dependency: transitive
dependency: "direct main"
description:
name: eip1559
sha256: c2b81ac85f3e0e71aaf558201dd9a4600f051ece7ebacd0c5d70065c9b458004
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: variance_dart
description: An Account Abstraction (4337) Development kit, for quickly building mobile web3 apps and smart wallets.
version: 0.1.4
version: 0.1.5
documentation: https://docs.variance.space
homepage: https://variance.space
repository: https://github.com/vaariance/variance-dart
Expand All @@ -19,6 +19,7 @@ dependencies:
web3dart: ^2.7.3
http: ^1.2.0
web3_signers: ^0.1.6
eip1559: ^0.6.2

dev_dependencies:
web3dart_builders: ^0.1.2
Expand Down

0 comments on commit 707023c

Please sign in to comment.