Ejemplo n.º 1
0
        /// <summary>
        /// Get the unspent coin references for a specific address.
        /// For now it only works using the NeoScan API.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="restService"></param>
        /// <returns></returns>
        public static async Task <IEnumerable <Coin> > GetUnspent(string address,
                                                                  INeoscanService restService)
        {
            if (restService == null)
            {
                throw new NullReferenceException("REST client not configured");
            }
            var addressBalance = await restService.GetBalanceAsync(address);

            var coinList = new List <Coin>();

            if (addressBalance.Balance != null)
            {
                coinList.AddRange(from balanceEntry in addressBalance.Balance
                                  let child = balanceEntry?.Unspent
                                              where child?.Count > 0
                                              from unspent in balanceEntry?.Unspent
                                              select new Coin
                {
                    Output = new TransactionOutput
                    {
                        AssetId    = UInt256.Parse(balanceEntry?.AssetHash),
                        ScriptHash = address?.ToScriptHash(),
                        Value      = Fixed8.FromDecimal((decimal)unspent.Value),
                    },
                    Reference = new CoinReference
                    {
                        PrevHash  = UInt256.Parse(unspent.TxId),
                        PrevIndex = (ushort)unspent.N,
                    }
                });
            }
            return(coinList);
        }
Ejemplo n.º 2
0
 public AccountSignerTransactionManager(IClient rpcClient, INeoscanService restService, IAccount account)
 {
     Account      = account ?? throw new ArgumentNullException(nameof(account));
     Client       = rpcClient;
     _restService = restService;
     if (account.PrivateKey != null)
     {
         _accountKey = new KeyPair(account.PrivateKey); //if account is watch only, it does not have private key
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Online Wallet Manager construtor
        /// </summary>
        /// <param name="wallet"></param>
        /// <param name="restService"></param>
        /// <param name="client"></param>
        public WalletManager(INeoscanService restService, IClient client, Wallet wallet = null)
        {
            _restService = restService;
            _client      = client;

            _wallet = wallet ?? new Wallet();
            if (_wallet.Accounts.Any())
            {
                foreach (var walletAccount in _wallet.Accounts)
                {
                    walletAccount.TransactionManager =
                        new AccountSignerTransactionManager(_client, _restService, walletAccount);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets nep5 tokens balance from Neoscan.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="restService"></param>
        /// <returns></returns>
        //TODO change this to become neoscan independent
        public static async Task <List <Balance> > GetNep5Balances(string address,
                                                                   INeoscanService restService)
        {
            if (restService == null)
            {
                throw new NullReferenceException("REST client not configured");
            }
            var addressBalance = await restService.GetBalanceAsync(address);

            var nep5Balances = new List <Balance>();

            if (addressBalance.Balance != null)
            {
                foreach (var balanceEntry in addressBalance.Balance)
                {
                    if (balanceEntry.Amount > 0)
                    {
                        nep5Balances.Add(balanceEntry);
                    }
                }
            }

            return(nep5Balances);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get claimable GAS from Neoscan.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="restService"></param>
        /// <returns></returns>
        public static async Task <(List <ClaimableElement>, decimal amount)> GetClaimable(string address, INeoscanService restService)
        {
            var claimable = await restService.GetClaimableAsync(address);

            var amount = claimable.Unclaimed;

            return(claimable.ClaimableList.ToList(), (decimal)amount);
        }
Ejemplo n.º 6
0
        public static async Task <Coin[]> FindUnspentCoins(UInt256 assetId, Fixed8 amount, UInt160 from, INeoscanService restService)
        {
            var address  = from.ToAddress(); //todo
            var unspents = await GetUnspent(address, restService);

            Coin[] unspentsAsset = unspents.Where(p => p.Output.AssetId == assetId).ToArray();
            Fixed8 sum           = unspentsAsset.Sum(p => p.Output.Value);

            if (sum < amount)
            {
                return(null);
            }
            if (sum == amount)
            {
                return(unspentsAsset);
            }
            Coin[] unspentsOrdered = unspentsAsset.OrderByDescending(p => p.Output.Value).ToArray();
            int    i = 0;

            while (unspentsOrdered[i].Output.Value <= amount)
            {
                amount -= unspentsOrdered[i++].Output.Value;
            }
            if (amount == Fixed8.Zero)
            {
                return(unspentsOrdered.Take(i).ToArray());
            }
            else
            {
                return(unspentsOrdered.Take(i).Concat(new[] { unspentsOrdered.Last(p => p.Output.Value >= amount) }).ToArray());
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 ///     Offline Wallet Manager construtor
 /// </summary>
 /// <param name="wallet"></param>
 public WalletManager(Wallet wallet)
 {
     _wallet      = wallet;
     _client      = null;
     _restService = null;
 }
Ejemplo n.º 8
0
 public void ChangeApiEndPoints(IClient client, string restUrl)
 {
     _client      = client;
     _restService = new NeoScanRestService(restUrl);
     ChangeAccountsClient();
 }
Ejemplo n.º 9
0
 //TODO: this client and rest stuff must be refractored
 public void ChangeApiEndPoints(IClient client, INeoscanService restService)
 {
     _client      = client;
     _restService = restService;
     ChangeAccountsClient();
 }