diff --git a/beem/account.py b/beem/account.py index 3e15c87d2ae2e474efed14061837f9f73defbec0..0e11a14a12a90c0c473d6260aa929e13925bea98 100644 --- a/beem/account.py +++ b/beem/account.py @@ -1097,7 +1097,7 @@ class Account(BlockchainObject): if not account: raise ValueError("You need to provide an account") if not to: - to = account # powerup on the same account + to = self # powerup on the same account account = Account(account, steem_instance=self.steem) if isinstance(amount, (string_types, Amount)): amount = Amount(amount, steem_instance=self.steem) diff --git a/beem/cli.py b/beem/cli.py index 6ff14e88d64f12d4fdcde4cddd401fdbe1e8ae4f..2effd3df38eb6ab56e0d43bda711012e64cc2dcd 100644 --- a/beem/cli.py +++ b/beem/cli.py @@ -45,11 +45,29 @@ def prompt_callback(ctx, param, value): ctx.abort() +def asset_callback(ctx, param, value): + if value not in ["STEEM", "SBD"]: + print("Please STEEM or SBD as asset!") + ctx.abort() + else: + return value + + def prompt_flag_callback(ctx, param, value): if not value: ctx.abort() +def unlock_wallet(stm, password): + stm.wallet.unlock(password) + if stm.wallet.locked(): + print("Wallet could not be unlocked!") + return False + else: + print("Wallet Unlocked!") + return True + + @click.group(chain=True) @click.option( '--node', '-n', default="", help="URL for public Steem API (e.g. https://api.steemit.com)") @@ -195,14 +213,9 @@ def addkey(password, unsafe_import_key): and a prompt for entering the private key are shown. """ stm = shared_steem_instance() - if not stm.wallet.locked(): + if not unlock_wallet(stm, password): return - stm.wallet.unlock(password) - if stm.wallet.locked(): - print("Could not be unlocked!") - else: - print("Unlocked!") - stm.wallet.addPrivateKey(unsafe_import_key) + stm.wallet.addPrivateKey(unsafe_import_key) set_shared_steem_instance(stm) @@ -221,15 +234,10 @@ def delkey(confirm, password, pub): which will be deleted from the wallet """ stm = shared_steem_instance() - if not stm.wallet.locked(): + if not unlock_wallet(stm, password): return - stm.wallet.unlock(password) - if stm.wallet.locked(): - print("Could not be unlocked!") - else: - print("Unlocked!") - if click.confirm('Do you want to continue to wipe the private key?'): - stm.wallet.removePrivateKeyFromPublicKey(pub) + if click.confirm('Do you want to continue to wipe the private key?'): + stm.wallet.removePrivateKeyFromPublicKey(pub) set_shared_steem_instance(stm) @@ -261,40 +269,53 @@ def listaccounts(): @cli.command() @click.argument('post', nargs=1) +@click.argument('vote_weight', nargs=1, required=False) +@click.option('--weight', '-w', help='Vote weight (from 0.1 to 100.0)') @click.option('--account', '-a', help='Voter account name') -@click.option('--weight', '-w', default=100.0, help='Vote weight (from 0.1 to 100.0)') -def upvote(post, account, weight): +@click.option('--password', prompt=True, hide_input=True, + confirmation_prompt=False, help='Password to unlock wallet') +def upvote(post, vote_weight, account, weight, password): """Upvote a post/comment POST is @author/permlink """ stm = shared_steem_instance() - if not weight: + if not weight and vote_weight: + weight = vote_weight + elif not weight and not vote_weight: weight = stm.config["default_vote_weight"] if not account: account = stm.config["default_account"] + if not unlock_wallet(stm, password): + return try: - post = Comment(post) + post = Comment(post, steem_instance=stm) + post.upvote(weight, voter=account) except Exception as e: log.error(str(e)) - return - post.upvote(weight, voter=account) @cli.command() @click.argument('post', nargs=1) +@click.argument('vote_weight', nargs=1, required=False) @click.option('--account', '-a', help='Voter account name') @click.option('--weight', '-w', default=100.0, help='Vote weight (from 0.1 to 100.0)') -def downvote(post, account, weight): +@click.option('--password', prompt=True, hide_input=True, + confirmation_prompt=False, help='Password to unlock wallet') +def downvote(post, vote_weight, account, weight, password): """Downvote a post/comment POST is @author/permlink """ stm = shared_steem_instance() - if not weight: + if not weight and vote_weight: + weight = vote_weight + elif not weight and not vote_weight: weight = stm.config["default_vote_weight"] if not account: account = stm.config["default_account"] + if not unlock_wallet(stm, password): + return try: post = Comment(post) except Exception as e: @@ -303,6 +324,77 @@ def downvote(post, account, weight): post.downvote(weight, voter=account) +@cli.command() +@click.argument('to', nargs=1) +@click.argument('amount', nargs=1) +@click.argument('asset', nargs=1, callback=asset_callback) +@click.argument('memo', nargs=1, required=False) +@click.option('--password', prompt=True, hide_input=True, + confirmation_prompt=False, help='Password to unlock wallet') +@click.option('--account', '-a', help='Transfer from this account') +def transfer(to, amount, asset, memo, password, account): + """Transfer SBD/STEEM""" + stm = shared_steem_instance() + if not account: + account = stm.config["default_account"] + if not bool(memo): + memo = '' + if not unlock_wallet(stm, password): + return + acc = Account(account, steem_instance=stm) + tx = acc.transfer(to, amount, asset, memo) + tx = json.dumps(tx, indent=4) + print(tx) + + +@cli.command() +@click.argument('amount', nargs=1) +@click.option('--password', prompt=True, hide_input=True, + confirmation_prompt=False, help='Password to unlock wallet') +@click.option('--account', '-a', help='Powerup from this account') +@click.option('--to', help='Powerup this account', default=None) +def powerup(amount, password, account, to): + """Power up (vest STEEM as STEEM POWER)""" + stm = shared_steem_instance() + if not account: + account = stm.config["default_account"] + if not unlock_wallet(stm, password): + return + acc = Account(account, steem_instance=stm) + try: + amount = float(amount) + except: + amount = str(amount) + tx = acc.transfer_to_vesting(amount, to=to) + tx = json.dumps(tx, indent=4) + print(tx) + + +@cli.command() +@click.argument('amount', nargs=1) +@click.option('--password', prompt=True, hide_input=True, + confirmation_prompt=False, help='Password to unlock wallet') +@click.option('--account', '-a', help='Powerup from this account') +def powerdown(amount, password, account): + """Power down (start withdrawing VESTS from Steem POWER) + + amount is in VESTS + """ + stm = shared_steem_instance() + if not account: + account = stm.config["default_account"] + if not unlock_wallet(stm, password): + return + acc = Account(account, steem_instance=stm) + try: + amount = float(amount) + except: + amount = str(amount) + tx = acc.withdraw_vesting(amount) + tx = json.dumps(tx, indent=4) + print(tx) + + @cli.command() @click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True) diff --git a/tests/beem/test_cli.py b/tests/beem/test_cli.py index ad2872800ac67e41bca998c594d7e121bf57eaa2..2acc490aa8f614393e2ea1723514428539709d1f 100644 --- a/tests/beem/test_cli.py +++ b/tests/beem/test_cli.py @@ -29,7 +29,9 @@ class Testcases(unittest.TestCase): runner.invoke(cli, ['set', 'default_vote_weight', '100']) runner.invoke(cli, ['set', 'default_account', 'beem']) runner.invoke(cli, ['set', 'nodes', 'wss://testnet.steem.vc']) - runner.invoke(cli, ['createwallet', '--wipe True'], input='test\ntest\n') + runner.invoke(cli, ['createwallet', '--wipe', '--password test'], input='y\n') + runner.invoke(cli, ['addkey', '--password test', '--unsafe-import-key ' + wif]) + # runner.invoke(cli, ['changewalletpassphrase', '--password test']) def test_balance(self): runner = CliRunner() @@ -43,6 +45,8 @@ class Testcases(unittest.TestCase): def test_addkey(self): runner = CliRunner() + result = runner.invoke(cli, ['createwallet', '--wipe', '--password test'], input='y\n') + self.assertEqual(result.exit_code, 2) result = runner.invoke(cli, ['addkey', '--password test', '--unsafe-import-key ' + wif]) self.assertEqual(result.exit_code, 2) @@ -50,6 +54,8 @@ class Testcases(unittest.TestCase): runner = CliRunner() result = runner.invoke(cli, ['delkey', '--password test', wif]) self.assertEqual(result.exit_code, 2) + result = runner.invoke(cli, ['addkey', '--password test', '--unsafe-import-key ' + wif]) + self.assertEqual(result.exit_code, 2) def test_listkeys(self): runner = CliRunner() @@ -82,12 +88,22 @@ class Testcases(unittest.TestCase): result = runner.invoke(cli, ['walletinfo']) self.assertEqual(result.exit_code, 0) - def test_createwallet(self): - runner = CliRunner() - result = runner.invoke(cli, ['createwallet', '--password test', '--wipe True']) - self.assertEqual(result.exit_code, 2) - def test_set(self): runner = CliRunner() result = runner.invoke(cli, ['set', 'set_default_vote_weight', '100']) self.assertEqual(result.exit_code, 0) + + def test_upvote(self): + runner = CliRunner() + result = runner.invoke(cli, ['upvote', '@test/abcd', '--weight 100'], input='test\n') + self.assertEqual(result.exit_code, 0) + + def test_downvote(self): + runner = CliRunner() + result = runner.invoke(cli, ['downvote', '@test/abcd', '--weight 100'], input='test\n') + self.assertEqual(result.exit_code, 0) + + def test_transfer(self): + runner = CliRunner() + result = runner.invoke(cli, ['transfer', 'beem1', '1', 'SBD', 'test'], input='test\n') + self.assertEqual(result.exit_code, 0)