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.Request;
import cn.hyperchain.sdk.response.ReceiptResponse;
import cn.hyperchain.sdk.response.TxHashResponse;
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 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"), providerManager, TxHashResponse.class, transaction, nodeIds);

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

        return txHashResponseContractRequest;
    }

    /**
     * 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"), providerManager, TxHashResponse.class, transaction, nodeIds);

        Map txParamMap = transaction.commonParamMap();

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

        return txHashResponseContractRequest;
    }

    /**
     * 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(CONTRACT_PREFIX + "maintainContract", providerManager, TxHashResponse.class, transaction, nodeIds);

        Map params = transaction.commonParamMap();
        params.put("opcode", transaction.getOpCode());

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

        return txHashResponseContractRequest;
    }

    private String methodName(String method) {
        return CONTRACT_PREFIX + method;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy