Пример #1
0
        public static bool initToken(byte[] addr)
        {
            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.");
            }

            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);

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

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

            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_INIT, 0);

            Nep55Operated(info.name.AsByteArray(), addr, info.txid, info.anchor.AsByteArray(), info.symbol.AsByteArray(), info.decimals);
            return(true);
        }
Пример #2
0
        public static SARInfo getSAR4B(byte[] addr)
        {
            var key = getSARKey(addr);

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

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

            return(info);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #6
0
        private static bool migrateSAR4B(byte[] addr)
        {
            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.");
            }

            //check SAR
            var key = getSARKey(addr);

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

            SARInfo sarInfo = Helper.Deserialize(bytes) as SARInfo;

            BigInteger locked    = sarInfo.locked;
            BigInteger hasDrawed = sarInfo.hasDrawed;

            byte[] newSARID   = Storage.Get(Storage.CurrentContext, getAccountKey(STORAGE_ACCOUNT_NEW.AsByteArray()));
            byte[] from       = ExecutionEngine.ExecutingScriptHash;
            byte[] sdsAssetID = Storage.Get(Storage.CurrentContext, getAccountKey(SDS_ACCOUNT.AsByteArray()));

            {
                object[] arg = new object[3];
                arg[0] = from;
                arg[1] = newSARID;
                arg[2] = locked;
                var nep5Contract = (NEP5Contract)sdsAssetID.ToDelegate();
                if (!(bool)nep5Contract("transfer_contract", arg))
                {
                    throw new InvalidOperationException("The sar operation is exception.");
                }
            }

            {
                var      newContract = (NEP5Contract)newSARID.ToDelegate();
                object[] args        = new object[9];
                args[0] = addr;
                args[1] = sarInfo.txid;
                args[2] = sarInfo.name;
                args[3] = sarInfo.symbol;
                args[4] = sarInfo.decimals;
                args[5] = sarInfo.locked;
                args[6] = sarInfo.hasDrawed;
                args[7] = sarInfo.status;
                args[8] = sarInfo.anchor;

                if (!(bool)newContract("createSAR4B", args))
                {
                    throw new InvalidOperationException("The sar operation is exception.");
                }
            }
            sarInfo.locked    = 0;
            sarInfo.hasDrawed = 0;
            Storage.Put(Storage.CurrentContext, key, Helper.Serialize(sarInfo));
            return(true);
        }
Пример #7
0
        public static bool redeem(byte[] addr, byte[] sarAddr)
        {
            if (addr.Length != 20)
            {
                throw new InvalidOperationException("The parameter addr SHOULD be 20-byte addresses.");
            }

            if (sarAddr.Length != 20)
            {
                throw new InvalidOperationException("The parameter sarAddr SHOULD be 20-byte addresses.");
            }

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

            var key = getSARKey(sarAddr);

            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.status != (int)ConfigSARStatus.SAR_STATUS_CLOSE)
            {
                throw new InvalidOperationException("The parameter is exception.");
            }

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

            byte[] from = ExecutionEngine.ExecutingScriptHash;

            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)
                {
                    throw new InvalidOperationException("The parameter is exception.");
                }
            }

            BigInteger totalSupply = 0;
            {
                arg    = new object[1];
                arg[0] = name;
                var TokenizedContract = (NEP5Contract)tokenizedAssetID.ToDelegate();
                totalSupply = (BigInteger)TokenizedContract("totalSupply", arg);
                if (totalSupply <= 0)
                {
                    throw new InvalidOperationException("The parameter is exception.");
                }
            }

            BigInteger redeem = balance * info.locked / totalSupply;

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

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

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

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

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

            var        redeemKey     = getRedeemKey(addr);
            BigInteger redeemBalance = Storage.Get(Storage.CurrentContext, redeemKey).AsBigInteger();

            Storage.Put(Storage.CurrentContext, redeemKey, redeemBalance + redeem);

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

            //notify
            Operated(info.name.AsByteArray(), addr, info.txid, txid, (int)ConfigTranType.TRANSACTION_TYPE_REDEEM, redeem);
            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);
        }
Пример #9
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);
        }
        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);
        }
Пример #15
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);
        }
Пример #16
0
        public static Object Main(string operation, params object[] args)
        {
            var magicstr = "2018-11-23 14:45";

            if (Runtime.Trigger == TriggerType.Verification)
            {
                return(false);
            }
            else if (Runtime.Trigger == TriggerType.Application)
            {
                var callscript = ExecutionEngine.CallingScriptHash;

                if (operation == "getSAR4B")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    byte[] addr = (byte[])args[0];
                    return(getSAR4B(addr));
                }
                if (operation == "openSAR4B")
                {
                    if (args.Length != 5)
                    {
                        return(false);
                    }
                    string name     = (string)args[0];
                    string symbol   = (string)args[1];
                    byte   decimals = (byte)args[2];
                    byte[] addr     = (byte[])args[3];
                    string anchor   = (string)args[4];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(openSAR4B(name, symbol, decimals, addr, anchor));
                }

                /**An example of upgrade 'accept' method, the createSARBC interface should been
                 *  implemented in the following new SARBC contract
                 */
                if (operation == "createSAR4B")
                {
                    if (args.Length != 9)
                    {
                        return(false);
                    }
                    byte[]     addr      = (byte[])args[0];
                    byte[]     txid      = (byte[])args[1];
                    string     name      = (string)args[2];
                    string     symbol    = (string)args[3];
                    byte       decimals  = (byte)args[4];
                    BigInteger locked    = (BigInteger)args[5];
                    BigInteger hasDrawed = (BigInteger)args[6];
                    int        status    = (int)args[7];
                    string     anchor    = (string)args[8];

                    SARInfo sar = new SARInfo();
                    sar.symbol    = symbol;
                    sar.anchor    = anchor;
                    sar.name      = name;
                    sar.decimals  = decimals;
                    sar.locked    = locked;
                    sar.hasDrawed = hasDrawed;
                    sar.owner     = addr;
                    sar.status    = status;
                    sar.txid      = txid;

                    byte[] account = Storage.Get(Storage.CurrentContext, getAccountKey(STORAGE_ACCOUNT_OLD.AsByteArray()));
                    if (account.AsBigInteger() != callscript.AsBigInteger())
                    {
                        return(false);
                    }

                    return(createSAR4B(addr, sar));
                }
                //Migrate SAR account to new contract by owner of SAR
                if (operation == "migrateSAR4B")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    byte[] addr = (byte[])args[0];

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

                if (operation == "initToken")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    byte[] addr = (byte[])args[0];

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

                //lock SDS to SAR as collateral
                if (operation == "reserve")
                {
                    if (args.Length != 3)
                    {
                        return(false);
                    }
                    string     name  = (string)args[0];
                    byte[]     addr  = (byte[])args[1];
                    BigInteger value = (BigInteger)args[2];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(reserve(name, addr, value));
                }
                //issue stablecoin
                if (operation == "expande")
                {
                    if (args.Length != 3)
                    {
                        return(false);
                    }
                    string     name  = (string)args[0];
                    byte[]     addr  = (byte[])args[1];
                    BigInteger value = (BigInteger)args[2];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(expande(name, addr, value));
                }
                //payback stablecoin
                if (operation == "contract")
                {
                    if (args.Length != 3)
                    {
                        return(false);
                    }
                    string     name  = (string)args[0];
                    byte[]     addr  = (byte[])args[1];
                    BigInteger value = (BigInteger)args[2];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(contract(name, addr, value));
                }
                //get SDS from SAR
                if (operation == "withdraw")
                {
                    if (args.Length != 3)
                    {
                        return(false);
                    }
                    string     name  = (string)args[0];
                    byte[]     addr  = (byte[])args[1];
                    BigInteger value = (BigInteger)args[2];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(withdraw(name, addr, value));
                }

                if (operation == "claimRedeem")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    byte[] addr = (byte[])args[0];

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

                if (operation == "setAccount")
                {
                    if (args.Length != 2)
                    {
                        return(false);
                    }
                    string key     = (string)args[0];
                    byte[] address = (byte[])args[1];
                    //only committee account
                    if (!checkAdmin())
                    {
                        return(false);
                    }
                    return(setAccount(key, address));
                }
                //stablecoin holder can redeem locked SDS if this SAR is unsafe
                if (operation == "redeem")
                {
                    if (args.Length != 2)
                    {
                        return(false);
                    }
                    byte[] addr    = (byte[])args[0];
                    byte[] sarAddr = (byte[])args[1];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(redeem(addr, sarAddr));
                }

                if (operation == "settingSAR")
                {
                    if (args.Length != 2)
                    {
                        return(false);
                    }
                    string name = (string)args[0];
                    byte[] addr = (byte[])args[1];
                    //only committee account
                    if (!checkAdmin())
                    {
                        return(false);
                    }
                    return(settingSAR(name, addr));
                }

                if (operation == "setConfig")
                {
                    if (args.Length != 2)
                    {
                        return(false);
                    }
                    string     key   = (string)args[0];
                    BigInteger value = (BigInteger)args[1];
                    //only committee account
                    if (!checkAdmin())
                    {
                        return(false);
                    }
                    return(setConfig(key, value));
                }

                if (operation == "destory")
                {
                    if (args.Length != 2)
                    {
                        return(false);
                    }
                    string name = (string)args[0];
                    byte[] addr = (byte[])args[1];

                    if (!Runtime.CheckWitness(addr))
                    {
                        return(false);
                    }
                    return(destory(name, addr));
                }

                if (operation == "getConfig")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    string key = (string)args[0];

                    return(getConfig(key));
                }

                if (operation == "getSARTxInfo")
                {
                    if (args.Length != 1)
                    {
                        return(false);
                    }
                    byte[] txid = (byte[])args[0];
                    return(getSARTxInfo(txid));
                }
                #region contract upgrade
                if (operation == "upgrade")
                {
                    //only committee account
                    if (!checkAdmin())
                    {
                        return(false);
                    }

                    if (args.Length != 1 && args.Length != 9)
                    {
                        return(false);
                    }

                    byte[] script     = Blockchain.GetContract(ExecutionEngine.ExecutingScriptHash).Script;
                    byte[] new_script = (byte[])args[0];

                    if (script == new_script)
                    {
                        return(false);
                    }

                    byte[] parameter_list = new byte[] { 0x07, 0x10 };
                    byte   return_type    = 0x05;
                    //1|2|4
                    bool   need_storage = (bool)(object)07;
                    string name         = "business";
                    string version      = "1";
                    string author       = "alchemint";
                    string email        = "0";
                    string description  = "alchemint";

                    if (args.Length == 9)
                    {
                        parameter_list = (byte[])args[1];
                        return_type    = (byte)args[2];
                        need_storage   = (bool)args[3];
                        name           = (string)args[4];
                        version        = (string)args[5];
                        author         = (string)args[6];
                        email          = (string)args[7];
                        description    = (string)args[8];
                    }
                    Contract.Migrate(new_script, parameter_list, return_type, need_storage, name, version, author, email, description);
                    return(true);
                }
                #endregion
            }
            return(true);
        }
Пример #17
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);
        }
Пример #18
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);
        }
Пример #19
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);
        }
        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);
        }