Search in sources :

Example 11 with AbiDefinition

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method decodeInputReturnObject.

/**
 * @param input
 * @return
 * @throws BaseException
 */
public InputAndOutputResult decodeInputReturnObject(String input) throws BaseException {
    String updatedInput = addHexPrefixToString(input);
    // select abi
    AbiDefinition abiDefinition = selectAbiDefinition(updatedInput);
    // decode input
    List<NamedType> inputTypes = abiDefinition.getInputs();
    List<TypeReference<?>> inputTypeReferences = ContractAbiUtil.paramFormat(inputTypes);
    Function function = new Function(abiDefinition.getName(), null, inputTypeReferences);
    List<Type> resultType = FunctionReturnDecoder.decode(updatedInput.substring(10), function.getOutputParameters());
    // set result to java bean
    List<ResultEntity> resultList = new ArrayList<ResultEntity>();
    for (int i = 0; i < inputTypes.size(); i++) {
        resultList.add(new ResultEntity(inputTypes.get(i).getName(), inputTypes.get(i).getType(), resultType.get(i)));
    }
    String methodSign = decodeMethodSign(abiDefinition);
    return new InputAndOutputResult(methodSign, FunctionEncoder.buildMethodId(methodSign), resultList);
}
Also used : NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList) Function(org.fisco.bcos.web3j.abi.datatypes.Function) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) Type(org.fisco.bcos.web3j.abi.datatypes.Type) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) TypeReference(org.fisco.bcos.web3j.abi.TypeReference)

Example 12 with AbiDefinition

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method decodeEventLogReturnObject.

/**
 * @param log
 * @return LogResult
 * @throws BaseException
 */
public LogResult decodeEventLogReturnObject(Log log) throws BaseException {
    // decode log
    List<AbiDefinition> abiDefinitions = ContractAbiUtil.getEventAbiDefinitions(abi);
    LogResult result = new LogResult();
    for (AbiDefinition abiDefinition : abiDefinitions) {
        // String eventName = decodeMethodSign(abiDefinition);
        String eventSignature = EventEncoder.buildEventSignature(decodeMethodSign(abiDefinition));
        List<String> topics = log.getTopics();
        if ((null == topics) || topics.isEmpty() || !topics.get(0).equals(eventSignature)) {
            continue;
        }
        EventValues eventValued = ContractAbiUtil.decodeEvent(log, abiDefinition);
        if (null != eventValued) {
            List<EventResultEntity> resultEntityList = new ArrayList<EventResultEntity>();
            List<NamedType> inputs = abiDefinition.getInputs();
            List<NamedType> indexedInputs = inputs.stream().filter(NamedType::isIndexed).collect(Collectors.toList());
            List<NamedType> nonIndexedInputs = inputs.stream().filter(p -> !p.isIndexed()).collect(Collectors.toList());
            for (int i = 0; i < indexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(indexedInputs.get(i).getName(), indexedInputs.get(i).getType(), true, eventValued.getIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            for (int i = 0; i < nonIndexedInputs.size(); i++) {
                EventResultEntity eventEntity = new EventResultEntity(nonIndexedInputs.get(i).getName(), nonIndexedInputs.get(i).getType(), false, eventValued.getNonIndexedValues().get(i));
                resultEntityList.add(eventEntity);
            }
            // result.setEventName(eventName);
            result.setLogParams(resultEntityList);
            result.setLog(log);
            logger.debug(" event log result: {}", result);
            return result;
        }
    }
    return null;
}
Also used : EventValues(org.fisco.bcos.web3j.abi.EventValues) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) Logger(org.slf4j.Logger) FunctionEncoder(org.fisco.bcos.web3j.abi.FunctionEncoder) EventEncoder(org.fisco.bcos.web3j.abi.EventEncoder) ObjectMapperFactory(org.fisco.bcos.web3j.protocol.ObjectMapperFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Log(org.fisco.bcos.web3j.protocol.core.methods.response.Log) Tuple2(org.fisco.bcos.web3j.tuples.generated.Tuple2) LoggerFactory(org.slf4j.LoggerFactory) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) CollectionType(com.fasterxml.jackson.databind.type.CollectionType) IOException(java.io.IOException) HashMap(java.util.HashMap) EventValues(org.fisco.bcos.web3j.abi.EventValues) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TypeReference(org.fisco.bcos.web3j.abi.TypeReference) List(java.util.List) Type(org.fisco.bcos.web3j.abi.datatypes.Type) Map(java.util.Map) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) FunctionReturnDecoder(org.fisco.bcos.web3j.abi.FunctionReturnDecoder) Function(org.fisco.bcos.web3j.abi.datatypes.Function) NamedType(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType) ArrayList(java.util.ArrayList) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)

Example 13 with AbiDefinition

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class TransactionDecoder method selectAbiDefinition.

/**
 * @param input
 * @return
 * @throws BaseException
 */
private AbiDefinition selectAbiDefinition(String input) throws BaseException {
    String methodID = input.substring(0, 10);
    AbiDefinition abiDefinition = methodIDMap.get(methodID);
    if (abiDefinition == null) {
        throw new BaseException(201203, String.format("the method is not found in abi, method id:[%s]", methodID));
    }
    return abiDefinition;
}
Also used : AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)

Example 14 with AbiDefinition

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class ContractAbiUtil method getFuncAbiDefinition.

/**
 * @param contractAbi
 * @return
 */
public static List<AbiDefinition> getFuncAbiDefinition(String contractAbi) {
    List<AbiDefinition> result = new ArrayList<>();
    try {
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
        AbiDefinition[] abiDefinitions = objectMapper.readValue(contractAbi, AbiDefinition[].class);
        for (AbiDefinition abiDefinition : abiDefinitions) {
            if (TYPE_FUNCTION.equals(abiDefinition.getType()) || TYPE_CONSTRUCTOR.equals(abiDefinition.getType())) {
                result.add(abiDefinition);
            }
        }
    } catch (JsonProcessingException e) {
        logger.warn(" invalid json, abi: {}, e: {} ", contractAbi, e);
    }
    return result;
}
Also used : AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 15 with AbiDefinition

use of org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition in project web3sdk by FISCO-BCOS.

the class SolidityFunctionWrapper method buildDeployMethods.

List<MethodSpec> buildDeployMethods(String className, TypeSpec.Builder classBuilder, List<AbiDefinition> functionDefinitions) {
    boolean constructor = false;
    List<MethodSpec> methodSpecs = new ArrayList<>();
    for (AbiDefinition functionDefinition : functionDefinitions) {
        if (functionDefinition.getType().equals("constructor")) {
            constructor = true;
            methodSpecs.add(buildDeploy(className, functionDefinition, Credentials.class, CREDENTIALS, true));
            methodSpecs.add(buildDeploy(className, functionDefinition, TransactionManager.class, TRANSACTION_MANAGER, true));
            methodSpecs.add(buildDeploy(className, functionDefinition, Credentials.class, CREDENTIALS, false));
            methodSpecs.add(buildDeploy(className, functionDefinition, TransactionManager.class, TRANSACTION_MANAGER, false));
        }
    }
    // constructor will not be specified in ABI file if its empty
    if (!constructor) {
        MethodSpec.Builder credentialsMethodBuilder = getDeployMethodSpec(className, Credentials.class, CREDENTIALS, true);
        methodSpecs.add(buildDeployNoParams(credentialsMethodBuilder, className, CREDENTIALS, true));
        MethodSpec.Builder credentialsMethodBuilderNoGasProvider = getDeployMethodSpec(className, Credentials.class, CREDENTIALS, false);
        methodSpecs.add(buildDeployNoParams(credentialsMethodBuilderNoGasProvider, className, CREDENTIALS, false));
        MethodSpec.Builder transactionManagerMethodBuilder = getDeployMethodSpec(className, TransactionManager.class, TRANSACTION_MANAGER, true);
        methodSpecs.add(buildDeployNoParams(transactionManagerMethodBuilder, className, TRANSACTION_MANAGER, true));
        MethodSpec.Builder transactionManagerMethodBuilderNoGasProvider = getDeployMethodSpec(className, TransactionManager.class, TRANSACTION_MANAGER, false);
        methodSpecs.add(buildDeployNoParams(transactionManagerMethodBuilderNoGasProvider, className, TRANSACTION_MANAGER, false));
    }
    return methodSpecs;
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) AbiDefinition(org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition) TransactionManager(org.fisco.bcos.web3j.tx.TransactionManager) ArrayList(java.util.ArrayList) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Aggregations

AbiDefinition (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition)22 ArrayList (java.util.ArrayList)15 NamedType (org.fisco.bcos.web3j.protocol.core.methods.response.AbiDefinition.NamedType)10 Type (org.fisco.bcos.web3j.abi.datatypes.Type)9 TypeReference (org.fisco.bcos.web3j.abi.TypeReference)8 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 List (java.util.List)7 Log (org.fisco.bcos.web3j.protocol.core.methods.response.Log)7 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)4 Address (org.fisco.bcos.web3j.abi.datatypes.Address)4 Bool (org.fisco.bcos.web3j.abi.datatypes.Bool)4 DynamicBytes (org.fisco.bcos.web3j.abi.datatypes.DynamicBytes)4 Event (org.fisco.bcos.web3j.abi.datatypes.Event)4 Function (org.fisco.bcos.web3j.abi.datatypes.Function)4 Bytes32 (org.fisco.bcos.web3j.abi.datatypes.generated.Bytes32)4 Int256 (org.fisco.bcos.web3j.abi.datatypes.generated.Int256)4 Uint256 (org.fisco.bcos.web3j.abi.datatypes.generated.Uint256)4 MethodSpec (com.squareup.javapoet.MethodSpec)3