Ejemplo n.º 1
0
        /// <summary>
        /// Utility to send ETH on blockchain signing it by given account
        /// </summary>
        /// <returns>Receipt of called transaction</returns>
        public static async Task <TransactionReceipt> EvaluateOnBC(Web3 web, Profile profile, string to, HexBigInteger amount)
        {
            Debug.Assert(profile != null);

            var        nonceService = new InMemoryNonceService(profile.ID, web.Client);
            BigInteger nonce        = await nonceService.GetNextNonceAsync();

            var    transaction  = new Nethereum.Signer.Transaction(to, amount, nonce);
            var    rlpEncodedTx = transaction.GetRLPEncodedRaw();
            string signature    = await profile.SignTransaction(rlpEncodedTx);

            transaction.SetSignature(Nethereum.Signer.EthECDSASignatureFactory.ExtractECDSASignature(signature));

            var    signedTransaction = transaction.GetRLPEncoded().ToHex(true);
            string txId = await web.Eth.Transactions.SendRawTransaction.SendRequestAsync(signedTransaction);

            TransactionReceipt receipt = await web.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txId);

            while (receipt == null)
            {
                Thread.Sleep(1000);
                receipt = await web.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txId);
            }
            return(receipt);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Utility to call functions on blockchain signing it by given account
        /// </summary>
        /// <returns>Receipt of called transaction</returns>
        public static async Task <TransactionReceipt> EvaluateOnBC(Web3 web, Profile profile, Function function, params object[] functionInput)
        {
            Debug.Assert(profile != null);

            var gasPrice = await web.Eth.GasPrice.SendRequestAsync();

            HexBigInteger gas = await function.EstimateGasAsync(profile.ID, gasPrice, new HexBigInteger(0), functionInput);

            var        nonceService = new InMemoryNonceService(profile.ID, web.Client);
            BigInteger nonce        = await nonceService.GetNextNonceAsync();

            string data         = function.GetData(functionInput);
            var    transaction  = new Nethereum.Signer.Transaction(function.ContractAddress, BigInteger.Zero, nonce, gasPrice, gas.Value, data);
            var    rlpEncodedTx = transaction.GetRLPEncodedRaw();

            string signature = await profile.SignTransaction(rlpEncodedTx);

            transaction.SetSignature(Nethereum.Signer.EthECDSASignatureFactory.ExtractECDSASignature(signature));

            var    signedTransaction = transaction.GetRLPEncoded().ToHex(true);
            string txId = await web.Eth.Transactions.SendRawTransaction.SendRequestAsync(signedTransaction).ConfigureAwait(false);

            TransactionReceipt receipt = await web.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txId);

            while (receipt == null)
            {
                Thread.Sleep(1000);
                receipt = await web.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txId);
            }
            return(receipt);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends a message to an ethereum smart contract with the intent to change a part of the contract on the blockchain.
        /// </summary>
        /// <typeparam name="TFunc"> The type of the function to execute. </typeparam>
        /// <param name="function"> The function to execute. </param>
        /// <param name="privateKey"> The private key of the address executing the contract function. </param>
        /// <param name="contractAddress"> The address of the contract which will process the message. </param>
        /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param>
        /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param>
        /// <param name="value"> The amount of eth (in wei) to send with the transaction. </param>
        /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns>
        public static async Task <TransactionPoller> SendContractMessage <TFunc>(
            TFunc function,
            string privateKey,
            string contractAddress,
            BigInteger gasPrice,
            BigInteger gasLimit,
            BigInteger value) where TFunc : FunctionMessage, new()
        {
            EthECKey ethECKey = new EthECKey(privateKey);

            function.SetDefaultFromAddressIfNotSet(ethECKey.GetPublicAddress());
            function.Gas      = gasLimit;
            function.GasPrice = gasPrice;

            InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client);

            TransactionSigner signer       = new TransactionSigner();
            string            signedTxData = signer.SignTransaction(
                privateKey,
                NetworkProvider.GetActiveNetworkChain(),
                contractAddress,
                value,
                (await nonceService.GetNextNonceAsync()).Value,
                gasPrice,
                gasLimit,
                function.CreateTransactionInput(contractAddress).Data);

            EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client);

            return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData)));
        }
Ejemplo n.º 4
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnMultipleWeb3sMultithreaded()
        {
            var senderAddress = EthereumClientIntegrationFixture.AccountAddress;
            var privateKey    = EthereumClientIntegrationFixture.AccountPrivateKey;
            var abi           =
                @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            JsonRpc.Client.RpcClient.ConnectionTimeout = TimeSpan.FromSeconds(30.0);
            var multiplier = 7;

            var client        = _ethereumClientIntegrationFixture.GetClient();
            var nonceProvider = new InMemoryNonceService(senderAddress, client);
            //tested with 1000
            var listTasks = 10;
            var taskItems = new List <int>();

            for (var i = 0; i < listTasks; i++)
            {
                taskItems.Add(i);
            }

            var numProcs             = Environment.ProcessorCount;
            var concurrencyLevel     = numProcs * 2;
            var concurrentDictionary = new ConcurrentDictionary <int, string>(concurrencyLevel, listTasks * 2);


            Parallel.ForEach(taskItems, (item, state) =>
            {
                var account          = new Account(privateKey, EthereumClientIntegrationFixture.ChainId);
                account.NonceService = nonceProvider;
                var web3             = new Web3.Web3(account, client);
                // Wait for task completion synchronously in order to Parallel.ForEach work correctly
                var txn = web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000),
                                                                   null, multiplier).Result;
                concurrentDictionary.TryAdd(item, txn);
            });

            var web31       = new Web3.Web3(new Account(privateKey), client);
            var pollService = new TransactionReceiptPollingService(web31.TransactionManager);

            for (var i = 0; i < listTasks; i++)
            {
                string txn = null;
                concurrentDictionary.TryGetValue(i, out txn);
                var receipt = await pollService.PollForReceiptAsync(txn).ConfigureAwait(false);

                Assert.NotNull(receipt);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates transactin passing typed argument
        /// </summary>
        /// <typeparam name="TFunctionInput"></typeparam>
        /// <param name="web"></param>
        /// <param name="addressFrom"></param>
        /// <param name="amount"></param>
        /// <param name="function"></param>
        /// <param name="functionInput"></param>
        /// <returns></returns>
        public static async Task <Nethereum.Signer.Transaction> CreateTransaction <TFunctionInput>(Web3 web, string addressFrom, BigInteger amount, Function <TFunctionInput> function, TFunctionInput functionInput)
        {
            var gasPrice = await web.Eth.GasPrice.SendRequestAsync();

            HexBigInteger gas = await function.EstimateGasAsync(functionInput, addressFrom.EnsureHexPrefix(), gasPrice, new HexBigInteger(amount));

            var        nonceService = new InMemoryNonceService(addressFrom.EnsureHexPrefix(), web.Client);
            BigInteger nonce        = await nonceService.GetNextNonceAsync();

            string data        = function.GetData(functionInput);
            var    transaction = new Nethereum.Signer.Transaction(function.ContractAddress, amount, nonce, gasPrice, gas.Value, data);

            return(transaction);
        }
Ejemplo n.º 6
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnMultipleWeb3sMultithreaded()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var privateKey    = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var abi           =
                @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var client        = ClientFactory.GetClient();
            var nonceProvider = new InMemoryNonceService(senderAddress, client);
            //tested with 1000
            var listTasks = 10;
            var taskItems = new List <int>();

            for (var i = 0; i < listTasks; i++)
            {
                taskItems.Add(i);
            }

            var numProcs             = Environment.ProcessorCount;
            var concurrencyLevel     = numProcs * 2;
            var concurrentDictionary = new ConcurrentDictionary <int, string>(concurrencyLevel, listTasks * 2);


            Parallel.ForEach(taskItems, (item, state) =>
            {
                var account          = new Account(privateKey);
                account.NonceService = nonceProvider;
                var web3             = new Web3.Web3(account, client);
                // Wait for task completion synchronously in order to Parallel.ForEach work correctly
                var txn = web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000),
                                                                   null, multiplier).Result;
                concurrentDictionary.TryAdd(item, txn);
            });

            var web31       = new Web3.Web3(new Account(privateKey), client);
            var pollService = new TransactionReceiptPollingService(web31.TransactionManager);

            for (var i = 0; i < listTasks; i++)
            {
                string txn = null;
                concurrentDictionary.TryGetValue(i, out txn);
                var receipt = await pollService.PollForReceiptAsync(txn);

                Assert.NotNull(receipt);
            }
        }
Ejemplo n.º 7
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnMultipleWeb3sSingleThreaded()
        {
            var senderAddress = EthereumClientIntegrationFixture.AccountAddress;
            var privateKey    = EthereumClientIntegrationFixture.AccountPrivateKey;

            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var client        = _ethereumClientIntegrationFixture.GetClient();
            var nonceProvider = new InMemoryNonceService(senderAddress, client);
            var account       = new Account(privateKey, EthereumClientIntegrationFixture.ChainId)
            {
                NonceService = nonceProvider
            };
            var web31 = new Web3.Web3(account, client);

            var txn1 = await
                       web31.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier).ConfigureAwait(false);

            var web32 = new Web3.Web3(account, client);


            var txn2 = await
                       web32.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier).ConfigureAwait(false);

            var web33 = new Web3.Web3(account, client);

            var txn3 = await
                       web33.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier).ConfigureAwait(false);

            var pollService = new TransactionReceiptPollingService(web31.TransactionManager);

            var receipt1 = await pollService.PollForReceiptAsync(txn1).ConfigureAwait(false);

            var receipt2 = await pollService.PollForReceiptAsync(txn2).ConfigureAwait(false);

            var receipt3 = await pollService.PollForReceiptAsync(txn3).ConfigureAwait(false);

            Assert.NotNull(receipt1);
            Assert.NotNull(receipt2);
            Assert.NotNull(receipt3);
        }
Ejemplo n.º 8
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnMultipleWeb3sSingleThreaded()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var privateKey    = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var abi           =
                @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var client        = ClientFactory.GetClient();
            var nonceProvider = new InMemoryNonceService(senderAddress, client);
            var account       = new Account(privateKey)
            {
                NonceService = nonceProvider
            };
            var web31 = new Web3.Web3(account, client);

            var txn1 = await
                       web31.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier);

            var web32 = new Web3.Web3(account, client);


            var txn2 = await
                       web32.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier);

            var web33 = new Web3.Web3(account, client);

            var txn3 = await
                       web33.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null,
                                                                 multiplier);

            var pollService = new TransactionReceiptPollingService(web31.TransactionManager);

            var receipt1 = await pollService.PollForReceiptAsync(txn1);

            var receipt2 = await pollService.PollForReceiptAsync(txn2);

            var receipt3 = await pollService.PollForReceiptAsync(txn3);

            Assert.NotNull(receipt1);
            Assert.NotNull(receipt2);
            Assert.NotNull(receipt3);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates a trade order for an ERC223 tokens in currency expressed in ERC233 tokens
        /// </summary>
        /// <param name="from">seller profile</param>
        /// <param name="tokenGet">type of currency</param>
        /// <param name="amountGet">price in given currency</param>
        /// <param name="tokenGive">type of token to sell</param>
        /// <param name="amountGive">amount of tokens to sell</param>
        /// <param name="blockTimeDuration">expiration time of order in block number</param>
        /// <returns>true if order has been created, false otherwise</returns>
        public async Task <bool> Order(
            Profile from,
            string tokenGet,
            BigInteger amountGet,
            string tokenGive,
            BigInteger amountGive,
            ulong blockTimeDuration)
        {
            // FIXME: should NOT take nonce from web3
            var        nonceService = new InMemoryNonceService(from.ID, web3.Client);
            BigInteger nonce        = await nonceService.GetNextNonceAsync();

            var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();

            var expires = blockNumber.Value + blockTimeDuration;

            var function = GetFunctionOrder();
            var receipt  = await BCComm.EvaluateOnBC(web3, from, function, tokenGet, amountGet, tokenGive, amountGive, expires, nonce);

            return(receipt.Status.Value == 1);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sends a given amount of ether to an address.
        /// </summary>
        /// <param name="privateKey"> The private key of the address which is sending the ether. </param>
        /// <param name="addressTo"> The address the ether is being sent to. </param>
        /// <param name="amount"> The amount of ether being sent, in eth. (not wei) </param>
        /// <param name="gasPrice"> The gas price (in wei) to use to send the transaction. </param>
        /// <param name="gasLimit"> The gas limit (in wei) to use to send the transaction. </param>
        /// <returns> Task which returns the TransactionPoller which will await the transaction result. </returns>
        public static async Task <TransactionPoller> SendEther(string privateKey, string addressTo, decimal amount, BigInteger gasPrice, BigInteger gasLimit)
        {
            BigInteger value = SolidityUtils.ConvertToUInt(amount, 18);

            EthECKey ethECKey = new EthECKey(privateKey);

            InMemoryNonceService nonceService = new InMemoryNonceService(ethECKey.GetPublicAddress(), NetworkProvider.GetWeb3().Client);

            TransactionSigner signer       = new TransactionSigner();
            string            signedTxData = signer.SignTransaction(
                privateKey,
                NetworkProvider.GetActiveNetworkChain(),
                addressTo,
                value,
                (await nonceService.GetNextNonceAsync()).Value,
                gasPrice,
                gasLimit,
                string.Empty);

            EthSendRawTransaction rawTransaction = new EthSendRawTransaction(NetworkProvider.GetWeb3().Client);

            return(new TransactionPoller(await rawTransaction.SendRequestAsync(signedTxData)));
        }