Exemple #1
0
        /// <summary>
        /// Store 0 generation gladiator price range
        /// </summary>
        public static bool SetGenerationZeroPrice(GenerationZeroPrice model)
        {
            //Make a minimum judgment to prevent the setting too small
            if (model.MinPrice < AdminOperations.GenerationZeroMinPrice)
            {
                model.MaxPrice = AdminOperations.GenerationZeroMaxPrice;
                model.MinPrice = AdminOperations.GenerationZeroMinPrice;
                model.Duration = AdminOperations.GenerationZeroAuctionDuration;
            }

            byte[] bytes = Helper.Serialize(model);
            Storage.Put(Storage.CurrentContext, Keys.GenerationZeroPricesKey, bytes);

            return(true);
        }
Exemple #2
0
        public static Good GetGoodInfo(string name)
        {
            if (name.Length <= 0)
            {
                throw new InvalidOperationException("The parameter name SHOULD be longer than 0.");
            }
            StorageMap goodToken = Storage.CurrentContext.CreateMap(nameof(goodToken));

            byte[] token = goodToken.Get(name);
            if (token.Length == 0)
            {
                return(null);
            }
            return(Helper.Deserialize(token) as Good);
        }
Exemple #3
0
 private static CoinPoolInfo getCoinPoolInfo()
 {
     byte[] data = Storage.Get(Storage.CurrentContext, "CoinPoolInfo");
     if (data.Length > 0)
     {
         return(Helper.Deserialize(data) as CoinPoolInfo);
     }
     else
     {
         var coinPoolInfo = new CoinPoolInfo();
         coinPoolInfo.fullblock = 0;
         coinPoolInfo.lastblock = 0;
         return(coinPoolInfo);
     }
 }
Exemple #4
0
        private static bool Init(string secret, BigInteger reservePrice, int durationBidding, int durationReveal, int durationResulting)
        {
            if (!Runtime.CheckWitness(Owner))
            {
                return(false);
            }

            //Auction
            Auction auction = new Auction(secret, Runtime.Time, durationBidding, durationReveal, durationResulting, reservePrice, Owner);

            //Storage.Put(Storage.CurrentContext, "auction", auction.Serialize());
            //Storage.Put(Storage.CurrentContext, "auction", Helper.Serialize(auction));
            Storage.Put("auction", Helper.Serialize(auction));
            return(true);
        }
Exemple #5
0
        public static TransferInfo GetTxInfo(byte[] txId)
        {
            if (txId.Length != 32)
            {
                throw new InvalidOperationException("The parameter txId SHOULD be 32-byte transaction hash.");
            }
            StorageMap txInfo = Storage.CurrentContext.CreateMap(nameof(txInfo));
            var        result = txInfo.Get(txId); //0.1

            if (result.Length == 0)
            {
                return(null);
            }
            return(Helper.Deserialize(result) as TransferInfo);
        }
Exemple #6
0
        private static void saveAuctionState(AuctionState state)
        {
            var fullhash = nameHashSub(state.parenthash, state.domain);

            byte[] _id = Storage.Get(Storage.CurrentContext, new byte[] { 0x01 }.Concat(fullhash));
            if (_id.AsBigInteger() != state.id.AsBigInteger())//没存过ID
            {
                Storage.Put(Storage.CurrentContext, new byte[] { 0x01 }.Concat(fullhash), state.id);
            }

            var key = new byte[] { 0x02 }.Concat(state.id);

            onChangeAuctionState(state);
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(state));
        }
Exemple #7
0
        /**
         * 当前分红池信息查询
         **/
        public static object[] getNowClaimPoolInfo()
        {
            BPECPoolInfo BPECPoolInfo = getBPECPoolInfo();
            var          key          = "claim_".AsByteArray().Concat(BPECPoolInfo.claimCode.AsByteArray());

            byte[] data = Storage.Get(Storage.CurrentContext, key);
            if (data.Length > 0)
            {
                return((object[])Helper.Deserialize(data));
            }
            else
            {
                return(new object[0]);
            }
        }
        public static bool getProposalVote(byte[] walletId, byte[] owner)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(owner), "owner address is not length of 20 bytes");
            _onlyWalletOwner(walletId, owner);
            // wpvBs means Wallet Proposal Votes ByteS
            byte[]             wpvBs = Storage.Get(Storage.CurrentContext, WalletsProposalVotesPrefix.Concat(walletId));
            Map <byte[], bool> wpv   = new Map <byte[], bool>();

            if (wpvBs.Length > 0)
            {
                wpv = Helper.Deserialize(wpvBs) as Map <byte[], bool>;
                return(wpv[owner]);
            }
            return(false);
        }
Exemple #9
0
        public static Map <BigInteger, BigInteger> BalanceOf(byte[] account)
        {
            //Map<BigInteger, BigInteger> m = new Map<BigInteger, BigInteger>();
            if (account.Length != 20)
            {
                throw new InvalidOperationException("The parameter account SHOULD be 20-byte addresses.");
            }
            StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));

            byte[] mp = asset.Get(account);
            if (mp != null && mp.Length != 0)
            {
                return(Helper.Deserialize(mp) as Map <BigInteger, BigInteger>);
            }
            return(null);
        }
Exemple #10
0
        /**
         * 存储拍卖成交记录
         */
        private static void _putAuctionRecord(byte[] tokenId, AuctionRecord info)
        {
            /*
             * // 用一个老式实现法
             * byte[] auctionInfo = _ByteLen(info.seller.Length).Concat(info.seller);
             * auctionInfo = _ByteLen(info.buyer.Length).Concat(info.buyer);
             * auctionInfo = auctionInfo.Concat(_ByteLen(info.sellPrice.AsByteArray().Length)).Concat(info.sellPrice.AsByteArray());
             * auctionInfo = auctionInfo.Concat(_ByteLen(info.sellTime.AsByteArray().Length)).Concat(info.sellTime.AsByteArray());
             */
            // 新式实现方法只要一行
            byte[] txInfo = Helper.Serialize(info);

            var key = "buy".AsByteArray().Concat(tokenId);

            Storage.Put(Storage.CurrentContext, key, txInfo);
        }
Exemple #11
0
        public static SaleInfo GetSale(byte[] txid)
        {
            if (txid.Length != 32)
            {
                throw new InvalidOperationException("The parameter txid MUST be 32-byte transaction hash.");
            }

            StorageMap saleInfo = Storage.CurrentContext.CreateMap(SalesMapName);
            var        result   = saleInfo.Get(txid);

            if (result.Length == 0)
            {
                return(null);
            }
            return(Helper.Deserialize(result) as SaleInfo);
        }
Exemple #12
0
        public static Boolean setAddr(string node, byte[] owner, byte[] addr)
        {
            Record record = getOwner(node);

            if (record != null && record.owner != owner)
            {
                Runtime.Notify(new Object[] { "set addr fail: not owner", node, owner, addr });
                return(false);
            }
            record.addr = addr;
            var recordInfo = Helper.Serialize(record);

            Storage.Put(Storage.CurrentContext, node, recordInfo);
            Runtime.Notify(new Object[] { "set addr", node, owner, addr, recordInfo });
            return(true);
        }
Exemple #13
0
        private static object Lock(byte[] from, byte[] to, BigInteger amount, byte[] userEthAddress)
        {
            // params length check
            if (from.Length != 20 || userEthAddress.Length != 20)
            {
                return("code:0, msg:The parameters error");
            }

            // amount check
            if (amount <= MinSwapAmount)
            {
                return("code:0, msg:The number of transfers cannot be less than 0");
            }

            // transfer qlc from user to contract
            object[] transferParams = new object[3];
            transferParams[0] = from;
            transferParams[1] = to;
            transferParams[2] = amount;
            if (QlcMain("transfer", transferParams))
            {
                // swap info
                var swapInfo = new SwapInfo()
                {
                    fromAddress    = from,
                    toAddress      = to,
                    amount         = amount,
                    userEthAddress = userEthAddress,
                    timestamp      = Blockchain.GetBlock(Blockchain.GetHeight()).Timestamp,
                    blockHeight    = Blockchain.GetHeight(),
                    type           = 1
                };

                var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
                swapInfo.txid = txid;
                StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
                SWAP_MAP.Put(txid, Helper.Serialize(swapInfo));

                // event
                LockEvent(txid, 1);
                return(txid);
            }
            else
            {
                return("code:0, msg:transfer error.");
            }
        }
Exemple #14
0
        public static object Main(string operation, object[] args)
        {
            StorageMap addrNFTlistMap = Storage.CurrentContext.CreateMap("addrNFTlist");

            if (operation == "getMap") //["(str)getMap",[]]
            {
                return(getMap());

                //Map<BigInteger, BigInteger> addrNFTlist = new Map<BigInteger, BigInteger>();
                //addrNFTlist[0] = 3;//
                //addrNFTlist[1] = 1;//
                //addrNFTlist[2] = 1;//
                //addrNFTlist[3] = 1;//
                //return addrNFTlist;
            }
            if (operation == "addMap") //["(str)addMap",["(int)1"]]
            {
                Map <BigInteger, BigInteger> addrNFTlist = getMap();
                addrNFTlist[(BigInteger)args[0]] = 1;
                if (addrNFTlist.HasKey(0))
                {
                    addrNFTlist[0] = addrNFTlist[0] + 1;
                }
                else
                {
                    addrNFTlist[0] = 1;
                }

                addrNFTlistMap.Put("1".AsByteArray(), Helper.Serialize(addrNFTlist));

                return(true);
            }
            if (operation == "removeMap") //["(str)removeMap",["(int)2"]]
            {
                Map <BigInteger, BigInteger> addrNFTlist = getMap();

                if (addrNFTlist.HasKey((BigInteger)args[0]))
                {
                    addrNFTlist.Remove((BigInteger)args[0]);
                    addrNFTlist[0] = addrNFTlist[0] - 1;
                    addrNFTlistMap.Put("1".AsByteArray(), Helper.Serialize(addrNFTlist));
                    return(true);
                }
            }

            return(false);
        }
Exemple #15
0
        public static Game GetGame(byte[] gameId)
        {
            var key = KeysFactory.GameId(gameId);
            var raw = Storage.Get(Storage.CurrentContext, key);

            if (raw.Length == 0)
            {
                return(new Game {
                    Id = gameId, State = GameState.WaitingForPlayers
                });
            }
            else
            {
                var deserialized = (object[])Helper.Deserialize(raw);
                return((Game)(object)deserialized);
            }
        }
Exemple #16
0
        /**
         * 奖池信息查询
         **/
        public static CoinPoolInfo getCoinPoolInfo(BigInteger coinId)
        {
            byte[]       key  = "coin_".AsByteArray().Concat(coinId.AsByteArray());
            byte[]       data = Storage.Get(Storage.CurrentContext, key);
            CoinPoolInfo info = null;

            if (data.Length > 0)
            {
                info = Helper.Deserialize(data) as CoinPoolInfo;
            }
            else
            {
                info = new CoinPoolInfo();
            }
            info.nowBlock = Blockchain.GetHeight();
            return(info);
        }
Exemple #17
0
        /**
         * 用户股权信息查询
         **/
        public static UserInfo getUserInfo(byte[] who)
        {
            var keyWho = new byte[] { 0x11 }.Concat(who);

            byte[]   data = Storage.Get(Storage.CurrentContext, keyWho);
            UserInfo info = null;

            if (data.Length > 0)
            {
                info = Helper.Deserialize(data) as UserInfo;
            }
            else
            {
                info = new UserInfo();
            }
            return(info);
        }
Exemple #18
0
        // 获取某账户名下所有售卖挂单
        private static Map <BigInteger, SaleMulInfo> GetMulSale(byte[] account)
        {
            StorageMap saleMul = Storage.CurrentContext.CreateMap(nameof(saleMul));

            byte[] smFrom = saleMul.Get(account);
            Map <BigInteger, SaleMulInfo> items;

            if (smFrom == null || smFrom.Length == 0)
            {
                items = new Map <BigInteger, SaleMulInfo>();
            }
            else
            {
                items = Helper.Deserialize(smFrom) as Map <BigInteger, SaleMulInfo>;
            }
            return(items);
        }
Exemple #19
0
        private static Object setResolver(string node, byte[] owner, byte[] resolver)
        {
            Record nodeOwner = getOwner(node);

            if (nodeOwner != null && nodeOwner.owner != owner)
            {
                Runtime.Notify(new Object[] { "set resolver", "fail: not owner", node, owner, resolver });
                return(false);
            }

            nodeOwner.resolver = resolver;
            var ownerInfo = Helper.Serialize(nodeOwner);

            Storage.Put(Storage.CurrentContext, node, ownerInfo);
            Runtime.Notify(new Object[] { "set resolver", node, owner, resolver });
            return(true);
        }
Exemple #20
0
        private static bool createSAR4B(byte[] addr, SARInfo sar)
        {
            byte[] key = getSARKey(addr);

            byte[] sarCurr = Storage.Get(Storage.CurrentContext, key);
            if (sarCurr.Length > 0)
            {
                return(false);
            }
            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(sar));

            //notify
            Operated(sar.name.AsByteArray(), addr, txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_OPEN, 0);
            return(true);
        }
Exemple #21
0
        public static object DeserializeMap(byte[] param)
        {
            Map <string, int> b2      = (Map <string, int>)Helper.Deserialize(param);
            StorageContext    context = Storage.CurrentContext;

            int value = 100;

            //&& b2["hello"] == "world"
            if (b2 != null && (int)b2["key"] == value)
            {
                Storage.Put(context, "result", "true");
                return(true);
            }

            Storage.Put(context, "result", "false");
            return((int)b2["key"]);
        }
Exemple #22
0
        public static object DeserializeStruct(byte[] param)
        {
            ClaimTx        b2      = (ClaimTx)Helper.Deserialize(param);
            StorageContext context = Storage.CurrentContext;

            int value = 100;

            //&& b2["hello"] == "world"
            if (b2 != null && (int)b2.claimId == value)
            {
                Storage.Put(context, "result", "true");
                return(true);
            }

            Storage.Put(context, "result", "false");
            return((int)b2.claimId);
        }
Exemple #23
0
        private static Object setTTL(string node, byte[] owner, BigInteger ttl)
        {
            Record nodeOwner = getOwner(node);

            if (nodeOwner != null && nodeOwner.owner != owner)
            {
                Runtime.Notify(new Object[] { "set ttl", "fail: not owner", node, owner, ttl });
                return(false);
            }

            nodeOwner.ttl = ttl;
            var ownerInfo = Helper.Serialize(nodeOwner);

            Storage.Put(Storage.CurrentContext, node, ownerInfo);
            Runtime.Notify(new Object[] { "set ttl", node, owner, ttl });
            return(true);
        }
        public static BigInteger getBalance(byte[] walletId, byte[] tokenAddress)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            Map <byte[], BigInteger> wBalanceMap = new Map <byte[], BigInteger>();

            byte[] wBalanceBs = Storage.Get(Storage.CurrentContext, WalletsBalancesPrefix.Concat(walletId));
            if (wBalanceBs.Length > 0)
            {
                wBalanceMap = Helper.Deserialize(wBalanceBs) as Map <byte[], BigInteger>;
            }
            if (wBalanceMap.HasKey(tokenAddress))
            {
                return(wBalanceMap[tokenAddress]);
            }
            return(0);
        }
Exemple #25
0
        public static bool Init(string name, string symbol, BigInteger totalSupply, byte[] addr, string desc)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (name.Length <= 0)
            {
                throw new InvalidOperationException("The parameter name SHOULD be longer than 0.");
            }

            if (totalSupply <= 0)
            {
                throw new InvalidOperationException("The parameter totalSupply SHOULD be longer than 0.");
            }

            StorageMap goodToken = Storage.CurrentContext.CreateMap(nameof(goodToken));

            byte[] token = goodToken.Get(name);
            if (token.Length != 0)
            {
                throw new InvalidOperationException("The good of name SHOULD be null.");
            }

            if (!transfer(name, null, addr, totalSupply))
            {
                throw new InvalidOperationException("Operation is error.");
            }

            Good t = new Good();

            t.owner       = addr;
            t.decimals    = 0;
            t.name        = name;
            t.symbol      = symbol;
            t.totalSupply = totalSupply;
            t.desc        = desc;

            goodToken.Put(name, Helper.Serialize(t));

            //创建物品资产
            Inited(addr, name.AsByteArray(), symbol.AsByteArray(), desc.AsByteArray(), totalSupply);
            return(true);
        }
Exemple #26
0
        internal static object Get(byte[] scriptHash, byte[] entityName, int id)
        {
            if (!Runtime.CheckWitness(scriptHash))
            {
                return(false);
            }

            string entityKey = Key.Generate(entityName, scriptHash, id);

            byte[] entityValue = DB.Get(entityKey);
            if (entityValue != null)
            {
                Runtime.Notify(entityName, id, entityValue.AsString());
                return(entityValue);
            }
            Runtime.Notify(entityName, id, "do not exist");
            return(NSFH.AsByteArray(0));
        }
        public static object removePauser(byte[] invoker, byte[] account)
        {
            BasicMethods.assert(Runtime.CheckWitness(invoker), "Checkwitness failed");

            // make sure the invoker is one effective pauser
            byte[] pauserBs = Storage.Get(Storage.CurrentContext, PauserKey);
            BasicMethods.assert(pauserBs.Length > 0, "pauser map is empty");
            Map <byte[], bool> pausers = Helper.Deserialize(pauserBs) as Map <byte[], bool>;

            BasicMethods.assert(pausers.HasKey(invoker), "invoker is not one pauser");
            BasicMethods.assert(pausers[invoker], "invoker is an effective pauser");

            // update the pausers map
            pausers[account] = false;
            Storage.Put(Storage.CurrentContext, PauserKey, Helper.Serialize(pausers));
            PauserRemoveded(account);
            return(true);
        }
Exemple #28
0
        /**
         * 用户股权信息查询
         **/
        public static Info getInfo(byte[] who)
        {
            var keyWho = new byte[] { 0x11 }.Concat(who);

            byte[] data = Storage.Get(Storage.CurrentContext, keyWho);
            Info   info = new Info();

            if (data.Length > 0)
            {
                info = Helper.Deserialize(data) as Info;
            }
            else
            {
                info.balance = 0;
                info.block   = 0;
            }
            return(info);
        }
        public static void addrNFTlistAdd(byte[] addr, BigInteger tokenID)
        {
            StorageMap addrNFTlistMap = Storage.CurrentContext.CreateMap("addrNFTlist");//0,存储addr拥有NFT总数

            Map <BigInteger, BigInteger> addrNFTlist = getAddrNFTlist(addr);

            if (addrNFTlist.HasKey(0))
            {
                addrNFTlist[0] = addrNFTlist[0] + 1;
            }
            else
            {
                addrNFTlist[0] = 1;
            }
            addrNFTlist[tokenID] = 1;

            addrNFTlistMap.Put(addr, Helper.Serialize(addrNFTlist));
        }
Exemple #30
0
    public static bool GenerateAlien(string alienName, byte[] owner)
    {
        // Check if the `owner` argument is the same as one who invoked contract
        if (!Runtime.CheckWitness(owner))
        {
            return(false);
        }

        uint  blockHeight = Blockchain.GetHeight();
        uint  xna         = FindXna(RandomNumber(blockHeight));
        Alien someAlien   = new Alien(xna, alienName, blockHeight);
        // add the object to storage
        StorageMap alienMap = Storage.CurrentContext.CreateMap(AlienMapName);

        alienMap.Put(someAlien.Id.ToByteArray(), Helper.Serialize(someAlien));
        Runtime.Notify("Alien created, ID: " + (someAlien.Id));
        return(true);
    }