Skip to content

Commit c0ac17b

Browse files
authored
Merge pull request #45 from cryptape/develop
Merge develop to master for v0.18
2 parents 808d4a3 + 5f0428c commit c0ac17b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1094
-283
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,35 @@ Java 8
1515
Gradle 4.3
1616

1717
### Install
18-
`git clone https://github.com/cryptape/nervosj.git`
19-
`gradle shadowJar` to generate a jar file for nervosj.
18+
Install from repositories:
19+
maven
20+
```
21+
<dependency>
22+
<groupId>org.nervos</groupId>
23+
<artifactId>core</artifactId>
24+
<version>0.18</version>
25+
</dependency>
26+
```
27+
Gradle
28+
```
29+
compile 'org.nervos:core:0.18'
30+
```
31+
Scala SBT
32+
```
33+
libraryDependencies += "org.nervos" % "core" % "0.18"
34+
```
35+
Install manually
36+
If you want to generate the jar and import manually.
37+
```
38+
git clone https://github.com/cryptape/nervosj.git
39+
gradle shadowJar
40+
```
41+
42+
Please refer to branch Android for usage for Android version.
43+
2044
### Test net
2145
Use Nervos AppChain test net (recommended):
22-
http://121.196.200.225:1337 is provided as a testnet for tests.
46+
http://121.196.200.225:1337 is provided as a test net.
2347

2448
Or build your own Nervos AppChain net:
2549
Please find more information in [how to set up client in your local](https://docs.nervos.org/Nervos-AppChain-Docs/#/quick-start/deploy-appchain).
@@ -78,7 +102,7 @@ Please check [TokenTransactionTest.java](https://github.com/cryptape/nervosj/blo
78102
### Working with smart contract with nervosj wrapper
79103
Besides interacting with smart contracts by sending transactions with binary code, nervosj provides a tool to help to convert solidity contract to a Java class from which smart contracts can be deployed and called.
80104

81-
Run `gradle shadowJar` to generate jars so that the tool can be found under `console/build/libs`. Name of the tool is `console-version-all.jar`.
105+
Download Nervosj jar flie from release page or run `gradle shadowJar` to generate jars so that the tool can be found under `console/build/libs`. Name of the tool is `console-version-all.jar`.
82106

83107
Usage of console-version-all is shown below:
84108
```

build.gradle

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ apply plugin: 'idea'
3636
apply plugin: 'io.codearte.nexus-staging'
3737
apply plugin: 'jacoco'
3838

39-
description 'web3j base project'
39+
description 'nervosj base project'
4040

4141
allprojects {
4242

4343
sourceCompatibility = 1.8
4444
targetCompatibility = 1.8
4545

46-
group 'org.web3j'
47-
version '0.17.1'
46+
group 'org.nervos'
47+
version '0.18'
4848

4949
apply plugin: 'java'
5050
apply plugin: 'jacoco'
@@ -92,19 +92,6 @@ subprojects {
9292
}
9393
}
9494

95-
//project(':tests') {
96-
// task runTests(type: JavaExec) {
97-
// classpath = sourceSets.main.runtimeClasspath
98-
// main = 'org.web3j.tests.PermissionSystemTest'
99-
// args = ['src/main/resources/config.json']
100-
// }
101-
//
102-
// task runToken(type: JavaExec) {
103-
// classpath = sourceSets.main.runtimeClasspath
104-
// main = 'org.web3j.tests.TokenTest'
105-
// }
106-
//}
107-
10895
project(':core') {
10996
task coreJar(type: Jar) {
11097
exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.MF'
@@ -151,6 +138,8 @@ configure(subprojects.findAll {it.name != 'integration-tests'}) {
151138
publishing {
152139
publications {
153140
mavenJava(MavenPublication) {
141+
groupId 'org.nervos'
142+
version '0.18'
154143
from components.java
155144

156145
artifact sourcesJar {
@@ -177,14 +166,14 @@ configure(subprojects.findAll {it.name != 'integration-tests'}) {
177166
}
178167

179168
pom.project {
180-
name 'web3j'
169+
name 'nervosj'
181170
packaging 'jar'
182171
description project.description
183-
url 'https://web3j.io'
172+
url 'https://docs.nervos.org/nervosj'
184173

185174
scm {
186-
connection 'scm:git:https://github.com/web3j/web3j.git'
187-
url 'https://github.com/web3j/web3j.git'
175+
connection 'scm:git@github.com:cryptape/nervosj.git'
176+
url 'https://github.com/cryptape/nervosj.git'
188177
}
189178

190179
licenses {
@@ -196,9 +185,9 @@ configure(subprojects.findAll {it.name != 'integration-tests'}) {
196185

197186
developers {
198187
developer {
199-
id 'conor10'
200-
name 'Conor Svensson'
201-
email 'conor10@gmail.com'
188+
id 'QingyangKong'
189+
name 'Qingyang Kong'
190+
email 'qingyangkong@cryptape.com'
202191
}
203192
}
204193
}
@@ -218,13 +207,13 @@ configure(subprojects.findAll {it.name != 'integration-tests'}) {
218207
publish = true
219208
pkg {
220209
repo = 'maven'
221-
name = 'org.web3j'
210+
name = 'org.nervos'
222211
desc = project.description
223-
userOrg = 'web3j'
212+
userOrg = 'nervos'
224213
licenses = ['Apache-2.0']
225-
issueTrackerUrl = 'https://github.com/web3j/web3j/issues'
226-
vcsUrl = 'https://github.com/web3j/web3j.git'
227-
websiteUrl = 'https://web3j.io'
214+
issueTrackerUrl = 'https://github.com/cryptape/nervosj/issues'
215+
vcsUrl = 'https://github.com/cryptape/nervosj.git'
216+
websiteUrl = 'https://docs.nervos.org/nervosj'
228217
publicDownloadNumbers = true
229218
}
230219
}

codegen/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
description 'web3j project code generators'
2+
description 'nervosj project code generators'
33

44
dependencies {
55
compile project(':core'),

codegen/src/main/java/org/nervos/appchain/codegen/SolidityFunctionWrapper.java

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
public class SolidityFunctionWrapper extends Generator {
5858

5959
private static final String BINARY = "BINARY";
60-
private static final String WEB3J = "nervosj";
60+
private static final String NERVOSJ = "nervosj";
6161
private static final String CREDENTIALS = "credentials";
6262
private static final String TRANSACTION_MANAGER = "transactionManager";
6363
private static final String INITIAL_VALUE = "initialWeiValue";
@@ -256,23 +256,23 @@ private static MethodSpec buildConstructor(Class authType, String authName) {
256256
return MethodSpec.constructorBuilder()
257257
.addModifiers(Modifier.PROTECTED)
258258
.addParameter(String.class, CONTRACT_ADDRESS)
259-
.addParameter(Nervosj.class, WEB3J)
259+
.addParameter(Nervosj.class, NERVOSJ)
260260
.addParameter(authType, authName)
261261
.addParameter(BigInteger.class, GAS_PRICE)
262262
.addParameter(BigInteger.class, GAS_LIMIT)
263263
.addStatement("super($N, $N, $N, $N, $N, $N)",
264-
BINARY, CONTRACT_ADDRESS, WEB3J, authName, GAS_PRICE, GAS_LIMIT)
264+
BINARY, CONTRACT_ADDRESS, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT)
265265
.build();
266266
}
267267

268268
private static MethodSpec buildConstructorAdaptToCita(Class authType, String authName) {
269269
return MethodSpec.constructorBuilder()
270270
.addModifiers(Modifier.PROTECTED)
271271
.addParameter(String.class, CONTRACT_ADDRESS)
272-
.addParameter(Nervosj.class, WEB3J)
272+
.addParameter(Nervosj.class, NERVOSJ)
273273
.addParameter(authType, authName)
274274
.addStatement("super($N, $N, $N, $N)",
275-
BINARY, CONTRACT_ADDRESS, WEB3J, authName)
275+
BINARY, CONTRACT_ADDRESS, NERVOSJ, authName)
276276
.build();
277277
}
278278

@@ -319,11 +319,11 @@ private static MethodSpec buildDeployWithParams(
319319
if (isPayable) {
320320
methodBuilder.addStatement(
321321
"return deployRemoteCall($L.class, $L, $L, $L, $L, $L, encodedConstructor, $L)",
322-
className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY, INITIAL_VALUE);
322+
className, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT, BINARY, INITIAL_VALUE);
323323
} else {
324324
methodBuilder.addStatement(
325325
"return deployRemoteCall($L.class, $L, $L, $L, $L, $L, encodedConstructor)",
326-
className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY);
326+
className, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT, BINARY);
327327
}
328328

329329
return methodBuilder.build();
@@ -340,7 +340,7 @@ private static MethodSpec buildDeployWithParams(
340340
methodBuilder.addStatement(
341341
"return deployRemoteCall"
342342
+ "($L.class, $L, $L, $L, $L, $L, $L, $L, $L, $L, encodedConstructor)",
343-
className, WEB3J, authName, QUOTA, NONCE, VALID_UNTIL_BLOCK,
343+
className, NERVOSJ, authName, QUOTA, NONCE, VALID_UNTIL_BLOCK,
344344
VERSION, CHAIN_ID, VALUE, BINARY);
345345
return methodBuilder.build();
346346
}
@@ -351,11 +351,11 @@ private static MethodSpec buildDeployNoParams(
351351
if (isPayable) {
352352
methodBuilder.addStatement(
353353
"return deployRemoteCall($L.class, $L, $L, $L, $L, $L, \"\", $L)",
354-
className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY, INITIAL_VALUE);
354+
className, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT, BINARY, INITIAL_VALUE);
355355
} else {
356356
methodBuilder.addStatement(
357357
"return deployRemoteCall($L.class, $L, $L, $L, $L, $L, \"\")",
358-
className, WEB3J, authName, GAS_PRICE, GAS_LIMIT, BINARY);
358+
className, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT, BINARY);
359359
}
360360

361361
return methodBuilder.build();
@@ -367,7 +367,7 @@ private static MethodSpec buildDeployNoParams(
367367
methodBuilder.addStatement(
368368
"return deployRemoteCall"
369369
+ "($L.class, $L, $L, $L, $L, $L, $L, $L, $L, $L, \"\")",
370-
className, WEB3J, authName, QUOTA, NONCE,
370+
className, NERVOSJ, authName, QUOTA, NONCE,
371371
VALID_UNTIL_BLOCK, VERSION, CHAIN_ID, VALUE, BINARY);
372372
return methodBuilder.build();
373373
}
@@ -378,7 +378,7 @@ private static MethodSpec.Builder getDeployMethodSpec(
378378
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
379379
.returns(
380380
buildRemoteCall(TypeVariableName.get(className, Type.class)))
381-
.addParameter(Nervosj.class, WEB3J)
381+
.addParameter(Nervosj.class, NERVOSJ)
382382
.addParameter(authType, authName)
383383
.addParameter(BigInteger.class, GAS_PRICE)
384384
.addParameter(BigInteger.class, GAS_LIMIT);
@@ -396,7 +396,7 @@ private static MethodSpec.Builder getDeployMethodSpec(
396396
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
397397
.returns(
398398
buildRemoteCall(TypeVariableName.get(className, Type.class)))
399-
.addParameter(Nervosj.class, WEB3J)
399+
.addParameter(Nervosj.class, NERVOSJ)
400400
.addParameter(authType, authName)
401401
.addParameter(Long.class, QUOTA)
402402
.addParameter(BigInteger.class, NONCE)
@@ -413,12 +413,12 @@ private static MethodSpec buildLoad(
413413
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
414414
.returns(TypeVariableName.get(className, Type.class))
415415
.addParameter(String.class, CONTRACT_ADDRESS)
416-
.addParameter(Nervosj.class, WEB3J)
416+
.addParameter(Nervosj.class, NERVOSJ)
417417
.addParameter(authType, authName)
418418
.addParameter(BigInteger.class, GAS_PRICE)
419419
.addParameter(BigInteger.class, GAS_LIMIT)
420420
.addStatement("return new $L($L, $L, $L, $L, $L)", className,
421-
CONTRACT_ADDRESS, WEB3J, authName, GAS_PRICE, GAS_LIMIT)
421+
CONTRACT_ADDRESS, NERVOSJ, authName, GAS_PRICE, GAS_LIMIT)
422422
.build();
423423
}
424424

@@ -428,10 +428,10 @@ private static MethodSpec buildLoadAdaptToCita(
428428
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
429429
.returns(TypeVariableName.get(className, Type.class))
430430
.addParameter(String.class, CONTRACT_ADDRESS)
431-
.addParameter(Nervosj.class, WEB3J)
431+
.addParameter(Nervosj.class, NERVOSJ)
432432
.addParameter(authType, authName)
433433
.addStatement("return new $L($L, $L, $L)", className,
434-
CONTRACT_ADDRESS, WEB3J, authName)
434+
CONTRACT_ADDRESS, NERVOSJ, authName)
435435
.build();
436436
}
437437

@@ -671,7 +671,7 @@ private void buildConstantFunction(
671671
buildVariableLengthReturnFunctionConstructor(
672672
methodBuilder, functionName, inputParams, outputParameterTypes);
673673

674-
buildTupleResultContainer(methodBuilder, parameterizedTupleType);
674+
buildTupleResultContainer(methodBuilder, parameterizedTupleType, outputParameterTypes);
675675
}
676676
}
677677

@@ -964,30 +964,54 @@ private static void buildVariableLengthReturnFunctionConstructor(
964964
}
965965

966966
private void buildTupleResultContainer(
967-
MethodSpec.Builder methodBuilder, ParameterizedTypeName tupleType)
967+
MethodSpec.Builder methodBuilder, ParameterizedTypeName tupleType,
968+
List<TypeName> outputParameterTypes)
968969
throws ClassNotFoundException {
969970

970971
List<TypeName> typeArguments = tupleType.typeArguments;
971972

972973
CodeBlock.Builder tupleConstructor = CodeBlock.builder();
973974
tupleConstructor.addStatement(
974-
"$T results = executeCallMultipleValueReturn(function);",
975-
ParameterizedTypeName.get(List.class, Type.class))
975+
"$T results = executeCallMultipleValueReturn(function);",
976+
ParameterizedTypeName.get(List.class, Type.class))
976977
.add("return new $T(", tupleType)
977978
.add("$>$>");
978979

979-
String resultString = "\n($T) results.get($L)";
980+
String resultStringSimple = "\n($T) results.get($L)";
980981
if (useNativeJavaTypes) {
981-
resultString += ".getValue()";
982+
resultStringSimple += ".getValue()";
982983
}
983984

985+
String resultStringNativeList =
986+
"\nconvertToNative(($T) results.get($L).getValue())";
987+
984988
int size = typeArguments.size();
985-
for (int i = 0; i < size - 1; i++) {
989+
ClassName classList = ClassName.get(List.class);
990+
991+
for (int i = 0; i < size; i++) {
992+
TypeName param = outputParameterTypes.get(i);
993+
TypeName convertTo = typeArguments.get(i);
994+
995+
String resultString = resultStringSimple;
996+
997+
// If we use native java types we need to convert
998+
// elements of arrays to native java types too
999+
if (useNativeJavaTypes && param instanceof ParameterizedTypeName) {
1000+
ParameterizedTypeName oldContainer = (ParameterizedTypeName)param;
1001+
ParameterizedTypeName newContainer = (ParameterizedTypeName)convertTo;
1002+
if (newContainer.rawType.compareTo(classList) == 0
1003+
&& newContainer.typeArguments.size() == 1) {
1004+
convertTo = ParameterizedTypeName.get(classList,
1005+
oldContainer.typeArguments.get(0));
1006+
resultString = resultStringNativeList;
1007+
}
1008+
}
1009+
9861010
tupleConstructor
987-
.add(resultString + ", ", typeArguments.get(i), i);
1011+
.add(resultString, convertTo, i);
1012+
tupleConstructor.add(i < size - 1 ? ", " : ");\n");
9881013
}
989-
tupleConstructor
990-
.add(resultString + ");\n", typeArguments.get(size - 1), size - 1);
1014+
9911015
tupleConstructor.add("$<$<");
9921016

9931017
TypeSpec callableType = TypeSpec.anonymousClassBuilder("")

codegen/src/test/java/org/nervos/appchain/codegen/SolidityFunctionWrapperGeneratorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public void testShipIt() throws Exception {
7979
testCodeGenerationSolidityTypes("shipit", "ShipIt");
8080
}
8181

82+
@Test
83+
public void testSimpleGet() throws Exception {
84+
testCodeGenerationJvmTypes("simpleget", "SimpleGet");
85+
testCodeGenerationSolidityTypes("simpleget", "SimpleGet");
86+
}
87+
8288
private void testCodeGenerationJvmTypes(
8389
String contractName, String inputFileName) throws Exception {
8490

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pragma solidity ^0.4.0;
2+
contract SimpleGet {
3+
bytes32[5] val;
4+
bytes32 a;
5+
function setVal() public {
6+
for (uint i=0; i<5; i++) {
7+
val[i] = 0x12121212;
8+
}
9+
a = 0x12121212;
10+
}
11+
12+
function getSingleVal() public view returns(bytes32) {
13+
return a;
14+
}
15+
16+
function getVal() public view returns(bytes32,bytes32[5]) {
17+
return (a,val);
18+
}
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"constant":true,"inputs":[],"name":"getSingleVal","outputs":[{"name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"setVal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getVal","outputs":[{"name":"","type":"bytes32"},{"name":"","type":"bytes32[5]"}],"payable":false,"stateMutability":"view","type":"function"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6060604052341561000f57600080fd5b6101c08061001e6000396000f3006060604052600436106100565763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166317371f45811461005b5780635cfb28e714610080578063e1cb0e5214610095575b600080fd5b341561006657600080fd5b61006e6100e7565b60405190815260200160405180910390f35b341561008b57600080fd5b6100936100ed565b005b34156100a057600080fd5b6100a8610120565b604051828152602081018260a080838360005b838110156100d35780820151838201526020016100bb565b505050509050019250505060405180910390f35b60055490565b60005b60058110156101155763121212126000826005811061010b57fe5b01556001016100f0565b506312121212600555565b600061012a61016b565b600554600080600580602002604051908101604052919060a0830182845b815481526001909101906020018083116101485750505050509050915091509091565b60a06040519081016040526005815b6000815260001991909101906020018161017a57905050905600a165627a7a72305820e51a5fa504bf7f785949598fa71c8fc1a919f2a00d6c703770b67641d82cc8520029

0 commit comments

Comments
 (0)