org.web3j.abi.datatypes.Utf8String Java Examples

The following examples show how to use org.web3j.abi.datatypes.Utf8String. 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: StakingParam.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
public List<Type> getSubmitInputParameters() {
    return Arrays.<Type>asList(new Uint16(stakingAmountType.getValue())
            , new BytesType(Numeric.hexStringToByteArray(benifitAddress))
            , new BytesType(Numeric.hexStringToByteArray(nodeId))
            , new Utf8String(externalId)
            , new Utf8String(nodeName)
            , new Utf8String(webSite)
            , new Utf8String(details)
            , new Int256(amount)
            , new Uint16(rewardPer)
            , new Uint32(processVersion.getProgramVersion())
            , new BytesType(Numeric.hexStringToByteArray(processVersion.getProgramVersionSign()))
            , new BytesType(Numeric.hexStringToByteArray(blsPubKey))
            , new BytesType(Numeric.hexStringToByteArray(blsProof))
    );
}
 
Example #2
Source File: PayWages.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
public RemoteCall<Tuple4<BigInteger, BigInteger, String, String>> getEmployee(String _account) {
	final Function function = new Function(FUNC_GETEMPLOYEE, Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(_account)),
			Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {
			}, new TypeReference<Uint8>() {
			}, new TypeReference<Utf8String>() {
			}, new TypeReference<Address>() {
			}));
	return new RemoteCall<Tuple4<BigInteger, BigInteger, String, String>>(new Callable<Tuple4<BigInteger, BigInteger, String, String>>() {
		@Override
		public Tuple4<BigInteger, BigInteger, String, String> call() throws Exception {
			List<Type> results = executeCallMultipleValueReturn(function);
			return new Tuple4<BigInteger, BigInteger, String, String>((BigInteger) results.get(0).getValue(),
					(BigInteger) results.get(1).getValue(), (String) results.get(2).getValue(), (String) results.get(3).getValue());
		}
	});
}
 
Example #3
Source File: GreeterContractIT.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private String sendCreateContractTransaction() throws Exception {
    BigInteger nonce = getNonce(ALICE.getAddress());

    String encodedConstructor =
            FunctionEncoder.encodeConstructor(Collections.singletonList(new Utf8String(VALUE)));

    Transaction transaction = Transaction.createContractTransaction(
            ALICE.getAddress(),
            nonce,
            GAS_PRICE,
            GAS_LIMIT,
            BigInteger.ZERO,
            getGreeterSolidityBinary() + encodedConstructor);

    org.web3j.protocol.core.methods.response.EthSendTransaction
            transactionResponse = web3j.ethSendTransaction(transaction)
            .sendAsync().get();

    return transactionResponse.getTransactionHash();
}
 
Example #4
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 #5
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private MethodSpec.Builder addTypes(MethodSpec.Builder builder, String packageName) {
    builder = addStatement(builder, packageName,
            Address.TYPE_NAME, Address.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Bool.TYPE_NAME, Bool.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Utf8String.TYPE_NAME, Utf8String.class.getSimpleName());

    builder = addStatement(builder, packageName,
            DynamicBytes.TYPE_NAME, DynamicBytes.class.getSimpleName());

    // TODO: Fixed array & dynamic array support
    return builder;
}
 
Example #6
Source File: SolidityFunctionWrapperTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetNativeType() {
    assertThat(getNativeType(TypeName.get(Address.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Uint256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Int256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Utf8String.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Bool.class)),
            equalTo(TypeName.get(Boolean.class)));
    assertThat(getNativeType(TypeName.get(Bytes32.class)),
            equalTo(TypeName.get(byte[].class)));
    assertThat(getNativeType(TypeName.get(DynamicBytes.class)),
            equalTo(TypeName.get(byte[].class)));
}
 
Example #7
Source File: TypeDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStaticArray() {
    assertThat(TypeDecoder.decodeStaticArray(
            "000000000000000000000000000000000000000000000000000000000000000a"
            + "0000000000000000000000000000000000000000000000007fffffffffffffff",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint256>>(2) {},
            2),
            is(new StaticArray<Uint256>(
                    new Uint256(BigInteger.TEN),
                    new Uint256(BigInteger.valueOf(Long.MAX_VALUE)))));

    assertThat(TypeDecoder.decodeStaticArray(
                    "000000000000000000000000000000000000000000000000000000000000000d"
                    + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
                    + "000000000000000000000000000000000000000000000000000000000000000d"
                    + "776f726c64212048656c6c6f2c00000000000000000000000000000000000000",
            0,
            new TypeReference.StaticArrayTypeReference<StaticArray<Utf8String>>(2){},
            2
            ),
            equalTo(new StaticArray<Utf8String>(
                    new Utf8String("Hello, world!"),
                    new Utf8String("world! Hello,"))));
}
 
Example #8
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testSimpleFunctionStringResultDecode() {
    Function function = new Function("simple",
            Arrays.asList(),
            Collections.singletonList(new TypeReference<Utf8String>() {
            }));

    List<Type> utf8Strings = FunctionReturnDecoder.decode(

            "0x0000000000000000000000000000000000000000000000000000000000000020"
                    + "000000000000000000000000000000000000000000000000000000000000000d"
                    + "6f6e65206d6f72652074696d6500000000000000000000000000000000000000",
            function.getOutputParameters());

    assertThat(utf8Strings.get(0).getValue(), is("one more time"));
}
 
Example #9
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private MethodSpec.Builder addTypes(MethodSpec.Builder builder, String packageName) {
    builder = addStatement(builder, packageName,
            Address.TYPE_NAME, Address.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Bool.TYPE_NAME, Bool.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Utf8String.TYPE_NAME, Utf8String.class.getSimpleName());

    builder = addStatement(builder, packageName,
            DynamicBytes.TYPE_NAME, DynamicBytes.class.getSimpleName());

    // TODO: Fixed array & dynamic array support
    return builder;
}
 
Example #10
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public Observable<NameChangedEventResponse> nameChangedEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
    final Event event = new Event("NameChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
    filter.addSingleTopic(EventEncoder.encode(event));
    return web3j.ethLogObservable(filter).map(new Func1<Log, NameChangedEventResponse>() {
        @Override
        public NameChangedEventResponse call(Log log) {
            EventValues eventValues = extractEventParameters(event, log);
            NameChangedEventResponse typedResponse = new NameChangedEventResponse();
            typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
            typedResponse.name = (String) eventValues.getNonIndexedValues().get(0).getValue();
            return typedResponse;
        }
    });
}
 
Example #11
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 #12
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testCallSingleValue() throws Exception {
    // Example taken from FunctionReturnDecoderTest

    EthCall ethCall = new EthCall();
    ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000020"
            + "0000000000000000000000000000000000000000000000000000000000000000");
    prepareCall(ethCall);

    assertThat(contract.callSingleValue().send(), equalTo(new Utf8String("")));
}
 
Example #13
Source File: FunctionReturnDecoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> int getDataOffset(String input, int offset, Class<T> type) {
    if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)
            || DynamicArray.class.isAssignableFrom(type)) {
        return TypeDecoder.decodeUintAsInt(input, offset) << 1;
    } else {
        return offset;
    }
}
 
Example #14
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static <T extends Type> int getSingleElementLength(String input, int offset, Class<T> type) {
    if (input.length() == offset) {
        return 0;
    } else if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)) {
        // length field + data value
        return (decodeUintAsInt(input, offset) / Type.MAX_BYTE_LENGTH) + 2;
    } else {
        return 1;
    }
}
 
Example #15
Source File: TypeDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testUtf8String() {
    assertThat(TypeDecoder.decodeUtf8String(
            "000000000000000000000000000000000000000000000000000000000000000d"  // length
                    + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000", 0),
            is(new Utf8String("Hello, world!")));
}
 
Example #16
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFunctionEmptyStringResultDecode() {
    Function function = new Function("test",
            Collections.emptyList(),
            Collections.singletonList(new TypeReference<Utf8String>() {
            }));

    List<Type> utf8Strings = FunctionReturnDecoder.decode(
            "0x0000000000000000000000000000000000000000000000000000000000000020"
                    + "0000000000000000000000000000000000000000000000000000000000000000",
            function.getOutputParameters());

    assertThat(utf8Strings.get(0).getValue(), is(""));
}
 
Example #17
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDecodeIndexedStringValue() {
    Utf8String string = new Utf8String("some text");
    String encoded = TypeEncoder.encodeString(string);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<Utf8String>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #18
Source File: FunctionReturnDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static <T extends Type> int getDataOffset(String input, int offset, Class<T> type) {
    if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)
            || DynamicArray.class.isAssignableFrom(type)) {
        return TypeDecoder.decodeUintAsInt(input, offset) << 1;
    } else {
        return offset;
    }
}
 
Example #19
Source File: FunctionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEncodeConstructorString() {
    assertThat(FunctionEncoder.encodeConstructor(
            Collections.singletonList(new Utf8String("Greetings!"))),
            is("0000000000000000000000000000000000000000000000000000000000000020"
                    + "000000000000000000000000000000000000000000000000000000000000000a"
                    + "4772656574696e67732100000000000000000000000000000000000000000000"));
}
 
Example #20
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static TypeName getEventNativeType(TypeName typeName) {
    if (typeName instanceof ParameterizedTypeName) {
        return TypeName.get(byte[].class);
    }

    String simpleName = ((ClassName) typeName).simpleName();
    if (simpleName.equals(Utf8String.class.getSimpleName())) {
        return TypeName.get(byte[].class);
    } else {
        return getNativeType(typeName);
    }
}
 
Example #21
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static TypeName getNativeType(TypeName typeName) {

        if (typeName instanceof ParameterizedTypeName) {
            return getNativeType((ParameterizedTypeName) typeName);
        }

        String simpleName = ((ClassName) typeName).simpleName();

        if (simpleName.equals(Address.class.getSimpleName())) {
            return TypeName.get(String.class);
        } else if (simpleName.startsWith("Uint")) {
            return TypeName.get(BigInteger.class);
        } else if (simpleName.startsWith("Int")) {
            return TypeName.get(BigInteger.class);
        } else if (simpleName.equals(Utf8String.class.getSimpleName())) {
            return TypeName.get(String.class);
        } else if (simpleName.startsWith("Bytes")) {
            return TypeName.get(byte[].class);
        } else if (simpleName.equals(DynamicBytes.class.getSimpleName())) {
            return TypeName.get(byte[].class);
        } else if (simpleName.equals(Bool.class.getSimpleName())) {
            return TypeName.get(Boolean.class);  // boolean cannot be a parameterized type
        } else {
            throw new UnsupportedOperationException(
                    "Unsupported type: " + typeName
                            + ", no native type mapping exists.");
        }
    }
 
Example #22
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public List<TextChangedEventResponse> getTextChangedEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("TextChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}, new TypeReference<Utf8String>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<TextChangedEventResponse> responses = new ArrayList<TextChangedEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        TextChangedEventResponse typedResponse = new TextChangedEventResponse();
        typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
        typedResponse.indexedKey = (String) eventValues.getIndexedValues().get(1).getValue();
        typedResponse.key = (String) eventValues.getNonIndexedValues().get(0).getValue();
        responses.add(typedResponse);
    }
    return responses;
}
 
Example #23
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public List<NameChangedEventResponse> getNameChangedEvents(TransactionReceipt transactionReceipt) {
    final Event event = new Event("NameChanged", 
            Arrays.<TypeReference<?>>asList(new TypeReference<Bytes32>() {}),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
    List<EventValues> valueList = extractEventParameters(event, transactionReceipt);
    ArrayList<NameChangedEventResponse> responses = new ArrayList<NameChangedEventResponse>(valueList.size());
    for (EventValues eventValues : valueList) {
        NameChangedEventResponse typedResponse = new NameChangedEventResponse();
        typedResponse.node = (byte[]) eventValues.getIndexedValues().get(0).getValue();
        typedResponse.name = (String) eventValues.getNonIndexedValues().get(0).getValue();
        responses.add(typedResponse);
    }
    return responses;
}
 
Example #24
Source File: HumanStandardToken.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Deprecated
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit,
		BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
	String encodedConstructor = FunctionEncoder.encodeConstructor(
			Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount), new org.web3j.abi.datatypes.Utf8String(_tokenName),
					new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits), new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
	return deployRemoteCall(HumanStandardToken.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor);
}
 
Example #25
Source File: HumanStandardToken.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static RemoteCall<HumanStandardToken> deploy(Web3j web3j, Credentials credentials, GasProvider contractGasProvider,
		BigInteger _initialAmount, String _tokenName, BigInteger _decimalUnits, String _tokenSymbol) {
	String encodedConstructor = FunctionEncoder.encodeConstructor(
			Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(_initialAmount), new org.web3j.abi.datatypes.Utf8String(_tokenName),
					new org.web3j.abi.datatypes.generated.Uint8(_decimalUnits), new org.web3j.abi.datatypes.Utf8String(_tokenSymbol)));
	return deployRemoteCall(HumanStandardToken.class, web3j, credentials, contractGasProvider, BINARY, encodedConstructor);
}
 
Example #26
Source File: ContractTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<Utf8String> callSingleValue() {
    Function function = new Function("call",
            Arrays.<Type>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
            }));
    return executeRemoteCallSingleValueReturn(function);
}
 
Example #27
Source File: ContractTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public RemoteCall<Utf8String> callSingleValue() {
    Function function = new Function("call",
            Arrays.<Type>asList(),
            Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {
            }));
    return executeRemoteCallSingleValueReturn(function);
}
 
Example #28
Source File: ContractTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallSingleValue() throws Exception {
    // Example taken from FunctionReturnDecoderTest

    PlatonCall ethCall = new PlatonCall();
    ethCall.setResult("0x0000000000000000000000000000000000000000000000000000000000000020"
            + "0000000000000000000000000000000000000000000000000000000000000000");
    prepareCall(ethCall);

    assertThat(contract.callSingleValue().send(), equalTo(new Utf8String("")));
}
 
Example #29
Source File: EnsResolverTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReverseResolve() throws Exception {
    configureSyncing(false);
    configureLatestBlock(System.currentTimeMillis() / 1000);  // block timestamp is in seconds

    NetVersion netVersion = new NetVersion();
    netVersion.setResult(Byte.toString(ChainId.MAINNET));

    String resolverAddress =
            "0x0000000000000000000000004c641fb9bad9b60ef180c31f56051ce826d21a9a";
    String contractName =
            "0x0000000000000000000000000000000000000000000000000000000000000020"
            + TypeEncoder.encode(new Utf8String("web3j.eth"));
    System.err.println(contractName);

    EthCall resolverAddressResponse = new EthCall();
    resolverAddressResponse.setResult(resolverAddress);

    EthCall contractNameResponse = new EthCall();
    contractNameResponse.setResult(contractName);

    when(web3jService.send(any(Request.class), eq(NetVersion.class)))
            .thenReturn(netVersion);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(resolverAddressResponse);
    when(web3jService.send(any(Request.class), eq(EthCall.class)))
            .thenReturn(contractNameResponse);

    assertThat(ensResolver.reverseResolve("0x19e03255f667bdfd50a32722df860b1eeaf4d635"),
            is("web3j.eth"));
}
 
Example #30
Source File: UpdateStakingParam.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public List<Type> getSubmitInputParameters() {
    return Arrays.asList(new BytesType(Numeric.hexStringToByteArray(benifitAddress)),
            new BytesType(Numeric.hexStringToByteArray(nodeId)),
            new Uint16(rewardPer),
            new Utf8String(externalId),
            new Utf8String(nodeName),
            new Utf8String(webSite),
            new Utf8String(details));
}