From 7f839a1a1abce9dd56ddf2c0255432f5bf06a8df Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Wed, 31 Jul 2024 15:19:13 +0200 Subject: [PATCH 1/5] 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/5] 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/5] 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/5] 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 6f6600a3b784267ce67476dd963e877467f7d078 Mon Sep 17 00:00:00 2001 From: Julius Lauterbach Date: Thu, 1 Aug 2024 10:34:04 +0200 Subject: [PATCH 5/5] updated README with new repo name --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4e5aa59..a19bfc8 100644 --- a/README.md +++ b/README.md @@ -77,9 +77,9 @@ 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 +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 @@ -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,14 +112,14 @@ 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-2-solution, in short you can see it [here](https://github.com/Julius278/eth-web3j/pull/1) +* A possible solution is shown in branch: challenge-2-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 * 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 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