public static bool openChannel(byte[] _openRequest)
        {
            byte[] tmpChannelId                     = getTmpChannelId();
            LedgerStruct.Channel       c            = LedgerStruct.getChannelMap(tmpChannelId);
            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile0 = peerProfiles[0];
            LedgerStruct.PeerProfile   peerProfile1 = peerProfiles[1];
            byte[][] peerAddrs =
            {
                peerProfile0.peerAddr,
                peerProfile1.peerAddr
            };
            BigInteger[] amounts =
            {
                peerProfile0.deposit,
                peerProfile1.deposit
            };

            PbEntity.TokenInfo token = c.token;
            LedgerStruct.setChannelMap(tmpChannelId, c);
            OpenChannelEvent(
                tmpChannelId,
                token.tokenType,
                token.address,
                peerAddrs,
                amounts
                );
            return(true);
        }
 public static LedgerStruct.ChannelMigrationArgs getChannelMigrationArgsInner(LedgerStruct.Channel _c)
 {
     LedgerStruct.ChannelMigrationArgs channelMigrationArgs = new LedgerStruct.ChannelMigrationArgs();
     channelMigrationArgs.disputeTimeout = _c.disputeTimeout;
     PbEntity.TokenInfo tokenInfo = _c.token;
     channelMigrationArgs.tokenType    = tokenInfo.tokenType;
     channelMigrationArgs.tokenAddress = tokenInfo.address;
     channelMigrationArgs.cooperativeWithdrawSeqNum = _c.cooperativeWithdrawSeqNum;
     _c.token = tokenInfo;
     return(channelMigrationArgs);
 }
    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 openChannelMockSet(byte[] _channelId, BigInteger _disputeTimeout, byte[] _tokenAddress, byte _tokenType, byte[][] _peerAddrs, BigInteger[] _deposits)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_tokenAddress), "_channelId illegal");
            BasicMethods.assert(_tokenType >= 0 && _tokenType <= 3, "_tokenType illegal");
            BasicMethods.assert(_peerAddrs.Length == 2, "_peerAddrs length illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerAddrs[0]), "_peerAddrs 0 illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_peerAddrs[1]), "_peerAddrs 1 illegal");
            BasicMethods.assert(_deposits.Length == 2, "_deposits length illegal");
            BasicMethods.assert(_deposits[0] >= 0, "_deposits 0 illegal");
            BasicMethods.assert(_deposits[1] >= 0, "_deposits 1 illegal");

            setTmpChannelId(_channelId);

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            c.disputeTimeout = _disputeTimeout;
            LedgerStruct.ChannelStatus channelStatus = LedgerStruct.getStandardChannelStatus();
            c = LedgerOperation._updateChannelStatus(c, channelStatus.Operable);
            PbEntity.TokenInfo token = new PbEntity.TokenInfo()
            {
                address   = _tokenAddress,
                tokenType = _tokenType
            };
            c.token = token;

            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile0 = peerProfiles[0];
            LedgerStruct.PeerProfile   peerProfile1 = peerProfiles[1];
            peerProfile0.peerAddr = _peerAddrs[0];
            peerProfile0.deposit  = _deposits[0];
            peerProfile1.peerAddr = _peerAddrs[1];
            peerProfile1.deposit  = _deposits[1];
            peerProfiles[0]       = peerProfile0;
            peerProfiles[1]       = peerProfile1;
            c.peerProfiles        = peerProfiles;
            LedgerStruct.setChannelMap(_channelId, c);
            return(true);
        }
        public static bool deposit(byte[] _channelId, byte[] _receiver, BigInteger _transferFromAmount)
        {
            BasicMethods.assert(BasicMethods._isByte32(_channelId), "_channelId illegal");
            BasicMethods.assert(BasicMethods._isLegalAddress(_receiver), "_receiver illegal");
            BasicMethods.assert(_transferFromAmount >= 0, "_transferFromAmount illegal");

            LedgerStruct.Channel c = LedgerStruct.getChannelMap(_channelId);
            byte rid = LedgerChannel._getPeerId(c, _receiver);

            BasicMethods.assert(rid == 0 || rid == 1, "rid illegal");
            PbEntity.TokenInfo token  = c.token;
            BigInteger         value  = LedgerStruct.getTransactionValue(token.tokenType, getCelerWallet());
            BigInteger         amount = _transferFromAmount + value;

            LedgerStruct.PeerProfile[] peerProfiles = c.peerProfiles;
            LedgerStruct.PeerProfile   peerProfile  = peerProfiles[rid];
            peerProfile.deposit = peerProfile.deposit + amount;
            peerProfiles[rid]   = peerProfile;
            c.peerProfiles      = peerProfiles;
            LedgerStruct.BalanceMap balanceMap = LedgerChannel.getBalanceMapInner(c);
            LedgerStruct.setChannelMap(_channelId, c);
            DepositEvent(_channelId, balanceMap.peerAddrs, balanceMap.deposits, balanceMap.withdrawals);
            return(true);
        }
 public static byte getTokenTypeInner(LedgerStruct.Channel _c)
 {
     PbEntity.TokenInfo tokenInfo = _c.token;
     return(tokenInfo.tokenType);
 }
 public static byte[] getTokenContractInner(LedgerStruct.Channel _c)
 {
     PbEntity.TokenInfo tokenInfo = _c.token;
     BasicMethods.assert(BasicMethods._isLegalAddress(tokenInfo.address), "address is illegal");
     return(tokenInfo.address);
 }