示例#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);
        }
示例#2
0
        /// <summary>
        /// Creates new contract deployment transaction and expects a revert.
        /// Throws an exception if transaction does not revert.
        /// </summary>
        public async Task ExpectRevert()
        {
            var txParams = await GetTransactionParams();

            (JsonRpcError error, Hash transactionHash) = await ContractFactory.TryDeploy(_contractAttribute, _rpcClient, _bytecode, txParams, _abiEncodedConstructorArgs);

            if (error == null)
            {
                var receipt = await _rpcClient.GetTransactionReceipt(transactionHash);

                if (receipt == null)
                {
                    throw new Exception($"Expected contract deployment transaction to revert, but server node did not return a transaction receipt for transaction hash: {transactionHash}");
                }

                if (receipt.Status != 0)
                {
                    throw new Exception($"Expected contract deployment transaction to revert");
                }
            }
        }
示例#3
0
 public virtual Task <TransactionReceipt> GetTransactionReceipt(Hash transactionHash)
 {
     return(_proxyClient.GetTransactionReceipt(transactionHash));
 }