Ejemplo n.º 1
0
        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.");
            }
        }