public static object withdraw(byte[] walletId, byte[] tokenAddress, byte[] receiver, BigInteger amount, byte[] callingScriptHash)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal, not byte32");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than zero");
            BasicMethods.assert(Runtime.CheckWitness(receiver), "CheckWitness failed");
            BigInteger[] tokenValues = getTokenWithdraw();
            if (tokenAddress.Equals(LedgerStruct.NeoAddress))
            {
                BasicMethods.assert(amount >= tokenValues[0], "withdrawn Neo is more than declared");
            }
            else if (tokenAddress.Equals(LedgerStruct.GasAddress))
            {
                BasicMethods.assert(amount >= tokenValues[1], "withdrawn Gas is more than declared");
            }

            _whenNotPaused();
            _onlyOperator(walletId, callingScriptHash);
            _onlyWalletOwner(walletId, receiver);

            BasicMethods.assert(_updateBalance(walletId, tokenAddress, amount, getStandardMathOperation().sub), "updateBalance failed");
            BasicMethods.assert(_withdrawToken(tokenAddress, receiver, amount), "withdrawToken failed");
            WithdrawFromWallet(walletId, tokenAddress, receiver, amount);
            return(true);
        }
        public static bool intendSettleMockSet(byte[] _channelId, byte[] _peerFrom, BigInteger _seqNum, BigInteger _transferOut, byte[] _nextPayIdListHash, BigInteger _lastPayResolveDeadline, BigInteger _pendingPayOut)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerFrom), "_peerFrom illegal");
            BasicMethods.assert(_seqNum >= 0, "_seqNum smaller than zero");
            BasicMethods.assert(_transferOut >= 0, "_transferOut smaller than zero");
            BasicMethods.assert(BasicMethods._isByte32(_nextPayIdListHash), "_nextPayIdListHash illegal");
            BasicMethods.assert(_lastPayResolveDeadline >= 0, "_lastPayResolveDeadline smaller than zero");
            BasicMethods.assert(_pendingPayOut >= 0, "_pendingPayOut smaller than zero");

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            byte peerFromId        = LedgerChannel._getPeerId(c, _peerFrom);

            BasicMethods.assert(peerFromId == 0 || peerFromId == 1, "peerFromId illegal");
            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile  = peerProfiles[peerFromId];
            LedgerStruct.PeerState     state        = peerProfile.state;

            state.seqNum                 = _seqNum;
            state.transferOut            = _transferOut;
            state.nextPayIdListHash      = _nextPayIdListHash;
            state.lastPayResolveDeadline = _lastPayResolveDeadline;
            state.pendingPayOut          = _pendingPayOut;
            peerProfile.state            = state;
            peerProfiles[peerFromId]     = peerProfile;
            c.peerProfiles               = peerProfiles;

            c = _updateOverallStatesByIntendState(c);
            LedgerStruct.setChannelMap(_channelId, c);
            setTmpChannelId(_channelId);
            return(true);
        }
Exemple #3
0
        public static bool deploy(byte[] avmCode, BigInteger nonce)
        {
            byte[] virtHashId     = SmartContract.Sha256(avmCode.Concat(nonce.AsByteArray()));
            byte[] storedVirtAddr = Storage.Get(Storage.CurrentContext, Virt2RealPrefix.Concat(virtHashId));
            BasicMethods.assert(storedVirtAddr.Length == 0, "currently stored real address is not empty");
            // now deploy the contract
            byte[]   parameter_list      = new byte[] { 0x07, 0x10 };
            byte     return_type         = 0x05;
            string   name                = "new contract";
            string   version             = "1";
            string   author              = "celer virtual contract resolver";
            string   email               = "empty";
            string   description         = "relay contract to deploy new contract to on-chain based on the avm/script code";
            Contract newDeployedContract = Contract.Create(avmCode, parameter_list, return_type, ContractPropertyState.HasStorage, name, version, author, email, description);

            //byte[] seemsToBeNewlyDeployedContractAddr = SmartContract.Hash160(avmCode);
            byte[] seemsToBeNewlyDeployedContractAddr = newDeployedContract.Script;

            BasicMethods.assert(BasicMethods._isLegalAddress(seemsToBeNewlyDeployedContractAddr), "Create contract failed");
            Storage.Put(Storage.CurrentContext, Virt2RealPrefix.Concat(virtHashId), seemsToBeNewlyDeployedContractAddr);

            Deploy(virtHashId);

            return(true);
        }
        public static object drainToken(byte[] invoker, byte[] tokenAddress, byte[] receiver, BigInteger amount)
        {
            BasicMethods.assert(Runtime.CheckWitness(invoker), "CheckWitness failed");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than zero");

            _whenPaused();
            _onlyPauser(invoker);

            BigInteger[] tokenValues = getTokenWithdraw();
            if (tokenAddress.Equals(LedgerStruct.NeoAddress))
            {
                BasicMethods.assert(amount >= tokenValues[0], "withdrawn Neo is more than declared");
            }
            else if (tokenAddress.Equals(LedgerStruct.GasAddress))
            {
                BasicMethods.assert(amount >= tokenValues[1], "withdrawn Gas is more than declared");
            }

            BasicMethods.assert(_withdrawToken(tokenAddress, receiver, amount), "withdrawToken failed");

            DrainToken(tokenAddress, receiver, amount);
            return(true);
        }
        public static byte[] create(byte[] invoker, byte[][] owners, byte[] theOperator, byte[] nonce)
        {
            BasicMethods.assert(Runtime.CheckWitness(invoker), "CheckWitness failed");
            BasicMethods.assert(BasicMethods._isLegalAddresses(owners), "owners addresses are not byte20");
            BasicMethods.assert(BasicMethods._isLegalAddress(theOperator), "the operator is not byte20");
            //TODO: no need to check the nonce byte[] length

            _whenNotPaused();

            BasicMethods.assert(BasicMethods._isLegalAddress(theOperator), "New operator is address zero");
            byte[] SelfContractHash = ExecutionEngine.ExecutingScriptHash;
            byte[] concatRes        = SelfContractHash.Concat(invoker).Concat(nonce);
            byte[] walletId         = SmartContract.Sha256(concatRes);
            BasicMethods.assert(getWallet(walletId).theOperator == null, "Occupied wallet id");
            Wallet w = new Wallet();

            BasicMethods.assert(BasicMethods._isLegalAddresses(owners), "owners contains illegal address");
            w.owners      = owners;
            w.theOperator = theOperator;
            BigInteger walletNum = Storage.Get(Storage.CurrentContext, WalletNum).AsBigInteger();

            Storage.Put(Storage.CurrentContext, WalletNum, (walletNum + 1).AsByteArray());
            Storage.Put(Storage.CurrentContext, WalletsPrefix.Concat(walletId), Helper.Serialize(w));
            CreateWallet(walletId, owners, theOperator);
            return(walletId);
        }
        public static bool snapshotStatesMockSet(byte[][] _channelIds, byte[][] _peerFroms, BigInteger[] _seqNums, BigInteger[] _transferOuts, BigInteger[] _pendingPayOuts)
        {
            BasicMethods.assert(_channelIds.Length == _peerFroms.Length &&
                                _peerFroms.Length == _seqNums.Length &&
                                _seqNums.Length == _transferOuts.Length &&
                                _transferOuts.Length == _pendingPayOuts.Length,
                                "Parameter length not the same");

            for (int i = 0; i < _channelIds.Length; i++)
            {
                BasicMethods.assert(BasicMethods._isByte32(_channelIds[i]), "_channelIds " + i + " illegal");
                BasicMethods.assert(BasicMethods._isLegalAddress(_peerFroms[i]), "_peerFroms " + i + " illegal");
                BasicMethods.assert(_seqNums[i] >= 0, "_seqNums " + i + " illegal");
                BasicMethods.assert(_transferOuts[i] >= 0, "_transferOuts " + i + " illegal");
                BasicMethods.assert(_pendingPayOuts[i] >= 0, "_pendingPayOuts " + i + " illegal");

                LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelIds[i]);
                byte peerFromId        = LedgerChannel._getPeerId(c, _peerFroms[i]);
                LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
                LedgerStruct.PeerProfile   peerProfile  = peerProfiles[peerFromId];
                LedgerStruct.PeerState     state        = peerProfile.state;
                state.seqNum             = _seqNums[i];
                state.transferOut        = _transferOuts[i];
                state.pendingPayOut      = _pendingPayOuts[i];
                peerProfile.state        = state;
                peerProfiles[peerFromId] = peerProfile;
                c.peerProfiles           = peerProfiles;
                LedgerStruct.setChannelMap(_channelIds[i], c);
            }
            setTmpChannelIdSet(_channelIds);
            return(true);
        }
        private static bool _updateBalance(byte[] _walletId, byte[] _tokenAddress, BigInteger _amount, byte _mathOperation)
        {
            BasicMethods.assert(_amount >= 0, "amount is less than zero");
            Wallet w = getWallet(_walletId);

            BasicMethods.assert(BasicMethods._isLegalAddress(w.theOperator), "wallet Object does not exist");
            byte[] wBalanceBs = Storage.Get(Storage.CurrentContext, WalletsBalancesPrefix.Concat(_walletId));
            Map <byte[], BigInteger> wBalanceMap = new Map <byte[], BigInteger>();

            if (wBalanceBs.Length > 0)
            {
                wBalanceMap = Helper.Deserialize(wBalanceBs) as Map <byte[], BigInteger>;
            }
            if (!wBalanceMap.HasKey(_tokenAddress))
            {
                wBalanceMap[_tokenAddress] = 0;
            }
            if (_mathOperation == getStandardMathOperation().add)
            {
                wBalanceMap[_tokenAddress] += _amount;
            }
            else if (_mathOperation == getStandardMathOperation().sub)
            {
                wBalanceMap[_tokenAddress] -= _amount;
                BasicMethods.assert(wBalanceMap[_tokenAddress] >= 0, "balance is less than zero");
            }
            else
            {
                BasicMethods.assert(false, "math operation illegal");
            }
            Storage.Put(Storage.CurrentContext, WalletsBalancesPrefix.Concat(_walletId), Helper.Serialize(wBalanceMap));
            return(true);
        }
        public static object transferToCelerWallet(byte[] invoker, byte[] from, byte[] walletAddr, byte[] walletId, BigInteger value)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(invoker), "invoker or spender address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(from), "from address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(walletAddr), "to address is illegal");
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId is not byte32");
            BasicMethods.assert(value >= 0, "amount is less than 0");

            BasicMethods.assert(Runtime.CheckWitness(invoker), "CheckWitness failed");

            BigInteger approvedBalance = allowance(from, invoker);

            BasicMethods.assert(value <= approvedBalance, "value is greater than allowance of spender allowed to spend");
            Storage.Put(Storage.CurrentContext, ApprovePrefix.Concat(from).Concat(invoker), approvedBalance - value);
            Approved(from, invoker, allowance(from, invoker));

            BigInteger fromBalance = balanceOf(from);

            BasicMethods.assert(value <= fromBalance, "value is greater than the owner's balance");
            Storage.Put(Storage.CurrentContext, BalancePrefix.Concat(from), fromBalance - value);
            Transferred(from, walletAddr, value);


            byte[] celerWalletHash      = Storage.Get(Storage.CurrentContext, CelerWalletHashKey);
            byte[] nep5Hash             = Storage.Get(Storage.CurrentContext, NEP5HashKey);
            DynamicCallContract dyncall = (DynamicCallContract)celerWalletHash.ToDelegate();

            BasicMethods.assert((bool)dyncall("depositNEP5", new object[] { invoker, walletId, nep5Hash, value }), "transfer NEP5 token to the to the celer wallet failed");

            return(true);
        }
 public static object init(byte[] revPayRegistryAddr, byte[] revVirtResolverAddr)
 {
     BasicMethods.assert(BasicMethods._isLegalAddress(revPayRegistryAddr), "invalid contract address");
     BasicMethods.assert(BasicMethods._isLegalAddress(revVirtResolverAddr), "invalid contract address");
     Storage.Put(Storage.CurrentContext, PayRegistryHashKey, revPayRegistryAddr);
     Storage.Put(Storage.CurrentContext, VirtResolverHashKey, revVirtResolverAddr);
     return(true);
 }
Exemple #10
0
 public static object setBalanceLimits(byte[][] tokenAddrs, BigInteger[] limits)
 {
     for (int i = 0; i < tokenAddrs.Length; i++)
     {
         BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddrs[i]), "Token address " + i + " is illegal");
         Storage.Put(Storage.CurrentContext, BalanceLimitsPrefix.Concat(tokenAddrs[i]), limits[i]);
     }
     return(true);
 }
 public static bool _isPeer(LedgerStruct.Channel _c, byte[] _addr)
 {
     BasicMethods.assert(BasicMethods._isLegalAddress(_addr), "_fromLedgerAddr parameter error");
     LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
     BasicMethods.assert(peerProfiles.Length == 2, "Illegal peerProfiles length");
     LedgerStruct.PeerProfile peer0 = peerProfiles[0];
     LedgerStruct.PeerProfile peer1 = peerProfiles[1];
     return(_addr.Equals(peer0.peerAddr) || _addr.Equals(peer1.peerAddr));
 }
        public static object transferOperatorship(byte[] walletId, byte[] newOperator, byte[] callingScriptHash)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal, not byte32");
            BasicMethods.assert(BasicMethods._isLegalAddress(newOperator), "newOperator address is illegal");
            // no need to checkwitness since _onlyOperator has already done it

            _whenNotPaused();
            _onlyOperator(walletId, callingScriptHash);
            _changeOperator(walletId, newOperator);
            return(true);
        }
        public static bool init(byte[] _payRegistryHash, byte[] _celerWalletHash)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(_payRegistryHash), "Pay registry contract hash illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_celerWalletHash), "celer wallet contract hash illegal");

            LedgerStruct.Ledger ledger = new LedgerStruct.Ledger();
            ledger.payRegistry = _payRegistryHash;
            ledger.celerWallet = _celerWalletHash;
            setLedger(ledger);
            LedgerBalanceLimit.enableBalanceLimitsInner();
            return(true);
        }
        public static bool refreshRouter(byte[] invoker)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(invoker), "invoker is illegal");
            BasicMethods.assert(Runtime.CheckWitness(invoker), "Checkwitness failed");
            BasicMethods.assert(Storage.Get(Storage.CurrentContext, routerInfoPrefix.Concat(invoker)).AsBigInteger() != 0, "Router address does not exist");
            Storage.Put(Storage.CurrentContext, routerInfoPrefix.Concat(invoker), Blockchain.GetHeight());

            RouterOperation operation = getRouterOperation();

            RouterUpdated(operation.Refresh, invoker);
            return(true);
        }
    public static LedgerStruct.Channel _addWithdrawal(LedgerStruct.Channel _c, byte[] _receiver, BigInteger _amount)
    {
        BasicMethods.assert(BasicMethods._isLegalAddress(_receiver), "_receiver is illegal");
        BasicMethods.assert(_amount >= 0, "_amount is negative");
        byte rid = _getPeerId(_c, _receiver);

        LedgerStruct.PeerProfile[] peerProfiles = _c.peerProfiles;
        LedgerStruct.PeerProfile   peerProfile  = peerProfiles[rid];
        peerProfile.withdrawal = peerProfile.withdrawal + _amount;
        peerProfiles[rid]      = peerProfile;
        BasicMethods.assert(getTotalBalanceInner(_c) >= 0, "Total balance is negative");
        return(_c);
    }
        public static BigInteger getBalance(byte[] walletId, byte[] tokenAddress)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            byte[] wBalanceBs = Storage.Get(Storage.CurrentContext, WalletsPrefix.Concat(WalletsBalancesPrefix).Concat(walletId));
            Map <byte[], BigInteger> wBalanceMap = Helper.Deserialize(wBalanceBs) as Map <byte[], BigInteger>;

            if (wBalanceMap.HasKey(tokenAddress))
            {
                return(wBalanceMap[tokenAddress]);
            }
            return(0);
        }
        private static void _changeOperator(byte[] _walletId, byte[] _newOperator)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(_newOperator), "new operator is illegal");
            Wallet w = getWallet(_walletId);

            byte[] oldOperator = w.theOperator;
            BasicMethods.assert(BasicMethods._isLegalAddress(oldOperator), "old operator is not legal");

            w.theOperator = _newOperator;

            Storage.Put(Storage.CurrentContext, WalletsPrefix.Concat(_walletId), Helper.Serialize(w));
            ChangeOperator(_walletId, oldOperator, _newOperator);
        }
        public static object increaseAllowance(byte[] owner, byte[] spender, BigInteger addValue)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(owner), "owner address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(spender), "spender address is illegal");
            BasicMethods.assert(addValue >= 0, "addValue is less than zero");

            BasicMethods.assert(Runtime.CheckWitness(owner), "Checkwitness failed");

            Storage.Put(Storage.CurrentContext, ApprovePrefix.Concat(owner).Concat(spender), allowance(owner, spender) + addValue);

            Approved(owner, spender, allowance(owner, spender));

            return(true);
        }
        public static object init(byte[] reversedNEP5Hash, BigInteger decimals, byte[] reversedCelerWalletHash)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(reversedNEP5Hash), "nep5 contract hash illegal");
            BasicMethods.assert(decimals >= 0, "decimals is less than 0");
            BasicMethods.assert(BasicMethods._isLegalAddress(reversedCelerWalletHash), "celer wallet contract hash illegal");

            BasicMethods.assert(Runtime.CheckWitness(Admin), "is not initialized by admin");

            Storage.Put(Storage.CurrentContext, NEP5HashKey, reversedNEP5Hash);
            Storage.Put(Storage.CurrentContext, DecimalsKey, decimals);
            Storage.Put(Storage.CurrentContext, CelerWalletHashKey, reversedCelerWalletHash);

            //TODO notify the event
            return(true);
        }
        public static object drainToken(byte[] invoker, byte[] tokenAddress, byte[] receiver, BigInteger amount)
        {
            BasicMethods.assert(Runtime.CheckWitness(invoker), "CheckWitness failed");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than zero");

            _whenPaused();
            _onlyPauser(invoker);

            BasicMethods.assert(_withdrawToken(tokenAddress, receiver, amount), "withdrawToken failed");

            DrainToken(tokenAddress, receiver, amount);
            return(true);
        }
        public static object approve(byte[] owner, byte[] spender, BigInteger value)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(owner), "owner address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(spender), "spender address is illegal");
            BasicMethods.assert(value >= 0, "amount is less than zero");

            BasicMethods.assert(Runtime.CheckWitness(owner), "Checkwitness failed");

            BasicMethods.assert(value <= balanceOf(owner), "value is greater than balance of owner");

            Storage.Put(Storage.CurrentContext, ApprovePrefix.Concat(owner).Concat(spender), value);

            Approved(owner, spender, value);

            return(true);
        }
        public static object withdraw(byte[] withdrawer, BigInteger value)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(withdrawer), "withdrawer address is illegal");
            BasicMethods.assert(value >= 0, "amount is less than zero");

            BasicMethods.assert(Runtime.CheckWitness(withdrawer), "Checkwitness failed");

            byte[] nep5Hash             = Storage.Get(Storage.CurrentContext, NEP5HashKey);
            DynamicCallContract dyncall = (DynamicCallContract)nep5Hash.ToDelegate();

            BasicMethods.assert((BigInteger)dyncall("balanceOf", new object[] { ExecutionEngine.ExecutingScriptHash }) >= value, "the contract accout nep5 balance not enough");
            BasicMethods.assert(balanceOf(withdrawer) >= value, "withdrawer does not have enough balance");

            BasicMethods.assert(_transfer(withdrawer, withdrawer, value), "withdraw nep5 token failed");
            return(true);
        }
    public static LedgerStruct.Channel _importChannelMigrationArgs(LedgerStruct.Channel _c, byte[] _fromLedgerAddr, byte[] _channelId)
    {
        BasicMethods.assert(BasicMethods._isLegalAddress(_fromLedgerAddr), "invalid contract address");
        BasicMethods.assert(BasicMethods._isByte32(_channelId), "invalid _channelId");

        DynamicCallContract dyncall = (DynamicCallContract)_fromLedgerAddr.ToDelegate();

        LedgerStruct.ChannelMigrationArgs args = (LedgerStruct.ChannelMigrationArgs)dyncall("getChannelMigrationArgs", new object[] { _channelId });
        _c.disputeTimeout = args.disputeTimeout;
        PbEntity.TokenInfo token = new PbEntity.TokenInfo();
        token.tokenType = args.tokenType;
        token.address   = args.tokenAddress;
        _c.token        = token;
        _c.cooperativeWithdrawSeqNum = args.cooperativeWithdrawSeqNum;
        return(_c);
    }
        public static bool getProposalVote(byte[] walletId, byte[] owner)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(owner), "owner address is not length of 20 bytes");
            _onlyWalletOwner(walletId, owner);
            // wpvBs means Wallet Proposal Votes ByteS
            byte[]             wpvBs = Storage.Get(Storage.CurrentContext, WalletsProposalVotesPrefix.Concat(walletId));
            Map <byte[], bool> wpv   = new Map <byte[], bool>();

            if (wpvBs.Length > 0)
            {
                wpv = Helper.Deserialize(wpvBs) as Map <byte[], bool>;
                return(wpv[owner]);
            }
            return(false);
        }
        public static object deposit(byte[] depositer, byte[] receiver, BigInteger amount)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(depositer), "depositer address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than 0");

            BasicMethods.assert(Runtime.CheckWitness(depositer), "Checkwitness failed");

            byte[] nep5Hash             = Storage.Get(Storage.CurrentContext, NEP5HashKey);
            DynamicCallContract dyncall = (DynamicCallContract)nep5Hash.ToDelegate();

            BasicMethods.assert((bool)dyncall("transfer", new object[] { depositer, ExecutionEngine.ExecutingScriptHash, amount }), "transfer NEP5 token to the contract failed");
            Storage.Put(Storage.CurrentContext, BalancePrefix.Concat(receiver), balanceOf(receiver) + amount);

            DepositEvent(receiver, amount);
            return(true);
        }
        public static object withdraw(byte[] walletId, byte[] tokenAddress, byte[] receiver, BigInteger amount)
        {
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId illegal, not byte32");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount > 0, "amount is less than zero");
            //assert(Runtime.CheckWitness(receiver), "CheckWitness failed");

            _whenNotPaused();
            _onlyOperator(walletId);
            _onlyWalletOwner(walletId, receiver);

            BasicMethods.assert(_updateBalance(walletId, tokenAddress, amount, "sub"), "updateBalance failed");
            BasicMethods.assert(_withdrawToken(tokenAddress, receiver, amount), "withdrawToken failed");
            WithdrawFromWallet(walletId, tokenAddress, receiver, amount);
            return(true);
        }
Exemple #27
0
        public static object transfer(byte[] from, byte[] to, BigInteger amount, byte[] callingScriptHash)
        {
            BasicMethods.assert(BasicMethods._isLegalAddress(from), "from address is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(to), "to address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than 0");
            BasicMethods.assert(Runtime.CheckWitness(from) || callingScriptHash.Equals(from), "CheckWitness failed");

            BigInteger fromBalance = balanceOf(from);

            BasicMethods.assert(fromBalance >= amount, "from address not enough balance");
            Storage.Put(Storage.CurrentContext, BalancePrefix.Concat(from), fromBalance - amount);
            BigInteger toBalance = balanceOf(to);

            Storage.Put(Storage.CurrentContext, BalancePrefix.Concat(to), toBalance + amount);

            Transferred(from, to, amount);
            return(true);
        }
        public static bool intendWithdrawMockSet(byte[] _channelId, BigInteger _amount, byte[] _recipientChannelId, byte[] _receiver)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(_amount >= 0, "_amount smaller than zero");
            BasicMethods.assert(BasicMethods._isByte32(_recipientChannelId), "_recipientChannelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_receiver), "_receiver illegal");

            LedgerStruct.Channel        c = LedgerStruct.getChannelMap(_channelId);
            LedgerStruct.WithdrawIntent withdrawIntent = c.withdrawIntent;

            withdrawIntent.receiver           = _receiver;
            withdrawIntent.amount             = _amount;
            withdrawIntent.requestTime        = Blockchain.GetHeight();
            withdrawIntent.recipientChannelId = _recipientChannelId;
            c.withdrawIntent = withdrawIntent;
            setTmpChannelId(_channelId);
            LedgerStruct.setChannelMap(_channelId, c);
            return(true);
        }
        public static object depositNEP5(byte[] invoker, byte[] walletId, byte[] tokenAddress, BigInteger amount)
        {
            BasicMethods.assert(Runtime.CheckWitness(invoker), "CheckWitness failed");
            BasicMethods.assert(BasicMethods._isByte32(walletId), "walletId is not byte32");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is not byte20");
            BasicMethods.assert(amount >= 0, "amount is less than zero");

            _whenNotPaused();

            BasicMethods.assert(_updateBalance(walletId, tokenAddress, amount, getStandardMathOperation().add), "updateBalance failed");
            byte[]       thisContract = ExecutionEngine.ExecutingScriptHash;
            NEP5Contract dyncall      = (NEP5Contract)tokenAddress.ToDelegate();
            bool         res          = (bool)dyncall("transfer", new object[] { invoker, thisContract, amount });

            BasicMethods.assert(res, "transfer NEP5 tokens failed");

            DepositToWallet(walletId, tokenAddress, amount);
            return(true);
        }
        public static object transferToWallet(byte[] fromWalletId, byte[] toWalletId, byte[] tokenAddress, byte[] receiver, BigInteger amount, byte[] callingScriptHash)
        {
            BasicMethods.assert(BasicMethods._isByte32(fromWalletId), "fromWalletId illegal, not byte32");
            BasicMethods.assert(BasicMethods._isByte32(toWalletId), "toWalletId illegal, not byte32");
            BasicMethods.assert(BasicMethods._isLegalAddress(tokenAddress), "tokenAddress is illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(receiver), "receiver address is illegal");
            BasicMethods.assert(amount >= 0, "amount is less than zero");
            BasicMethods.assert(Runtime.CheckWitness(receiver), "CheckWitness failed");

            _whenNotPaused();
            _onlyOperator(fromWalletId, callingScriptHash);
            _onlyWalletOwner(fromWalletId, receiver);
            _onlyWalletOwner(toWalletId, receiver);

            BasicMethods.assert(_updateBalance(fromWalletId, tokenAddress, amount, getStandardMathOperation().sub), "sub balance in from wallet failed");
            BasicMethods.assert(_updateBalance(toWalletId, tokenAddress, amount, getStandardMathOperation().add), "add balance in to wallet failed");

            TransferToWallet(fromWalletId, toWalletId, tokenAddress, receiver, amount);
            return(true);
        }