Skip to content

Commit 978051e

Browse files
committed
Fix codegen for empty parameters
1 parent 4edbfd7 commit 978051e

File tree

4 files changed

+93
-14
lines changed

4 files changed

+93
-14
lines changed

lib/src/builder/generator.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class _ContractGeneration {
8282
final Documentation? documentation;
8383

8484
final List<Spec> _additionalSpecs = [];
85+
final Map<FunctionParameter, String> _parameterNames = {};
8586
final Map<ContractFunction, Reference> _functionToResultClass = {};
8687

8788
// The `self` field, storing a reference to the deployed contract.
@@ -92,6 +93,13 @@ class _ContractGeneration {
9293

9394
_ContractGeneration(this._abi, this._abiCode, this.documentation);
9495

96+
String _nameOfParameter(FunctionParameter p) {
97+
return _parameterNames.putIfAbsent(p, () {
98+
if (p.name.isEmpty) return '\$param${_parameterNames.length}';
99+
return p.name;
100+
});
101+
}
102+
95103
Library generate() {
96104
return Library((b) {
97105
b.body
@@ -204,15 +212,16 @@ class _ContractGeneration {
204212
final parameters = <Parameter>[];
205213
for (final param in function.parameters) {
206214
parameters.add(Parameter((b) => b
207-
..name = param.name
215+
..name = _nameOfParameter(param)
208216
..type = param.type.toDart()));
209217
}
210218

211219
return parameters;
212220
}
213221

214222
Code _bodyForImmutable(ContractFunction function) {
215-
final params = function.parameters.map((e) => refer(e.name)).toList();
223+
final params =
224+
function.parameters.map((e) => refer(_nameOfParameter(e))).toList();
216225

217226
final outputs = function.outputs;
218227
Expression returnValue;

test/builder/data.dart

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const testCases = <String, String>{
2-
'''
2+
r'''
33
[
44
{
55
"inputs": [
@@ -35,7 +35,7 @@ const testCases = <String, String>{
3535
"stateMutability": "pure",
3636
"type": "function"
3737
}
38-
]''': '''
38+
]''': r'''
3939
// Generated code, do not modify. Run `build_runner build` to re-generate!
4040
// @dart=2.12
4141
import 'package:web3dart/web3dart.dart' as _i1;
@@ -76,7 +76,7 @@ class Retrieve3 {
7676
final bool var3;
7777
}
7878
''',
79-
'''
79+
r'''
8080
[
8181
{
8282
"inputs": [],
@@ -127,7 +127,7 @@ class Retrieve3 {
127127
"stateMutability": "nonpayable",
128128
"type": "function"
129129
}
130-
]''': '''
130+
]''': r'''
131131
// Generated code, do not modify. Run `build_runner build` to re-generate!
132132
// @dart=2.12
133133
import 'package:web3dart/web3dart.dart' as _i1;
@@ -188,7 +188,56 @@ class GiveMeHello {
188188
final BigInt num2;
189189
}
190190
''',
191-
'''
191+
r'''
192+
[
193+
{
194+
"inputs": [
195+
{
196+
"internalType": "address",
197+
"name": "",
198+
"type": "address"
199+
}
200+
],
201+
"name": "userTotalAmount",
202+
"outputs": [
203+
{
204+
"internalType": "uint256",
205+
"name": "",
206+
"type": "uint256"
207+
}
208+
],
209+
"stateMutability": "view",
210+
"type": "function"
211+
}
212+
]''': r'''
213+
// Generated code, do not modify. Run `build_runner build` to re-generate!
214+
// @dart=2.12
215+
import 'package:web3dart/web3dart.dart' as _i1;
216+
217+
final _contractAbi = _i1.ContractAbi.fromJson(
218+
'[{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userTotalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]',
219+
'Contract');
220+
221+
class Contract extends _i1.GeneratedContract {
222+
Contract(
223+
{required _i1.EthereumAddress address,
224+
required _i1.Web3Client client,
225+
int? chainId})
226+
: super(_i1.DeployedContract(_contractAbi, address), client, chainId);
227+
228+
/// The optional [atBlock] parameter can be used to view historical data. When
229+
/// set, the function will be evaluated in the specified block. By default, the
230+
/// latest on-chain block will be used.
231+
Future<BigInt> userTotalAmount(_i1.EthereumAddress $param0,
232+
{_i1.BlockNum? atBlock}) async {
233+
final function = self.function('userTotalAmount');
234+
final params = [$param0];
235+
final response = await read(function, params, atBlock);
236+
return (response[0] as BigInt);
237+
}
238+
}
239+
''',
240+
r'''
192241
[
193242
{
194243
"payable": true,
@@ -199,7 +248,7 @@ class GiveMeHello {
199248
"stateMutability":"payable",
200249
"type":"receive"
201250
}
202-
]''': '''
251+
]''': r'''
203252
// Generated code, do not modify. Run `build_runner build` to re-generate!
204253
// @dart=2.12
205254
import 'package:web3dart/web3dart.dart' as _i1;
@@ -216,7 +265,7 @@ class Contract extends _i1.GeneratedContract {
216265
: super(_i1.DeployedContract(_contractAbi, address), client, chainId);
217266
}
218267
''',
219-
'''
268+
r'''
220269
[
221270
{
222271
"inputs": [],
@@ -231,7 +280,7 @@ class Contract extends _i1.GeneratedContract {
231280
"stateMutability": "view",
232281
"type": "function"
233282
}
234-
]''': '''
283+
]''': r'''
235284
// Generated code, do not modify. Run `build_runner build` to re-generate!
236285
// @dart=2.12
237286
import 'package:web3dart/web3dart.dart' as _i1;
@@ -264,15 +313,15 @@ class Contract extends _i1.GeneratedContract {
264313
}
265314
}
266315
''',
267-
'''
316+
r'''
268317
{
269318
"abi": [],
270319
"devdoc": {
271320
"kind": "dev",
272321
"methods": {},
273322
"version": 1
274323
}
275-
}''': '''
324+
}''': r'''
276325
// Generated code, do not modify. Run `build_runner build` to re-generate!
277326
// @dart=2.12
278327
import 'package:web3dart/web3dart.dart' as _i1;

test/builder/mapping.abi.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"inputs": [
4+
{
5+
"internalType": "address",
6+
"name": "",
7+
"type": "address"
8+
}
9+
],
10+
"name": "userTotalAmount",
11+
"outputs": [
12+
{
13+
"internalType": "uint256",
14+
"name": "",
15+
"type": "uint256"
16+
}
17+
],
18+
"stateMutability": "view",
19+
"type": "function"
20+
}
21+
]

tool/generate_goldens.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ Future<void> main() async {
3434
final resultBuilder = StringBuffer();
3535
resultBuilder.writeln('const testCases = <String, String>{');
3636
data.forEach((key, value) {
37-
resultBuilder.writeln("'''");
37+
resultBuilder.writeln("r'''");
3838
resultBuilder.write(key);
3939
resultBuilder.writeln("''':");
4040

41-
resultBuilder.writeln("'''");
41+
resultBuilder.writeln("r'''");
4242
resultBuilder.write(value);
4343
resultBuilder.writeln("''',");
4444
});

0 commit comments

Comments
 (0)