Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Smart contract compilation update and utility class #1456

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,15 @@ void testWithFrameworkEntrypoint() {
.address(Address.fromHexString("0x22222"))
.balance(Wei.of(1000))
.nonce(6)
.code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class))
.build();

ToyAccount snippetAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.of(1000))
.nonce(7)
.code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class))
.build();

Function snippetFunction =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ void testWithFrameworkEntrypoint() {
.address(Address.fromHexString("0x22222"))
.balance(Wei.ONE)
.nonce(5)
.code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class))
.build();

ToyAccount snippetAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class))
.build();

Function snippetFunction =
Expand Down Expand Up @@ -151,7 +151,7 @@ void testSnippetIndependently() {
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SmartContractUtils.getSolidityContractByteCode(TestSnippet_Events.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestSnippet_Events.class))
.build();

Function function =
Expand Down Expand Up @@ -201,7 +201,7 @@ void testContractNotRelatedToTestingFramework() {
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SmartContractUtils.getSolidityContractByteCode(TestStorage.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(TestStorage.class))
.build();

Function function =
Expand Down Expand Up @@ -240,15 +240,15 @@ void testYul() {
.address(Address.fromHexString("0x22222"))
.balance(Wei.ONE)
.nonce(5)
.code(SmartContractUtils.getSolidityContractByteCode(FrameworkEntrypoint.class))
.code(SmartContractUtils.getSolidityContractRuntimeByteCode(FrameworkEntrypoint.class))
.build();

ToyAccount yulAccount =
ToyAccount.builder()
.address(Address.fromHexString("0x11111"))
.balance(Wei.ONE)
.nonce(6)
.code(SmartContractUtils.getYulContractByteCode("DynamicBytecode.yul"))
.code(SmartContractUtils.getYulContractRuntimeByteCode("DynamicBytecode.yul"))
.build();

Function yulFunction = new Function("Write", Collections.emptyList(), Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class SmartContractUtils {
* runtime bytecode for solidity contract.
*
* @param contractClass: The web3j wrapper class that is autogenerated from the solidity contract
* @return The depolyed or runtime bytecode for the solidity contract
* @return The deployed or runtime bytecode for the solidity contract
*/
public static Bytes getSolidityContractByteCode(Class<? extends Contract> contractClass) {
public static Bytes getSolidityContractRuntimeByteCode(Class<? extends Contract> contractClass) {
String contractResourcePath = "solidity/%s.json".formatted(contractClass.getSimpleName());
URL contractResourceURL = classLoader.getResource(contractResourcePath);
try {
Expand All @@ -56,15 +56,37 @@ public static Bytes getSolidityContractByteCode(Class<? extends Contract> contra
throw new RuntimeException("Could not find contract bytecode");
}

/**
* @param contractClass: The web3j wrapper class that is autogenerated from the solidity contract
* @return The compiled bytecode for the solidity contract
*/
public static Bytes getSolidityContractCompiledByteCode(Class<? extends Contract> contractClass) {
String contractResourcePath = "solidity/%s.json".formatted(contractClass.getSimpleName());
URL contractResourceURL = classLoader.getResource(contractResourcePath);
try {
JsonNode jsonRoot = objectMapper.readTree(contractResourceURL);
Iterator<Map.Entry<String, JsonNode>> contracts = jsonRoot.get("contracts").fields();
while (contracts.hasNext()) {
Map.Entry<String, JsonNode> contract = contracts.next();
if (contract.getKey().contains(contractClass.getSimpleName())) {
return Bytes.fromHexStringLenient(contract.getValue().get("bin").asText());
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
throw new RuntimeException("Could not find contract bytecode");
}

/**
* The yul contracts under testing/src/main/yul are compiled using the solidity compiler via the
* yul plugin This method reads the output of the solidity compiler output file and returns the
* deployed or runtime bytecode for yul contract.
*
* @param yulFileName: The yul contract filename along with .yul suffix
* @return The depolyed or runtime bytecode for the yul contract
* @return The deployed or runtime bytecode for the yul contract
*/
public static Bytes getYulContractByteCode(final String yulFileName) {
public static Bytes getYulContractRuntimeByteCode(final String yulFileName) {
if (!yulFileName.contains(".yul")) {
throw new IllegalArgumentException("Yul file name should contain the suffix: .yul");
}
Expand All @@ -84,7 +106,35 @@ public static Bytes getYulContractByteCode(final String yulFileName) {
.asText();
return Bytes.fromHexStringLenient(byteCode);
} catch (Exception e) {
throw new RuntimeException(e);
throw new RuntimeException("Could not find contract bytecode", e);
}
}

/**
* @param yulFileName: The yul contract filename along with .yul suffix
* @return The compiled bytecode for the yul contract
*/
public static Bytes getYulContractCompiledByteCode(final String yulFileName) {
if (!yulFileName.contains(".yul")) {
throw new IllegalArgumentException("Yul file name should contain the suffix: .yul");
}
String yulFileNameWithoutSuffix = yulFileName.replace(".yul", "");
String contractResourcePath = "yul/%s.json".formatted(yulFileNameWithoutSuffix);
URL contractResourceURL = classLoader.getResource(contractResourcePath);
try {
JsonNode jsonRoot = objectMapper.readTree(contractResourceURL);
String byteCode =
jsonRoot
.get("contracts")
.get(yulFileName)
.get(yulFileNameWithoutSuffix)
.get("evm")
.get("bytecode")
.get("object")
.asText();
return Bytes.fromHexStringLenient(byteCode);
} catch (Exception e) {
throw new RuntimeException("Could not find contract bytecode", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"settings": {
"evmVersion": "london",
"optimizer": { "enabled": true, "details": { "yul": true } },
"optimizer": { "enabled": false, "details": { "yul": true } },
"outputSelection": {
"*": {
"": ["ast"],
Expand Down
Loading