private static object WrapperLock(byte[] keyHash, byte[] from, byte[] to, BigInteger amount, byte[] userEthAddress, BigInteger overtimeBlocks)
        {
            // params length check
            if (keyHash.Length <= 0 || from.Length != 20)
            {
                return("code:0, msg:The parameters error");
            }

            // Whether keyHash already exists
            StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
            var        result   = SWAP_MAP.Get(keyHash);

            if (result.Length != 0)
            {
                return("code:0, msg:The keyHash already exists.");
            }

            // check overtime blocks
            if (overtimeBlocks < ERC20ToNEP5OvertimeBlocks)
            {
                overtimeBlocks = ERC20ToNEP5OvertimeBlocks;
            }

            // transfer qlc from wrapper to contract
            object[] transferParams = new object[3];
            transferParams[0] = from;
            transferParams[1] = to;
            transferParams[2] = amount;
            if (QlcMain("transfer", transferParams))
            {
                // swap info
                var swapInfo = new SwapInfo()
                {
                    wrapperNeoAddress = from,
                    userEthAddress    = userEthAddress,
                    amount            = amount,
                    lockTimestamp     = Blockchain.GetBlock(Blockchain.GetHeight()).Timestamp,
                    blockHeight       = Blockchain.GetHeight(),
                    overtimeBlocks    = overtimeBlocks,
                    type  = 2,
                    state = 4
                };

                var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
                swapInfo.txidIn = txid;
                SWAP_MAP.Put(keyHash, Helper.Serialize(swapInfo));

                // event
                WrapperLockEvent(keyHash, 3);
                return(txid);
            }
            else
            {
                return("code:0, msg:transfer error.");
            }
        }
Exemple #2
0
        private static object Unlock(byte[] from, byte[] to, BigInteger amount, byte[] userEthAddress, byte[] ethTxid)
        {
            // params length check
            if (to.Length != 20 || ethTxid.Length == 0)
            {
                return("code:0, msg:The parameters error");
            }

            // amount check
            if (amount <= MinSwapAmount)
            {
                return("code:0, msg:The number of transfers cannot be less than 0");
            }

            // Whether ethTxid already exists
            StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
            var        result   = SWAP_MAP.Get(ethTxid);

            if (result.Length != 0)
            {
                return("code:0, msg:The ethTxid already exists.");
            }

            // transfer qlc from user to contract
            object[] transferParams = new object[3];
            transferParams[0] = from;
            transferParams[1] = to;
            transferParams[2] = amount;
            if (QlcMain("transfer", transferParams))
            {
                // swap info
                var swapInfo = new SwapInfo()
                {
                    fromAddress    = from,
                    toAddress      = to,
                    amount         = amount,
                    userEthAddress = userEthAddress,
                    timestamp      = Blockchain.GetBlock(Blockchain.GetHeight()).Timestamp,
                    blockHeight    = Blockchain.GetHeight(),
                    type           = 2
                };

                var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
                swapInfo.txid = txid;
                SWAP_MAP.Put(ethTxid, Helper.Serialize(swapInfo));

                // event
                UnlockEvent(ethTxid, 2);
                return(txid);
            }
            else
            {
                return("code:0, msg:transfer error.");
            }
        }
        private static object UserUnlock(string key, byte[] from, byte[] to)
        {
            // params length check
            if (key.Length <= 0 || to.Length != 20)
            {
                return("code:0, msg:The parameters error.");
            }

            // get swap info
            var        keyHash  = SmartContract.Sha256(key.AsByteArray());
            StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
            var        result   = SWAP_MAP.Get(keyHash);

            if (result.Length == 0)
            {
                return("code:0, msg:The swap info not found.");
            }
            SwapInfo swapInfo = Helper.Deserialize(result) as SwapInfo;

            // timeout check
            if (Blockchain.GetHeight() - swapInfo.blockHeight > swapInfo.overtimeBlocks)
            {
                return("code:0, msg:The swap has timed out.");
            }

            // state check
            if (4 != swapInfo.state)
            {
                return("code:0, msg:The state doesn't meet the criteria.");
            }

            // transfer qlc from contract to user
            object[] transferParams = new object[3];
            transferParams[0] = from;
            transferParams[1] = to;
            transferParams[2] = swapInfo.amount;
            if (QlcMain("transfer", transferParams))
            {
                var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
                swapInfo.originText      = key;
                swapInfo.userNeoAddress  = to;
                swapInfo.txidOut         = txid;
                swapInfo.unLockTimestamp = Blockchain.GetBlock(Blockchain.GetHeight()).Timestamp;
                swapInfo.state           = 5;
                SWAP_MAP.Put(keyHash, Helper.Serialize(swapInfo));

                // event
                UserUnlockEvent(keyHash, 4);
                return(txid);
            }
            else
            {
                return("code:0, msg:transfer qlc to wrapper fail.");
            }
        }
        private static object RefundWrapper(byte[] keyHash, byte[] from)
        {
            // params length check
            if (keyHash.Length <= 0)
            {
                return("code:0, msg:The parameters error.");
            }

            // get swap info
            StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
            var        result   = SWAP_MAP.Get(keyHash);

            if (result.Length == 0)
            {
                return("code:0, msg:The swap info not found.");
            }
            SwapInfo swapInfo = Helper.Deserialize(result) as SwapInfo;

            // timeout check
            if (Blockchain.GetHeight() - swapInfo.blockHeight <= swapInfo.overtimeBlocks)
            {
                return("code:0, msg:No timeout.");
            }

            // state check
            if (4 != swapInfo.state)
            {
                return("code:0, msg:The state doesn't meet the criteria.");
            }

            // refund qlc from contract to wrapper
            object[] transferParams = new object[3];
            transferParams[0] = from;
            transferParams[1] = swapInfo.wrapperNeoAddress;
            transferParams[2] = swapInfo.amount;
            if (QlcMain("transfer", transferParams))
            {
                var txid = (ExecutionEngine.ScriptContainer as Transaction).Hash;
                swapInfo.txidRefund      = txid;
                swapInfo.refundTimestamp = Blockchain.GetBlock(Blockchain.GetHeight()).Timestamp;
                swapInfo.state           = 6;
                SWAP_MAP.Put(keyHash, Helper.Serialize(swapInfo));

                // event
                RefundWrapperEvent(keyHash, 5);
                return(txid);
            }
            else
            {
                return("code:0, msg:Refund qlc to wrapper fail.");
            }
        }
        private static object DeleteSwapInfo(byte[] keyHash)
        {
            // get swap info
            StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
            var        result   = SWAP_MAP.Get(keyHash);

            if (result.Length == 0)
            {
                return("code:0, msg:The swap info not found.");
            }
            SwapInfo swapInfo = Helper.Deserialize(result) as SwapInfo;

            // state check
            if (swapInfo.state == 1 || swapInfo.state == 4)
            {
                return("code:0, msg:Not completed swap info.");
            }
            SWAP_MAP.Delete(keyHash);
            return(true);
        }
Exemple #6
0
        public static object Main(string operation, object[] args)
        {
            if (Runtime.Trigger == TriggerType.Verification)
            {
                return(Runtime.CheckWitness(GetOwner()));
            }
            else if (Runtime.Trigger == TriggerType.Application)
            {
                byte[] executingScriptHash = ExecutionEngine.ExecutingScriptHash;

                // Add the code associated with the contract invocation here
                switch (operation)
                {
                case "name": return("Qlc Swap");

                case "lock":
                    if (args.Length != 3)
                    {
                        return("code:0, msg:The lock method need three parameters.");
                    }
                    return(Lock((byte[])args[0], executingScriptHash, (BigInteger)args[1], (byte[])args[2]));

                case "init":
                    return(Init());

                case "querySwapInfo":
                {
                    if (args.Length != 1)
                    {
                        return("code:0, msg:The querySwapInfo method need one parameter.");
                    }
                    StorageMap SWAP_MAP = Storage.CurrentContext.CreateMap(nameof(SWAP_MAP));
                    var        result   = SWAP_MAP.Get((byte[])args[0]);
                    if (result.Length == 0)
                    {
                        return("code:0, msg:The swap info not found.");
                    }
                    SwapInfo swapInfo = Helper.Deserialize(result) as SwapInfo;
                    return(swapInfo);
                };

                case "getOwner":
                    return(GetOwner());
                }

                // owner check
                if (!Runtime.CheckWitness(GetOwner()))
                {
                    return("code:0, msg:Only owner.");
                }

                switch (operation)
                {
                case "unlock":
                    if (args.Length != 4)
                    {
                        return("code:0, msg:The unlock method need four parameters.");
                    }
                    return(Unlock(executingScriptHash, (byte[])args[0], (BigInteger)args[1], (byte[])args[2], (byte[])args[3]));

                case "transferOwner":
                    if (args.Length != 1)
                    {
                        return("code:0, msg:The transferOwner method need one parameter.");
                    }
                    return(TransferOwner((byte[])args[0]));
                }
            }
            return(false);
        }