예제 #1
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)));
        }
예제 #2
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)));
        }