org.web3j.abi.datatypes.Uint Java Examples

The following examples show how to use org.web3j.abi.datatypes.Uint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SendingTokensActivity.java    From guarda-android-wallets with GNU General Public License v3.0 7 votes vote down vote up
private String getContractTransfer() {
    int tokenDecimals = tokensManager.getTokenByCode(tokenCode).getDecimal();
    BigInteger tokenValue = Converter.toDecimals(new BigDecimal(amountToSend), tokenDecimals).toBigInteger();
    List<Type> inputParams = new ArrayList<>();

    Type address = new Address(walletNumber);
    inputParams.add(address);

    Type value = new Uint(tokenValue);
    inputParams.add(value);

    Function function = new Function(
            "transfer",
            inputParams,
            Collections.<TypeReference<?>>emptyList());

    return FunctionEncoder.encode(function);
}
 
Example #2
Source File: TypeEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testDynamicArray() {
    DynamicArray<Uint> array = new DynamicArray<>(
            new Uint(BigInteger.ONE),
            new Uint(BigInteger.valueOf(2)),
            new Uint(BigInteger.valueOf(3))
    );

    assertThat(
            TypeEncoder.encodeDynamicArray(array),
            is("0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
            ));
}
 
Example #3
Source File: FunctionEncoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private static String encodeParameters(List<Type> parameters, StringBuilder result) {
    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    StringBuilder dynamicData = new StringBuilder();

    for (Type parameter:parameters) {
        String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            String encodedDataOffset = TypeEncoder.encodeNumeric(
                    new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example #4
Source File: TypeEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testUintEncode() {
    Uint zero = new Uint64(BigInteger.ZERO);
    assertThat(TypeEncoder.encodeNumeric(zero),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Uint maxLong = new Uint64(BigInteger.valueOf(Long.MAX_VALUE));
    assertThat(TypeEncoder.encodeNumeric(maxLong),
            is("0000000000000000000000000000000000000000000000007fffffffffffffff"));

    Uint maxValue = new Uint(
            new BigInteger("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            16));
    assertThat(TypeEncoder.encodeNumeric(maxValue),
            is("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));

    Uint largeValue = new Uint(
            new BigInteger("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
            16));
    assertThat(TypeEncoder.encodeNumeric(largeValue),
            is("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"));
}
 
Example #5
Source File: TypeEncoderTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicArray() {
    DynamicArray<Uint> array =
            new DynamicArray<>(
                    Uint.class,
                    new Uint(BigInteger.ONE),
                    new Uint(BigInteger.valueOf(2)),
                    new Uint(BigInteger.valueOf(3)));

    assertEquals(
            TypeEncoder.encodeDynamicArray(array),
            ("0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"));
}
 
Example #6
Source File: TypeEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicArray() {
    DynamicArray<Uint> array = new DynamicArray<>(
            new Uint(BigInteger.ONE),
            new Uint(BigInteger.valueOf(2)),
            new Uint(BigInteger.valueOf(3))
    );

    assertThat(
            TypeEncoder.encodeDynamicArray(array),
            is("0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
            ));
}
 
Example #7
Source File: UtilsTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() {
    assertEquals(Utils.getTypeName(new TypeReference<Uint>() {}), ("uint256"));
    assertEquals(Utils.getTypeName(new TypeReference<Int>() {}), ("int256"));
    assertEquals(Utils.getTypeName(new TypeReference<Ufixed>() {}), ("ufixed256"));
    assertEquals(Utils.getTypeName(new TypeReference<Fixed>() {}), ("fixed256"));

    assertEquals(Utils.getTypeName(new TypeReference<Uint64>() {}), ("uint64"));
    assertEquals(Utils.getTypeName(new TypeReference<Int64>() {}), ("int64"));
    assertEquals(Utils.getTypeName(new TypeReference<Bool>() {}), ("bool"));
    assertEquals(Utils.getTypeName(new TypeReference<Utf8String>() {}), ("string"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicBytes>() {}), ("bytes"));

    assertEquals(
            Utils.getTypeName(
                    new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5) {}),
            ("uint256[5]"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicArray<Uint>>() {}), ("uint256[]"));
}
 
Example #8
Source File: Utils.java    From web3j with Apache License 2.0 6 votes vote down vote up
static String getSimpleTypeName(Class<?> type) {
    String simpleName = type.getSimpleName().toLowerCase();

    if (type.equals(Uint.class)
            || type.equals(Int.class)
            || type.equals(Ufixed.class)
            || type.equals(Fixed.class)) {
        return simpleName + "256";
    } else if (type.equals(Utf8String.class)) {
        return "string";
    } else if (type.equals(DynamicBytes.class)) {
        return "bytes";
    } else {
        return simpleName;
    }
}
 
Example #9
Source File: UtilsTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #10
Source File: DefaultFunctionEncoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
private static String encodeParameters(
        final List<Type> parameters, final StringBuilder result) {

    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    final StringBuilder dynamicData = new StringBuilder();

    for (Type parameter : parameters) {
        final String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            final String encodedDataOffset =
                    TypeEncoder.encodeNumeric(new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example #11
Source File: FunctionEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionMDynamicArrayEncode1() {
    Function function = new Function(
            "sam",
            Arrays.asList(
                new DynamicBytes("dave".getBytes()),
                new Bool(true),
                new DynamicArray<>(
                        new Uint(BigInteger.ONE),
                        new Uint(BigInteger.valueOf(2)),
                        new Uint(BigInteger.valueOf(3)))),
            Collections.<TypeReference<?>>emptyList()
    );

    assertThat(FunctionEncoder.encode(function),
            is("0xa5643bf2"
                    + "0000000000000000000000000000000000000000000000000000000000000060"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "00000000000000000000000000000000000000000000000000000000000000a0"
                    + "0000000000000000000000000000000000000000000000000000000000000004"
                    + "6461766500000000000000000000000000000000000000000000000000000000"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"));
}
 
Example #12
Source File: SendingTokensActivity.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
private String getContractTransfer() {
    int tokenDecimals = tokensManager.getTokenByCode(tokenCode).getDecimal();
    BigInteger tokenValue = Converter.toDecimals(new BigDecimal(amountToSend), tokenDecimals).toBigInteger();
    List<Type> inputParams = new ArrayList<>();

    Type address = new Address(walletNumber);
    inputParams.add(address);

    Type value = new Uint(tokenValue);
    inputParams.add(value);

    Function function = new Function(
            "transfer",
            inputParams,
            Collections.<TypeReference<?>>emptyList());

    return FunctionEncoder.encode(function);
}
 
Example #13
Source File: SendingTokensActivity.java    From guarda-android-wallets with GNU General Public License v3.0 6 votes vote down vote up
private String getContractTransfer() {
    int tokenDecimals = tokensManager.getTokenByCode(tokenCode).getDecimal();
    BigInteger tokenValue = Converter.toDecimals(new BigDecimal(amountToSend), tokenDecimals).toBigInteger();
    List<Type> inputParams = new ArrayList<>();

    Type address = new Address(walletNumber);
    inputParams.add(address);

    Type value = new Uint(tokenValue);
    inputParams.add(value);

    Function function = new Function(
            "transfer",
            inputParams,
            Collections.<TypeReference<?>>emptyList());

    return FunctionEncoder.encode(function);
}
 
Example #14
Source File: FunctionEncoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private static String encodeParameters(List<Type> parameters, StringBuilder result) {
    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    StringBuilder dynamicData = new StringBuilder();

    for (Type parameter:parameters) {
        String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            String encodedDataOffset = TypeEncoder.encodeNumeric(
                    new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example #15
Source File: FunctionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFunctionMDynamicArrayEncode1() {
    Function function = new Function(
            "sam",
            Arrays.asList(
                new DynamicBytes("dave".getBytes()),
                new Bool(true),
                new DynamicArray<>(
                        new Uint(BigInteger.ONE),
                        new Uint(BigInteger.valueOf(2)),
                        new Uint(BigInteger.valueOf(3)))),
            Collections.<TypeReference<?>>emptyList()
    );

    assertThat(FunctionEncoder.encode(function),
            is("0xa5643bf2"
                    + "0000000000000000000000000000000000000000000000000000000000000060"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "00000000000000000000000000000000000000000000000000000000000000a0"
                    + "0000000000000000000000000000000000000000000000000000000000000004"
                    + "6461766500000000000000000000000000000000000000000000000000000000"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"));
}
 
Example #16
Source File: UtilsTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #17
Source File: TypeEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testUintEncode() {
    Uint zero = new Uint64(BigInteger.ZERO);
    assertThat(TypeEncoder.encodeNumeric(zero),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Uint maxLong = new Uint64(BigInteger.valueOf(Long.MAX_VALUE));
    assertThat(TypeEncoder.encodeNumeric(maxLong),
            is("0000000000000000000000000000000000000000000000007fffffffffffffff"));

    Uint maxValue = new Uint(
            new BigInteger("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
            16));
    assertThat(TypeEncoder.encodeNumeric(maxValue),
            is("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));

    Uint largeValue = new Uint(
            new BigInteger("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
            16));
    assertThat(TypeEncoder.encodeNumeric(largeValue),
            is("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"));
}
 
Example #18
Source File: TypeEncoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTooLargeUintEncode() {
    // 1 more than "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    assertThrows(
            UnsupportedOperationException.class,
            () ->
                    new Uint(
                            new BigInteger(
                                    "10000000000000000000000000000000000000000000000000000000000000000",
                                    16)));
}
 
Example #19
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static <T extends Type> String encodeDynamicArray(DynamicArray<T> value) {
    int size = value.getValue().size();
    String encodedLength = encode(new Uint(BigInteger.valueOf(size)));
    String valuesOffsets = encodeArrayValuesOffsets(value);
    String encodedValues = encodeArrayValues(value);

    StringBuilder result = new StringBuilder();
    result.append(encodedLength);
    result.append(valuesOffsets);
    result.append(encodedValues);
    return result.toString();
}
 
Example #20
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static String encodeDynamicBytes(DynamicBytes dynamicBytes) {
    int size = dynamicBytes.getValue().length;
    String encodedLength = encode(new Uint(BigInteger.valueOf(size)));
    String encodedValue = encodeBytes(dynamicBytes);

    StringBuilder result = new StringBuilder();
    result.append(encodedLength);
    result.append(encodedValue);
    return result.toString();
}
 
Example #21
Source File: TestnetConfig.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public String encodedEvent() {
    Event event =
            new Event(
                    "Notify",
                    Arrays.asList(
                            new TypeReference<Uint>(true) {}, new TypeReference<Uint>() {}));

    return EventEncoder.encode(event);
}
 
Example #22
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private MethodSpec.Builder generateIntTypes(MethodSpec.Builder builder, String packageName) {
    for (int bitSize = 8; bitSize <= Type.MAX_BIT_LENGTH; bitSize += 8) {

        builder = addStatement(builder, packageName,
                Uint.TYPE_NAME + bitSize, Uint.class.getSimpleName() + bitSize);
        builder = addStatement(builder, packageName,
                Int.TYPE_NAME + bitSize, Int.class.getSimpleName() + bitSize);
    }
    return builder;
}
 
Example #23
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleFunctionDecode() {
    Function function =
            new Function(
                    "test",
                    Collections.<Type>emptyList(),
                    Collections.singletonList(new TypeReference<Uint>() {}));

    assertEquals(
            FunctionReturnDecoder.decode(
                    "0x0000000000000000000000000000000000000000000000000000000000000037",
                    function.getOutputParameters()),
            (Collections.singletonList(new Uint(BigInteger.valueOf(55)))));
}
 
Example #24
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleResultFunctionDecode() {
    Function function =
            new Function(
                    "test",
                    Collections.<Type>emptyList(),
                    Arrays.asList(new TypeReference<Uint>() {}, new TypeReference<Uint>() {}));

    assertEquals(
            FunctionReturnDecoder.decode(
                    "0x0000000000000000000000000000000000000000000000000000000000000037"
                            + "0000000000000000000000000000000000000000000000000000000000000007",
                    function.getOutputParameters()),
            (Arrays.asList(new Uint(BigInteger.valueOf(55)), new Uint(BigInteger.valueOf(7)))));
}
 
Example #25
Source File: FunctionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEncodeConstructorUint() {
    assertThat(FunctionEncoder.encodeConstructor(
            Arrays.asList(new Uint(BigInteger.ONE), new Uint(BigInteger.valueOf(0x20)))),
            is("0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000020"));
}
 
Example #26
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyResultFunctionDecode() {
    Function function =
            new Function(
                    "test",
                    Collections.emptyList(),
                    Collections.singletonList(new TypeReference<Uint>() {}));

    assertEquals(
            FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
            (Collections.emptyList()));
}
 
Example #27
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEmptyResultFunctionDecode() {
    Function function = new Function(
            "test",
            Collections.emptyList(),
            Collections.singletonList(new TypeReference<Uint>() { }));

    assertThat(FunctionReturnDecoder.decode("0x", function.getOutputParameters()),
            is(Collections.emptyList()));
}
 
Example #28
Source File: AbiTypesGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
private void generate(String destinationDir) throws IOException {
    generateIntTypes(Int.class, destinationDir);
    generateIntTypes(Uint.class, destinationDir);

    // TODO: Enable once Solidity supports fixed types - see
    // https://github.com/ethereum/solidity/issues/409
    // generateFixedTypes(Fixed.class, destinationDir);
    // generateFixedTypes(Ufixed.class, destinationDir);

    generateBytesTypes(destinationDir);
    generateStaticArrayTypes(destinationDir);
}
 
Example #29
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testMultipleResultFunctionDecode() {
    Function function = new Function(
            "test",
            Collections.<Type>emptyList(),
            Arrays.asList(new TypeReference<Uint>() { }, new TypeReference<Uint>() { })
    );

    assertThat(FunctionReturnDecoder.decode(
            "0x0000000000000000000000000000000000000000000000000000000000000037"
            + "0000000000000000000000000000000000000000000000000000000000000007",
            function.getOutputParameters()),
            equalTo(Arrays.asList(new Uint(BigInteger.valueOf(55)),
                    new Uint(BigInteger.valueOf(7)))));
}
 
Example #30
Source File: AbiTypesGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void generate(String destinationDir) throws IOException {
    generateIntTypes(Int.class, destinationDir);
    generateIntTypes(Uint.class, destinationDir);

    // TODO: Enable once Solidity supports fixed types - see
    // https://github.com/ethereum/solidity/issues/409
    // generateFixedTypes(Fixed.class, destinationDir);
    // generateFixedTypes(Ufixed.class, destinationDir);

    generateBytesTypes(Bytes.class, destinationDir);
    generateStaticArrayTypes(StaticArray.class, destinationDir);
}