private void UpdateBalance(UInt160 account, UInt160 asset, DataCache snapshot) { try { var balance = account.GetBalanceOf(asset, snapshot).Value; _db.UpdateBalance(account, asset, balance, snapshot.GetHeight()); } catch { var backupBalance = _levelDb.GetBalance(account, asset); if (backupBalance != null) { _db.UpdateBalance(account, asset, backupBalance.Balance, backupBalance.Height); } } }
/// <summary> /// send asset /// </summary> /// <param name="sender"></param> /// <param name="receiver"></param> /// <param name="amount"></param> /// <param name="asset"></param> /// <returns></returns> public async Task <object> SendToAddress(UInt160 receiver, string amount, string asset = "neo", UInt160 sender = null) { if (CurrentWallet == null) { return(Error(ErrorCode.WalletNotOpen)); } if (receiver == null) { return(Error(ErrorCode.ParameterIsNull, $"receiver address is null!")); } UInt160 assetHash = ConvertToAssetId(asset, out var convertError); if (assetHash == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } var assetInfo = AssetCache.GetAssetInfo(assetHash); if (assetInfo == null) { return(Error(ErrorCode.InvalidPara, $"asset is not valid:{convertError}")); } if (!BigDecimal.TryParse(amount, assetInfo.Decimals, out BigDecimal sendAmount) || sendAmount.Sign <= 0) { return(Error(ErrorCode.InvalidPara, "Incorrect Amount Format")); } if (sender != null) { var account = CurrentWallet.GetAccount(sender); if (account == null) { return(Error(ErrorCode.AddressNotFound)); } var balance = sender.GetBalanceOf(assetHash); if (balance.Value < sendAmount.Value) { return(Error(ErrorCode.BalanceNotEnough)); } } try { Transaction tx = CurrentWallet.MakeTransaction(Helpers.GetDefaultSnapshot(), new[] { new TransferOutput { AssetId = assetHash, Value = sendAmount, ScriptHash = receiver } }, sender); if (tx == null) { return(Error(ErrorCode.BalanceNotEnough, "Insufficient funds")); } var(signSuccess, context) = CurrentWallet.TrySignTx(tx); if (!signSuccess) { return(Error(ErrorCode.SignFail, context.SafeSerialize())); } await tx.Broadcast(); return(new TransactionModel(tx)); } catch (Exception ex) { if (ex.Message.Contains("Insufficient GAS")) { return(Error(ErrorCode.GasNotEnough)); } return(Error(ErrorCode.TransferError, ex.Message)); } }