Skip to content

Commit

Permalink
check for custom network
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikebbers committed Jul 14, 2024
1 parent 9ffa3c6 commit 7936ab3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hedera.hashgraph.sdk.ContractId;
import com.openelements.hedera.base.data.ContractVerificationState;
import com.openelements.hedera.base.implementation.HederaNetwork;
import com.openelements.spring.hedera.api.ContractVerificationClient;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
Expand All @@ -24,18 +25,24 @@ public class ContractVerificationClientImplementation implements ContractVerific

private record VerifyRequest(String address, String chain, String creatorTxHash, String chosenContract, Map<String, String> files) {}

private final String chainId;
private final HederaNetwork hederaNetwork;

private final ObjectMapper objectMapper;

private final RestClient restClient;

public ContractVerificationClientImplementation(@NonNull String chainId) {
this.chainId = Objects.requireNonNull(chainId, "hederaNetwork must not be null");
public ContractVerificationClientImplementation(@NonNull HederaNetwork hederaNetwork) {
this.hederaNetwork = Objects.requireNonNull(hederaNetwork, "hederaNetwork must not be null");
objectMapper = new ObjectMapper();
restClient = RestClient.create();
}

private void checkSupportedNetwork() {
if(hederaNetwork == HederaNetwork.CUSTOM) {
throw new IllegalArgumentException("Custom network is not supported");
}
}

private void handleError(HttpRequest request, ClientHttpResponse response) throws IOException {
final String error;
try {
Expand Down Expand Up @@ -64,6 +71,7 @@ private void handleError(HttpRequest request, ClientHttpResponse response) throw

@Override
public ContractVerificationState verify(ContractId contractId, String contractName, Map<String, String> files) {
checkSupportedNetwork();

final ContractVerificationState state = checkVerification(contractId);
if(state != ContractVerificationState.NONE) {
Expand All @@ -72,7 +80,7 @@ public ContractVerificationState verify(ContractId contractId, String contractNa

final VerifyRequest verifyRequest = new VerifyRequest(
contractId.toSolidityAddress(),
chainId,
hederaNetwork.getChainId(),
"",
"",
files
Expand Down Expand Up @@ -130,7 +138,9 @@ public ContractVerificationState verify(ContractId contractId, String contractNa

@Override
public ContractVerificationState checkVerification(ContractId contractId) {
final String uri = CONTRACT_VERIFICATION_URL + "/check-by-addresses" + "?addresses=" + contractId.toSolidityAddress() + "&chainIds=" + chainId;
checkSupportedNetwork();

final String uri = CONTRACT_VERIFICATION_URL + "/check-by-addresses" + "?addresses=" + contractId.toSolidityAddress() + "&chainIds=" + hederaNetwork.getChainId();

final String resultBody = restClient.get()
.uri(uri)
Expand Down Expand Up @@ -171,12 +181,14 @@ public ContractVerificationState checkVerification(ContractId contractId) {

@Override
public boolean checkVerification(ContractId contractId, String fileName, String fileContent) {
checkSupportedNetwork();

final ContractVerificationState state = checkVerification(contractId);
if(state != ContractVerificationState.FULL) {
throw new IllegalStateException("Contract is not verified");
}

final String uri = CONTRACT_VERIFICATION_URL + "/files/" + chainId + "/" + contractId.toSolidityAddress();
final String uri = CONTRACT_VERIFICATION_URL + "/files/" + hederaNetwork.getChainId() + "/" + contractId.toSolidityAddress();

final String resultBody = restClient.get()
.uri(uri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ HederaClient hederaClient(final Client client) {

@Bean
ContractVerificationClient contractVerificationClient(HederaNetwork hederaNetwork) {
if(hederaNetwork == HederaNetwork.CUSTOM) {
throw new IllegalArgumentException("Contract verification is not supported for custom networks");
}
return new ContractVerificationClientImplementation(hederaNetwork.getChainId() + "");
return new ContractVerificationClientImplementation(hederaNetwork);
}
}

0 comments on commit 7936ab3

Please sign in to comment.