class TestRealP2PTrade(unittest.TestCase): def setUp(self): from pwallet import PersistentWallet from wallet_controller import WalletController self.pwallet = PersistentWallet() self.pwallet.init_model() def test_real(self): ewctrl = EWalletController(self.pwallet.get_model()) config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30} comm1 = MockComm() comm2 = MockComm() comm1.add_peer(comm2) comm2.add_peer(comm1) agent1 = EAgent(ewctrl, config, comm1) agent2 = EAgent(ewctrl, config, comm2) frobla_color_desc = "obc:cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd:0:263370" foo_color_desc = "obc:caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac:0:265577" ag1_offer = MyEOffer( None, {"color_spec": frobla_color_desc, "value": 100}, {"color_spec": foo_color_desc, "value": 200} ) ag2_offer = MyEOffer( None, {"color_spec": foo_color_desc, "value": 200}, {"color_spec": frobla_color_desc, "value": 100}, False ) agent1.register_my_offer(ag1_offer) agent2.register_my_offer(ag2_offer) for _ in xrange(3): agent1.update_state() agent2.update_state()
def main(): import sys import getopt try: opts, args = getopt.getopt(sys.argv[1:], "", []) except getopt.GetoptError: print "arg error" sys.exit(2) if len(args) == 0: args = ['help'] # special command if args[0] == 'import_config': with open(args[1], "r") as fp: config = json.load(fp) pw = PersistentWallet(config) sys.exit(0) pw = PersistentWallet() try: pw.init_model() except Exception as e: print "failed to initialize wallet model: %s" % e wallet_model = pw.get_model() controller = WalletController(wallet_model) \ if wallet_model else None interpreter = CommandInterpreter(pw, controller) interpreter.run_command(*args)
def __getattribute__(self, name): if name in ['controller', 'model', 'wallet']: try: data = self.data except AttributeError: self.data = data = {} pw = PersistentWallet(self.args.get('wallet_path')) pw.init_model() wallet_model = pw.get_model() data.update({ 'controller': WalletController(wallet_model) if wallet_model else None, 'wallet': pw, 'model': wallet_model if pw else None, }) return data[name] return object.__getattribute__(self, name)
class TestRealP2PTrade(unittest.TestCase): def setUp(self): from pwallet import PersistentWallet from wallet_controller import WalletController self.pwallet = PersistentWallet() self.pwallet.init_model() def test_real(self): ewctrl = EWalletController(self.pwallet.get_model()) config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30} comm1 = MockComm() comm2 = MockComm() comm1.add_peer(comm2) comm2.add_peer(comm1) agent1 = EAgent(ewctrl, config, comm1) agent2 = EAgent(ewctrl, config, comm2) frobla_color_desc = "obc:cc8e64cef1a880f5132e73b5a1f52a72565c92afa8ec36c445c635fe37b372fd:0:263370" foo_color_desc = "obc:caff27b3fe0a826b776906aceafecac7bb34af16971b8bd790170329309391ac:0:265577" ag1_offer = MyEOffer(None, { "color_spec": frobla_color_desc, "value": 100 }, { "color_spec": foo_color_desc, "value": 200 }) ag2_offer = MyEOffer(None, { "color_spec": foo_color_desc, "value": 200 }, { "color_spec": frobla_color_desc, "value": 100 }, False) agent1.register_my_offer(ag1_offer) agent2.register_my_offer(ag2_offer) for _ in xrange(3): agent1.update_state() agent2.update_state()
class Wallet(object): def __init__(self): self.wallet = PersistentWallet() self.wallet.init_model() self.model = self.wallet.get_model() self.controller = WalletController(self.wallet.get_model()) def get_asset_definition(self, moniker): adm = self.wallet.get_model().get_asset_definition_manager() asset = adm.get_asset_by_moniker(moniker) if asset: return asset else: raise Exception("asset not found") def get_all_monikers(self): return [ asset.get_monikers()[0] for asset in self.wallet.get_model().get_asset_definition_manager() .get_all_assets()] def get_balance(self, color): if not isinstance(color, AssetDefinition): color = self.get_asset_definition(color) return self.controller.get_balance(color) def get_all_addresses(self, color): if not isinstance(color, AssetDefinition): color = self.get_asset_definition(color) return [addr.get_address() for addr in self.controller.get_all_addresses(color)] def send_coins(self, items): for item in items: self.controller.send_coins( item['address'], item['asset'] if 'asset' in item else self.get_asset_definition(item['moniker']), item['value'])
This file connects ngccc-server.py to wallet_controller.py The main functions that this file has are to take the JSON-RPC commands from the server and pass them through to the wallet controller. Note console_interface.py does a similar thing for ngccc.py to wallet_controller.py """ from wallet_controller import WalletController from pwallet import PersistentWallet import pyjsonrpc import json # create a global wallet for this use. wallet = PersistentWallet() wallet.init_model() model = wallet.get_model() controller = WalletController(model) def get_asset_definition(moniker): """Get the asset/color associated with the moniker. """ adm = model.get_asset_definition_manager() asset = adm.get_asset_by_moniker(moniker) if asset: return asset else: raise Exception("asset %s not found" % moniker)
class Wallet(object): thread_comm = None def __init__(self): parser = argparse.ArgumentParser() parser.add_argument("--wallet", dest="wallet_path") parsed_args = vars(parser.parse_args()) self.wallet = PersistentWallet(parsed_args.get('wallet_path')) self.wallet.init_model() self.model = self.wallet.get_model() self.controller = WalletController(self.wallet.get_model()) def get_asset_definition(self, moniker): if isinstance(moniker, AssetDefinition): return moniker adm = self.wallet.get_model().get_asset_definition_manager() asset = adm.get_asset_by_moniker(moniker) if asset: return asset else: raise Exception("asset not found") def add_asset(self, params): self.controller.add_asset_definition({ "monikers": [params['moniker']], "color_set": [params['color_desc']], "unit": params['unit'] }) if len(self.get_all_addresses(params['moniker'])) == 0: self.get_new_address(params['moniker']) def get_all_asset(self): return self.wallet.wallet_config['asset_definitions'] def issue(self, params): self.controller.issue_coins( params['moniker'], params['coloring_scheme'], params['units'], params['atoms']) if len(self.get_all_addresses(params['moniker'])) == 0: self.get_new_address(params['moniker']) def get_all_monikers(self): monikers = [asset.get_monikers()[0] for asset in self.model.get_asset_definition_manager().get_all_assets()] monikers.remove('bitcoin') monikers = ['bitcoin'] + monikers return monikers def get_balance(self, color): return self.controller.get_balance(self.get_asset_definition(color)) def get_all_addresses(self, color): return [addr.get_address() for addr in self.controller.get_all_addresses(self.get_asset_definition(color))] def get_address_balance(self, color): asset = self.get_asset_definition(color) return self.controller.get_address_balance(asset) def get_some_address(self, color): wam = self.model.get_address_manager() cs = self.get_asset_definition(color).get_color_set() ar = wam.get_some_address(cs) return ar.get_address() def get_new_address(self, color): return self.controller. \ get_new_address(self.get_asset_definition(color)).get_address() def scan(self): self.controller.scan_utxos() def send_coins(self, items): if isinstance(items, dict): items = [items] for item in items: self.controller.send_coins( item['asset'] if 'asset' in item \ else self.get_asset_definition(item['moniker']), [item['address']], [item['value']]) def p2ptrade_init(self): ewctrl = EWalletController(self.model, self.controller) config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30} comm = HTTPComm( config, 'http://p2ptrade.btx.udoidio.info/messages') self.thread_comm = ThreadedComm(comm) self.p2p_agent = EAgent(ewctrl, config, self.thread_comm) self.thread_comm.start() def p2ptrade_stop(self): if self.thread_comm is not None: self.thread_comm.stop() def p2ptrade_make_offer(self, we_sell, params): asset = self.get_asset_definition(params['moniker']) value = asset.parse_value(params['value']) bitcoin = self.get_asset_definition('bitcoin') price = bitcoin.parse_value(params['price']) total = int(float(value)/float(asset.unit)*float(price)) color_desc = asset.get_color_set().color_desc_list[0] sell_side = {"color_spec": color_desc, "value": value} buy_side = {"color_spec": "", "value": total} if we_sell: return MyEOffer(None, sell_side, buy_side) else: return MyEOffer(None, buy_side, sell_side) def p2ptrade_make_mirror_offer(self, offer): data = offer.get_data() return MyEOffer(None, data['B'], data['A'])