Skip to content

Commit

Permalink
spotless apply
Browse files Browse the repository at this point in the history
  • Loading branch information
tonykwok1992 committed Mar 13, 2024
1 parent e8afcda commit 1d56059
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1460,13 +1460,20 @@ List<MethodSpec> buildFunctions(
MethodSpec buildLinkLibraryMethod() {
MethodSpec.Builder methodBuilder =
MethodSpec.methodBuilder("linkLibraries")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(ParameterizedTypeName.get(
ClassName.get(List.class), ClassName.get(Contract.LinkReference.class)), "references")
.addStatement(BINARY + " = " + "linkBinaryWithReferences(" + BINARY + ", references)");
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(
ParameterizedTypeName.get(
ClassName.get(List.class),
ClassName.get(Contract.LinkReference.class)),
"references")
.addStatement(
BINARY
+ " = "
+ "linkBinaryWithReferences("
+ BINARY
+ ", references)");

return methodBuilder.build();

}

private void buildConstantFunction(
Expand Down
14 changes: 12 additions & 2 deletions codegen/src/test/java/org/web3j/codegen/ContractJsonParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,18 @@ public void testLinkBinaryWithReferences() throws Exception {
Contract mc = parseContractJson(contractBaseDir, "MetaCoin", "MetaCoin");
assertTrue(mc.getBytecode().contains("__ConvertLib____________________________"));

String linked = linkBinaryWithReferences(mc.getBytecode(), Collections.singletonList(new org.web3j.tx.Contract.LinkReference("./ConvertLib.sol", "ConvertLib" , Address.DEFAULT)));
String linked =
linkBinaryWithReferences(
mc.getBytecode(),
Collections.singletonList(
new org.web3j.tx.Contract.LinkReference(
"./ConvertLib.sol", "ConvertLib", Address.DEFAULT)));
assertFalse(linked.contains("__ConvertLib____________________________"));
assertEquals(mc.getBytecode().replace("__ConvertLib____________________________", Address.DEFAULT.toString().substring(2)), linked);
assertEquals(
mc.getBytecode()
.replace(
"__ConvertLib____________________________",
Address.DEFAULT.toString().substring(2)),
linked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,9 @@ public void testBuildFunctionLinkBinaryWithReferences() throws Exception {
MethodSpec methodSpec = solidityFunctionWrapper.buildLinkLibraryMethod();

String expected =
"public static void linkLibraries(java.util.List<org.web3j.tx.Contract.LinkReference> references) {\n" +
" BINARY = linkBinaryWithReferences(BINARY, references);\n" +
"}\n";
"public static void linkLibraries(java.util.List<org.web3j.tx.Contract.LinkReference> references) {\n"
+ " BINARY = linkBinaryWithReferences(BINARY, references);\n"
+ "}\n";

assertEquals(methodSpec.toString(), (expected));
}
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,17 +501,18 @@ public LinkReference(String source, String libraryName, Address address) {
}
}

public static String linkBinaryWithReferences(String binary, List<LinkReference> links){
public static String linkBinaryWithReferences(String binary, List<LinkReference> links) {
String replacingBinary = binary;
for(LinkReference link: links) {
//solc / hardhat convention
for (LinkReference link : links) {
// solc / hardhat convention
String libSourceName = link.source + ":" + link.libraryName;
String placeHolder = "__$" + sha3String(libSourceName).substring(2, 36) + "$__";
String addressReplacement = cleanHexPrefix(link.address.toString());
replacingBinary = replacingBinary.replace(placeHolder, addressReplacement);

//truffle old version
String trufflePlaceHolder = "__" + link.libraryName + "_".repeat(40 - link.libraryName.length() - 2);
// truffle old version
String trufflePlaceHolder =
"__" + link.libraryName + "_".repeat(40 - link.libraryName.length() - 2);
replacingBinary = replacingBinary.replace(trufflePlaceHolder, addressReplacement);
}
return replacingBinary;
Expand Down

0 comments on commit 1d56059

Please sign in to comment.