Example #1
0
        public async Task <Response <int> > GetCurrentBlockNumber(CryptoAdapterModel cryptoAdapter)
        {
            var response = new Response <int>();

            try
            {
                var client = InstantiateRpcClient(cryptoAdapter);

                var neoApiService = new NeoApiService(client);

                var blockNumber = await neoApiService.Blocks.GetBlockCount.SendRequestAsync();

                response.Status = StatusEnum.Success;
                response.Value  = blockNumber;
            }
            catch (Exception ex)
            {
                response.Status  = StatusEnum.Error;
                response.Message = ex.Message;
                _logger.Information($"NEOAdapter.GetCurrentBlockNumber(cryptoAdapter: {cryptoAdapter.Id}");
                _logger.Error(ex.Message);
            }

            return(response);
        }
Example #2
0
        public BaseViewModel()
        {
            var rpc = new RpcClient(new Uri("http://seed1.redpulse.com:10332"));

            NeoScanService = new NeoScanRestService(NeoScanNet.MainNet);
            NeoService     = new NeoApiService(rpc);
        }
Example #3
0
        static void Main(string[] args)
        {
            var    rpcClient      = new RpcClient(new Uri("http://neo.org:10332"));
            var    NeoApiServices = new NeoApiService(rpcClient);
            var    walletManager  = new WalletManager(new NeoScanRestService(NeoScanNet.MainNet), rpcClient);
            string accresult      = "";

            try
            {
                var acc = walletManager.GetDefaultAccount();
                accresult = JsonConvert.SerializeObject(acc, Formatting.Indented);
                Console.WriteLine(accresult);
                Process.Start("https://neotracker.io/address/" + acc.Address);
            }
            catch
            {
                var cacc = walletManager.CreateAccount("metinyakarNet", "wordpress").Result;
                cacc.IsDefault = true;
                walletManager.AddAccount(cacc);
                //walletManager.DeleteAccount(cacc.Address.ToAddress());
                var acc = walletManager.GetAccount(cacc.Address);
                accresult = JsonConvert.SerializeObject(acc, Formatting.Indented);
                File.WriteAllText("neoAccResult.txt", accresult);
                Console.WriteLine(accresult);
                string trackerurl = "https://neotracker.io/address/" + acc.Address.ToAddress();
                Process.Start(trackerurl);
            }

            Console.Read();
        }
Example #4
0
    async Task <Invoke> InvokeContract()
    {
        var neoRpcClient  = new RpcClient(new Uri(URI));
        var neoRpcService = new NeoApiService(neoRpcClient);

        // Invoke script
        var contractScriptHash = UInt160.Parse(contractHash).ToArray();
        var script             = Utils.GenerateScript(contractScriptHash, "getAddressFromMailbox",
                                                      new object[] { "teste" });

        return(await neoRpcService.Contracts.InvokeScript.SendRequestAsync(script.ToHexString()));
    }
        // Block api demonstration
        private static async Task BlockApiTest(NeoApiService service)
        {
            Block genesisBlock = await service.Blocks.GetBlock.SendRequestAsync(0);             // Get genesis block by index (can pass a string with block hash as parameter too)

            string bestBlockHash = await service.Blocks.GetBestBlockHash.SendRequestAsync();

            int blockCount = await service.Blocks.GetBlockCount.SendRequestAsync();

            string blockHash = await service.Blocks.GetBlockHash.SendRequestAsync(0);

            string serializedBlock = await service.Blocks.GetBlockSerialized.SendRequestAsync(0);             // (can pass a string with block hash as parameter too)

            string blockFee = await service.Blocks.GetBlockSysFee.SendRequestAsync(0);
        }
Example #6
0
        static void Main(string[] args)
        {
            const string contractHash = "0x417d6706e5899fbc6ea533b17c91752a040d902a";

            var rpcClient     = new RpcClient(new Uri("http://localhost:30333"));
            var neoApiService = new NeoApiService(rpcClient);

            var script = NeoModules.NEP6.Utils.GenerateScript(UInt160.Parse(contractHash).ToArray(), "CheckDeployContract", new object[] { });

            var resultTask = neoApiService.Contracts.InvokeScript.SendRequestAsync(script.ToHexString());

            resultTask.ContinueWith(x =>
            {
                var content = x.Result.Stack[0].Value.ToString();
                Console.WriteLine($"bytes: {content.HexToBytes()}");
                Console.WriteLine($"value: {Encoding.UTF8.GetString(content.HexToBytes())}");
            });

            Console.ReadLine();
        }
Example #7
0
        public async Task <Response <NoValue> > TestConnectionSource(CryptoAdapterModel cryptoAdapter)
        {
            var response = new Response <NoValue>();

            try
            {
                var client = InstantiateRpcClient(cryptoAdapter);

                var neoApiService = new NeoApiService(client);

                await neoApiService.Blocks.GetBlockCount.SendRequestAsync();

                response.Status = StatusEnum.Success;
            }
            catch (Exception ex)
            {
                response.Message = Message.TestConnectionFailed;
                response.Status  = StatusEnum.Error;
                _logger.Information($"NEOAdapter.TestConnectionSource(CryptoAdapterId: {cryptoAdapter.Id})");
                _logger.Error(ex.Message);
            }

            return(response);
        }
Example #8
0
 public PhantasmaService()
 {
     ApiService = new NeoApiService(AppSettings.RpcClient);
 }
Example #9
0
        public async Task <Response <List <NeoBlockModel> > > GetBlocksWithTransactions(CryptoAdapterModel cryptoAdapter, int fromBlock, int toBlock, string address)
        {
            var response = new Response <List <NeoBlockModel> >()
            {
                Value = new List <NeoBlockModel>()
            };

            try
            {
                var client = InstantiateRpcClient(cryptoAdapter);

                var neoApiService = new NeoApiService(client);

                if (toBlock < fromBlock)
                {
                    _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}");
                    _logger.Error($"FromBlock value {fromBlock} is greater then ToBlock value {toBlock}");
                    response.Status = StatusEnum.Error;
                    return(response);
                }

                var stopwatch = Stopwatch.StartNew();

                for (int i = fromBlock; i <= toBlock; i++)
                {
                    var block = await neoApiService.Blocks.GetBlock.SendRequestAsync(i);

                    var blockModel = new NeoBlockModel()
                    {
                        BlockNumber = i,
                        Time        = DateTimeOffset.FromUnixTimeSeconds(block.Time).LocalDateTime
                    };

                    foreach (var transaction in block.Transactions)
                    {
                        var blockTransactionModel = new NeoBlockTransactionModel()
                        {
                            TransactionId   = transaction.Txid,
                            TransactionType = transaction.Type
                        };

                        foreach (var transactionInput in transaction.Vin)
                        {
                            var transactionInModel = new NeoTransactionInputModel()
                            {
                                TransactionId = transactionInput.TransactionId
                            };

                            blockTransactionModel.TransactionInputs.Add(transactionInModel);
                        }

                        foreach (var transactionOutput in transaction.Vout)
                        {
                            var transactionOutModel = new NeoTransactionOutputModel()
                            {
                                Address = transactionOutput.Address,
                                Asset   = transactionOutput.Asset,
                                Value   = Decimal.Parse(transactionOutput.Value, CultureInfo.InvariantCulture)
                            };

                            if (address == String.Empty || (String.Equals(address, transactionOutModel.Address)))
                            {
                                blockTransactionModel.TransactionOutputs.Add(transactionOutModel);
                            }
                        }

                        if (address == String.Empty || blockTransactionModel.TransactionOutputs.Exists(to => String.Equals(to.Address, address)))
                        {
                            blockModel.TransactionList.Add(blockTransactionModel);
                        }
                    }

                    response.Value.Add(blockModel);
                }

                stopwatch.Stop();
                _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}). Time elapsed: {stopwatch.Elapsed.TotalSeconds} seconds.");

                response.Status = StatusEnum.Success;
            }
            catch (Exception ex)
            {
                response.Status  = StatusEnum.Error;
                response.Message = ex.Message;
                _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}");
                _logger.Error(ex.Message);
            }

            return(response);
        }
Example #10
0
        private static async Task WalletAndTransactionsTest()
        {
            // Create online wallet and import account
            var walletManager   = new WalletManager(new NeoScanRestService(NeoScanNet.MainNet), RpcClient);
            var importedAccount = walletManager.ImportAccount("WIF HERE (or use NEP2 bellow)", "Account_label");
            //var importedAccount = walletManager.ImportAccount("encryptedPrivateKey", "password", "Account_label");

            var neoService = new NeoApiService(RpcClient);

            //invoke test example using Phantasma smart contract
            var method             = "getMailboxFromAddress";                    //operation
            var contractScriptHash = "ed07cffad18f1308db51920d99a2af60ac66a7b3"; // smart contract scripthash
            var script             = NEP6.Helpers.Utils.GenerateScript(contractScriptHash, method,
                                                                       new object[] { "ARcZoZPn1ReBo4LPLvkEteyLu6S2A5cvY2".ToScriptHash().ToArray() });
            var result = await neoService.Contracts.InvokeScript.SendRequestAsync(script.ToHexString());

            var content = Encoding.UTF8.GetString(result.Stack[0].Value.ToString().HexToBytes()); //the way you read a result can vary

            //call contract example using Phantasma smart contract
            var RegisterInboxOperation = "registerMailbox"; //operation
            var user = importedAccount.Address.ToArray();
            var args = new object[] { user, "neomodules" };

            var transaction = await importedAccount.TransactionManager.CallContract("ed07cffad18f1308db51920d99a2af60ac66a7b3",
                                                                                    RegisterInboxOperation, //operation
                                                                                    args,                   // arguments
                                                                                    null,                   // array of transfer outputs you want to attach gas or neo with the contract call (optional)
                                                                                    0m,                     // network fee (optional)
                                                                                    null                    // attributes (optional)
                                                                                    );


            // list of transfer outputs
            var transferOutputWithNep5AndGas = new List <TransferOutput>
            {
                new TransferOutput
                {
                    AssetId    = UInt160.Parse("a58b56b30425d3d1f8902034996fcac4168ef71d"), //  e.g. Script Hash of ASA
                    Value      = BigDecimal.Parse("0.0001", byte.Parse("8")),               // majority of NEP5 tokens have 8 decimals
                    ScriptHash = "AddressScriptHash to sent here".ToScriptHash(),
                },
                new TransferOutput
                {
                    AssetId    = NEP6.Helpers.Utils.GasToken,                  //GAS
                    Value      = BigDecimal.Parse("0.00001", byte.Parse("8")), // GAS has 8 decimals too
                    ScriptHash = "AddressScriptHash to sent here".ToScriptHash(),
                }
            };

            var transferOutputWithOnlyGas = new List <TransferOutput>
            {
                new TransferOutput
                {
                    AssetId    = NEP6.Helpers.Utils.GasToken,                  //GAS
                    Value      = BigDecimal.Parse("0.00001", byte.Parse("8")), // GAS has 8 decimals too
                    ScriptHash = "AddressScriptHash to sent here".ToScriptHash(),
                }
            };

            // Claims unclaimed gas. Does not spent your neo to make gas claimable, you have to do it yourself!
            var claim = await importedAccount.TransactionManager.ClaimGas();

            // Transfer NEP5 and gas with fee
            var invocationTx = await importedAccount.TransactionManager.TransferNep5(null, transferOutputWithNep5AndGas, null, 0.00001m);

            // Send native assets (NEO and GAS) with fee
            var nativeTx = await importedAccount.TransactionManager.SendNativeAsset(null, transferOutputWithOnlyGas, null, 0.0001m);

            // Call contract
            var scriptHash = "a58b56b30425d3d1f8902034996fcac4168ef71d".ToScriptHash().ToArray(); // ASA e.g
            var operation  = "your operation here";
            var arguments  = new object[] { "arg1", "arg2", "etc" };

            // Estimate Gas consumed from contract call
            var estimateContractGasCall =
                await importedAccount.TransactionManager.EstimateGasContractInvocation(scriptHash, operation, arguments);

            // Confirm a transaction
            var confirmedTransaction =
                await importedAccount.TransactionManager.WaitForTxConfirmation(invocationTx.Hash.ToString(), 10000, 10);
        }