Beispiel #1
0
        public static TransferNotifyItem GetTransferNotify(this VmArray notifyArray, UInt160 asset)
        {
            if (notifyArray.Count < 4)
            {
                return(null);
            }
            // Event name should be encoded as a byte array.
            if (notifyArray[0].NotVmByteArray())
            {
                return(null);
            }

            var eventName = notifyArray[0].GetString();

            if (eventName != "Transfer")
            {
                return(null);
            }

            var fromItem = notifyArray[1];

            if (fromItem.NotVmByteArray() && fromItem.NotVmNull())
            {
                return(null);
            }

            var fromBytes = fromItem.GetByteSafely();

            if (fromBytes?.Length != 20)
            {
                fromBytes = null;
            }

            var toItem = notifyArray[2];

            if (toItem != null && toItem.NotVmByteArray())
            {
                return(null);
            }

            byte[] toBytes = toItem.GetByteSafely();
            if (toBytes?.Length != 20)
            {
                toBytes = null;
            }
            if (fromBytes == null && toBytes == null)
            {
                return(null);
            }

            var amountItem = notifyArray[3];

            if (amountItem.NotVmByteArray() && amountItem.NotVmInt())
            {
                return(null);
            }

            var transfer = new TransferNotifyItem()
            {
                Asset  = asset,
                From   = fromBytes == null ? null : new UInt160(fromBytes),
                To     = new UInt160(toBytes),
                Amount = amountItem.GetBigInteger(),
            };

            return(transfer);
        }
Beispiel #2
0
 /// <summary>
 /// convert to Big Endian hex string without "Ox"
 /// </summary>
 /// <param name="address"></param>
 /// <returns></returns>
 public static string ToBigEndianHex(this UInt160 address)
 {
     return(address.ToArray().ToHexString(reverse: true));
 }
Beispiel #3
0
        /// <summary>
        /// convert bigint to asset decimal value
        /// </summary>
        /// <param name="amount"></param>
        /// <param name="assetId"></param>
        /// <returns></returns>
        public static (BigDecimal amount, AssetInfo asset) GetAssetAmount(this BigInteger amount, UInt160 assetId)
        {
            var asset = AssetCache.GetAssetInfo(assetId);

            if (asset == null)
            {
                return(new BigDecimal(0, 0), null);
            }

            return(new BigDecimal(amount, asset.Decimals), asset);
        }
Beispiel #4
0
 /// <summary>
 /// query balance
 /// </summary>
 /// <param name="address"></param>
 /// <param name="assetId"></param>
 /// <returns></returns>
 public static BigDecimal GetBalanceOf(this UInt160 address, UInt160 assetId)
 {
     using var snapshot = Blockchain.Singleton.GetSnapshot();
     return(GetBalanceOf(address, assetId, snapshot));
 }
Beispiel #5
0
        /// <summary>
        /// query balance
        /// </summary>
        /// <param name="addresses"></param>
        /// <param name="assetId"></param>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        public static List <BigDecimal> GetBalanceOf(this IEnumerable <UInt160> addresses, UInt160 assetId, StoreView snapshot)
        {
            var assetInfo = AssetCache.GetAssetInfo(assetId, snapshot);

            if (assetInfo == null)
            {
                throw new ArgumentException($"invalid assetId:[{assetId}]");
            }

            using var sb = new ScriptBuilder();
            foreach (var address in addresses)
            {
                sb.EmitAppCall(assetId, "balanceOf", address);
            }

            using ApplicationEngine engine = ApplicationEngine.Run(sb.ToArray(), snapshot, testMode: true);
            if (engine.State.HasFlag(VMState.FAULT))
            {
                throw new Exception($"query balance error");
            }

            var result = engine.ResultStack.Select(p => p.GetBigInteger());

            return(result.Select(bigInt => new BigDecimal(bigInt, assetInfo.Decimals)).ToList());
        }
Beispiel #6
0
 /// <summary>
 /// query balance
 /// </summary>
 /// <param name="addresses"></param>
 /// <param name="assetId"></param>
 /// <returns></returns>
 public static List <BigDecimal> GetBalanceOf(this IEnumerable <UInt160> addresses, UInt160 assetId)
 {
     using var snapshot = Blockchain.Singleton.GetSnapshot();
     return(GetBalanceOf(addresses, assetId, snapshot));
 }