Exemple #1
0
        public async Task <bool> Insert(SolidityContractAggregate contract)
        {
            if (contract == null)
            {
                throw new ArgumentNullException(nameof(contract));
            }

            var exists = await _currentDbContext.SolidityContracts.AnyAsync(w => w.Address == contract.Address).ConfigureAwait(false);

            if (exists)
            {
                return(false);
            }

            var record = new SolidityContract
            {
                Address = contract.Address,
                Code    = contract.Code,
                Abi     = contract.Abi
            };

            _currentDbContext.SolidityContracts.Add(record);
            await _currentDbContext.SaveChangesAsync().ConfigureAwait(false);

            return(true);
        }
Exemple #2
0
        private void PersistSmartContract(object sender, EventArgs e)
        {
            if (_viewModel == null)
            {
                return;
            }
            var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet();

            if (authenticatedWallet == null)
            {
                MainWindowStore.Instance().DisplayError("You're not authenticated");
                return;
            }

            if (string.IsNullOrWhiteSpace(_viewModel.TransactionId))
            {
                MainWindowStore.Instance().DisplayError("The transaction address must be filled in");
                return;
            }

            if (_publishedSolidityContract == null)
            {
                MainWindowStore.Instance().DisplayError("The smart contract must be published");
                return;
            }

            IEnumerable <byte> txId = null;

            try
            {
                txId = _viewModel.TransactionId.FromHexString();
            }
            catch (Exception)
            {
                MainWindowStore.Instance().DisplayError("The transaction address is not a valid hex");
                return;
            }

            var rpcClient = new RpcClient(authenticatedWallet.Network);

            rpcClient.GetTransactionReceipt(txId).ContinueWith((r) =>
            {
                try
                {
                    _publishedSolidityContract.Address = r.Result.ContractAddress;
                    _solidityContractsRepository.Insert(_publishedSolidityContract).ContinueWith((t) =>
                    {
                        try
                        {
                            if (t.Result)
                            {
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    _viewModel.NewSmartContractAddress = r.Result.ContractAddress;
                                    _publishedSolidityContract         = null;
                                    MainWindowStore.Instance().DisplayMessage("The transaction has been inserted");
                                });
                            }
                            else
                            {
                                Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract"));
                            }
                        }
                        catch
                        {
                            Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract"));
                        }
                    });
                }
                catch (AggregateException)
                {
                    Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to insert the smart contract"));
                }
            });
        }
Exemple #3
0
        private void PublishContract(object sender, EventArgs e)
        {
            if (_viewModel == null)
            {
                return;
            }
            var authenticatedWallet = WalletStore.Instance().GetAuthenticatedWallet();

            if (authenticatedWallet == null)
            {
                MainWindowStore.Instance().DisplayError("You're not authenticated");
                return;
            }

            if (string.IsNullOrWhiteSpace(_viewModel.SmartContract))
            {
                MainWindowStore.Instance().DisplayError("The solidity contract must be filled in");
                return;
            }

            var rpcClient = new RpcClient(authenticatedWallet.Network);

            _publishedSolidityContract = null;
            rpcClient.CompileSolidity(_viewModel.SmartContract).ContinueWith((t) =>
            {
                try
                {
                    var compilationResult = t.Result;
                    if (compilationResult.Infos == null || !compilationResult.Infos.Any())
                    {
                        return;
                    }

                    UpdateSmartContractDefinition(compilationResult);
                    var newKey   = _walletHelper.CreateNewAddress();
                    var fromAddr = newKey.GetPublicKeyHashed();
                    var smartContractTransaction = new SmartContractTransaction
                    {
                        From  = fromAddr,
                        Data  = compilationResult.Infos.First().Code.FromHexString(),
                        Nonce = NonceHelper.GetNonceInt32()
                    };
                    rpcClient.SendRawTransaction(smartContractTransaction).ContinueWith((c) =>
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            _viewModel.TransactionId = c.Result;
                        });
                        _publishedSolidityContract = new SolidityContractAggregate
                        {
                            Abi  = compilationResult.Infos.First().AbiDefinition.ToString(),
                            Code = compilationResult.Infos.First().Code
                        };
                    });
                }
                catch (AggregateException)
                {
                    Application.Current.Dispatcher.Invoke(() => MainWindowStore.Instance().DisplayError("An error occured while trying to build the solidity contract"));
                }
            });
        }