示例#1
0
        public static bool settingSAR(string name, byte[] addr)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                throw new InvalidOperationException("The sar must not be null.");
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                throw new InvalidOperationException("The sar must be self.");
            }

            info.status = (int)ConfigSARStatus.SAR_STATUS_CLOSE;

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

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

            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_SHUT;
            detail.operated  = 0;
            detail.hasLocked = info.locked;
            detail.hasDrawed = info.hasDrawed;
            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(info.name.AsByteArray(), committee, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_SHUT, 0);
            return(true);
        }
        public static bool settingSAR(string name, byte[] addr)
        {
            if (addr.Length != 20)
            {
                return(false);
            }
            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            // SAR制造者操作
            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                return(false);
            }

            info.status = (int)ConfigSARStatus.SAR_STATUS_CLOSE;

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));
            //记录交易详细数据
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_SHUT;
            detail.operated  = 0;
            detail.hasLocked = info.locked;
            detail.hasDrawed = info.hasDrawed;
            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(info.name.AsByteArray(), admin, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_SHUT, 0);
            return(true);
        }
示例#3
0
        public static bool openSAR4B(string name, string symbol, byte decimals, byte[] addr, string anchor)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }
            //check SAR
            if (!checkState(SAR_STATE))
            {
                throw new InvalidOperationException("The sar state MUST be pause.");
            }

            if (name.Length <= 0)
            {
                throw new InvalidOperationException("The parameter name MUST bigger than 0.");
            }

            if (!checkName(name))
            {
                throw new InvalidOperationException("The parameter name is invalid.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length > 0)
            {
                throw new InvalidOperationException("The sar must be null.");
            }

            byte[] oracleAssetID    = Storage.Get(Storage.CurrentContext, getAccountKey(ORACLE_ACCOUNT.AsByteArray()));
            byte[] tokenizedAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(TOKENIZED_ACCOUNT.AsByteArray()));

            {
                object[] arg = new object[1];
                arg[0] = string.Concat(NAME_PREFIX, name);

                var    TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                string str = (string)TokenizedContract("name", arg);
                if (str.Length > 0)
                {
                    throw new InvalidOperationException("The name must be null.");
                }
            }

            {
                object[] arg = new object[1];
                arg[0] = anchor;

                var        OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();
                BigInteger re             = (BigInteger)OracleContract("getTypeA", arg);
                if (re != 1)
                {
                    throw new InvalidOperationException("The param is exception.");
                }
            }

            byte[]  txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            SARInfo info = new SARInfo();

            info.symbol    = symbol;
            info.decimals  = decimals;
            info.name      = string.Concat(NAME_PREFIX, name);
            info.hasDrawed = 0;
            info.locked    = 0;
            info.owner     = addr;
            info.txid      = txid;
            info.status    = 1;
            info.anchor    = anchor;

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

            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_OPEN;
            detail.operated  = 0;
            detail.hasLocked = 0;
            detail.hasDrawed = 0;

            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(name.AsByteArray(), addr, txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_OPEN, 0);
            return(true);
        }
示例#4
0
        public static bool contract(string name, byte[] addr, BigInteger value)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (value <= 0)
            {
                throw new InvalidOperationException("The param is exception.");
            }

            if (!checkState(SAR_STATE))
            {
                throw new InvalidOperationException("The sar state MUST be pause.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                throw new InvalidOperationException("The sar must not be null.");
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                throw new InvalidOperationException("The param is exception.");
            }

            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                throw new InvalidOperationException("The param is exception.");
            }

            byte[] tokenizedAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(TOKENIZED_ACCOUNT.AsByteArray()));

            BigInteger hasDrawed = info.hasDrawed;

            if (value > hasDrawed)
            {
                throw new InvalidOperationException("The param is exception.");
            }

            object[] arg = new object[3];
            arg[0] = name;
            arg[1] = addr;
            arg[2] = value;

            var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract("destory", arg))
            {
                throw new InvalidOperationException("The param is exception.");
            }

            info.hasDrawed = hasDrawed - value;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_CONTRACT;
            detail.operated  = value;
            detail.hasLocked = info.locked;
            detail.hasDrawed = info.hasDrawed;
            detail.txid      = txid;
            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_CONTRACT, value);
            return(true);
        }
示例#5
0
        public static bool destory(string name, byte[] addr)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (!checkState(SAR_STATE))
            {
                throw new InvalidOperationException("The sar state MUST be pause.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                throw new InvalidOperationException("The sar must not be null.");
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                throw new InvalidOperationException("The operation is exception.");
            }

            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                throw new InvalidOperationException("The operation is exception.");
            }
            byte[] oracleAssetID    = Storage.Get(Storage.CurrentContext, getAccountKey(ORACLE_ACCOUNT.AsByteArray()));
            byte[] sdsAssetID       = Storage.Get(Storage.CurrentContext, getAccountKey(SDS_ACCOUNT.AsByteArray()));
            byte[] tokenizedAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(TOKENIZED_ACCOUNT.AsByteArray()));
            byte[] from             = ExecutionEngine.ExecutingScriptHash;

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;

            object[] arg = new object[0];

            BigInteger serviceFree = 1000000000;

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                arg    = new object[1];
                arg[0] = SERVICE_FEE;
                BigInteger re = (BigInteger)OracleContract("getTypeA", arg);
                if (re != 0)
                {
                    serviceFree = re;
                }
            }

            if (locked > serviceFree)
            {
                throw new InvalidOperationException("The param is exception.");
            }


            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            object[] param = new object[3];
            param[0] = from;
            param[1] = addr;
            param[2] = locked;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer_contract", param))
            {
                throw new InvalidOperationException("The operation is exception.");
            }

            param    = new object[2];
            param[0] = name;
            param[1] = addr;

            var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract("close", param))
            {
                throw new InvalidOperationException("The operation is exception.");
            }

            Storage.Delete(Storage.CurrentContext, key);

            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_DESTORY;
            detail.operated  = locked;
            detail.hasLocked = 0;
            detail.hasDrawed = hasDrawed;
            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_DESTORY, locked);
            return(true);
        }
示例#6
0
        public static bool withdraw(string name, byte[] addr, BigInteger value)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (value <= 0)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (!checkState(SAR_STATE))
            {
                throw new InvalidOperationException("The sar state MUST be pause.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                throw new InvalidOperationException("The sar must not be null.");
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                throw new InvalidOperationException("The sar must be self.");
            }

            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                throw new InvalidOperationException("The sar status must not be close.");
            }

            byte[] oracleAssetID    = Storage.Get(Storage.CurrentContext, getAccountKey(ORACLE_ACCOUNT.AsByteArray()));
            byte[] sdsAssetID       = Storage.Get(Storage.CurrentContext, getAccountKey(SDS_ACCOUNT.AsByteArray()));
            byte[] tokenizedAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(TOKENIZED_ACCOUNT.AsByteArray()));

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;

            object[]   arg          = new object[0];
            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                arg       = new object[1];
                arg[0]    = CONFIG_SDS_PRICE;
                sds_price = (BigInteger)OracleContract("getTypeB", arg);
            }

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                arg          = new object[1];
                arg[0]       = info.anchor;
                anchor_price = (BigInteger)OracleContract("getTypeB", arg);
            }

            BigInteger rate = 100;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                arg    = new object[1];
                arg[0] = CONFIG_LIQUIDATION_RATE_B;
                BigInteger re = (BigInteger)OracleContract("getTypeA", arg);
                if (re != 0)
                {
                    rate = re;
                }
            }

            BigInteger serviceFree = 1000000000;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                arg    = new object[1];
                arg[0] = SERVICE_FEE;
                BigInteger re = (BigInteger)OracleContract("getTypeA", arg);
                if (re != 0)
                {
                    serviceFree = re;
                }
            }

            BigInteger hasDrawSDS = hasDrawed * rate * FOURTEEN_POWER / (sds_price * anchor_price);

            if (value > locked - hasDrawSDS)
            {
                throw new InvalidOperationException("The param is exception.");
            }

            if (locked - value < serviceFree)
            {
                throw new InvalidOperationException("The param is exception.");
            }

            byte[] from = ExecutionEngine.ExecutingScriptHash;

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            object[] param = new object[3];
            param[0] = from;
            param[1] = addr;
            param[2] = value;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer_contract", param))
            {
                throw new InvalidOperationException("The operation is exception.");
            }

            info.locked = locked - value;

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

            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_WITHDRAW;
            detail.operated  = value;
            detail.hasLocked = locked - value;
            detail.hasDrawed = hasDrawed;
            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_WITHDRAW, value);
            return(true);
        }
示例#7
0
        public static bool expande(string name, byte[] addr, BigInteger value)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (value <= 0)
            {
                throw new InvalidOperationException("The parameter is exception.");
            }

            //check SAR
            if (!checkState(SAR_STATE))
            {
                throw new InvalidOperationException("The sar state MUST be pause.");
            }

            var key = getSARKey(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                throw new InvalidOperationException("The sar must not be null.");
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                throw new InvalidOperationException("The parameter is exception.");
            }

            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                throw new InvalidOperationException("The parameter is exception.");
            }

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;

            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;

            byte[] oracleAssetID    = Storage.Get(Storage.CurrentContext, getAccountKey(ORACLE_ACCOUNT.AsByteArray()));
            byte[] tokenizedAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(TOKENIZED_ACCOUNT.AsByteArray()));

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                object[] arg = new object[1];
                arg[0]    = CONFIG_SDS_PRICE;
                sds_price = (BigInteger)OracleContract("getTypeB", arg);
            }

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                object[] arg = new object[1];
                arg[0]       = info.anchor;
                anchor_price = (BigInteger)OracleContract("getTypeB", arg);
            }

            BigInteger sds_rate = 100;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                object[] arg = new object[1];
                arg[0] = CONFIG_LIQUIDATION_RATE_B;
                BigInteger re = (BigInteger)OracleContract("getTypeA", arg);
                if (re != 0)
                {
                    sds_rate = re;
                }
            }

            BigInteger sdusd_limit = sds_price * locked * 100 / (anchor_price * sds_rate);

            if (sdusd_limit < hasDrawed + value)
            {
                throw new InvalidOperationException("The parameter is exception.");
            }

            {
                object[] arg = new object[3];
                arg[0] = name;
                arg[1] = addr;
                arg[2] = value;

                var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                if (!(bool)TokenizedContract("increase", arg))
                {
                    throw new InvalidOperationException("The parameter is exception.");
                }
            }

            info.hasDrawed = hasDrawed + value;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));


            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_EXPANDE;
            detail.operated  = value;
            detail.hasLocked = locked;
            detail.hasDrawed = info.hasDrawed;

            Storage.Put(Storage.CurrentContext, getTxidKey(txid), Helper.Serialize(detail));

            //notify
            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_EXPANDE, value);
            return(true);
        }
        public static bool initToken(byte[] addr, byte[] oracleAssetID, byte[] sdsAssetID, byte[] tokenizedAssetID)
        {
            //判断该地址是否拥有SAR
            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            //验证name
            object[] arg = new object[1];
            arg[0] = info.name;

            var    TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
            string str = (string)TokenizedContract("name", arg);

            if (str.Length > 0)
            {
                return(false);
            }

            byte[] to = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (to.Length == 0)
            {
                return(false);
            }

            //当前兑换率,默认是100,需要从配置中心获取
            BigInteger serviceFree = 1000000000;

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询手续费,如:50 => 50%
                arg    = new object[1];
                arg[0] = SERVICE_FEE;
                BigInteger re = (BigInteger)OracleContract("getConfig", arg);
                if (re != 0)
                {
                    serviceFree = re;
                }
            }

            //转入抵押手续费
            arg    = new object[3];
            arg[0] = addr;
            arg[1] = to;
            arg[2] = serviceFree;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer", arg))
            {
                return(false);
            }

            info.locked = info.locked + serviceFree;

            arg    = new object[4];
            arg[0] = info.name;
            arg[1] = info.symbol;
            arg[2] = info.decimals;
            arg[3] = addr;

            //保存标准
            var TokenizedContract2 = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract2("init", arg))
            {
                return(false);
            }

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

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            //交易详细信息
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_INIT;
            detail.operated  = 0;
            detail.hasLocked = 0;
            detail.hasDrawed = 0;

            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_INIT, 0);
            return(true);
        }
        /*开启一个新的债仓*/
        public static bool openSAR4B(string name, string symbol, byte decimals, byte[] addr, string anchor, byte[] oracleAssetID, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }

            //判断该地址是否拥有SAR
            var key = new byte[] { 0x12 }.Concat(addr);

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

            {
                //调用标准合约
                object[] arg = new object[1];
                arg[0] = name;

                //验证name
                var    TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                string str = (string)TokenizedContract("name", arg);

                if (str.Length > 0)
                {
                    return(false);
                }
            }

            {
                //判断是否是白名单
                object[] arg = new object[1];
                arg[0] = anchor;

                var        OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();
                BigInteger re             = (BigInteger)OracleContract("getConfig", arg);
                if (re != 1)
                {
                    return(false);
                }
            }

            byte[]  txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            SARInfo info = new SARInfo();

            info.symbol    = symbol;
            info.decimals  = 8;
            info.name      = name;
            info.hasDrawed = 0;
            info.locked    = 0;
            info.owner     = addr;
            info.txid      = txid;
            info.status    = 1;
            info.anchor    = anchor;

            //保存SAR
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));

            //交易详细信息
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_OPEN;
            detail.operated  = 0;
            detail.hasLocked = 0;
            detail.hasDrawed = 0;

            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(name.AsByteArray(), addr, txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_OPEN, 0);
            return(true);
        }
        /*返还部分仓位*/
        public static bool contract(string name, byte[] addr, BigInteger value, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }
            if (value == 0)
            {
                return(false);
            }
            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            // SAR制造者操作
            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                return(false);
            }

            //SAR状态
            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                return(false);
            }

            byte[] to = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (to.Length == 0)
            {
                return(false);
            }

            BigInteger hasDrawed = info.hasDrawed;

            //不能超过已经获取
            if (value > hasDrawed)
            {
                return(false);
            }

            object[] arg = new object[3];
            arg[0] = name;
            arg[1] = addr;
            arg[2] = value;

            var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract("destory", arg))
            {
                return(false);
            }

            info.hasDrawed = hasDrawed - value;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            //记录交易详细数据
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_CONTRACT;
            detail.operated  = value;
            detail.hasLocked = info.locked;
            detail.hasDrawed = info.hasDrawed;
            detail.txid      = txid;
            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_CONTRACT, value);
            return(true);
        }
        /*提现抵押资产*/
        public static bool destory(string name, byte[] addr, byte[] oracleAssetID, byte[] sdsAssetID, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }

            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            // SAR制造者操作
            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                return(false);
            }

            //SAR状态
            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                return(false);
            }

            byte[] to = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (to.Length == 0)
            {
                return(false);
            }

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;


            object[]   arg          = new object[0];
            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;


            //当前兑换率,默认是100,需要从配置中心获取
            BigInteger serviceFree = 1000000000;

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询手续费,如:1000000000
                arg    = new object[1];
                arg[0] = SERVICE_FEE;
                BigInteger re = (BigInteger)OracleContract("getConfig", arg);
                if (re != 0)
                {
                    serviceFree = re;
                }
            }

            //总量大于固定费率,不能操作
            if (locked > serviceFree)
            {
                return(false);
            }

            byte[] from = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (from.Length == 0)
            {
                return(false);
            }

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            object[] param = new object[3];
            param[0] = from;
            param[1] = addr;
            param[2] = locked;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer_contract", param))
            {
                return(false);
            }

            param    = new object[2];
            param[0] = name;
            param[1] = addr;

            var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract("close", param))
            {
                return(false);
            }

            info.locked = 0;
            info.status = (int)ConfigSARStatus.SAR_STATUS_CLOSE;

            //Storage.Put(Storage.CurrentContext,key, Helper.Serialize(info));
            Storage.Delete(Storage.CurrentContext, key);
            //记录交易详细数据
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_DESTORY;
            detail.operated  = locked;
            detail.hasLocked = 0;
            detail.hasDrawed = hasDrawed;
            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_DESTORY, locked);
            return(true);
        }
        /*提现抵押资产*/
        public static bool withdraw(string name, byte[] addr, BigInteger value, byte[] oracleAssetID, byte[] sdsAssetID, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }
            if (value == 0)
            {
                return(false);
            }

            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            // SAR制造者操作
            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                return(false);
            }

            //SAR状态
            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                return(false);
            }

            byte[] to = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (to.Length == 0)
            {
                return(false);
            }

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;


            object[]   arg          = new object[0];
            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询SDS价格,如:8$=价格*100000000
                arg       = new object[1];
                arg[0]    = CONFIG_SDS_PRICE;
                sds_price = (BigInteger)OracleContract("getPrice", arg);
            }

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询锚定价格,如:100$=价格*100000000
                arg          = new object[1];
                arg[0]       = info.anchor;
                anchor_price = (BigInteger)OracleContract("getPrice", arg);
            }
            //当前兑换率,默认是100,需要从配置中心获取
            BigInteger rate = 100;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询抵押率,如:50 => 50%
                arg    = new object[1];
                arg[0] = CONFIG_LIQUIDATION_RATE_B;
                BigInteger re = (BigInteger)OracleContract("getConfig", arg);
                if (re != 0)
                {
                    rate = re;
                }
            }

            //当前兑换率,默认是100,需要从配置中心获取
            BigInteger serviceFree = 1000000000;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询手续费,如:1000000000
                arg    = new object[1];
                arg[0] = SERVICE_FEE;
                BigInteger re = (BigInteger)OracleContract("getConfig", arg);
                if (re != 0)
                {
                    serviceFree = re;
                }
            }

            //计算已经兑换SDS量
            BigInteger hasDrawSDS = hasDrawed * rate * FOURTEEN_POWER / (sds_price * anchor_price);

            //释放的总量大于已经剩余,不能操作
            if (value > locked - hasDrawSDS)
            {
                return(false);
            }

            //剩余必须大于
            if (locked - value < serviceFree)
            {
                return(false);
            }

            byte[] from = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (from.Length == 0)
            {
                return(false);
            }

            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;

            object[] param = new object[3];
            param[0] = from;
            param[1] = addr;
            param[2] = value;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer_contract", param))
            {
                return(false);
            }

            //重新设置锁定量
            info.locked = locked - value;

            ////提现为0了,需要关闭token
            //if (info.locked == 0)
            //{
            //    param = new object[2];
            //    param[0] = name;
            //    param[1] = addr;

            //    var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
            //    if (!(bool)TokenizedContract("close", param)) return false;
            //}

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

            //记录交易详细数据
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_WITHDRAW;
            detail.operated  = value;
            detail.hasLocked = locked - value;
            detail.hasDrawed = hasDrawed;
            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_WITHDRAW, value);
            return(true);
        }
        public static bool redeem(byte[] addr, byte[] sarAddr, byte[] sdsAssetID, byte[] oracleAssetID, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }

            var key = new byte[] { 0x12 }.Concat(sarAddr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            //SAR状态,必须是关闭状态
            if (info.status != (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                return(false);
            }

            //转账给用户SDS
            byte[] from = Storage.Get(Storage.CurrentContext, STORAGE_ACCOUNT);
            if (from.Length == 0)
            {
                return(false);
            }

            //清算的币种
            string name = info.name;

            //调用标准查询余额
            object[] arg = new object[2];
            arg[0] = name;
            arg[1] = addr;
            BigInteger balance = 0;
            {
                var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                balance = (BigInteger)TokenizedContract("balanceOf", arg);
                if (balance <= 0)
                {
                    return(false);
                }
            }

            BigInteger totalSupply = 0;
            {
                arg    = new object[1];
                arg[0] = name;
                var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                totalSupply = (BigInteger)TokenizedContract("totalSupply", arg);
                if (totalSupply <= 0)
                {
                    return(false);
                }
            }

            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询SDS价格,如:8$=价格*100000000
                arg       = new object[1];
                arg[0]    = CONFIG_SDS_PRICE;
                sds_price = (BigInteger)OracleContract("getPrice", arg);
            }

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();
                //调用Oracle,查询锚定价格,如:100$=价格*100000000
                arg          = new object[1];
                arg[0]       = info.anchor;
                anchor_price = (BigInteger)OracleContract("getPrice", arg);
            }

            //计算可赎回的SDS
            BigInteger redeem = balance * info.locked / totalSupply;

            if (redeem <= 0)
            {
                return(false);
            }

            //销毁用户的稳定代币
            arg    = new object[3];
            arg[0] = name;
            arg[1] = addr;
            arg[2] = balance;

            var TokenizedContract2 = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract2("destory", arg))
            {
                return(false);
            }

            object[] param = new object[3];
            param[0] = from;
            param[1] = addr;
            param[2] = redeem;

            var SDSContract = (NEP5Contract)sdsAssetID.ToDelegate();

            if (!(bool)SDSContract("transfer_contract", param))
            {
                return(false);
            }


            info.locked    = info.locked - redeem;
            info.hasDrawed = info.hasDrawed - balance;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));

            //记录交易详细数据
            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_REDEEM;
            detail.operated  = redeem;
            detail.hasLocked = info.locked;
            detail.hasDrawed = info.hasDrawed;

            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));
            //触发操作事件
            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_REDEEM, redeem);
            return(true);
        }
        public static bool expande(string name, byte[] addr, BigInteger value, byte[] oracleAssetID, byte[] tokenizedAssetID)
        {
            if (addr.Length != 20)
            {
                return(false);
            }
            if (value == 0)
            {
                return(false);
            }

            var key = new byte[] { 0x12 }.Concat(addr);

            byte[] sar = Storage.Get(Storage.CurrentContext, key);
            if (sar.Length == 0)
            {
                return(false);
            }

            SARInfo info = (SARInfo)Helper.Deserialize(sar);

            // SAR制造者操作
            if (info.owner.AsBigInteger() != addr.AsBigInteger())
            {
                return(false);
            }

            //SAR状态
            if (info.status == (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                return(false);
            }

            BigInteger locked    = info.locked;
            BigInteger hasDrawed = info.hasDrawed;

            BigInteger sds_price    = 0;
            BigInteger anchor_price = 0;

            object[] arg = new object[0];

            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询SDS价格,如:8$=价格*100000000
                arg       = new object[1];
                arg[0]    = CONFIG_SDS_PRICE;
                sds_price = (BigInteger)OracleContract("getPrice", arg);
            }


            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询锚定价格,如:100$=价格*100000000
                arg          = new object[1];
                arg[0]       = info.anchor;
                anchor_price = (BigInteger)OracleContract("getPrice", arg);
            }

            //当前兑换率,默认是100,需要从配置中心获取
            BigInteger sds_rate = 100;
            {
                var OracleContract = (NEP5Contract)oracleAssetID.ToDelegate();

                //调用Oracle,查询抵押率,如:50 => 50%
                arg    = new object[1];
                arg[0] = CONFIG_LIQUIDATION_RATE_B;
                BigInteger re = (BigInteger)OracleContract("getConfig", arg);
                if (re != 0)
                {
                    sds_rate = re;
                }
            }

            BigInteger sdusd_limit = sds_price * anchor_price * locked / (sds_rate * FOURTEEN_POWER);

            if (sdusd_limit < hasDrawed + value)
            {
                return(false);
            }

            //调用标准增发
            arg    = new object[3];
            arg[0] = name;
            arg[1] = addr;
            arg[2] = value;

            var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();

            if (!(bool)TokenizedContract("increase", arg))
            {
                return(false);
            }

            info.hasDrawed = hasDrawed + value;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(info));

            //记录交易详细数据
            var txid = ((Transaction)ExecutionEngine.ScriptContainer).Hash;
            SARTransferDetail detail = new SARTransferDetail();

            detail.from      = addr;
            detail.sarTxid   = info.txid;
            detail.txid      = txid;
            detail.type      = (int)ConfigTranType.TRANSACTION_TYPE_EXPANDE;
            detail.operated  = value;
            detail.hasLocked = locked;
            detail.hasDrawed = hasDrawed;

            Storage.Put(Storage.CurrentContext, new byte[] { 0x13 }.Concat(txid), Helper.Serialize(detail));

            //触发操作事件
            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_EXPANDE, value);
            return(true);
        }