def sync(self):
        """Synchronize ORV address(wallet) on bitcoin blockchain with local database.
            Normally runs be scheduler.
            Step 1: Get wallet by address from database
            Step 2: Check wallet balance via bitcoin client
            Step 3: if balance changed - update it locally (otherwise just update updated_at field)
            Step 4: if balance changed - trigger appropriate smart contract via ETH client

        """
        addr = session.query(Address).filter_by(address=self.address).first()
        if addr:
            bclient = BitcoinClient()
            blockchain_address = BTCAddress(bclient, self.address)
            # TODO: Uncomment once on mainnet, checking mainnet address on dev-btc-node fails
            # if not blockchain_address.is_valid():
            #     raise ValueError("bitcoin address %s is not valid in the blockchain" % self.address)

            if int(os.environ['ETHEREUM_TESTRPC_ENABLED']) == 1:
                balance = blockchain_address.balance('test')
                # balance = blockchain_address.balance()
            else:
                balance = blockchain_address.balance()

            transaction = None
            if balance != addr.balance:
                persisted_contract = ContractService.find('PricingStrategy')
                if persisted_contract is None:
                    raise RuntimeError(
                        'PricingStrategy contract is not available in the database'
                    )

                contract_address = persisted_contract.address
                contract_abi = json.loads(persisted_contract.abi)

                # TODO: Remove this hack once we get rid of testrpc
                if int(os.environ['ETHEREUM_TESTRPC_ENABLED']) == 1:
                    executor_address = os.environ[
                        'ETHEREUM_TESTRPC_MASTER_ADDRESS']
                    eclient = EthereumClient('testrpc')
                else:
                    executor_address = os.environ['ETHEREUM_CONTRACT_EXECUTOR']
                    eclient = EthereumClient()

                contract = PricingStrategyContract(eclient, contract_abi,
                                                   contract_address)
                transaction = contract.set_available_satoshi(
                    balance, executor_address)

            addr.balance = balance
            session.commit()
            return transaction
        else:
            raise ValueError("bitcoin address %s not found in the database" %
                             self.address)
    def email_login(email, password):
        query = session.query(User).filter(User.email == email)
        user = query.one_or_none()
        if not user:
            # Probably we could raise error here: e-mail not found
            return None
        hashed_password = User.encode_password(password)
        if hashed_password == user.password_hash:
            return user

        return None
    def check_for_updates(cls):
        """Spawn synchronization of each ORV bitcoin wallet

            Runs periodically:
                Step 1: Get all ORV wallets from database
                Step 2: Spawn sync_orv() job for each wallet

        """
        query = session.query(Address).filter(
            Address.wallet_type == 'orv').order_by(Address.id)
        addresses = query.all()
        results = []
        for item in addresses:
            result = sync_orv_wallet.delay(item.address)
            results.append(result)
        return results
    def check_for_updates(cls):
        """Spawn synchronization of each user bitcoin wallet

            Runs periodically:
                Step 1: Get all users bitcoin wallets
                Step 2: Spawn sync_user_bitcoins() job for each wallet

        """
        query = session.query(Address). \
            filter(Address.wallet_type == 'user'). \
            filter(Address.currency == 'bitcoin'). \
            order_by(Address.id)

        addresses = query.all()
        results = []
        for item in addresses:
            result = sync_user_wallet.delay(item.address)
            results.append(result)
        return results
 def find(name):
     query = session.query(Contract).filter(Contract.name == name)
     contract = query.one_or_none()
     return contract
 def update_password(self, password):
     user = session.query(User).get(self.id)
     user.password_hash = User.encode_password(password)
     session.commit()
     return user
 def delete(self):
     user = session.query(User).get(self.id)
     session.delete(user)
     session.commit()
 def find(self):
     user = session.query(User).get(self.id)
     return user
 def find_all():
     query = session.query(User)
     users = query.all()
     return users
    def sync(self):
        """Synchronize user address(wallet) on bitcoin blockchain with local database.
            Normally runs by scheduler.
            Step 1: Step 1: get wallet by address from database
            Step 2: Check wallet balance via bitcoin client
            Step 3: if balance > 0 - transfer all funds to ORV wallet
            Step 4: if balance > 0 - trigger appropriate smart contract via ETH client

        """
        addr = session.query(Address).filter_by(address=self.address).first()
        if addr:
            bclient = BitcoinClient()
            blockchain_address = BTCAddress(bclient, self.address)
            if not blockchain_address.is_valid():
                raise ValueError(
                    "bitcoin address %s is not valid in the blockchain" %
                    self.address)

            try:
                balance = blockchain_address.balance()
                if balance > 0:
                    to_address = os.environ['BITCOIN_ORV_WALLET']
                    # Send all money to ORV wallet
                    blockchain_address.send(to_address, balance)

                    # Step 1: get user of the address
                    # Step 2: get ethereum wallet of the user
                    # Step 3: Run contract transfer_to
                    user = addr.user
                    for user_address in user.addresses:
                        # Assuming user could have just one ethereum wallet
                        if user_address.currency == 'ethereum':
                            persisted_contract = ContractService.find(
                                'PricingStrategy')
                            if persisted_contract is None:
                                raise RuntimeError(
                                    'PricingStrategy contract is not available in the database'
                                )

                            contract_address = persisted_contract.address
                            contract_abi = json.loads(persisted_contract.abi)

                            # TODO: Remove this hack once we get rid of testrpc
                            if int(os.environ['ETHEREUM_TESTRPC_ENABLED']
                                   ) == 1:
                                executor_address = os.environ[
                                    'ETHEREUM_TESTRPC_MASTER_ADDRESS']
                                eclient = EthereumClient('testrpc')
                            else:
                                executor_address = os.environ[
                                    'ETHEREUM_CONTRACT_EXECUTOR']
                                eclient = EthereumClient()

                            contract = PricingStrategyContract(
                                eclient, contract_abi, contract_address)
                            user_address = user_address.address
                            transaction = contract.transfer_to(
                                user_address, balance, executor_address)

                    # Update wallet status
                    addr.balance = 0
                    session.commit()
                    return transaction
            except RuntimeError:
                # This issue should not happen in production,
                # just in dev/test where we create local accounts, but verify them on mainnet using blockexporer
                print("bitcoin address %s does not exist, doing nothing" %
                      blockchain_address.public_key)
        else:
            raise ValueError("bitcoin address %s not found in the database" %
                             self.address)