public int Insert(string _sql)
        {
            int    num;
            string sql = _sql;

            try
            {
                using (NpgsqlConnection connection = this.CreateConnection())
                {
                    using (NpgsqlCommand command = new NpgsqlCommand())
                    {
                        command.Connection     = connection;
                        command.CommandText    = sql;
                        command.CommandTimeout = 30;
                        connection.Open();
                        num = command.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception exception)
            {
                SanitaLog.Error("NpgsqlBaseDao", "Insert('" + sql + "') error !");
                SanitaLog.Exception(exception);
                num = -100;
            }
            return(num);
        }
        public int UpdateContract(NpgsqlConnection connection, IDbTransaction trans, ContractInfo mContractInfo)
        {
            try
            {
                lock (lockObject)
                {
                    if (mContractInfo == null)
                    {
                        return(DataTypeModel.RESULT_NG);
                    }

                    StringBuilder builder = new StringBuilder();
                    builder.Append(" UPDATE Contract ");
                    builder.Append("  SET  ");
                    builder.Append("      Id        = " + DatabaseUtility.Escape(mContractInfo.Id) + ", ");
                    builder.Append("      Name      = " + DatabaseUtility.Escape(mContractInfo.Name) + ", ");
                    builder.Append("      Address   = " + DatabaseUtility.Escape(mContractInfo.Address) + ", ");
                    builder.Append("      Abi       = " + DatabaseUtility.Escape(mContractInfo.Abi) + ", ");
                    builder.Append("      ByteCode  = " + DatabaseUtility.Escape(mContractInfo.ByteCode) + ", ");
                    builder.Append("      Active    = " + DatabaseUtility.Escape(mContractInfo.Active) + " ");
                    builder.Append("  WHERE Id = " + DatabaseUtility.Escape(mContractInfo.Id));

                    return(base.baseDAO.DoUpdate(connection, trans, builder.ToString()));
                }
            }
            catch (Exception ex)
            {
                SanitaLog.Exception(ex);
                throw ex;
            }
        }
        public DataSet GetDataSet(string sql)
        {
            DataSet set2;

            try
            {
                using (NpgsqlConnection connection = this.CreateConnection())
                {
                    using (NpgsqlCommand command = new NpgsqlCommand())
                    {
                        command.Connection     = connection;
                        command.CommandType    = CommandType.Text;
                        command.CommandText    = sql;
                        command.CommandTimeout = 0x3e8;
                        using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
                        {
                            adapter.SelectCommand = command;
                            DataSet dataSet = new DataSet();
                            connection.Open();
                            adapter.Fill(dataSet);
                            connection.Close();
                            set2 = dataSet;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                SanitaLog.Error("NpgsqlBaseDao", "GetDataSet error !");
                SanitaLog.Exception(exception);
                set2 = null;
            }
            return(set2);
        }
 public DataRow GetDataRow(string sql)
 {
     try
     {
         DataRow   row       = null;
         DataTable dataTable = this.GetDataTable(sql);
         if (dataTable != null)
         {
             if (dataTable.Rows.Count > 0)
             {
                 row = dataTable.Rows[0];
             }
             else
             {
                 dataTable.Rows.Add(new object[] { 0 });
                 row = dataTable.Rows[0];
             }
         }
         return(row);
     }
     catch (Exception exception)
     {
         SanitaLog.Exception(exception);
         return(null);
     }
 }
        public DataTable GetDataTable(string sql)
        {
            Exception exception;
            DataTable table2;

            try
            {
                bool      flag;
                int       num       = 0;
                DataTable dataTable = new DataTable();
                goto Label_0118;
Label_0010:
                using (NpgsqlConnection connection = this.CreateConnection())
                {
                    using (NpgsqlCommand command = new NpgsqlCommand())
                    {
                        command.Connection     = connection;
                        command.CommandType    = CommandType.Text;
                        command.CommandText    = "SET enable_seqscan = off; " + sql;
                        command.CommandTimeout = 0x3e8;
                        try
                        {
                            using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
                            {
                                adapter.SelectCommand = command;
                                connection.Open();
                                adapter.Fill(dataTable);
                                connection.Close();
                                return(dataTable);
                            }
                        }
                        catch (Exception exception1)
                        {
                            exception = exception1;
                            SanitaLog.Error("NpgsqlBaseDao" + "GetDataTable 1 error !");
                            SanitaLog.Exception(exception1);
                        }
                        num++;
                        if (num > 5)
                        {
                            SanitaLog.Error("NpgsqlBaseDao" + "GetDataTable 2 error ! TryMax > 5");
                            return(null);
                        }
                    }
                }
Label_0118:
                flag = true;
                goto Label_0010;
            }
            catch (Exception exception2)
            {
                exception = exception2;
                SanitaLog.Exception(exception2);
                table2 = null;
            }
            return(table2);
        }
        public int Update(string _sql)
        {
            int num3;

            lock (this.lockDBObject)
            {
                Exception exception;
                string    str = _sql;
                try
                {
                    bool flag2;
                    int  num = 0;
                    goto Label_0108;
Label_0020:
                    using (NpgsqlConnection connection = this.CreateConnection())
                    {
                        using (NpgsqlCommand command = new NpgsqlCommand())
                        {
                            try
                            {
                                command.Connection     = connection;
                                command.CommandText    = str;
                                command.CommandTimeout = 0x3e8;
                                connection.Open();
                                return(command.ExecuteNonQuery());
                            }
                            catch (Exception exception1)
                            {
                                exception = exception1;
                                SanitaLog.Exception(exception);
                            }
                            num++;
                            if (num > 3)
                            {
                                SanitaLog.Error("NpgsqlBaseDao" + "Update('" + str + "') error !");
                                return(-100);
                            }
                            SanitaLog.Error("NpgsqlBaseDao", "Try Update N = " + num.ToString());
                        }
                    }
Label_0108:
                    flag2 = true;
                    goto Label_0020;
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    SanitaLog.Error("NpgsqlBaseDao", "Update('" + str + "') error !");
                    SanitaLog.Exception(exception);
                    num3 = -100;
                }
            }
            return(num3);
        }
        public DataTable GetDataTable(IDbConnection connection, IDbTransaction trans, string sql)
        {
            Exception exception;
            DataTable table2;

            try
            {
                bool      flag;
                int       num       = 0;
                DataTable dataTable = new DataTable();
                goto Label_00EF;
Label_0010:
                using (NpgsqlCommand command = new NpgsqlCommand())
                {
                    command.Connection     = connection as NpgsqlConnection;
                    command.Transaction    = trans as NpgsqlTransaction;
                    command.CommandType    = CommandType.Text;
                    command.CommandText    = sql;
                    command.CommandTimeout = 0x3e8;
                    try
                    {
                        using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
                        {
                            adapter.SelectCommand = command;
                            adapter.Fill(dataTable);
                            return(dataTable);
                        }
                    }
                    catch (Exception exception1)
                    {
                        exception = exception1;
                        SanitaLog.Exception(exception1);
                    }
                    num++;
                    if (num > 5)
                    {
                        SanitaLog.Error("NpgsqlBaseDao" + "GetDataTable 2 error ! TryMax > 5");
                        return(null);
                    }
                }
Label_00EF:
                flag = true;
                goto Label_0010;
            }
            catch (Exception exception2)
            {
                exception = exception2;
                SanitaLog.Error("NpgsqlBaseDao" + "GetDataTable 3 error");
                SanitaLog.Exception(exception2);
                table2 = null;
            }
            return(table2);
        }
        public int Insert(IDbConnection connection, IDbTransaction trans, string _sql)
        {
            int    num;
            string sql = _sql;

            try
            {
                using (NpgsqlCommand command = new NpgsqlCommand())
                {
                    command.Connection     = connection as NpgsqlConnection;
                    command.Transaction    = trans as NpgsqlTransaction;
                    command.CommandText    = sql;
                    command.CommandTimeout = 30;
                    num = command.ExecuteNonQuery();
                }
            }
            catch (Exception exception)
            {
                SanitaLog.Error("NpgsqlBaseDao", "Insert('" + sql + "') error !");
                SanitaLog.Exception(exception);
                SanitaLog.Error("NpgsqlBaseDao", "=>Do rollback: --START");
                try
                {
                    if (trans != null)
                    {
                        trans.Rollback();
                        SanitaLog.Error("NpgsqlBaseDao", "=>Do rollback OK");
                    }
                    if (connection != null)
                    {
                        connection.Close();
                        SanitaLog.Error("NpgsqlBaseDao", "=>Do close connection OK");
                    }
                }
                catch (Exception exception2)
                {
                    SanitaLog.Error("NpgsqlBaseDao", "=>Do rollback NG");
                    SanitaLog.Exception(exception2);
                }
                SanitaLog.Error("NpgsqlBaseDao", "=>Do rollback: --END");
                num = -100;
            }
            return(num);
        }
 public DataRow GetDataRow(IDbConnection connection, IDbTransaction trans, string sql)
 {
     try
     {
         DataRow   row   = null;
         DataTable table = this.GetDataTable(connection, trans, sql);
         if ((table != null) && (table.Rows.Count > 0))
         {
             row = table.Rows[0];
         }
         return(row);
     }
     catch (Exception exception)
     {
         SanitaLog.Error("NpgsqlBaseDao" + "GetDataRow 1 error !");
         SanitaLog.Exception(exception);
         return(null);
     }
 }
        public int InsertContract(NpgsqlConnection connection, IDbTransaction trans, ContractInfo mContractInfo)
        {
            try
            {
                lock (lockObject)
                {
                    if (mContractInfo == null)
                    {
                        return(DataTypeModel.RESULT_NG);
                    }

                    StringBuilder builder = new StringBuilder();
                    builder.Append(" INSERT INTO Contract (");
                    builder.Append("            Id,");
                    builder.Append("            Name,");
                    builder.Append("            Address,");
                    builder.Append("            Abi,");
                    builder.Append("            ByteCode,");
                    builder.Append("            Active)");
                    builder.Append("  VALUES( ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.Id) + ", ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.Name) + ", ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.Address) + ", ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.Abi) + ", ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.ByteCode) + ", ");
                    builder.Append("          " + DatabaseUtility.Escape(mContractInfo.Active) + ") ");

                    int num = base.baseDAO.DoInsert(connection, trans, builder.ToString());
                    if (num > 0)
                    {
                        return(DataTypeModel.RESULT_OK);
                    }

                    return(DataTypeModel.RESULT_NG);
                }
            }
            catch (Exception ex)
            {
                SanitaLog.Exception(ex);
                throw ex;
            }
        }
Beispiel #11
0
 public NpgsqlConnection GetConnection()
 {
     try
     {
         string host         = "localhost";
         string port         = "5432";
         string user         = "******";
         string password     = "******";
         string dataBaseName = "smartcontract";
         // PostgeSQL-style connection string
         string connstring = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};", host, port, user, password, dataBaseName);
         // Making connection with Npgsql provider
         NpgsqlConnection conn = new NpgsqlConnection(connstring);
         return(conn);
     }
     catch (Exception e)
     {
         // something went wrong, and you wanna know why
         SanitaLog.Exception(e);
         throw e;
     }
 }
        public async Task <string> GET()
        {
            try
            {
                SanitaLog.Method("GET", "TestController");

                // this will leave the account unlucked for 2 minutes
                string        contractName      = "King.sol";
                int           accountUnlockTime = 120;
                string        accountPublicKey  = "0xd6b37e4590c65787437f57e1ad7bb6b9a6f7ba8f";
                string        accountPassword   = "******";
                HexBigInteger gas = new HexBigInteger(3000000);
                //HexBigInteger balance = new HexBigInteger(120);
                var multiplier = 7;


                // Unlock the caller's account with the given password
                bool unlockResult = await Web3Api.UnlockAccount(accountPublicKey, accountPassword, accountUnlockTime);

                //Get abi of contract
                TheContract mTheContract = await Web3Api.GetTheContract(contractName);

                if (mTheContract != null)
                {
                    string abi      = mTheContract.Abi;
                    string byteCode = "0x" + mTheContract.Bytecode;

                    BaseDAO baseDAO = new BaseDAO();

                    //Get connection
                    using (NpgsqlConnection connection = baseDAO.GetConnection())
                    {
                        try
                        {
                            //Open connection
                            connection.Open();

                            HexBigInteger estimateGas = await Web3Api.mWeb3.Eth.DeployContract.EstimateGasAsync(abi, byteCode, accountPublicKey);

                            SanitaLog.Log("Estimate gas", estimateGas);

                            //TODO: Add code to save address of transactionHash => not deploy multiple times
                            //Find contract is deployed

                            ContractInfo mContractInfo = ContractPresenter.GetContract(contractName);

                            //If contract is not exist or not update
                            if (mContractInfo == null || !String.Equals(mContractInfo.ByteCode, byteCode))
                            {
                                string transactionHash = await Web3Api.WaitDeploy(abi, byteCode, accountPublicKey, gas, multiplier);

                                //Minner
                                TransactionReceipt receipt = await Web3Api.WaitMiner(transactionHash);

                                //Insert or update contract
                                mContractInfo          = new ContractInfo();
                                mContractInfo.Name     = contractName;
                                mContractInfo.Address  = receipt.ContractAddress;
                                mContractInfo.Abi      = abi;
                                mContractInfo.ByteCode = byteCode;
                                mContractInfo.Active   = DataTypeModel.ACTIVE;

                                int _result = ContractPresenter.SaveContract(connection, null, mContractInfo);

                                if (_result == DataTypeModel.RESULT_NG)
                                {
                                    SanitaLog.Error("No save contract to databse");
                                    return("Error occur. Please try again.");
                                }
                            }

                            //Close connection
                            connection.Close();

                            //Get contract
                            Nethereum.Contracts.Contract contract = Web3Api.GetContract(abi, mContractInfo.Address);

                            #region Error
                            //Get event
                            //Event multiplyEvent = contract.GetEvent("Multiplied");

                            //var filterAll = await multiplyEvent.CreateFilterAsync();
                            //var filter7 = await multiplyEvent.CreateFilterAsync(7);

                            //var gasFunc = await multiplyFunction.EstimateGasAsync(accountPublicKey, null, null, newAddress, amountToSend);

                            //Call event
                            //transactionHash = await multiplyFunction.SendTransactionAsync(accountPublicKey, 7);
                            //SanitaLog.Log("transaction hash", transactionHash);

                            //Call event
                            //transactionHash = await multiplyFunction.SendTransactionAsync(accountPublicKey, 8);
                            //SanitaLog.Log("transaction hash", transactionHash);

                            //Minner
                            //receipt = await Web3Api.WaitMiner(transactionHash);
                            #endregion Error

                            Function funct = null;

                            funct = Web3Api.getFunction(contract, "getOwner");
                            var result = await funct.CallAsync <string>();

                            SanitaLog.Log("Result of function getOwner", result);

                            funct = Web3Api.getFunction(contract, "showListShareHolders");
                            var mListShareHolders = await funct.CallAsync <List <string> >();

                            SanitaLog.Log("Result of function showListShareHolders", Utility.ToStringList(mListShareHolders));

                            funct = Web3Api.getFunction(contract, "addListShareHolders");
                            await funct.CallAsync <object>("0x5e98ff12d889945488ddd53ffb71f6580cae1571");

                            funct = Web3Api.getFunction(contract, "addListShareHolders");
                            await funct.CallAsync <object>("0x9bb7f17d1d53774e0fdfa4b76ba68c48e8a4daba");

                            funct = Web3Api.getFunction(contract, "addListShareHolders");
                            await funct.CallAsync <object>("0xee2d522d8f10769a5f150ed55f40f305c8d0595f");

                            funct             = Web3Api.getFunction(contract, "showListShareHolders");
                            mListShareHolders = await funct.CallAsync <List <string> >();

                            SanitaLog.Log("Result of function showListShareHolders", Utility.ToStringList(mListShareHolders));

                            SanitaLog.Success("Success deploy contract");
                            return("success");
                        }
                        catch (Exception exception1)
                        {
                            //Close connection
                            connection.Close();

                            SanitaLog.Exception(exception1);
                            return("Error occur. Please try again.");
                        }
                    }
                }
                else
                {
                    SanitaLog.Error("Fail deploy contract");
                    return("fail");
                }
            }
            catch (Exception exception2)
            {
                SanitaLog.Exception(exception2);
                return("Error occur. Please try again.");
            }
        }