All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fisco.bcos.sdk.abi.wrapper.ContractABIDefinition Maven / Gradle / Ivy

The newest version!
package org.fisco.bcos.sdk.abi.wrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.fisco.bcos.sdk.crypto.CryptoSuite;
import org.fisco.bcos.sdk.utils.Numeric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ContractABIDefinition {

    private static final Logger logger = LoggerFactory.getLogger(ContractABIDefinition.class);

    private ABIDefinition constructor = null;
    private Map> functions = new HashMap<>();
    private Map> events = new HashMap<>();
    // method id => function
    private Map methodIDToFunctions = new HashMap<>();
    // event topic => topic
    private Map eventTopicToEvents = new HashMap<>();
    private CryptoSuite cryptoSuite;

    public ContractABIDefinition(CryptoSuite cryptoSuite) {
        this.cryptoSuite = cryptoSuite;
    }

    public ABIDefinition getConstructor() {
        return constructor;
    }

    public void setConstructor(ABIDefinition constructor) {
        this.constructor = constructor;
    }

    public Map> getFunctions() {
        return functions;
    }

    public void setFunctions(Map> functions) {
        this.functions = functions;
    }

    public Map> getEvents() {
        return events;
    }

    public void setEvents(Map> events) {
        this.events = events;
    }

    public Map getMethodIDToFunctions() {
        return methodIDToFunctions;
    }

    public void setMethodIDToFunctions(Map methodIDToFunctions) {
        this.methodIDToFunctions = methodIDToFunctions;
    }

    public Map getEventTopicToEvents() {
        return eventTopicToEvents;
    }

    public void setEventTopicToEvents(Map eventTopicToEvents) {
        this.eventTopicToEvents = eventTopicToEvents;
    }

    public void addFunction(String name, ABIDefinition abiDefinition) {

        List abiDefinitions = functions.get(name);
        if (abiDefinitions == null) {
            functions.put(name, new ArrayList<>());
            abiDefinitions = functions.get(name);
        } else {
            logger.info(" overload method ??? name: {}, abiDefinition: {}", name, abiDefinition);
        }
        abiDefinitions.add(abiDefinition);

        // calculate method id and add abiDefinition to methodIdToFunctions
        String methodId = abiDefinition.getMethodId(cryptoSuite);
        methodIDToFunctions.put(methodId, abiDefinition);

        logger.info(
                " name: {}, methodId: {}, methodSignature: {}, abi: {}",
                name,
                methodId,
                abiDefinition.getMethodSignatureAsString(),
                abiDefinition);
    }

    public void addEvent(String name, ABIDefinition abiDefinition) {
        events.putIfAbsent(name, new ArrayList<>());
        List abiDefinitions = events.get(name);
        abiDefinitions.add(abiDefinition);
        logger.info(" name: {}, abi: {}", name, abiDefinition);

        // calculate method id and add abiDefinition to eventTopicToEvents
        String methodId = abiDefinition.getMethodId(cryptoSuite);
        eventTopicToEvents.put(methodId, abiDefinition);
    }

    public ABIDefinition getABIDefinitionByMethodId(String methodId) {
        return methodIDToFunctions.get(Numeric.prependHexPrefix(methodId));
    }

    public ABIDefinition getABIDefinitionByEventTopic(String topic) {
        return eventTopicToEvents.get(Numeric.prependHexPrefix(topic));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy