-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
gtebrean
merged 8 commits into
LFDT-web3j:main
from
psychoplasma:custom-error-type-generation
Apr 9, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b864b7c
Add CustomError class for solidity error type
psychoplasma b4b09df
Add encoder for CustomError
psychoplasma 2452a40
Add support to generate CustomError definitions for solidity error ty…
psychoplasma b273181
Fix linting issue
psychoplasma 3423c59
Merge remote-tracking branch 'upstream/main' into custom-error-type-g…
psychoplasma abf0f6b
Remove generation of list of custom errors which would create a depen…
psychoplasma 54e1b61
Update CHANGELOG file
psychoplasma 1320362
fix change log
gtebrean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
abi/src/main/java/org/web3j/abi/datatypes/CustomError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
abi/src/test/java/org/web3j/abi/CustomErrorEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
@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
43
abi/src/test/java/org/web3j/abi/datatypes/CustomErrorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 toEventEncoder
, andEventEncoder
could be used instead, which makesCustomErrorEncoder
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.