diff --git a/README.md b/README.md index 9b02600..64834c5 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,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 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..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 @@ -30,4 +30,23 @@ 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; + } + + @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 7e6a562..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 @@ -19,12 +19,12 @@ 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); 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 = "0x67b9bbe7c81550363a8a0769e15d642079cbc42e"; // only change these three public static final String EXTERNAL_PROPERTY_ID = ""; @@ -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 e78702a..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 @@ -47,6 +47,33 @@ public String getPropertyName(String propertyId) { } } + 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); + } + } + + 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 837e56b..2c22682 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,4 +11,11 @@ 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 + +#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 556eee2..81baca2 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,15 @@ contract Property is IProperty { return name; } + function setDescription(string memory _description) public{ + description = _description; + } + + function getDescription() public view returns (string memory){ + return description; + } + + function sum(int a, int b) public pure returns (int){ + return a + b; + } }