Пример #1
0
        public async void ShouldBeAbleToReplaceContractToAccessState()
        {
            if (_ethereumClientIntegrationFixture.EthereumClient == EthereumClient.Geth)
            {
                var account = EthereumClientIntegrationFixture.GetAccount();
                var web3    = new Web3Geth(account, _ethereumClientIntegrationFixture.GetHttpUrl());

                var deploymentMessage = new SimpleStorageDeployment()
                {
                    Owner = EthereumClientIntegrationFixture.AccountAddress
                };

                var deploymentHandler = web3.Eth.GetContractDeploymentHandler <SimpleStorageDeployment>();
                var deploymentReceipt = await deploymentHandler.SendRequestAndWaitForReceiptAsync(deploymentMessage);

                var stateChanges = new Dictionary <string, StateChange>();
                stateChanges.Add(deploymentReceipt.ContractAddress,
                                 new StateChange()
                {
                    Code = SimpleStorage2DeployedByteCode.EnsureHexPrefix()
                });
                var result = await web3.GethEth.Call.SendRequestAsync(
                    new GetOwnerFunction().CreateTransactionInput(deploymentReceipt.ContractAddress),
                    BlockParameter.CreateLatest(), stateChanges);

                var output = new GetOwnerFunctionOutput();
                output = output.DecodeOutput(result);
                Assert.True(output.Owner.IsTheSameAddress(EthereumClientIntegrationFixture.AccountAddress));
            }
        }
        private ContractDeployment GetContractDeployment <T>() where T : ContractDeploymentMessage, new()
        {
            T contract = new T();
            ContractDeployment      deployment = null;
            WatchContractDeployment watchContractDeployment = null;
            SimpleStorageDeployment simpleStorageDeployment = null;

            if (contract is WatchContractDeployment)
            {
                watchContractDeployment = contract as WatchContractDeployment;
            }
            else if (contract is SimpleStorageDeployment)
            {
                simpleStorageDeployment = contract as SimpleStorageDeployment;
            }
            try {
                deployment = this.context.ContractDeployments.Where(s => s.ByteCode == contract.ByteCode && s.ServerUrl == this.url).First();
            } catch (InvalidOperationException ex) {
                logger.LogError($"contract does not currently exist: {ex}");
            }
            if (deployment == null)
            {
                deployment = new ContractDeployment();
                if (watchContractDeployment != null)
                {
                    watchContractDeployment.GasPrice = 0;
                    TransactionReceipt receipt = WatchContractService.DeployContractAndWaitForReceiptAsync(web3, watchContractDeployment).GetAwaiter().GetResult();
                    deployment.DeploymentAddress = receipt.ContractAddress;
                    deployment.ByteCode          = watchContractDeployment.ByteCode;
                }
                else if (simpleStorageDeployment != null)
                {
                    simpleStorageDeployment.GasPrice = 0;
                    TransactionReceipt receipt = SimpleStorageService.DeployContractAndWaitForReceiptAsync(web3, simpleStorageDeployment).GetAwaiter().GetResult();
                    deployment.DeploymentAddress = receipt.ContractAddress;
                    deployment.ByteCode          = simpleStorageDeployment.ByteCode;
                }
                deployment.ServerUrl = this.url;
                this.context.Add(deployment);
                this.context.SaveChanges();
            }
            return(deployment);
        }
        public static async Task <SimpleStorageService> DeployContractAndGetServiceAsync(Nethereum.Web3.Web3 web3, SimpleStorageDeployment simpleStorageDeployment, CancellationTokenSource cancellationTokenSource = null)
        {
            var receipt = await DeployContractAndWaitForReceiptAsync(web3, simpleStorageDeployment, cancellationTokenSource);

            return(new SimpleStorageService(web3, receipt.ContractAddress));
        }
 public static Task <string> DeployContractAsync(Nethereum.Web3.Web3 web3, SimpleStorageDeployment simpleStorageDeployment)
 {
     return(web3.Eth.GetContractDeploymentHandler <SimpleStorageDeployment>().SendRequestAsync(simpleStorageDeployment));
 }
 public static Task <TransactionReceipt> DeployContractAndWaitForReceiptAsync(Nethereum.Web3.Web3 web3, SimpleStorageDeployment simpleStorageDeployment, CancellationTokenSource cancellationTokenSource = null)
 {
     return(web3.Eth.GetContractDeploymentHandler <SimpleStorageDeployment>().SendRequestAndWaitForReceiptAsync(simpleStorageDeployment, cancellationTokenSource));
 }