Skip to content

Commit 2c098f4

Browse files
committed
gas optimization
1 parent adc19f9 commit 2c098f4

File tree

4 files changed

+85
-68
lines changed

4 files changed

+85
-68
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.1.6
2+
3+
* Remove repetated function call in safe plugin
4+
* Split gas overrides individually
5+
* Change point of gas override
6+
17
## 0.1.5
28

39
* improve gas fees estimation

lib/src/4337/wallet.dart

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -85,61 +85,66 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
8585
UserOperation buildUserOperation({
8686
required Uint8List callData,
8787
BigInt? customNonce,
88-
}) =>
89-
UserOperation.partial(
90-
callData: callData,
91-
initCode: _initCode,
92-
sender: _walletAddress,
93-
nonce: customNonce);
88+
}) {
89+
return UserOperation.partial(
90+
callData: callData,
91+
initCode: _initCode,
92+
sender: _walletAddress,
93+
nonce: customNonce);
94+
}
9495

9596
@override
9697
Future<UserOperationResponse> send(
97-
EthereumAddress recipient, EtherAmount amount) =>
98-
sendUserOperation(buildUserOperation(
99-
callData: Contract.execute(_walletAddress,
100-
to: recipient, amount: amount, isSafe: isSafe)));
98+
EthereumAddress recipient, EtherAmount amount) {
99+
final cd = Contract.execute(_walletAddress,
100+
to: recipient, amount: amount, isSafe: isSafe);
101+
return sendUserOperation(buildUserOperation(callData: cd));
102+
}
101103

102104
@override
103105
Future<UserOperationResponse> sendTransaction(
104-
EthereumAddress to, Uint8List encodedFunctionData,
105-
{EtherAmount? amount}) =>
106-
sendUserOperation(buildUserOperation(
107-
callData: Contract.execute(_walletAddress,
108-
to: to,
109-
amount: amount,
110-
innerCallData: encodedFunctionData,
111-
isSafe: isSafe)));
106+
EthereumAddress to, Uint8List encodedFunctionData,
107+
{EtherAmount? amount}) {
108+
final cd = Contract.execute(_walletAddress,
109+
to: to,
110+
amount: amount,
111+
innerCallData: encodedFunctionData,
112+
isSafe: isSafe);
113+
return sendUserOperation(buildUserOperation(callData: cd));
114+
}
112115

113116
@override
114117
Future<UserOperationResponse> sendBatchedTransaction(
115118
List<EthereumAddress> recipients, List<Uint8List> calls,
116119
{List<EtherAmount>? amounts}) {
120+
Uint8List cd;
117121
if (isSafe) {
118122
final innerCall = plugin<_SafePlugin>('safe')
119123
.getSafeMultisendCallData(recipients, amounts, calls);
120-
return sendUserOperation(buildUserOperation(
121-
callData: Contract.executeBatch(
122-
walletAddress: _walletAddress,
123-
recipients: [Constants.safeMultiSendaddress],
124-
amounts: [],
125-
innerCalls: [innerCall],
126-
isSafe: true)));
124+
cd = Contract.executeBatch(
125+
walletAddress: _walletAddress,
126+
recipients: [Constants.safeMultiSendaddress],
127+
amounts: [],
128+
innerCalls: [innerCall],
129+
isSafe: true);
130+
return sendUserOperation(buildUserOperation(callData: cd));
127131
} else {
128-
return sendUserOperation(buildUserOperation(
129-
callData: Contract.executeBatch(
130-
walletAddress: _walletAddress,
131-
recipients: recipients,
132-
amounts: amounts,
133-
innerCalls: calls)));
132+
cd = Contract.executeBatch(
133+
walletAddress: _walletAddress,
134+
recipients: recipients,
135+
amounts: amounts,
136+
innerCalls: calls);
137+
return sendUserOperation(buildUserOperation(callData: cd));
134138
}
135139
}
136140

137141
@override
138-
Future<UserOperationResponse> sendSignedUserOperation(UserOperation op) =>
139-
plugin<BundlerProviderBase>('bundler')
140-
.sendUserOperation(
141-
op.toMap(_chain.entrypoint.version), _chain.entrypoint)
142-
.catchError((e) => throw SendError(e.toString(), op));
142+
Future<UserOperationResponse> sendSignedUserOperation(UserOperation op) {
143+
return plugin<BundlerProviderBase>('bundler')
144+
.sendUserOperation(
145+
op.toMap(_chain.entrypoint.version), _chain.entrypoint)
146+
.catchError((e) => throw SendError(e.toString(), op));
147+
}
143148

144149
@override
145150
Future<UserOperationResponse> sendUserOperation(UserOperation op) =>
@@ -198,39 +203,41 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
198203
/// Otherwise, retrieves the nonce by calling the 'getNonce' function on the entrypoint.
199204
///
200205
/// If an error occurs during the nonce retrieval process, a [NonceError] exception is thrown.
201-
Future<Uint256> _getNonce() => isDeployed.then((deployed) => !deployed
202-
? Future.value(Uint256.zero)
203-
: plugin<Contract>("contract")
204-
.read(_chain.entrypoint.address, ContractAbis.get('getNonce'),
205-
"getNonce",
206-
params: [_walletAddress, BigInt.zero])
207-
.then((value) => Uint256(value[0]))
208-
.catchError((e) => throw NonceError(e.toString(), _walletAddress)));
206+
Future<Uint256> _getNonce() {
207+
return isDeployed.then((deployed) => !deployed
208+
? Future.value(Uint256.zero)
209+
: plugin<Contract>("contract")
210+
.read(_chain.entrypoint.address, ContractAbis.get('getNonce'),
211+
"getNonce",
212+
params: [_walletAddress, BigInt.zero])
213+
.then((value) => Uint256(value[0]))
214+
.catchError((e) => throw NonceError(e.toString(), _walletAddress)));
215+
}
209216

210217
/// Returns the balance for the Smart Wallet address.
211218
///
212219
/// If an error occurs during the balance retrieval process, a [FetchBalanceError] exception is thrown.
213-
Future<EtherAmount> _getBalance() => plugin<Contract>("contract")
214-
.getBalance(_walletAddress)
215-
.catchError((e) => throw FetchBalanceError(e.toString(), _walletAddress));
220+
Future<EtherAmount> _getBalance() {
221+
return plugin<Contract>("contract").getBalance(_walletAddress).catchError(
222+
(e) => throw FetchBalanceError(e.toString(), _walletAddress));
223+
}
216224

217225
/// Updates the user operation with the latest nonce and gas prices.
218226
///
219227
/// [op] is the user operation to update.
220228
///
221229
/// Returns a [Future] that resolves to the updated [UserOperation] object.
222-
Future<UserOperation> _updateUserOperation(UserOperation op) =>
223-
Future.wait<dynamic>([
224-
_getNonce(),
225-
plugin<JsonRPCProviderBase>('jsonRpc').getGasPrice()
226-
]).then((responses) {
227-
op = op.copyWith(
228-
nonce: op.nonce > BigInt.zero ? op.nonce : responses[0].value,
229-
initCode: responses[0] > Uint256.zero ? Uint8List(0) : null,
230-
signature: dummySignature);
231-
232-
return _updateUserOperationGas(op, responses[1]);
233-
});
230+
Future<UserOperation> _updateUserOperation(UserOperation op) async {
231+
final responses = await Future.wait<dynamic>(
232+
[_getNonce(), plugin<JsonRPCProviderBase>('jsonRpc').getGasPrice()]);
233+
234+
op = op.copyWith(
235+
nonce: op.nonce > BigInt.zero ? op.nonce : responses[0].value,
236+
initCode: responses[0] > Uint256.zero ? Uint8List(0) : null,
237+
signature: dummySignature);
238+
239+
return _updateUserOperationGas(op, responses[1]);
240+
}
234241

235242
/// Updates the gas information for the user operation.
236243
///
@@ -240,10 +247,11 @@ class SmartWallet with _PluginManager, _GasSettings implements SmartWalletBase {
240247
/// Returns a [Future] that resolves to the updated [UserOperation] object.
241248
///
242249
/// If an error occurs during the gas estimation process, a [GasEstimationError] exception is thrown.
243-
Future<UserOperation> _updateUserOperationGas(UserOperation op, Fee fee) =>
244-
plugin<BundlerProviderBase>('bundler')
245-
.estimateUserOperationGas(
246-
op.toMap(_chain.entrypoint.version), _chain.entrypoint)
247-
.then((opGas) => op.updateOpGas(opGas, fee))
248-
.catchError((e) => throw GasEstimationError(e.toString(), op));
250+
Future<UserOperation> _updateUserOperationGas(UserOperation op, Fee fee) {
251+
return plugin<BundlerProviderBase>('bundler')
252+
.estimateUserOperationGas(
253+
op.toMap(_chain.entrypoint.version), _chain.entrypoint)
254+
.then((opGas) => op.updateOpGas(opGas, fee))
255+
.catchError((e) => throw GasEstimationError(e.toString(), op));
256+
}
249257
}

lib/src/common/factories.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ class _SafeProxyFactory extends SafeProxyFactory
4444
final setup = {
4545
"owners": owners.toList(),
4646
"threshold": BigInt.from(threshold),
47-
"to": module.setup,
48-
"data": encodeModuleSetup(),
47+
"to": null,
48+
"data": null,
4949
"fallbackHandler": module.address,
5050
};
5151

5252
if (encodeWebauthnSetup != null) {
5353
setup["to"] = Constants.safeMultiSendaddress;
5454
setup["data"] = encodeWebauthnSetup(encodeModuleSetup);
55+
} else {
56+
setup["to"] = module.setup;
57+
setup["data"] = encodeModuleSetup();
5558
}
5659

5760
return Contract.encodeFunctionCall(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: variance_dart
22
description: An Account Abstraction (4337) Development kit, for quickly building mobile web3 apps and smart wallets.
3-
version: 0.1.5
3+
version: 0.1.6
44
documentation: https://docs.variance.space
55
homepage: https://variance.space
66
repository: https://github.com/vaariance/variance-dart

0 commit comments

Comments
 (0)