From 7f839a1a1abce9dd56ddf2c0255432f5bf06a8df Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:19:13 +0200 Subject: [PATCH 1/7] possible solution for challenge 2 --- .../controller/PropertyController.java | 13 +++++++++++ .../boot/ethweb3/demo/PlayAroundDemo.java | 8 ++++++- .../boot/ethweb3/model/PropertyModel.java | 1 + .../ethweb3/service/PropertySafeService.java | 6 +++++ .../boot/ethweb3/service/PropertyService.java | 23 ++++++++++++++++++- src/main/resources/contracts/Property.sol | 9 ++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java b/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java index 3212e9e..454e823 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java @@ -30,4 +30,17 @@ public String retrievePropertyName(@PathVariable("propertyId") String propertyId logger.info("retrievePropertyName"); return propertyService.getPropertyName(propertyId); } + + @GetMapping(path = "/api/property/{propertyId}/description") + public String retrievePropertyDescription(@PathVariable("propertyId") String propertyId){ + logger.info("retrievePropertyDescription"); + return propertyService.getPropertyDescription(propertyId); + } + + @PostMapping(path = "/api/property/{propertyId}/description") + public String setPropertyDescription(@PathVariable("propertyId") String propertyId, @RequestParam("description") String description){ + logger.info("setPropertyDescription"); + propertyService.setPropertyDescription(propertyId, description); + return "successfully set description for property " + propertyId; + } } diff --git a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java index 7e6a562..089d3df 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java @@ -19,7 +19,7 @@ import java.io.IOException; import java.math.BigInteger; -@SuppressWarnings("java:S112") +@SuppressWarnings({"java:S112", "java:S2629"}) public class PlayAroundDemo { public static final Logger LOGGER = LoggerFactory.getLogger(PlayAroundDemo.class); @@ -67,6 +67,12 @@ public static void main(String[] args) throws Exception { BigInteger value = property.value().send(); LOGGER.info("value of deployed property: {}", value); + + //Challenge 2 from here + property.setDescription("this is a description").send(); + + LOGGER.info("new description for property wit id '{}': {}", EXTERNAL_PROPERTY_ID, property.getDescription().send()); + } public static Credentials loadCredentials() { diff --git a/src/main/java/com/julius/spring/boot/ethweb3/model/PropertyModel.java b/src/main/java/com/julius/spring/boot/ethweb3/model/PropertyModel.java index 32c0d3d..e09c34d 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/model/PropertyModel.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/model/PropertyModel.java @@ -13,5 +13,6 @@ public class PropertyModel { private String id; private String name; + private String description; private BigInteger value; } diff --git a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertySafeService.java b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertySafeService.java index 2582923..b10cac7 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertySafeService.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertySafeService.java @@ -81,12 +81,18 @@ public List listPropertySafe() { String id = p.getPropertyId().send(); String name = p.getName().send(); + // if you're for the challenge 2 solution + // remember that you will need a new PropertySafe cause the Property contracts in the given safe do not have the new parameter + // it will fail to retrieve the description in this case + // at first, deploy a new PropertySafe and, create + String description = p.getDescription().send(); BigInteger value = p.getValue().send(); PropertyModel pm = new PropertyModel(); pm.setId(id); pm.setName(name); + pm.setDescription(description); pm.setValue(value); properties.add(pm); diff --git a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java index 37c0b56..db240bf 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java @@ -11,7 +11,7 @@ import java.math.BigInteger; -@SuppressWarnings("java:S2629") +@SuppressWarnings({"java:S2629", "java:S112"}) @Service public class PropertyService { @@ -31,6 +31,7 @@ public PropertyService(Web3j web3client, ContractGasProvider gasProvider, Proper public BigInteger getPropertyValue(String propertyId) { try { + logger.info("get property value for propertyId: {}", propertyId); return retrieveProperty(propertyId).getValue().send(); } catch (Exception e) { throw new RuntimeException(e); @@ -39,14 +40,34 @@ public BigInteger getPropertyValue(String propertyId) { public String getPropertyName(String propertyId) { try { + logger.info("get property name for propertyId: {}", propertyId); return retrieveProperty(propertyId).getName().send(); } catch (Exception e) { throw new RuntimeException(e); } } + public String getPropertyDescription(String propertyId) { + try { + logger.info("get property description for propertyId: {}", propertyId); + return retrieveProperty(propertyId).getDescription().send(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public void setPropertyDescription(String propertyId, String description) { + try { + logger.info("set property description for propertyId: {}", propertyId); + retrieveProperty(propertyId).setDescription(description).send(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + private Property retrieveProperty(String propertyId) { try { + logger.info("retrieve property by propertyId: {}", propertyId); var propertyAddress = propertySafe.getPropertyById(propertyId).send(); return Property.load(propertyAddress, web3client, credentials, gasProvider); } catch (Exception e) { diff --git a/src/main/resources/contracts/Property.sol b/src/main/resources/contracts/Property.sol index 556eee2..03ab60e 100644 --- a/src/main/resources/contracts/Property.sol +++ b/src/main/resources/contracts/Property.sol @@ -7,6 +7,7 @@ contract Property is IProperty { string internal id; string internal name; + string internal description; int public value; constructor(string memory _name, int _value){ @@ -38,4 +39,12 @@ contract Property is IProperty { return name; } + function setDescription(string memory _description) public{ + description = _description; + } + + function getDescription() public view returns (string memory){ + return description; + } + } From c7070977ab95caf66955efeaea007642c6f07508 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:22:49 +0200 Subject: [PATCH 2/7] possible solution for challenge 2 --- .../com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java | 2 +- src/main/resources/application.properties | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java index 089d3df..59375f1 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java @@ -24,7 +24,7 @@ public class PlayAroundDemo { public static final Logger LOGGER = LoggerFactory.getLogger(PlayAroundDemo.class); public static final String ETH_SERVER_ADDRESS = "https://ethereum-holesky-rpc.publicnode.com"; - private static final String SAFE_CONTRACT_ADDRESS = "0x3be21b25ef8eca53b3de604a4a57e46569cc2e49"; + private static final String SAFE_CONTRACT_ADDRESS = "0xf7c76a829d311963c9efd415635b69147c6bafa0"; // only change these three public static final String EXTERNAL_PROPERTY_ID = ""; diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 837e56b..b9e4ed7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,4 +11,8 @@ web3.keyfile.password=password #web3.propertySafe.address=0xe94487720ad6515d2e6ca98e20fe98580da96083 #holesky -web3.propertySafe.address=0x3be21b25ef8eca53b3de604a4a57e46569cc2e49 \ No newline at end of file +#challenge 1 +#web3.propertySafe.address=0x3be21b25ef8eca53b3de604a4a57e46569cc2e49 + +#challenge 2 +web3.propertySafe.address=0xf7c76a829d311963c9efd415635b69147c6bafa0 \ No newline at end of file From a2b28d163a1459e1b80eeb96e3fde70d3f39b0db Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:24:57 +0200 Subject: [PATCH 3/7] updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c4792fb..8d37e87 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ function getTest() external view returns (string memory){....} mvn clean install ``` * Use the new getter function in your Java code -* A possible solution is shown in branch: ????? +* A possible solution is shown in branch: challenge-2-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j/pull/1) ## Challenge 3 Let's do a calculation on-chain From 167e2e8c226e4f4bb4953b26803ba9cffebdd653 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:37:50 +0200 Subject: [PATCH 4/7] updated README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8d37e87..4e5aa59 100644 --- a/README.md +++ b/README.md @@ -116,10 +116,10 @@ mvn clean install ## Challenge 3 Let's do a calculation on-chain -* Add a new solidity function which takes two numbers (int) and returns +* Add a new solidity function which takes two numbers (int) and returns an int (like sum or multiply it) * Build the Java maven project again -* Use the new getter function in your Java code -* A possible solution is shown in branch: ????? +* Use the new function in your Java code +* A possible solution is shown in branch: challenge-3-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j/pull/2) ## Challenge 99 From 372977a42da13e3e1c412bdc37fbfc8b6a0f3546 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:51:04 +0200 Subject: [PATCH 5/7] possible solution for challenge 3 --- .../boot/ethweb3/controller/PropertyController.java | 6 ++++++ .../julius/spring/boot/ethweb3/demo/PlayAroundDemo.java | 2 +- .../spring/boot/ethweb3/service/PropertyService.java | 9 +++++++++ src/main/resources/application.properties | 5 ++++- src/main/resources/contracts/Property.sol | 3 +++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java b/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java index 454e823..6b014b7 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/controller/PropertyController.java @@ -43,4 +43,10 @@ public String setPropertyDescription(@PathVariable("propertyId") String property propertyService.setPropertyDescription(propertyId, description); return "successfully set description for property " + propertyId; } + + @GetMapping(path = "/api/property/{propertyId}/sum") + public BigInteger usePropertyContractSumFunction(@PathVariable("propertyId") String propertyId, @RequestParam("a") int a, @RequestParam("b") int b){ + logger.info("usePropertyContractSumFunction"); + return propertyService.useSumFunction(propertyId, a, b); + } } diff --git a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java index 59375f1..973529a 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java @@ -24,7 +24,7 @@ public class PlayAroundDemo { public static final Logger LOGGER = LoggerFactory.getLogger(PlayAroundDemo.class); public static final String ETH_SERVER_ADDRESS = "https://ethereum-holesky-rpc.publicnode.com"; - private static final String SAFE_CONTRACT_ADDRESS = "0xf7c76a829d311963c9efd415635b69147c6bafa0"; + private static final String SAFE_CONTRACT_ADDRESS = "0x67b9bbe7c81550363a8a0769e15d642079cbc42e"; // only change these three public static final String EXTERNAL_PROPERTY_ID = ""; diff --git a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java index db240bf..1d61f2a 100644 --- a/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java +++ b/src/main/java/com/julius/spring/boot/ethweb3/service/PropertyService.java @@ -65,6 +65,15 @@ public void setPropertyDescription(String propertyId, String description) { } } + public BigInteger useSumFunction(String propertyId, int a, int b) { + try { + logger.info("use sum function with property '{}', int a: '{}', int b: '{}'", propertyId, a, b); + return retrieveProperty(propertyId).sum(BigInteger.valueOf(a), BigInteger.valueOf(b)).send(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + private Property retrieveProperty(String propertyId) { try { logger.info("retrieve property by propertyId: {}", propertyId); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b9e4ed7..2c22682 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,4 +15,7 @@ web3.keyfile.password=password #web3.propertySafe.address=0x3be21b25ef8eca53b3de604a4a57e46569cc2e49 #challenge 2 -web3.propertySafe.address=0xf7c76a829d311963c9efd415635b69147c6bafa0 \ No newline at end of file +#web3.propertySafe.address=0xf7c76a829d311963c9efd415635b69147c6bafa0 + +#challenge 3 +web3.propertySafe.address=0x67b9bbe7c81550363a8a0769e15d642079cbc42e \ No newline at end of file diff --git a/src/main/resources/contracts/Property.sol b/src/main/resources/contracts/Property.sol index 03ab60e..81baca2 100644 --- a/src/main/resources/contracts/Property.sol +++ b/src/main/resources/contracts/Property.sol @@ -47,4 +47,7 @@ contract Property is IProperty { return description; } + function sum(int a, int b) public pure returns (int){ + return a + b; + } } From 0e259ba1e5d813ce86d697de8993562fa4fb81a8 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Thu, 1 Aug 2024 10:32:48 +0200 Subject: [PATCH 6/7] updated README with new repo name --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9b02600..9a1fbd1 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ Hey, glad you're ready to learn about Web3j.
Feel free to fork (or just clone) and play around. ## Challenge 1 -* Git clone project (https://github.com/Julius278/eth-web3j) +* Git clone project (https://github.com/Julius278/eth-web3j-java) ``` bash git clone https://github.com/Julius278/eth-web3j.git ``` @@ -97,7 +97,7 @@ mvn clean install ``` bash mvn spring-boot:run ``` -or use the [PlayAroundDemo](https://github.com/Julius278/eth-web3j/blob/main/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java) +or use the [PlayAroundDemo](https://github.com/Julius278/eth-web3j-java/blob/main/src/main/java/com/julius/spring/boot/ethweb3/demo/PlayAroundDemo.java) * Everything is prepared * you can just start by choosing your PropertyName and ID (uniqueness is checked on the PropertySafe) * you don't have to worry about setting up a node, wallet, funding of your account, transaction fees or the connection @@ -112,7 +112,7 @@ function getTest() external view returns (string memory){....} mvn clean install ``` * Use the new getter function in your Java code -* A possible solution is shown in branch: challenge-3-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j/pull/1) +* A possible solution is shown in branch: challenge-3-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j-java/pull/1) ## Challenge 3 Let's do a calculation on-chain @@ -123,7 +123,7 @@ Let's do a calculation on-chain * int in Solidity is way bigger than the normal int in Java, so in Java you will have to use BigInteger * Build the Java maven project again * Use the new function in your Java code -* A possible solution is shown in branch: challenge-3-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j/pull/2) +* A possible solution is shown in branch: challenge-3-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j-java/pull/2) ## Challenge 99 From ecc871d66e0118e40bfdf91ad6b7dd35d3e02e78 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Thu, 1 Aug 2024 10:35:06 +0200 Subject: [PATCH 7/7] updated README with new repo name --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a1fbd1..64834c5 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ Feel free to fork (or just clone) and play around. ## Challenge 1 * Git clone project (https://github.com/Julius278/eth-web3j-java) ``` bash -git clone https://github.com/Julius278/eth-web3j.git +git clone https://github.com/Julius278/eth-web3j-java.git ``` * (if you have a GitHub account and are familiar with the platform, you can also fork and clone your own repository) * you're on the "main" branch now