Beispiel #1
0
        /// <summary>
        /// Deploys a contract with constructor arguments
        /// </summary>
        /// <param name="abiEncodedConstructorArgs">ABI encoded function selector and constructor parameters</param>
        public static async Task <Address> Deploy(
            SolidityContractAttribute contractAttribute,
            IJsonRpcClient rpcClient,
            byte[] bytecode,
            TransactionParams sendParams,
            ReadOnlyMemory <byte> abiEncodedConstructorArgs = default)
        {
            (JsonRpcError error, Hash transactionHash) = await TryDeploy(expectException : false, contractAttribute, rpcClient, bytecode, sendParams, abiEncodedConstructorArgs);

            if (error != null)
            {
                if (rpcClient.ErrorFormatter != null)
                {
                    var formattedException = await rpcClient.ErrorFormatter(rpcClient, error);

                    throw formattedException;
                }
                else
                {
                    throw error.ToException();
                }
            }

            var receipt = await rpcClient.GetTransactionReceipt(transactionHash);

            if (receipt == null)
            {
                throw new Exception("Contract deployment transaction failed: no transaction receipt returned from server node.");
            }

            if (receipt.Status == 0)
            {
                // TODO: the server should have returned a json rpc error for this transaction rather than ending up here.
                if (rpcClient.ErrorFormatter != null)
                {
                    var formattedException = await rpcClient.ErrorFormatter(rpcClient, null);

                    throw formattedException;
                }

                throw new Exception("Transaction failed: bad status code on transaction receipt.");
            }

            var contractAddress = receipt.ContractAddress.Value;

            return(contractAddress);
        }