def command_add_ether_wallet(self): try: wallet = Vault().find_wallet(name=self.name) except ValueError: return logging.error("The wallet's name can not be found.") wallet.add_ether(2000000000000000000) logging.info("Some ethers have been added.")
def command_buy(self): if not isinstance(self.amount, float) or self.amount < 0: return logging.error("Please add a positive amount to buy.") vault = Vault() try: wallet = vault.find_wallet(name=self.wallet) except ValueError: return logging.error( 'The wallet is not recognized. Add a wallet with the wallet command.' ) c_org_manager = ContinuousOrganisationManager(self.name) if not c_org_manager.is_built(): return logging.error( 'The continuous organisation is not deployed. Run first `c-org deploy --help`.' ) logging.debug('Buying an amount of {:.3f} to {}'.format( self.amount, self.name)) c_org_manager.buy(self.amount, wallet) balance = c_org_manager.get_balance(wallet) logging.info( "Great! Your buy tokens! Your balance is now {:d}".format(balance))
def command_rm_wallet(self): vault = Vault() if not vault.exist_wallet(name=self.name): return logging.error("The wallet's name can not be found.") logging.debug("Removing a wallet with name {}.".format(self.name)) vault.remove_wallet(self.name) logging.info("The wallet is removed.")
def command_create_wallet(self): vault = Vault() if vault.exist_wallet(name=self.name): return logging.error("The wallet's name already exists.") logging.debug("Creating a wallet with name {}.".format(self.name)) wallet = vault.create_wallet(self.name) logging.info( "The wallet is created. Please add some ethers to its address {}. You can use Metamask for that (see https://youtu.be/-uJjfn4wizE)." .format(wallet.address))
class TestBase(unittest.TestCase): def generate_wallet(self): if not self.has_temporary_files(): self.temp_files() self.wallet = Vault().create_wallet('test') self.wallet.add_ether(init_ether) def temp_files(self): self.workdir = tempfile.TemporaryDirectory() os.environ['HOME'] = self.workdir.name self._temporary = True def generate_c_org(self): if not self.has_temporary_files(): self.temp_files() rootdir = os.path.dirname(os.path.abspath(__file__)) c_orgs = utils.get_c_org_path() os.makedirs(c_orgs) contracts = os.path.join(c_orgs, "contracts") os.makedirs(contracts) contract = os.path.join(rootdir, "ressources", ".c-org", "contracts", "ContinuousOrganisation.sol") shutil.copy(contract, contracts) global_file = os.path.join(rootdir, "ressources", ".c-org", "global.yaml") shutil.copy(global_file, c_orgs) vault_file = os.path.join(rootdir, "ressources", ".c-org", "vault.yaml") shutil.copy(vault_file, c_orgs) def has_temporary_files(self): return hasattr(self, '_temporary') and self._temporary def generate_manager(self): if not self.has_temporary_files(): self.temp_files() name = "my-co" c_name = utils.clean_name(name) self.my_co_path = os.path.join(utils.get_c_org_path(), c_name) os.makedirs(self.my_co_path) global_params = GlobalParams() global_params.create_or_update(name, self.my_co_path) rootdir = os.path.dirname(os.path.abspath(__file__)) config_file = os.path.join(rootdir, "ressources", "config.yaml") shutil.copy(config_file, self.my_co_path) self.c_org_manager = ContinuousOrganisationManager(name) def cleanup(self): if self.has_temporary_files(): self.workdir.cleanup() def tearDown(self): self.cleanup()
def command_add_wallet(self): vault = Vault() if vault.exist_wallet(name=self.name): return logging.error("The wallet's name already exists.") if self.name: logging.debug("Adding a wallet with name {}.".format(self.name)) else: logging.debug("Adding a wallet with private key") wallet = Wallet(name=self.name, private_key=self.private_key) vault.store_wallet(wallet) logging.info("The wallet is added.")
def test_add_wallet(self): sys.argv = [exe_cli] + [ "wallet", "add", "test", utils.generate_random_private_key() ] main() self.assertTrue(Vault().exist_wallet("test"))
def command_deploy(self): if os.path.isdir(self.output): self.output = os.path.join(self.output, "config.yaml") if not os.path.isfile(self.output): return logging.error('The path is not valid. Please add a path to the config.yaml file.') dirname = os.path.dirname(os.path.abspath(self.output)) if not self.wallet: return logging.error('No wallet has been specified. Please add --wallet NAME. Add a wallet with the wallet command.') else: try: wallet = Vault().find_wallet(name=self.wallet) except ValueError: return logging.error('The wallet is not recognized. Add a wallet with the wallet command.') name = LocalParams(self.output).name GlobalParams().create_or_update(name, dirname) c_org_manager = ContinuousOrganisationManager(name) try: c_org_manager.deploy(wallet) except ValueError as e: err = e.args[0] logging.error('''Oops... Something wrong happened! {}'''.format(err['message'])) if err['code'] == -32000: logging.error('The specified wallet has insufficient funds to deploy your Continuous Organization, please send ~0.1eth to {}'.format(wallet.address)) return logging.info("Great! Your continuous organisation exists!")
def test_deploy_default_wallet(self): Vault().default_wallet().add_ether(init_ether) config_file = os.path.join(self.my_co_path, "config.yaml") sys.argv = [exe_cli, "deploy", config_file] main() self.assertTrue(os.path.isfile(self.c_org_manager.build_file)) contract = self.c_org_manager.contract self.assertTrue(len(contract.find_functions_by_name('buy')))
def test_no_enough_funds(self): config_file = os.path.join(self.my_co_path, "config.yaml") wallet = Vault().create_wallet('poor-wallet') command = [exe_cli, "deploy", config_file, "--wallet", wallet.name] p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (out, err) = p.communicate() self.assertEqual(out, b'') self.assertIn(wallet.address.encode('utf-8'), err)
def command_stats(self): vault = Vault() try: wallet = vault.find_wallet(name=self.wallet) except ValueError: return logging.error( 'The wallet is not recognized. Add a wallet with the wallet command.' ) c_org_manager = ContinuousOrganisationManager(self.name) if not c_org_manager.is_built(): return logging.error( 'The continuous organisation is not deployed. Run first `c-org deploy --help`.' ) logging.debug('Retrieving the statistics') balance = c_org_manager.get_balance(wallet) logging.info("Balance: {:.3f} tokens".format(balance)) tokens = c_org_manager.get_n_tokens() logging.info("Total tokens: {:.3f} tokens".format(tokens)) reserve = c_org_manager.get_sell_reserve() logging.info("Reserve: {:.3f}".format(reserve))
def test_rm_wallet(self): Vault().create_wallet(name="name") self.assertTrue(Vault().exist_wallet("name")) sys.argv = [exe_cli, "wallet", "remove", "name"] main() self.assertFalse(Vault().exist_wallet("name"))
def test_add_ether_wallet(self): wallet = Vault().create_wallet(name="test-add-ether") sys.argv = [exe_cli, "wallet", "add_ether", "test-add-ether"] main() self.assertEqual(init_ether, wallet.balance)
def test_list_wallet(self): Vault().create_wallet(name="test-list") with self.assertLogs() as cm: sys.argv = [exe_cli, "wallet", "list"] main() self.assertIn(True, ['test-list' in i for i in cm.output])
def test_create_wallet(self): sys.argv = [exe_cli, "wallet", "create", "my-wallet"] main() self.assertTrue(Vault().exist_wallet("my-wallet"))
def generate_wallet(self): if not self.has_temporary_files(): self.temp_files() self.wallet = Vault().create_wallet('test') self.wallet.add_ether(init_ether)
def command_list_wallet(self): for i, wallet in enumerate(Vault().wallets): logging.info("[{}] at {}".format(wallet['name'], wallet['address']))
def setUp(self): self.generate_c_org() self.generate_wallet() self.v = Vault()
class TestVault(TestBase): '''Vault tests''' def setUp(self): self.generate_c_org() self.generate_wallet() self.v = Vault() def test_vault(self): self.assertIn('infura', self.v.data) self.assertIn('wallets', self.v.data) self.assertIn('my-wallet', self.v.names) def test_save(self): self.v.data['infura'] = "TEST" self.v.save() self.v.data = [] self.v.load() self.assertEqual(self.v.data['infura'], "TEST") def test_exist(self): self.assertTrue(self.v.exist_wallet('my-wallet')) def test_create_wallet(self): self.assertFalse(self.v.exist_wallet('New Wallet')) self.v.create_wallet('New Wallet') self.assertTrue(self.v.exist_wallet('New Wallet')) def test_store_wallet_dict(self): self.assertFalse(self.v.exist_wallet('dict-wallet')) wallet = { 'name': "dict-wallet", 'private_key': utils.generate_random_private_key() } self.v.store_wallet(wallet) self.assertTrue(self.v.exist_wallet('dict-wallet')) def test_store_wallet(self): self.assertFalse(self.v.exist_wallet('wallet-wallet')) wallet = utils.Wallet(name="wallet-wallet", private_key=utils.generate_random_private_key()) self.v.store_wallet(wallet) self.assertTrue(self.v.exist_wallet('wallet-wallet')) def test_remove_wallet(self): self.assertTrue(self.v.exist_wallet('my-wallet')) self.v.remove_wallet('my-wallet') self.assertFalse(self.v.exist_wallet('my-wallet')) def test_no_wallet(self): self.v.data['wallets'] = [] self.v.save() self.assertFalse(self.v.exist_wallet('my-wallet'))