示例#1
0
        private static bool WithdrawalAll(byte[] locker, byte[] assetId, byte[] address, BigInteger isGlobal)
        {
            if (address.Length != 20)
            {
                return(false);
            }

            byte[] from = ExecutionEngine.ExecutingScriptHash;

            BigInteger balance = GetBalance(locker, assetId);

            bool success = false;

            if (isGlobal == 1)
            {
                success = NativeAsset.Call("TransferApp", assetId, from, address, balance);
            }
            else
            {
                var args     = new object[] { from, address, balance };
                var contract = (NEP5Contract)assetId.ToDelegate();
                success = (bool)contract("transferApp", args);
            }

            if (success)
            {
                ReduceBalance(locker, assetId, balance);
                EmitUnlocked(locker, assetId, address, balance);
                return(true);
            }
            else
            {
                throw new Exception("Failed to withdrawal transfer");
            }
        }
示例#2
0
        private static bool Send(byte[] txid, byte[] originator, byte[] assetId, BigInteger amount)
        {
            if (assetId.Length != 20)
            {
                return(false);
            }
            if (originator.Length != 20)
            {
                return(false);
            }
            if (amount <= 0)
            {
                return(false);
            }

            var txidUsedKey = TxidUsedKey(txid);
            var txidIsUsed  = Storage.Get(Context(), txidUsedKey);

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

            var twoLevelAmount = GetTwoLevelAmount();

            if (amount >= twoLevelAmount)
            {
                var twoLevelMutiSign = GetTwoLevelMutiSign();
                if (!Runtime.CheckWitness(twoLevelMutiSign))
                {
                    return(false);
                }
            }
            else
            {
                var oneLevelMutiSign = GetOneLevelMutiSign();
                if (!Runtime.CheckWitness(oneLevelMutiSign))
                {
                    return(false);
                }
            }

            bool success = false;

            byte[] from = ExecutionEngine.ExecutingScriptHash;

            success = NativeAsset.Call("TransferApp", assetId, from, originator, amount);

            if (success)
            {
                Storage.Put(Context(), txidUsedKey, 1);

                EmitSend(txid, originator, assetId, amount);
                return(true);
            }
            else
            {
                throw new Exception("Failed to withdrawal transfer");
            }
        }
示例#3
0
        private static bool Lock(byte[] assetId, byte[] from, BigInteger value)
        {
            if (!Runtime.CheckWitness(from))
            {
                return(false);
            }
            if (assetId.Length != 20)
            {
                return(false);
            }
            if (value <= 0)
            {
                return(false);
            }

            byte[] to      = ExecutionEngine.ExecutingScriptHash;
            bool   success = false;

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

            if (success)
            {
                EmitLocked(from, assetId, value);
                return(true);
            }
            else
            {
                throw new Exception("Failed to TransferFrom");
            }
        }
示例#4
0
        /***********
        * Withdrawal *
        ***********/
        private static bool Withdrawal(byte[] originator, byte[] assetId, BigInteger amount, BigInteger isGlobal)
        {
            if (!Runtime.CheckWitness(originator))
            {
                return(false);
            }

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

            var originatorBalance = GetAvailabelBalance(originator, assetId);

            if (originatorBalance < amount)
            {
                return(false);
            }

            bool success = false;

            byte[] from = ExecutionEngine.ExecutingScriptHash;

            if (isGlobal == 1)
            {
                success = NativeAsset.Call("TransferApp", assetId, from, originator, amount);
            }
            if (isGlobal == 0)
            {
                var args     = new object[] { from, originator, amount };
                var contract = (NftContract)assetId.ToDelegate();
                success = (bool)contract("transferApp", args);
            }

            if (success)
            {
                ReduceBalance(originator, assetId, amount);

                ReduceAvailabelBalance(originator, assetId, amount);

                EmitWithdrawn(originator, assetId, amount);
                return(true);
            }
            else
            {
                throw new Exception("Failed to withdrawal transfer");
            }
        }
示例#5
0
        /***********
        * Deposit *
        ***********/
        private static bool Deposit(byte[] originator, byte[] assetId, BigInteger value, BigInteger isGlobal)
        {
            if (!Runtime.CheckWitness(originator))
            {
                return(false);
            }

            // Check that the contract is safe
            if (!GetIsWhitelisted(assetId))
            {
                return(false);
            }

            byte[] to      = ExecutionEngine.ExecutingScriptHash;
            bool   success = false;

            //全局资产 native nep5
            if (isGlobal == 1)
            {
                success = NativeAsset.Call("TransferFrom", assetId, originator, to, value);
            }
            if (isGlobal == 0)
            {
                var args     = new object[] { originator, to, value };
                var contract = (NftContract)assetId.ToDelegate();
                success = (bool)contract("transferFrom", args);
            }
            if (success)
            {
                IncreaseBalance(originator, assetId, value);

                IncreaseAvailableBalance(originator, assetId, value);

                EmitDepositted(originator, assetId, value);
                return(true);
            }
            else
            {
                throw new Exception("Failed to transferFrom");
            }
        }
示例#6
0
        public static object Main(string method, object[] args)
        {
            var magicstr = "Test_Contract_v0.35";

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

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

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

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

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

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

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

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

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

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

                    BigInteger value = (BigInteger)args[3];

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

                    Runtime.Notify(1, aa);

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

                    Runtime.Notify(1, ba);
                }

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

                    BigInteger value = (BigInteger)args[3];

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

                    Runtime.Notify(1, success);

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

                    Runtime.Notify(1, ba);
                }

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

                    byte[] from = ExecutionEngine.ExecutingScriptHash;

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

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

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

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

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

            return(false);
        }
示例#7
0
        private static bool Resonance(byte[] address, byte[] assetId, BigInteger value)
        {
            // Check that the contract is safe
            if (!GetIsWhitelisted(assetId))
            {
                return(false);
            }

            var bcpHash = new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

            byte[] contract            = ExecutionEngine.ExecutingScriptHash;
            bool   transferFromSuccess = false;

            //转入 BTC/NEO 等
            var args     = new object[] { address, contract, value };
            var deleCall = (deleContract)assetId.ToDelegate();

            transferFromSuccess = (bool)deleCall("transferFrom", args);

            if (transferFromSuccess)
            {
                //共振池总层数
                BigInteger allLayer = 3756;
                //每一层 BTC 数
                BigInteger perLayerNum = 10 * 100000000;

                //na1+n(n-1)d/2
                //BigInteger allAmount = allLayer * 10 + allLayer * (allLayer - 1) * 10 / 2;

                var balanceBytes = NativeAsset.Call("BalanceOf", bcpHash, contract);

                //共振池 BCP 余额
                BigInteger balance = balanceBytes.AsBigInteger();

                if (balance <= 0)
                {
                    throw new Exception("Resonance balance not enough!");
                }

                //当前共振池所在层
                BigInteger curLayer = Storage.Get(Context(), "curLayer").AsBigInteger();
                if (curLayer == 0)
                {
                    curLayer = allLayer;
                }
                BigInteger newLayer = curLayer;

                //可兑换 BCP 数量
                BigInteger amount;

                //当前层 BCP 余额 = 共振池余额 - 当前层以下所有层的总和
                BigInteger curLayerBalance = balance - ((curLayer - 1) * perLayerNum + (curLayer - 1) * (curLayer - 2) * perLayerNum / 2);

                if (curLayerBalance <= 0)
                {
                    throw new Exception("Resonance balance not enough!");
                }

                //当前层可供兑换的 BTC 额度
                BigInteger curLayerBtcLines = curLayerBalance / curLayer;

                //当前层不够兑换
                if (value >= curLayerBtcLines)
                {
                    //池子中代币不足
                    if (value > curLayerBtcLines && newLayer <= 1)
                    {
                        throw new Exception("Resonance balance not enough!");
                    }

                    amount = curLayerBalance;

                    //未兑换的 BTC
                    BigInteger remainingBtc = value - curLayerBtcLines;
                    BigInteger n            = remainingBtc / perLayerNum;

                    //10 的整数倍部分处理,10 个兑换一层
                    for (int i = 1; i <= n; i++)
                    {
                        amount += perLayerNum * (curLayer - i);
                    }

                    //整除 10 剩余部分
                    amount += (remainingBtc - n * perLayerNum) * (curLayer - n - 1);

                    //更新当前层高度
                    newLayer = curLayer - n - 1;
                }

                //当前层内足够兑换
                else
                {
                    amount = curLayer * value;
                }

                if (amount <= 0)
                {
                    throw new Exception("Invalid available amount!");
                }

                bool TransferAppSuccess = NativeAsset.Call("TransferApp", bcpHash, contract, address, amount);

                if (TransferAppSuccess)
                {
                    Storage.Put(Context(), "curLayer", newLayer);
                    EmitResonance(address, assetId, value, amount);
                    return(true);
                }

                else
                {
                    throw new Exception("Invalid available balance!");
                }
            }

            return(false);
        }