Skip to content

Commit

Permalink
Add encodeToHex method to AbiFunction
Browse files Browse the repository at this point in the history
Introduced encodeToHex method in AbiFunction to encode arguments into a hexadecimal string. Also added a unit test to verify the new method with Uniswap function encoding.
  • Loading branch information
wkennedy committed Jul 31, 2024
1 parent 59a8edd commit ab2f598
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/github/wkennedy/abi/entry/AbiFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.github.wkennedy.abi.SolidityType;
import com.github.wkennedy.util.ByteUtil;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;

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

import static com.github.wkennedy.abi.SolidityType.IntType.encodeInt;
Expand All @@ -28,10 +30,27 @@ public AbiFunction(boolean constant, String name, List<AbiParam> inputs, List<Ab
super(null, constant, name, inputs, outputs, AbiType.function, payable);
}

/**
* Encodes the given arguments into a byte array by merging the encoded signature and encoded arguments.
*
* @param args The arguments to encode.
* @return The encoded byte array.
*/
public byte[] encode(Object... args) {
return ByteUtil.merge(encodeSignature(), encodeArguments(args));
}

/**
* Encodes the given arguments into a hexadecimal string by merging the encoded signature and encoded arguments.
*
* @param args The arguments to encode.
* @return The encoded hexadecimal string.
*/
public String encodeToHex(Object... args) {
byte[] methodBytes = encode(args);
return HEX_PREFIX + Hex.encodeHexString(methodBytes);
}

private ImmutablePair<Integer, Integer> computeStaticSizeAndDynamicParams(Object... args) {
int staticSize = 0;
int dynamicCnt = 0;
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/com/github/wkennedy/abi/entry/AbiFunctionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ public void testUniswapFunctionDecode() throws IOException, DecoderException {
assertEquals(BigInteger.valueOf(18), decode.getFirst());
}

@Test
public void testUniswapFunctionEncode() throws IOException, DecoderException {
String abiJson = new String(Files.readAllBytes(Paths.get("./src/test/resources/uniswap_abi.json")));
Decoder decoder = new Decoder();
decoder.addAbi("0x731847de5b19b26039f283826ae5218ac7e070ed1b7fff689c2253a3035d8bd6", abiJson);
Predicate<AbiFunction> exists = fn -> fn.name.equals("getAmountsOut");
BigInteger amountIn = new BigInteger("1000000000000000000");
String[] path = {"0x0000000000000000000000000000456E65726779", "0x45429A2255e7248e57fce99E7239aED3f84B7a53"};

Optional<AbiFunction> function = decoder.getAbis().get("0x731847de5b19b26039f283826ae5218ac7e070ed1b7fff689c2253a3035d8bd6").findFunction(exists);
String encodeToHex = function.get().encodeToHex(amountIn, path);
assertNotNull(encodeToHex);
assertEquals("0xd06ca61f0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000456e6572677900000000000000000000000045429a2255e7248e57fce99e7239aed3f84b7a53", encodeToHex);
}

@Test
void encode_withValidArgs_shouldReturnBytes() {
AbiFunction testedAbiFunction = new AbiFunction(
Expand Down

0 comments on commit ab2f598

Please sign in to comment.