Beispiel #1
0
        internal static object List(object[] args)
        {
            byte[] scriptHash = (byte[])args[0];
            byte[] entityName = (byte[])args[1];

            int itemPerView = 10;

            if (args.Length == 3)
            {
                itemPerView = (int)args[2];
            }

            int startId = 1;

            if (args.Length == 3)
            {
                startId = (int)args[3];
            }

            if (!Runtime.CheckWitness(scriptHash))
            {
                return(false);
            }

            BigInteger entityMaxId, endId;
            string     entityMaxIdKey;

            entityMaxIdKey = Key.Generate(Prefix.EntityMaxId, entityName, scriptHash);
            byte[] entityMaxIdBytes = DB.Get(entityMaxIdKey);
            if (entityMaxIdBytes == null)
            {
                Runtime.Notify("total " + entityName + " available", 0);
                return(NSFH.AsByteArray(0));
            }
            entityMaxId = NSFH.AsBigInteger(entityMaxIdBytes);
            if (itemPerView <= 0)
            {
                itemPerView = 10;
            }
            if (startId > entityMaxId)
            {
                startId = 1;
            }
            endId = startId + itemPerView - 1;

            for (int i = startId; i <= endId; i++)
            {
                string entityKey   = Key.Generate(entityName, scriptHash, i);
                byte[] entityValue = DB.Get(entityKey);
                if (entityValue != null)
                {
                    Runtime.Notify(entityName, i, entityValue.AsString());
                }
            }
            //TODO Changed to return Array of values
            return(NSFH.AsByteArray(1));
        }
        public static byte[][] convertStringMapToArray(CMap <int, string> sMap)
        {
            byte[][] arr = new byte[sMap.size][];
            for (int i = 0; i < sMap.size; ++i)
            {
                arr[i] = Helper.AsByteArray(sMap.map[i]);
            }

            return(arr);
        }
Beispiel #3
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));
        }
Beispiel #4
0
        //首先在nep5基础上将账户分为两个部分
        //一个部分是saving
        //saving 有一个产生块的标记
        //一个部分是cash
        //当你收到一笔转账,他算在cash里面
        //当你花费一笔钱,先扣cash,扣完再扣saving

        //余额是saving 和 cash 的总和

        //cash 如何转换为为saving
        //通过claim   指令
        //claim指令 将领奖并且把所有的余额转换为saving,产生块标记为当前块

        //*claim指令
        //claim指令取得已经公布的奖励,仅有自己的saving的block <奖励的startblock 才可以领奖
        //领奖后全部资产+领到的资产变成saving状态

        //消耗池
        //所有花费掉的资产进入消耗池,等候公布奖励
        //公布奖励
        //将现有奖励从消耗池中取出,变成一个奖励,奖励规定只有savingblock <startblock 才可以领取。
        //根据 消耗池数量/总发行数量 确定一个领奖比例。
        //将同时保持五个公布奖励。
        //当公布第六个奖励时,第一个奖励被删除,并将他的余额丢入消耗池

        //*检查奖励指令
        //程序约定好公布奖励的block间隔,当有人检查奖励并且奖励的block间隔已经大于等于程序设置,则公布奖励。
        //用户可以用检查奖励指令获知最近的五个奖励,以此可以推算,如果领奖自己可以获取多少收益。

        //增加两条指令
        //checkpool*检查奖励
        //claim*领取奖励

        //可循环分配资产
        //最终确定加四个接口(暂定名Nep5.1)
        //检查奖励,只读(everyone)
        public static object[] checkBonus()
        {
            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            if (data.Length == 0)
            {
                return(null);
            }



            BigInteger lastBounsBlock = Helper.AsBigInteger(data);

            object[] retarray = new object[bonusCount];

            for (var i = 0; i < bonusCount; i++)
            {
                byte[]   bIndex      = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(lastBounsBlock));
                byte[]   bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
                byte[]   bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
                byte[]   bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
                byte[]   bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
                byte[]   StartBlock  = Storage.Get(Storage.CurrentContext, bStartBlock);
                byte[]   BonusValue  = Storage.Get(Storage.CurrentContext, bBonusValue);
                byte[]   BonusCount  = Storage.Get(Storage.CurrentContext, bBonusCount);
                byte[]   LastIndex   = Storage.Get(Storage.CurrentContext, bLastIndex);
                object[] bonusItem   = new object[4];
                bonusItem[0] = StartBlock;
                bonusItem[1] = BonusCount;
                bonusItem[2] = BonusValue;
                bonusItem[3] = LastIndex;
                retarray[i]  = bonusItem;
                if (LastIndex.Length == 0)
                {
                    break;
                }
                lastBounsBlock = Helper.AsBigInteger(LastIndex);
                if (lastBounsBlock == 0)
                {
                    break;
                }
            }
            return(retarray);
        }
Beispiel #5
0
        private static bool DoSchedulePayment(byte[] from, byte[] to, BigInteger value, byte[] txid, StorageMap agreement, bool isToHandleMaxFund)
        //long scheduleTime, BigInteger scheduleTypeFromStorage)
        {
            //Evaluate time
            long currentTime  = UAV.GetCurrentTime();
            uint scheduleType = (uint)agreement.Get("schedule").AsBigInteger();
            long scheduleTime = (long)agreement.Get("nextTime").AsBigInteger();


            if (currentTime < scheduleTime)
            {
                Error("Pull schedule payment: not in time yet");
                return(false);
            }
            uint[] paymentInfo = new uint[3];
            //get payment possibilities
            // 0 - nexTime
            // 1 - count
            // 2 - iswithdraw
            if (scheduleType < 3)
            {
                paymentInfo = ProcessDWPayment(scheduleType, currentTime, scheduleTime);
            }
            else
            {
                paymentInfo = ProcessMPayment(currentTime, scheduleTime);
            }

            //update
            uint duration = (uint)agreement.Get("duration").AsBigInteger();

            duration = duration - paymentInfo[1];

            if (duration < 0)
            {
                Error("Contract is no longer valid");
                DeleteAgreement(agreement, txid);
                return(false);
            }
            else if (duration == 0)
            {
                DeleteAgreement(agreement, txid);
            }
            else
            {
                agreement.Put("duration", duration);
                agreement.Put("nextTime", scheduleTime + paymentInfo[0]);
            }

            //---if over maxfund
            if (isToHandleMaxFund)
            {
                string     maxfundId  = Helper.Concat(txid, Helper.AsByteArray(duration)).AsString();
                StorageMap maxFundMap = Storage.CurrentContext.CreateMap(maxfundId);
                maxFundMap.Put("value", value);
                maxFundMap.Put("from", from);
                maxFundMap.Put("to", to);
                //Throw event
                MaxFund(maxfundId, value, from, to);
                return(false);
            }
            //-----End of handle max fund

            if (paymentInfo[2] == 1 && duration >= 0) //case of withdrwal
            {
                return(NEP.Transfer(from, to, value, (uint)ModuleCommission.getCommission("scheduleP")));
            }

            //if withdrawal
            Error("Pull scheduled payment: not duly");
            return(false);
        }
Beispiel #6
0
        //领取奖励(个人)
        public static BigInteger getBonus(byte[] to)
        {
            if (!Runtime.CheckWitness(to))
            {
                return(0);
            }

            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            if (data.Length == 0)
            {
                return(0);
            }

            var toinfo = GetAccountDetail(to);
            //var indexcashto = to.Concat(new byte[] { 0 });
            //var indexsavingto = to.Concat(new byte[] { 1 });
            //var indexsavingblockto = to.Concat(new byte[] { 2 });
            //BigInteger to_value_cash = Storage.Get(Storage.CurrentContext, indexcashto).AsBigInteger();
            //BigInteger to_value_saving = Storage.Get(Storage.CurrentContext, indexsavingto).AsBigInteger();
            //BigInteger to_value_savingblock = Storage.Get(Storage.CurrentContext, indexsavingblockto).AsBigInteger();

            BigInteger lastBonusBlock = Helper.AsBigInteger(data);
            BigInteger addValue       = 0;

            for (var i = 0; i < bonusCount; i++)
            {
                byte[] bIndex      = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(lastBonusBlock));
                byte[] bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
                byte[] bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
                byte[] bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
                byte[] bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
                var    StartBlock  = Storage.Get(Storage.CurrentContext, bStartBlock).AsBigInteger();
                var    BonusValue  = Storage.Get(Storage.CurrentContext, bBonusValue).AsBigInteger();
                var    BonusCount  = Storage.Get(Storage.CurrentContext, bBonusCount).AsBigInteger();
                if (toinfo.savingblock < StartBlock)         //有领奖资格
                {
                    var cangot = toinfo.saving * BonusValue; //要领走多少
                    addValue += cangot;
                    Storage.Put(Storage.CurrentContext, bBonusCount, BonusCount - cangot);
                }
                byte[] LastIndex = Storage.Get(Storage.CurrentContext, bLastIndex);
                if (LastIndex.Length == 0)
                {
                    break;
                }
                lastBonusBlock = Helper.AsBigInteger(LastIndex);
                if (lastBonusBlock == 0)
                {
                    break;
                }
            }
            //领奖写入
            BigInteger balanceto = toinfo.saving * factor + toinfo.cash;

            {
                var lastv  = balanceto + addValue;
                var bigN   = lastv / (factor);
                var smallN = lastv % (factor);
                toinfo.cash        = smallN;
                toinfo.saving      = bigN;
                toinfo.savingblock = Blockchain.GetHeight();
                SetAccountDetail(to, toinfo);
                //Storage.Put(Storage.CurrentContext, indexcashto, smallN);
                //Storage.Put(Storage.CurrentContext, indexsavingto, bigN);
                //BigInteger block = (Blockchain.GetHeight());
                //Storage.Put(Storage.CurrentContext, indexsavingblockto, block);
            }
            return(0);
        }
Beispiel #7
0
        //新奖励,(everyone)随便调用,不符合规则就不会创建奖励,谁都可以调用这个,催促发奖励。
        public static BigInteger newBonus()
        {
            byte[] data = Storage.Get(Storage.CurrentContext, "!bonus:L");
            //if (data.Length == 0)
            //    ;

            BigInteger index = Blockchain.GetHeight() / bonusInterval;

            if (index < 1)
            {
                return(0);
            }

            BigInteger bounsheight = (index - 1) * bonusInterval;

            BigInteger lastBounsBlock = Helper.AsBigInteger(data);

            if (bounsheight == lastBounsBlock)
            {
                return(0);
            }

            //清掉奖池
            var poolv = Storage.Get(Storage.CurrentContext, "!pool").AsBigInteger();

            Storage.Delete(Storage.CurrentContext, "!pool");
            byte[] bIndex = Helper.AsByteArray("bonus").Concat(Helper.AsByteArray(bounsheight));

            //找到第一个奖池
            byte[] firstIndex  = bIndex;
            byte[] secondIndex = bIndex;
            bool   skipdel     = false;

            for (var i = 0; i < bonusCount; i++)
            {
                secondIndex = firstIndex;
                byte[] _bLastIndex = firstIndex.Concat(Helper.AsByteArray(":L"));
                var    _index      = Storage.Get(Storage.CurrentContext, _bLastIndex);//+1+2+3
                if (_index.Length == 0)
                {
                    Runtime.Log("skipdel");
                    skipdel = true;
                    break;
                }
                firstIndex = Helper.AsByteArray("bonus").Concat(_index);
            }
            if (skipdel == false)//删除最后一个的连接,并把他的钱也放进新奖池里
            {
                byte[] secondLastIndex = secondIndex.Concat(Helper.AsByteArray(":L"));
                Storage.Delete(Storage.CurrentContext, secondLastIndex);
                byte[] firstBonusCount = firstIndex.Concat(Helper.AsByteArray(":C"));
                var    count           = Storage.Get(Storage.CurrentContext, firstBonusCount).AsBigInteger();
                poolv += count;
            }


            byte[] bStartBlock = bIndex.Concat(Helper.AsByteArray(":S"));
            byte[] bBonusValue = bIndex.Concat(Helper.AsByteArray(":V"));
            byte[] bBonusCount = bIndex.Concat(Helper.AsByteArray(":C"));
            byte[] bLastIndex  = bIndex.Concat(Helper.AsByteArray(":L"));
            Storage.Put(Storage.CurrentContext, bStartBlock, bounsheight.AsByteArray());
            Storage.Put(Storage.CurrentContext, bBonusCount, poolv.AsByteArray());
            BigInteger bonusV = poolv / (totalSupply() / factor);//整数部分一发几

            Storage.Put(Storage.CurrentContext, bBonusValue, bonusV.AsByteArray());
            Storage.Put(Storage.CurrentContext, bLastIndex, lastBounsBlock.AsByteArray());

            //写入lastblock
            Storage.Put(Storage.CurrentContext, "!bonus:L", bounsheight.AsByteArray());


            return(bounsheight);
        }