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

cn.hyperchain.sdk.service.impl.ContractServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package cn.hyperchain.sdk.service.impl;

import cn.hyperchain.sdk.provider.ProviderManager;
import cn.hyperchain.sdk.request.ContractRequest;
import cn.hyperchain.sdk.request.PollingRequest;
import cn.hyperchain.sdk.request.ReceiptRequest;
import cn.hyperchain.sdk.request.Request;
import cn.hyperchain.sdk.request.StringRequest;
import cn.hyperchain.sdk.response.ReceiptResponse;
import cn.hyperchain.sdk.response.TxHashResponse;
import cn.hyperchain.sdk.response.contract.CompileContractResponse;
import cn.hyperchain.sdk.response.contract.DeployerListResponse;
import cn.hyperchain.sdk.response.contract.StringResponse;
import cn.hyperchain.sdk.service.ContractService;
import cn.hyperchain.sdk.transaction.Transaction;

import java.util.Map;

/**
 * default contract service interface's implement.
 *
 * @author tomkk
 * @version 0.0.1
 */
public class ContractServiceImpl implements ContractService {
    private ProviderManager providerManager;
    private static final String CONTRACT_PREFIX = "contract_";
    private static final String SIMULATE_PREFIX = "simulate_";
    private String namespace = "global";
    private String jsonrpc = "2.0";

    public ContractServiceImpl(ProviderManager providerManager) {
        this.providerManager = providerManager;
    }

    /**
     * deploy a contract.
     *
     * @param transaction deploy transaction
     * @param nodeIds     specific ids
     * @return {@link Request} of {@link TxHashResponse}
     */
    @Override
    public Request deploy(Transaction transaction, int... nodeIds) {
        ContractRequest txHashResponseContractRequest = new ContractRequest(methodName("deployContract", transaction), providerManager, TxHashResponse.class, transaction, nodeIds);

        Map txParamMap = transaction.commonParamMap();
        txParamMap.remove("to");
        txHashResponseContractRequest.addParams(txParamMap);
        txHashResponseContractRequest.setJsonrpc(jsonrpc);
        txHashResponseContractRequest.setNamespace(namespace);

        return txHashResponseContractRequest;
    }

    @Override
    public Request grpcDeployReturnReceipt(Transaction transaction, int... nodeIds) {
        ReceiptRequest receiptRequest = new ReceiptRequest(methodName("deployContractReturnReceipt", transaction), providerManager, ReceiptResponse.class, nodeIds);

        Map txParamMap = transaction.commonParamMap();
        txParamMap.remove("to");
        receiptRequest.addParams(txParamMap);
        receiptRequest.setNamespace(namespace);

        return receiptRequest;
    }


    /**
     * invoke a contract.
     *
     * @param transaction invoke transaction
     * @param nodeIds     specific ids
     * @return {@link Request} of {@link TxHashResponse}
     */
    @Override
    public Request invoke(Transaction transaction, int... nodeIds) {
        ContractRequest txHashResponseContractRequest = new ContractRequest(methodName("invokeContract", transaction), providerManager, TxHashResponse.class, transaction, nodeIds);

        Map txParamMap = transaction.commonParamMap();

        txHashResponseContractRequest.addParams(txParamMap);
        txHashResponseContractRequest.setJsonrpc(jsonrpc);
        txHashResponseContractRequest.setNamespace(namespace);

        return txHashResponseContractRequest;
    }

    @Override
    public Request grpcInvokeReturnReceipt(Transaction transaction, int... nodeIds) {
        ReceiptRequest receiptRequest = new ReceiptRequest(methodName("invokeContractReturnReceipt", transaction), providerManager, ReceiptResponse.class, nodeIds);

        Map txParamMap = transaction.commonParamMap();

        receiptRequest.addParams(txParamMap);
        receiptRequest.setNamespace(namespace);

        return receiptRequest;
    }

    /**
     * get transaction receipt by txHash.
     *
     * @param txHash  transaction hash
     * @param nodeIds specific ids
     * @return {@link Request} of {@link ReceiptResponse}
     */
    @Override
    public Request getReceipt(String txHash, int... nodeIds) {
        PollingRequest receiptResponsePollingRequest = new PollingRequest("tx_getTransactionReceipt", providerManager, ReceiptResponse.class, nodeIds);

        receiptResponsePollingRequest.addParams(txHash);
        receiptResponsePollingRequest.setJsonrpc(jsonrpc);
        receiptResponsePollingRequest.setNamespace(namespace);

        return receiptResponsePollingRequest;
    }

    @Override
    public Request maintain(Transaction transaction, int... nodeIds) {
        ContractRequest txHashResponseContractRequest = new ContractRequest(methodName("maintainContract", transaction), providerManager, TxHashResponse.class, transaction, nodeIds);
        Map params = transaction.commonParamMap();

        txHashResponseContractRequest.addParams(params);
        txHashResponseContractRequest.setJsonrpc(jsonrpc);
        txHashResponseContractRequest.setNamespace(namespace);

        return txHashResponseContractRequest;
    }

    @Override
    public Request grpcMaintainReturnReceipt(Transaction transaction, int... nodeIds) {
        ReceiptRequest receiptRequest = new ReceiptRequest(methodName("maintainContractReturnReceipt", transaction), providerManager, ReceiptResponse.class, nodeIds);
        Map params = transaction.commonParamMap();

        receiptRequest.addParams(params);
        receiptRequest.setNamespace(namespace);

        return receiptRequest;
    }


    @Override
    public Request manageContractByVote(Transaction transaction, int... nodeIds) {
        ContractRequest txHashResponseContractRequest = new ContractRequest(methodName("manageContractByVote", transaction), providerManager, TxHashResponse.class, transaction, nodeIds);
        Map params = transaction.commonParamMap();

        txHashResponseContractRequest.addParams(params);
        txHashResponseContractRequest.setJsonrpc(jsonrpc);
        txHashResponseContractRequest.setNamespace(namespace);

        return txHashResponseContractRequest;
    }

    @Override
    public Request grpcManageContractByVoteReturnReceipt(Transaction transaction, int... nodeIds) {
        ReceiptRequest receiptRequest = new ReceiptRequest(methodName("manageContractByVoteReturnReceipt", transaction), providerManager, ReceiptResponse.class, nodeIds);
        Map params = transaction.commonParamMap();

        receiptRequest.addParams(params);
        receiptRequest.setNamespace(namespace);

        return receiptRequest;
    }

    @Override
    public Request getDeployedList(String address, int... nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getDeployedList", providerManager, DeployerListResponse.class, nodeIds);

        stringRequest.addParams(address);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);

        return stringRequest;
    }

    @Override
    public Request compileContract(String code, int... nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "compileContract", providerManager, CompileContractResponse.class, nodeIds);

        stringRequest.addParams(code);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);

        return stringRequest;
    }

    @Override
    public Request getCode(String addr, int... nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getCode", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(addr);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getContractCountByAddr(String addr, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getContractCountByAddr", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(addr);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getStatus(String addr, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getStatus", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(addr);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getCreator(String addr, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getCreator", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(addr);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getCreateTime(String addr, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getCreateTime", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(addr);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getStatusByCName(String cname, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getStatusByCName", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(cname);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getCreatorByCName(String cname, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getCreatorByCName", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(cname);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    @Override
    public Request getCreateTimeByCName(String cname, int...nodeIds) {
        StringRequest stringRequest = new StringRequest(CONTRACT_PREFIX + "getCreateTimeByCName", providerManager, StringResponse.class, nodeIds);

        stringRequest.addParams(cname);
        stringRequest.setJsonrpc(jsonrpc);
        stringRequest.setNamespace(namespace);
        return stringRequest;
    }

    private String methodName(String method, Transaction transaction) {
        if (transaction.isSimulate() && transaction.getTxVersion().getVersion() != "1.0") {
            return SIMULATE_PREFIX + method;
        } else {
            return CONTRACT_PREFIX + method;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy