Exemplo n.º 1
0
        private async Task <string> SendTransactionAsync(string from, string to, string data, BigInteger value, BigInteger?gasPrice, BigInteger?gasValue)
        {
            from = from == Constants.AddressForRoundRobinTransactionSending
                 ? await _transactionRouter.GetNextSenderAddressAsync()
                 : from;

            var semaphore = _semaphores.GetOrAdd(from, f => new SemaphoreSlim(1, 1));

            try
            {
                await semaphore.WaitAsync();

                (gasPrice, gasValue) = await GetGasPriceAndValueAsync(gasPrice, gasValue);

                var nonce = await _nonceCalculator.GetNonceAsync(from, true);

                var transaction = new Nethereum.Signer.Transaction(to, value, nonce.Value, gasPrice.Value, gasValue.Value, data);
                var signRequest = new EthereumTransactionSignRequest
                {
                    FromProperty = new AddressUtil().ConvertToChecksumAddress(from),
                    Transaction  = transaction.GetRLPEncoded().ToHex()
                };

                var signResponse = await _signingApi.ApiEthereumSignPostAsync(signRequest);

                var txHash = await _sendRawTransaction.SendRequestAsync(signResponse.SignedTransaction.EnsureHexPrefix());

                return(txHash);
            }
            finally
            {
                semaphore.Release();
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        private async Task <string> SignTransaction(Profile profile, Nethereum.Signer.Transaction transaction)
        {
            string signature = await profile.SignTransaction(transaction.GetRLPEncodedRaw());

            transaction.SetSignature(Nethereum.Signer.EthECDSASignatureFactory.ExtractECDSASignature(signature));
            return(transaction.GetRLPEncoded().ToHex(true));
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        public string BuildTransaction(string to, BigInteger amount, BigInteger nonce, BigInteger gasPrice, BigInteger gasAmount)
        {
            var transaction = new Transaction
                              (
                to,
                amount,
                nonce,
                gasPrice,
                gasAmount,
                null
                              );

            return(transaction.GetRLPEncoded().ToHex());
        }
        public async Task <string> GetTransactionForSigning(EthTransaction ethTransaction, bool useTxPool = false)
        {
            string from = ethTransaction.FromAddress;

            var gas      = new Nethereum.Hex.HexTypes.HexBigInteger(ethTransaction.GasAmount);
            var gasPrice = new Nethereum.Hex.HexTypes.HexBigInteger(ethTransaction.GasPrice);
            var nonce    = await _nonceCalculator.GetNonceAsync(from, useTxPool);

            var to    = ethTransaction.ToAddress;
            var value = new Nethereum.Hex.HexTypes.HexBigInteger(ethTransaction.Value);
            var tr    = new Nethereum.Signer.Transaction(to, value, nonce, gasPrice, gas);
            var hex   = tr.GetRLPEncoded().ToHex();

            return(hex);
        }