from pyopentxs import account
from pyopentxs.nym import Nym
from pyopentxs.asset import Asset
from pyopentxs.instrument import transfer
btc_contract_file = "../test-data/sample-contracts/btc.xml"


class TransferAccounts:
    '''A class to hold issuer/source/target accounts to test transfers.
       Start with 100 balance in the source account'''
    def __init__(self):
        self.asset = Asset().issue(Nym().register(),
                                   open(btc_contract_file))

        self.source = account.Account(self.asset, Nym().register()).create()
        self.target = account.Account(self.asset, Nym().register()).create()
        self.issuer = self.asset.issuer_account

    def initial_balance(self, balance=100):
        # send 100 from issuer to nym_source_id
        # self.cheque = new_cheque(self.issuer, self.source, balance)
        # transfer(self.cheque, self.issuer, self.source)
        transfer(balance, self.issuer, self.source)
        return self

    def assert_balances(self, issuer, source, target):
        assert (issuer, source, target) == (self.issuer.balance(),
                                            self.source.balance(),
                                            self.target.balance()),\
            "Issuer/source/target balances do not match."

from pyopentxs.nym import Nym
from pyopentxs.asset import Asset
from pyopentxs.instrument import transfer
from pyopentxs.account import Account
btc_contract_file = "../test-data/sample-contracts/btc.xml"
silver_contract_file = "../test-data/sample-contracts/silver.xml"


class TransferAccounts:
    '''A class to hold issuer/source/target accounts to test transfers.
       Start with 100 balance in the source account'''
    def __init__(self, asset=None):
        self.asset = asset or Asset().issue(Nym().register(),
                                            open(btc_contract_file))

        self.source = Account(self.asset, Nym().register()).create()
        self.target = Account(self.asset, Nym().register()).create()
        self.issuer = self.asset.issuer_account

    def initial_balance(self, balance=100):
        # send 100 from issuer to nym_source_id
        # self.cheque = new_cheque(self.issuer, self.source, balance)
        # transfer(self.cheque, self.issuer, self.source)
        transfer(balance, self.issuer, self.source)
        return self

    def assert_balances(self, issuer, source, target):
        assert (issuer, source, target) == (self.issuer.balance(),
                                            self.source.balance(),
                                            self.target.balance()),\
            "Issuer/source/target balances do not match."


class TradeAccount:
    def __init__(self, nym, asset1, asset2):
        self.nym = nym
        self.account1 = Account(asset1, self.nym).create()
        self.account2 = Account(asset2, self.nym).create()

    def create(self):
        self.account1.create()
        self.account2.create()
        return self


class MarketAccounts:
    '''A class to hold market trading accounts'''

    def __init__(self, asset1=None, asset2=None):
        self.asset1 = asset1 or Asset().issue(Nym().register(),
                                              open(btc_contract_file))
        self.asset2 = asset2 or Asset().issue(Nym().register(),
                                              open(silver_contract_file))

        accounts = [
            TradeAccount(Nym().register(), self.asset1, self.asset2).create()
            for _ in range(3)
        ]
        self.alice = accounts[0]
        self.bob = accounts[1]
        self.charlie = accounts[2]

    def initial_balance(self, balance=100):
        # start everyone off with 100 of each asset

        transfer(balance, self.asset1.issuer, self.alice)
        transfer(balance, self.asset1.issuer, self.bob)
        transfer(balance, self.asset1.issuer, self.charlie)

        transfer(balance, self.asset2.issuer, self.alice)
        transfer(balance, self.asset2.issuer, self.bob)
        transfer(balance, self.asset2.issuer, self.charlie)
        return self

Add marketaccounts to hold relevant market trade data