Skip to content

Support for Generation of Custom Error type #2173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 9, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

* bump snapshot version to 4.13.1 [#2160](https://github.com/hyperledger-web3j/web3j/pull/2160)
* Upgrade to https://github.com/Consensys/tuweni/releases/tag/v2.7.0 [#2170](https://github.com/LFDT-web3j/web3j/pull/2170)
* Add support for code generation of custom error type introduced with `solidity v0.8.4` [#2173](https://github.com/LFDT-web3j/web3j/pull/2173)


### BREAKING CHANGES
Expand Down
53 changes: 53 additions & 0 deletions abi/src/main/java/org/web3j/abi/CustomErrorEncoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.abi;

import java.util.List;
import java.util.stream.Collectors;

import org.web3j.abi.datatypes.CustomError;
import org.web3j.abi.datatypes.Type;
import org.web3j.crypto.Hash;
import org.web3j.utils.Numeric;

/**
* Ethereum custom error encoding. Further limited details are available <a
* href="https://docs.soliditylang.org/en/develop/abi-spec.html#errors">here</a>.
*/
public class CustomErrorEncoder {

private CustomErrorEncoder() {}

public static String encode(CustomError error) {
return calculateSignatureHash(buildErrorSignature(error.getName(), error.getParameters()));
}

static <T extends Type> String buildErrorSignature(
String errorName, List<TypeReference<T>> parameters) {

StringBuilder result = new StringBuilder();
result.append(errorName);
result.append("(");
String params =
parameters.stream().map(Utils::getTypeName).collect(Collectors.joining(","));
result.append(params);
result.append(")");
return result.toString();
}

public static String calculateSignatureHash(String errorSignature) {
byte[] input = errorSignature.getBytes();
byte[] hash = Hash.sha3(input);
return Numeric.toHexString(hash);
}
}
38 changes: 38 additions & 0 deletions abi/src/main/java/org/web3j/abi/datatypes/CustomError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.abi.datatypes;

import java.util.List;

import org.web3j.abi.TypeReference;

import static org.web3j.abi.Utils.convert;

/** CustomError wrapper type. */
public class CustomError {
private String name;
private List<TypeReference<Type>> parameters;

public CustomError(String name, List<TypeReference<?>> parameters) {
this.name = name;
this.parameters = convert(parameters);
}

public String getName() {
return name;
}

public List<TypeReference<Type>> getParameters() {
return parameters;
}
}
90 changes: 90 additions & 0 deletions abi/src/test/java/org/web3j/abi/CustomErrorEncoderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2019 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.abi;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.CustomError;
import org.web3j.abi.datatypes.DynamicArray;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.web3j.abi.Utils.convert;

public class CustomErrorEncoderTest {
@Test
public void testCalculateSignatureHash() {
assertEquals(
CustomErrorEncoder.calculateSignatureHash("InvalidAccess(address,string,uint256)"),
("0xcb5157bf1b439b9573ea7a95f7c00cc33f832ed728345c2bd29146ce58bbab57"));

assertEquals(
CustomErrorEncoder.calculateSignatureHash("RandomError(address[],bytes)"),
("0xbf37b77ddf0fbbf29ee6a3ebda3d177c2d438123b10571806c57958230d9f905"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All unit tests are on point, but I'm interested how did you generate the expected results for all the tests?

Copy link
Contributor Author

@psychoplasma psychoplasma Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generated the expected signature hashes used in test cases with ethers.js.

Actually, CustomErrorEncoder is totally identical to EventEncoder, and EventEncoder could be used instead, which makes CustomErrorEncoder looking like a code duplication. But, I think, keeping separate classes for Solidity abi types would be a better option in case of any changes on Solidity side in the future.

}

@Test
public void testEncode() {
CustomError error =
new CustomError(
"InvalidAccess",
Arrays.<TypeReference<?>>asList(
new TypeReference<Address>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Uint256>() {}));

assertEquals(
CustomErrorEncoder.encode(error),
"0xcb5157bf1b439b9573ea7a95f7c00cc33f832ed728345c2bd29146ce58bbab57");
}

@Test
public void testBuildErrorSignature() {
List<TypeReference<?>> parameters =
Arrays.<TypeReference<?>>asList(
new TypeReference<Address>() {},
new TypeReference<Utf8String>() {},
new TypeReference<Uint256>() {});

assertEquals(
"InvalidAccess(address,string,uint256)",
CustomErrorEncoder.buildErrorSignature("InvalidAccess", convert(parameters)));
}

@Test
void testBuildErrorSignatureWithDynamicStructs() {
List<TypeReference<?>> parameters =
Arrays.asList(
new TypeReference<AbiV2TestFixture.Nazz>() {},
new TypeReference<AbiV2TestFixture.Foo>() {});

assertEquals(
"DynamicStructError((((string,string)[])[],uint256),(string,string))",
CustomErrorEncoder.buildErrorSignature("DynamicStructError", convert(parameters)));
}

@Test
void testBuildErrorSignatureWithDynamicArrays() {
List<TypeReference<?>> parameters =
Arrays.asList(new TypeReference<DynamicArray<AbiV2TestFixture.Nazz>>() {});

assertEquals(
"DynamicArrayError((((string,string)[])[],uint256)[])",
EventEncoder.buildMethodSignature("DynamicArrayError", convert(parameters)));
}
}
43 changes: 43 additions & 0 deletions abi/src/test/java/org/web3j/abi/datatypes/CustomErrorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2020 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.abi.datatypes;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.junit.jupiter.api.Test;

import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.generated.Uint256;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CustomErrorTest {

@Test
public void testCreation() {

List<TypeReference<?>> parameters =
Arrays.<TypeReference<?>>asList(
new TypeReference<Address>() {}, new TypeReference<Uint256>() {});
CustomError event = new CustomError("MyError", parameters);

assertEquals(event.getName(), "MyError");

Iterator<TypeReference<?>> expectedParameter = parameters.iterator();
for (TypeReference<?> actualParameter : event.getParameters()) {
assertEquals(expectedParameter.next(), actualParameter);
}
}
}
Loading