"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const log_1 = tslib_1.__importDefault(require("../log"));
const network_1 = require("../network");
const proto_1 = require("./proto");
/**
 * BEvm service
 */
class BEvmService {
    constructor(srvid) {
        this.timeout = 60 * 1000; // 60 seconds
        this.conn = new network_1.WebSocketConnection(srvid.getWebSocketAddress(), BEvmService.serviceName);
    }
    /**
     * Set a new timeout value for future requests.
     *
     * @param value Timeout in [ms]
     */
    setTimeout(value) {
        this.timeout = value;
    }
    /**
     * Prepare an EVM transaction to deploy a contract.
     *
     * @param gasLimit  Gas limit for the transaction
     * @param gasPrice  Gas price for the transaction
     * @param amount    Amount transferred during the transaction
     * @param nonce     Nonce for the transaction
     * @param bytecode  Compiled bytecode of the smart contract to be deployed
     * @param abi       ABI of the smart contract to be deployed
     * @param args      Arguments for the smart contract contractor
     *
     * @return Prepared transaction and its hash to be signed
     */
    prepareDeployTx(gasLimit, gasPrice, amount, nonce, bytecode, abi, args) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.conn.setTimeout(this.timeout);
            log_1.default.lvl3("Sending BEvm deploy contract request...");
            const msg = new proto_1.DeployRequest({
                abi,
                amount,
                args,
                bytecode,
                gasLimit,
                gasPrice,
                nonce,
            });
            return this.conn.send(msg, proto_1.TransactionHashResponse);
        });
    }
    /**
     * Prepare an EVM transaction for a R/W method execution
     *
     * @param gasLimit          Gas limit for the transaction
     * @param gasPrice          Gas price for the transaction
     * @param amount            Amount transferred during the transaction
     * @param contractAddress   Address of the smart contract
     * @param nonce             Nonce for the transaction
     * @param abi               ABI of the smart contract to be deployed
     * @param method            Name of the method to execute
     * @param args              Arguments for the smart contract method
     *
     * @return Prepared transaction and its hash to be signed
     */
    prepareTransactionTx(gasLimit, gasPrice, amount, contractAddress, nonce, abi, method, args) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.conn.setTimeout(this.timeout);
            log_1.default.lvl3("Sending BEvm transaction execution request...");
            const msg = new proto_1.TransactionRequest({
                abi,
                amount,
                args,
                contractAddress,
                gasLimit,
                gasPrice,
                method,
                nonce,
            });
            return this.conn.send(msg, proto_1.TransactionHashResponse);
        });
    }
    /**
     * Finalize a transaction
     *
     * @param transaction   Transaction to finalize
     * @param signature     Transaction signature
     *
     * @return Signed transaction
     */
    finalizeTx(transaction, signature) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.conn.setTimeout(this.timeout);
            log_1.default.lvl3("Sending BEvm transaction finalization request...");
            const msg = new proto_1.TransactionFinalizationRequest({
                signature,
                transaction,
            });
            return this.conn.send(msg, proto_1.TransactionResponse);
        });
    }
    /**
     * Execute a view method (read-only)
     *
     * @param byzcoinId         ByzCoin ID
     * @param serverConfig      Cothority server config in TOML
     * @param bevmInstanceId    BEvm instance ID
     * @param accountAddress    Address of the EVM account
     * @param contractAddress   Address of the smart contract
     * @param abi               ABI of the smart contract to be deployed
     * @param method            Name of the view method to execute
     * @param args              Arguments for the smart contract method
     *
     * @return Result of the view method execution
     */
    performCall(byzcoinId, serverConfig, bevmInstanceId, accountAddress, contractAddress, abi, method, args) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.conn.setTimeout(this.timeout);
            log_1.default.lvl3("Sending BEvm call request...");
            const msg = new proto_1.CallRequest({
                abi,
                accountAddress,
                args,
                bevmInstanceId,
                byzcoinId,
                contractAddress,
                method,
                serverConfig,
            });
            return this.conn.send(msg, proto_1.CallResponse);
        });
    }
}
exports.BEvmService = BEvmService;
BEvmService.serviceName = "BEvm";
