Java Code Examples for org.web3j.abi.datatypes.Type#getValue()

The following examples show how to use org.web3j.abi.datatypes.Type#getValue() . 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: EventUtils.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private String getValueFromParams(List<Type> responseParams, int selectIndex)
{
    Type t = responseParams.get(selectIndex);
    String typeName = t.getTypeAsString();
    //strip numbers
    int i = typeName.length() - 1;
    while (Character.isDigit(typeName.charAt(i))) { i--; }; //strip
    typeName = typeName.substring(0, i+1);
    byte[] val;

    String selectVal;

    // Note this param gets interpreted according to the script 'syntax'
    // by the attribute value conversion function getSyntaxVal(String ...) in class Attribute
    switch (typeName.toLowerCase())
    {
        case "string":
        case "address":
        case "uint":
        case "int":
        case "bool":
        case "fixed":
            selectVal = t.getValue().toString();
            break;
        case "bytes":
            val = (byte[])(t.getValue());
            selectVal = Numeric.toHexString(val);
            break;

        default:
            selectVal = "Unexpected type: " + t.getTypeAsString();
            break;
    }

    return selectVal;
}
 
Example 2
Source File: TransactionHandler.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private List callSmartContractFunctionArray(
        org.web3j.abi.datatypes.Function function, String contractAddress, String address) throws Exception
{
    String encodedFunction = FunctionEncoder.encode(function);
    String value = makeEthCall(
            org.web3j.protocol.core.methods.request.Transaction
                    .createEthCallTransaction(address, contractAddress, encodedFunction));

    List<Type> values = FunctionReturnDecoder.decode(value, function.getOutputParameters());
    if (values.isEmpty()) return null;

    Type T = values.get(0);
    Object o = T.getValue();
    return (List) o;
}
 
Example 3
Source File: TransactionHandler.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
private List callSmartContractFunctionArray(
        org.web3j.abi.datatypes.Function function, String contractAddress, String address) throws Exception
{
    String encodedFunction = FunctionEncoder.encode(function);
    String value = makeEthCall(
            org.web3j.protocol.core.methods.request.Transaction
                    .createEthCallTransaction(address, contractAddress, encodedFunction));

    List<Type> values = FunctionReturnDecoder.decode(value, function.getOutputParameters());
    if (values.isEmpty()) return null;

    Type T = values.get(0);
    Object o = T.getValue();
    return (List) o;
}
 
Example 4
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}
 
Example 5
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}
 
Example 6
Source File: TokenscriptFunction.java    From alpha-wallet-android with MIT License 4 votes vote down vote up
private String handleTransactionResult(TransactionResult result, Function function, String responseValue, Attribute attr, long lastTransactionTime)
{
    String transResult = null;
    try
    {
        //try to interpret the value. For now, just use the raw return value - this is more reliable until we need to interpret arrays
        List<Type> response = FunctionReturnDecoder.decode(responseValue, function.getOutputParameters());
        if (response.size() > 0)
        {
            result.resultTime = lastTransactionTime;
            Type val = response.get(0);

            BigInteger value;
            byte[] bytes = Bytes.trimLeadingZeroes(Numeric.hexStringToByteArray(responseValue));
            String hexBytes = Numeric.toHexString(bytes);

            switch (attr.syntax)
            {
                case Boolean:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.equals(BigDecimal.ZERO) ? "FALSE" : "TRUE";
                    break;
                case Integer:
                    value = Numeric.toBigInt(hexBytes);
                    transResult = value.toString();
                    break;
                case BitString:
                case NumericString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else
                    {
                        //should be a decimal string
                        value = Numeric.toBigInt(hexBytes);
                        transResult = value.toString();
                    }
                    break;
                case IA5String:
                case DirectoryString:
                case GeneralizedTime:
                case CountryString:
                    if (val.getTypeAsString().equals("string"))
                    {
                        transResult = (String)val.getValue();
                        if (responseValue.length() > 2 && transResult.length() == 0)
                        {
                            transResult = checkBytesString(responseValue);
                        }
                    }
                    else if (val.getTypeAsString().equals("address"))
                    {
                        transResult = (String)val.getValue();
                    }
                    else
                    {
                        transResult = hexBytes;
                    }
                    break;
                default:
                    transResult = hexBytes;
                    break;
            }
        }
        else
        {
            result.resultTime = lastTransactionTime == -1 ? -1 : 0;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return transResult;
}