예제 #1
0
        public async Task <ReturnObject> TestSmartContractFunction()
        {
            try
            {
                //  var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(senderAddress, password, new HexBigInteger(120));
                //  Assert.True(unlockResult);
                // bool unlockResult = await Web3Api.UnlockAccount(AppSettingHelper.GetSmartContractPublicKey(), AppSettingHelper.GetSmartContractPrivateKey(), 120);
                string abi = AppSettingHelper.GetSmartContractAbi();
                Nethereum.Contracts.Contract contract =
                    Web3Api.GetContract(abi, AppSettingHelper.GetSmartContractAddress());
                Function funct1  = Web3Api.getFunction(contract, "balanceDollars");
                var      result1 = await funct1.CallAsync <BigInteger>("0xc942F1D286d9b8002206CbB3196f46Fa892aAD93");

                Function funct  = Web3Api.getFunction(contract, "transfer");
                var      _thing = await funct.SendTransactionAndWaitForReceiptAsync(
                    AppSettingHelper.GetSmartContractPublicKey(), null, "0xc942F1D286d9b8002206CbB3196f46Fa892aAD93",
                    100000000000000);

                // var result = await funct.CallDeserializingToObjectAsync<bool>("0xc942F1D286d9b8002206CbB3196f46Fa892aAD93", 100000000000000);
                Console.WriteLine(_thing);
                return(new ReturnObject
                {
                    Status = Status.STATUS_COMPLETED,
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new ReturnObject
                {
                    Status = Status.STATUS_ERROR,
                    Message = e.Message
                });
            }
        }
예제 #2
0
 public static void DecodeInput(string input, Nethereum.Contracts.Contract contracts)
 {
     //mWeb3.Eth.
     //Nethereum.ABI.FunctionEncoding.ParameterDecoder dec = new Nethereum.ABI.FunctionEncoding.ParameterDecoder();
     //dec.DecodeDefaultData(input,);
     //   = new Nethereum.Contracts.Contract();
 }
        // Retrieve the total number of transactions of your sender address

        public static Nethereum.Contracts.Contract GetContract(string abi, string contractAddress)
        {
            Nethereum.Contracts.Contract contract = mWeb3.Eth.GetContract(abi, contractAddress);
            SanitaLog.Log("abi", abi);
            SanitaLog.Log("contract address", contractAddress);
            SanitaLog.LogObject("contract", contract);
            return(contract);
        }
예제 #4
0
        public List <ParameterOutput> DecodeInput(string input)
        {
            string abi = AppSettingHelper.GetSmartContractAbi();

            Nethereum.Contracts.Contract
                     contract = Web3Api.GetContract(abi, AppSettingHelper.GetSmartContractAddress());
            Function funct    = Web3Api.getFunction(contract, "transferFromByOwner");

            return(Web3Api.DecodeInput(input, funct));
        }
예제 #5
0
        public async Task <ReturnObject> SendTransactionAsync1(BlockchainTransaction blockchainTransaction)
        {
            try
            {
                var account = new Account(AppSettingHelper.GetSmartContractPrivateKey());

                var web3    = new Web3(account, AppSettingHelper.GetSmartContractInfura());
                var txCount =
                    await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(AppSettingHelper
                                                                                     .GetSmartContractPublicKey());

                var value = (BigInteger)EtherToWei(blockchainTransaction.Amount);

                var transactionMessage = new TransferFunction()
                {
                    FromAddress = blockchainTransaction.FromAddress,
                    To          = blockchainTransaction.ToAddress,
                    Value       = value
                };

                string abi = AppSettingHelper.GetSmartContractAbi();
                Nethereum.Contracts.Contract contract =
                    web3.Eth.GetContract(abi, AppSettingHelper.GetSmartContractAddress());
                var transferHandler = web3.Eth.GetContractTransactionHandler <TransferFunction>();

                var gas = await transferHandler.EstimateGasAsync(transactionMessage,
                                                                 AppSettingHelper.GetSmartContractAddress());

                web3.TransactionManager.DefaultGas = gas.Value * 2;
                Function funct  = contract.GetFunction("transferFromByOwner");
                var      _thing = await funct.SendTransactionAndWaitForReceiptAsync(
                    AppSettingHelper.GetSmartContractPublicKey(), null, blockchainTransaction.FromAddress,
                    blockchainTransaction.ToAddress, value);

                return(new ReturnObject
                {
                    Status = Status.STATUS_COMPLETED,
                    Data = _thing.TransactionHash
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(new ReturnObject
                {
                    Status = Status.STATUS_ERROR,
                    Message = e.Message
                });
            }
        }
예제 #6
0
 public static void setContract(string abi, string addr)
 {
     address  = addr;
     ABI      = abi;
     contract = new Nethereum.Contracts.Contract(null, abi, addr);
 }
        public bool LoadContract(string address)
        {
            _contract = _web3.Eth.GetContract(ABI, address);

            return(_contract != null);
        }
 public CoinService(Web3 web3, string address)
 {
     this.web3     = web3;
     this.contract = web3.Eth.GetContract(ABI, address);
 }
        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.");
            }
        }
예제 #10
0
        public static Function getFunction(Nethereum.Contracts.Contract contract, string functionName)
        {
            Function funct = contract.GetFunction(functionName);

            return(funct);
        }
예제 #11
0
        // Retrieve the total number of transactions of your sender address

        public static Nethereum.Contracts.Contract GetContract(string abi, string contractAddress)
        {
            Nethereum.Contracts.Contract contract = mWeb3.Eth.GetContract(abi, contractAddress);
            return(contract);
        }