FunctionReturnDecoder { public static List<Type> decode( String rawInput, List<TypeReference<Type>> outputParameters) { String input = Numeric.cleanHexPrefix(rawInput); if (input.isEmpty()) { return Collections.emptyList(); } else { return build(input, outputParameters); } } private  FunctionReturnDecoder(); static List<Type> decode(
            String rawInput, List<TypeReference<Type>> outputParameters); @SuppressWarnings("unchecked") static Type decodeIndexedValue(
            String rawInput, TypeReference<T> typeReference);  }
@Test public void testSimpleFunctionDecode() { Function function = new Function( "test", Collections.<Type>emptyList(), Collections.<TypeReference<?>>singletonList(new TypeReference<Uint>(){}) ); assertThat(FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000037", function.getOutputParameters()), IsEqual.<List>equalTo(Collections.singletonList(new Uint(BigInteger.valueOf(55))))); }
@Test public void testSimpleFunctionStringResultDecode() { Function function = new Function("simple", Arrays.<Type>asList(), Collections.<TypeReference<?>>singletonList(new TypeReference<Utf8String>() { })); List<Type> utf8Strings = FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000020" + "000000000000000000000000000000000000000000000000000000000000000d" + "6f6e65206d6f72652074696d6500000000000000000000000000000000000000", function.getOutputParameters()); assertThat((String) utf8Strings.get(0).getValue(), is("one more time")); }
@Test public void testFunctionEmptyStringResultDecode() { Function function = new Function("test", Collections.<Type>emptyList(), Collections.<TypeReference<?>>singletonList(new TypeReference<Utf8String>() { })); List<Type> utf8Strings = FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000000", function.getOutputParameters()); assertThat((String) utf8Strings.get(0).getValue(), is("")); }
@Test public void testMultipleResultFunctionDecode() { Function function = new Function( "test", Collections.<Type>emptyList(), Arrays.<TypeReference<?>>asList( new TypeReference<Uint>() { }, new TypeReference<Uint>() { }) ); assertThat(FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000037" + "0000000000000000000000000000000000000000000000000000000000000007", function.getOutputParameters()), IsEqual.<List>equalTo(Arrays.asList(new Uint(BigInteger.valueOf(55)), new Uint(BigInteger.valueOf(7))))); }
@Test public void testDecodeMultipleStringValues() { Function function = new Function("function", Collections.<Type>emptyList(), Arrays.<TypeReference<?>>asList( new TypeReference<Utf8String>() { }, new TypeReference<Utf8String>() { }, new TypeReference<Utf8String>() { }, new TypeReference<Utf8String>() { })); assertThat(FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000080" + "00000000000000000000000000000000000000000000000000000000000000c0" + "0000000000000000000000000000000000000000000000000000000000000100" + "0000000000000000000000000000000000000000000000000000000000000140" + "0000000000000000000000000000000000000000000000000000000000000004" + "6465663100000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000004" + "6768693100000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000004" + "6a6b6c3100000000000000000000000000000000000000000000000000000000" + "0000000000000000000000000000000000000000000000000000000000000004" + "6d6e6f3200000000000000000000000000000000000000000000000000000000", function.getOutputParameters()), equalTo(Arrays.<Type>asList( new Utf8String("def1"), new Utf8String("ghi1"), new Utf8String("jkl1"), new Utf8String("mno2")))); }
@Test @SuppressWarnings("unchecked") public void testDecodeStaticArrayValue() { List<TypeReference<Type>> outputParameters = new ArrayList<TypeReference<Type>>(1); outputParameters.add((TypeReference) new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {}); outputParameters.add((TypeReference) new TypeReference<Uint256>() {}); List<Type> decoded = FunctionReturnDecoder.decode( "0x0000000000000000000000000000000000000000000000000000000000000037" + "0000000000000000000000000000000000000000000000000000000000000001" + "000000000000000000000000000000000000000000000000000000000000000a", outputParameters); List<Type> expected = Arrays.<Type>asList( new StaticArray<Uint256>( new Uint256(BigInteger.valueOf(55)), new Uint256(BigInteger.ONE)), new Uint256(BigInteger.TEN)); assertThat(decoded, is(expected)); }
@Test public void testVoidResultFunctionDecode() { Function function = new Function( "test", Collections.<Type>emptyList(), Collections.<TypeReference<?>>emptyList()); assertThat(FunctionReturnDecoder.decode("0x", function.getOutputParameters()), CoreMatchers.<List>is(Collections.emptyList())); }
@Test public void testEmptyResultFunctionDecode() { Function function = new Function( "test", Collections.<Type>emptyList(), Collections.<TypeReference<?>>singletonList(new TypeReference<Uint>() { })); assertThat(FunctionReturnDecoder.decode("0x", function.getOutputParameters()), CoreMatchers.<List>is(Collections.emptyList())); }