Пример #1
0
        private static bool FreezeNft(byte[] address, byte[] tokenId)
        {
            if (tokenId.Length != 32)
            {
                return(false);
            }
            NFTInfo nftInfo = GetNftByTokenId(tokenId);

            //已经冻结
            if (nftInfo.IsFrozen)
            {
                return(false);
            }

            if (nftInfo.Owner != address)
            {
                return(false);
            }
            //已经激活的不能冻结
            if (nftInfo.IsActivated)
            {
                return(false);
            }

            nftInfo.IsFrozen = true;

            SaveNftInfo(nftInfo);

            Frozen(address, tokenId);

            return(true);
        }
Пример #2
0
        private static bool UnFreezeNft(byte[] address, byte[] tokenId)
        {
            if (tokenId.Length != 32)
            {
                return(false);
            }
            NFTInfo nftInfo = GetNftByTokenId(tokenId);

            //未冻结
            if (!nftInfo.IsFrozen)
            {
                return(false);
            }

            if (nftInfo.Owner != address)
            {
                return(false);
            }

            nftInfo.IsFrozen = false;

            SaveNftInfo(nftInfo);

            UnFrozen(address, tokenId);

            return(true);
        }
Пример #3
0
        public static void SaveNftInfo(NFTInfo nftInfo)
        {
            var key = NftInfoKey(nftInfo.TokenId);

            byte[] nftInfoBytes = Helper.Serialize(nftInfo);
            Storage.Put(Context(), key, nftInfoBytes);
        }
Пример #4
0
        private static bool Activate(byte[] tokenId, BigInteger oneLevelInviterPoint, BigInteger twoLevelInviterPoint)
        {
            var nftInfo = GetNftByTokenId(tokenId);

            //已冻结
            if (nftInfo.IsFrozen)
            {
                return(false);
            }

            if (nftInfo.Owner.Length != 20)
            {
                return(false);
            }

            //已经激活不能重复激活
            if (nftInfo.IsActivated)
            {
                return(false);
            }

            //获取邀请者证书信息
            NFTInfo inviterNftInfo = GetNftByTokenId(nftInfo.InviterTokenId);

            if (inviterNftInfo.Owner.Length != 20)
            {
                return(false);
            }

            //激活
            nftInfo.IsActivated = true;

            SaveNftInfo(nftInfo);

            BigInteger activatedCount = Storage.Get(Context(), "activatedCount").AsBigInteger();

            Storage.Put(Context(), "activatedCount", activatedCount + 1);

            //上线加分
            AddPoint(inviterNftInfo, oneLevelInviterPoint, "invited".AsByteArray());

            //二级上线加分
            var twoLevelInviterNftInfo = GetNftByTokenId(inviterNftInfo.InviterTokenId);

            if (twoLevelInviterNftInfo.Owner.Length == 20)
            {
                AddPoint(twoLevelInviterNftInfo, twoLevelInviterPoint, "invited".AsByteArray());
            }

            Activated(nftInfo.Owner, tokenId);

            return(true);
        }
Пример #5
0
        private static bool DestroyNft(byte[] address, byte[] tokenId)
        {
            if (tokenId.Length != 32)
            {
                return(false);
            }
            NFTInfo nftInfo = GetNftByTokenId(tokenId);

            //只有冻结的可以删除
            if (!nftInfo.IsFrozen)
            {
                return(false);
            }

            if (nftInfo.Owner != address)
            {
                return(false);
            }
            //已经激活的不能删除
            if (nftInfo.IsActivated)
            {
                return(false);
            }

            //删除 nftInfo
            Storage.Delete(Context(), NftInfoKey(tokenId));

            Storage.Delete(Context(), PropertiesKey(tokenId));

            var allowanceKey = AllowanceKey(tokenId);

            Storage.Delete(Context(), allowanceKey);

            BigInteger userNftCount = GetUserNftCount(address);

            if (userNftCount > 0)
            {
                Storage.Put(Context(), UserNftCountKey(address), userNftCount - 1);
            }

            //已发行数量减 1
            BigInteger nftCount = Storage.Get(Context(), "allNftCount").AsBigInteger();

            if (nftCount > 0)
            {
                Storage.Put(Context(), "allNftCount", nftCount - 1);
            }

            Destroyed(address, tokenId);
            return(true);
        }
Пример #6
0
        private static bool BindNft(byte[] address, byte[] tokenId)
        {
            if (!Runtime.CheckWitness(address))
            {
                return(false);
            }

            if (address.Length != 20 || tokenId.Length != 32)
            {
                return(false);
            }

            NFTInfo nftInfo = GetNftByTokenId(tokenId);

            //已冻结
            if (nftInfo.IsFrozen)
            {
                return(false);
            }

            //没激活的不能绑定
            if (!nftInfo.IsActivated)
            {
                return(false);
            }

            var userTokenId = GetTokenIdByAddress(address);

            //重复绑定
            if (userTokenId == nftInfo.TokenId)
            {
                return(true);
            }

            //不是自己的证书不能绑定
            if (nftInfo.Owner != address)
            {
                return(false);
            }

            Storage.Put(Context(), BindNftKey(address), nftInfo.TokenId);

            //notify
            Bound(address, tokenId);

            return(true);
        }
Пример #7
0
        private static bool AddPoint(NFTInfo nftInfo, BigInteger pointValue, byte[] type)
        {
            if (pointValue == 0)
            {
                return(true);
            }

            nftInfo.AvailablePoint += pointValue;
            if (pointValue > 0)
            {
                nftInfo.AllPoint += pointValue;
            }

            SaveNftInfo(nftInfo);
            //notify
            AddPointed(nftInfo.TokenId, nftInfo.Owner, pointValue, type);
            return(true);
        }
Пример #8
0
        public static NFTInfo CreateNft(byte[] owner, byte[] inviterTokenId, int num)
        {
            BigInteger nonce = num;

            byte[] tokenId = Hash256((ExecutionEngine.ScriptContainer as Transaction).Hash.Concat(nonce.AsByteArray()));

            NFTInfo nftInfo = new NFTInfo()
            {
                TokenId        = tokenId,
                AllPoint       = 0,
                AvailablePoint = 0,
                Owner          = owner,
                Grade          = 1,
                InviterTokenId = inviterTokenId,
                IsActivated    = false
            };

            return(nftInfo);
        }
Пример #9
0
        private static bool DeployFirstNft(byte[] address, byte[] properties)
        {
            if (address.Length != 20)
            {
                return(false);
            }

            //判断初始发行是否已完成
            byte[] deploy_data = Storage.Get(Context(), "initDeploy");
            if (deploy_data.Length > 0)
            {
                return(false);
            }

            //构建一个证书
            NFTInfo newNftInfo = CreateNft(address, new byte[] { }, 1);

            //创始证书自动激活
            newNftInfo.IsActivated = true;

            byte[] propertiesKey = PropertiesKey(newNftInfo.TokenId);

            Storage.Put(Context(), propertiesKey, properties);

            //保存nft信息
            SaveNftInfo(newNftInfo);

            Storage.Put(Context(), UserNftCountKey(address), 1);

            Storage.Put(Context(), "allNftCount", 1);
            Storage.Put(Context(), "activatedCount", 1);

            //notify
            MintedToken(address, newNftInfo.TokenId, properties, null);

            Storage.Put(Context(), "initDeploy", 1);

            return(true);
        }
Пример #10
0
        private static bool BuyNewNft(byte[] assetId, byte[] txid, int count, byte[] inviterTokenId, BigInteger receivableValue, byte[] properties)
        {
            if (assetId.Length != 20 || txid.Length != 32 || inviterTokenId.Length != 32)
            {
                return(false);
            }
            if (count < 1 || receivableValue < 1)
            {
                return(false);
            }

            byte[] v = Storage.Get(Storage.CurrentContext, BctTxidUsedKey(txid));
            if (v.Length != 0)
            {
                return(false);
            }

            //获取邀请者证书信息 未激活不能邀请
            var inviterNftInfo = GetNftByTokenId(inviterTokenId);

            if (inviterNftInfo.Owner.Length != 20)
            {
                return(false);
            }
            if (!inviterNftInfo.IsActivated)
            {
                return(false);
            }

            //判断是否已达数量上限
            BigInteger nftCount   = Storage.Get(Context(), "allNftCount").AsBigInteger();
            BigInteger totalCount = Storage.Get(Context(), "totalCount").AsBigInteger();

            if (nftCount + count > totalCount)
            {
                return(false);
            }
            byte[] gatherAddress = Storage.Get(Context(), "gatherAddress");

            //获取 bct 转账信息
            TransferLog tx = NativeAsset.GetTransferLog(assetId, txid);

            //钱没给够或收款地址不对 false
            if (tx.From.Length == 0 || tx.To != gatherAddress || (BigInteger)tx.Value < receivableValue)
            {
                return(false);
            }

            byte[] address = tx.From;

            for (int i = 1; i <= count; i++)
            {
                NFTInfo nftInfo = CreateNft(address, inviterTokenId, i);

                int num = (int)nftCount + i;

                byte[] propertiesKey = PropertiesKey(nftInfo.TokenId);

                Storage.Put(Context(), propertiesKey, properties);

                SaveNftInfo(nftInfo);

                MintedToken(address, nftInfo.TokenId, properties, inviterTokenId);
            }

            BigInteger userNftCount = GetUserNftCount(address);

            Storage.Put(Context(), UserNftCountKey(address), userNftCount + count);

            //更新数量
            Storage.Put(Context(), "allNftCount", nftCount + count);

            SetTxUsed(txid);

            return(true);
        }