Skip to content
Open
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ Hey, glad you're ready to learn about Web3j.</br>
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check warning

Code scanning / CodeQL

Cross-site scripting

Cross-site scripting vulnerability due to a [user-provided value](1).
}

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<PLACE_YOUR_ID_HERE>";
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
public class PropertyModel {
private String id;
private String name;
private String description;
private BigInteger value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,18 @@ public List<PropertyModel> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ web3.keyfile.password=password
#web3.propertySafe.address=0xe94487720ad6515d2e6ca98e20fe98580da96083

#holesky
web3.propertySafe.address=0x3be21b25ef8eca53b3de604a4a57e46569cc2e49
#challenge 1
#web3.propertySafe.address=0x3be21b25ef8eca53b3de604a4a57e46569cc2e49

#challenge 2
#web3.propertySafe.address=0xf7c76a829d311963c9efd415635b69147c6bafa0

#challenge 3
web3.propertySafe.address=0x67b9bbe7c81550363a8a0769e15d642079cbc42e
12 changes: 12 additions & 0 deletions src/main/resources/contracts/Property.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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;
}
}