示例#1
0
        private static bool Lock(byte[] txid, byte[] locker, byte[] assetId, BigInteger isGlobal)
        {
            if (TxidUsed(txid))
            {
                return(false);
            }

            byte[] lockAddress = ExecutionEngine.ExecutingScriptHash;

            //全局资产 native nep5
            if (isGlobal == 1)
            {
                var tx = NativeAsset.GetTransferLog(assetId, txid);

                if (tx.From.Length == 20 && tx.Value > 0 && tx.To == lockAddress)
                {
                    IncreaseBalance(locker, assetId, tx.Value);
                    EmitLocked(locker, assetId, tx.Value);
                }
            }
            else
            {
                var tx = GetTxInfo(assetId, txid);

                if (tx.from.Length == 20 && tx.value > 0 && tx.to == lockAddress)
                {
                    IncreaseBalance(locker, assetId, tx.value);
                    EmitLocked(locker, assetId, tx.value);
                }
            }

            SetTxidUsed(txid);

            return(true);
        }
示例#2
0
        private static bool UpgradeNft(byte[] assetId, byte[] txid, byte[] tokenId, BigInteger receivableValue, BigInteger needPoint)
        {
            if (assetId.Length != 20 || txid.Length != 32 || tokenId.Length != 32)
            {
                return(false);
            }

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

            var nftInfo = GetNftByTokenId(tokenId);

            //冻结的不能升级
            if (nftInfo.IsFrozen)
            {
                return(false);
            }

            byte[] gatherAddress = Storage.Get(Context(), "gatherAddress");
            //获取 bct 转账信息
            TransferLog tx = NativeAsset.GetTransferLog(assetId, txid);

            if (tx.To != gatherAddress || (BigInteger)tx.Value < receivableValue)
            {
                return(false);
            }
            if (nftInfo.Owner != tx.From)
            {
                return(false);
            }

            if (nftInfo.AvailablePoint < needPoint)
            {
                return(false);
            }
            //升级
            nftInfo.Grade += 1;
            //扣除消耗贡献值
            nftInfo.AvailablePoint -= needPoint;

            SaveNftInfo(nftInfo);
            SetTxUsed(txid);

            //notify
            Upgraded(tokenId, nftInfo.Owner, nftInfo.Grade - 1, nftInfo.Grade);
            AddPointed(tokenId, nftInfo.Owner, 0 - needPoint, "upgrade".AsByteArray());

            return(true);
        }
示例#3
0
        public static object Main(string method, object[] args)
        {
            var magicstr = "Test_Contract_v0.35";

            if (Runtime.Trigger == TriggerType.Application)
            {
                var callscript = ExecutionEngine.CallingScriptHash;

                if (method == "witnessTest")
                {
                    Notify(new byte[] { }, 0);

                    if (!Runtime.CheckWitness((byte[])args[0]))
                    {
                        return(false);
                    }
                    Notify((byte[])args[0], 1);

                    if (!Runtime.CheckWitness((byte[])args[1]))
                    {
                        return(false);
                    }
                    Notify((byte[])args[1], 2);
                }

                if (method == "set")
                {
                    byte[] key  = (byte[])args[0];
                    byte[] data = (byte[])args[1];
                    Storage.Put(Storage.CurrentContext, key, data);
                }
                if (method == "get")
                {
                    byte[] key = (byte[])args[0];
                    return(Storage.Get(Storage.CurrentContext, key));
                }

                if (method == "test")
                {
                    return(1);
                }
                if (method == "call")
                {
                    return("yes");
                }
                if (method == "return")
                {
                    byte[] asset_id = (byte[])args[0];
                    return(asset_id);
                }
                if (method == "getheight")
                {
                    return(Blockchain.GetHeight());
                }
                if (method == "getheader")
                {
                    var height = (uint)args[0];
                    return(Blockchain.GetHeader(height));
                }

                if (method == "strToByte")
                {
                    var result = "hello world".AsByteArray();
                    return(result);
                }

                if (method == "balanceOf")
                {
                    byte[] asset_id = (byte[])args[0];
                    byte[] address  = (byte[])args[1];
                    var    aa       = NativeAsset.Call("BalanceOf", asset_id, address);
                    return(aa);
                }

                if (method == "balanceOf1")
                {
                    var    asset_id = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    byte[] address  = (byte[])args[0];
                    var    aa       = NativeAsset.Call("BalanceOf", asset_id, address).AsBigInteger();
                    return(aa);
                }

                if (method == "transferFrom")
                {
                    byte[] asset_id = (byte[])args[0];
                    byte[] from     = (byte[])args[1];
                    byte[] to       = (byte[])args[2];

                    BigInteger value = (BigInteger)args[3];

                    var para = new object[3] {
                        from, to, value
                    };
                    deleCall contract = (deleCall)asset_id.ToDelegate();
                    var      aa       = (bool)contract("transferFrom", para);

                    Runtime.Notify(1, aa);

                    var par = new object[2] {
                        from, to
                    };
                    BigInteger ba = (BigInteger)contract("allowance", par);

                    Runtime.Notify(1, ba);
                }

                if (method == "transferFrom1")
                {
                    byte[] asset_id = (byte[])args[0];
                    byte[] from     = (byte[])args[1];
                    byte[] to       = (byte[])args[2];

                    BigInteger value = (BigInteger)args[3];

                    var success = NativeAsset.Call("TransferFrom", asset_id, from, to, value);

                    Runtime.Notify(1, success);

                    var par = new object[3] {
                        asset_id, from, to
                    };
                    BigInteger ba = NativeAsset.Call("Allowance", par).AsBigInteger();

                    Runtime.Notify(1, ba);
                }

                if (method == "transferApp")
                {
                    byte[]     asset_id = (byte[])args[0];
                    byte[]     to       = (byte[])args[1];
                    BigInteger value    = (BigInteger)args[2];

                    byte[] from = ExecutionEngine.ExecutingScriptHash;

                    var para = new object[3] {
                        from, to, value
                    };
                    var contract = (deleCall)asset_id.ToDelegate();
                    var aa       = (bool)contract("transferApp", para);
                    Runtime.Notify(from, to, value);
                    Runtime.Notify(1, aa);
                }

                if (method == "GetTransferLog")
                {
                    byte[] asset_id = (byte[])args[0];
                    byte[] txid     = (byte[])args[1];

                    var tInfo = new TransferInfo();
                    var info  = NativeAsset.Call("GetTransferLog", asset_id, txid);
                    return(info);
                }

                if (method == "GetTransferLog3")
                {
                    byte[] asset_id = (byte[])args[0];
                    byte[] txid     = (byte[])args[1];

                    var tInfo = new TransferInfo();
                    var info  = NativeAsset.GetTransferLog(asset_id, txid);
                    return(info.Value);
                }
            }

            return(false);
        }
示例#4
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);
        }