Created
May 24, 2018 17:25
-
-
Save danintel/43068b9f59102c6a873a2be32ee36b23 to your computer and use it in GitHub Desktop.
sawtooth-simple-wallet new feature demo: interest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://github.com/askmish/sawtooth-simplewallet | |
28 pyclient/wallet/simplewallet_cli.py | |
@@ -71,6 +71,22 @@ def add_deposit_parser(subparsers, parent_parser): | |
type=str, | |
help='the name of customer to deposit to') | |
+def add_interest_parser(subparsers, parent_parser): | |
+ parser = subparsers.add_parser( | |
+ 'interest', | |
+ help='Adds interest of a certain percentage to an account', | |
+ parents=[parent_parser]) | |
+ | |
+ parser.add_argument( | |
+ 'value', | |
+ type=int, | |
+ help='the percentage of interest to deposit') | |
+ | |
+ parser.add_argument( | |
+ 'customerName', | |
+ type=str, | |
+ help='the name of customer to deposit to') | |
+ | |
def add_withdraw_parser(subparsers, parent_parser): | |
parser = subparsers.add_parser( | |
'withdraw', | |
@@ -149,6 +165,7 @@ def create_parser(prog_name): | |
subparsers.required = True | |
add_deposit_parser(subparsers, parent_parser) | |
+ add_interest_parser(subparsers, parent_parser) | |
add_withdraw_parser(subparsers, parent_parser) | |
add_balance_parser(subparsers, parent_parser) | |
add_transfer_parser(subparsers, parent_parser) | |
@@ -176,6 +193,15 @@ def do_deposit(args): | |
print("Response: {}".format(response)) | |
+def do_interest(args): | |
+ keyfile = _get_keyfile(args.customerName) | |
+ | |
+ client = SimpleWalletClient(baseUrl=DEFAULT_URL, keyFile=keyfile) | |
+ | |
+ response = client.interest(args.value) | |
+ | |
+ print("Response: {}".format(response)) | |
+ | |
def do_withdraw(args): | |
keyfile = _get_keyfile(args.customerName) | |
@@ -221,6 +247,8 @@ def main(prog_name=os.path.basename(sys.argv[0]), args=None): | |
# Get the commands from cli args and call corresponding handlers | |
if args.command == 'deposit': | |
do_deposit(args) | |
+ elif args.command == 'interest': | |
+ do_interest(args) | |
elif args.command == 'withdraw': | |
do_withdraw(args) | |
elif args.command == 'balance': | |
5 pyclient/wallet/simplewallet_client.py | |
@@ -81,6 +81,11 @@ def deposit(self, value): | |
"deposit", | |
value) | |
+ def interest(self, value): | |
+ return self._wrap_and_send( | |
+ "interest", | |
+ value) | |
+ | |
def withdraw(self, value): | |
try: | |
retValue = self._wrap_and_send( | |
28 pyprocessor/processor/simplewallet_tp.py | |
@@ -56,14 +56,16 @@ def apply(self, transaction, context): | |
if operation == "deposit": | |
self._make_deposit(context, amount, from_key) | |
+ elif operation == "interest": | |
+ self._make_interest(context, amount, from_key) | |
elif operation == "withdraw": | |
self._make_withdraw(context, amount, from_key) | |
elif operation == "transfer": | |
if len(payload_list) == 3: | |
to_key = payload_list[2] | |
self._make_transfer(context, amount, to_key, from_key) | |
else: | |
- LOGGER.info("Unhandled action. Operation should be deposit, withdraw or transfer") | |
+ LOGGER.info("Unhandled action. Operation should be deposit, withdraw, interest, or transfer") | |
def _make_deposit(self, context, amount, from_key): | |
wallet_key = self._get_wallet_key(from_key) | |
@@ -84,6 +86,30 @@ def _make_deposit(self, context, amount, from_key): | |
if len(addresses) < 1: | |
raise InternalError("State Error") | |
+ def _make_interest(self, context, amount, from_key): | |
+ wallet_key = self._get_wallet_key(from_key) | |
+ LOGGER.info('Got the key {} and the wallet key {} '.format(from_key, wallet_key)) | |
+ current_entry = context.get_state([wallet_key]) | |
+ new_balance = 0 | |
+ LOGGER.info('DEBUG: amount (interest) passed is {} '.format(amount)) | |
+ | |
+ if current_entry == []: | |
+ LOGGER.info('No previous deposits or interest, creating new deposit {} '.format(from_key)) | |
+ new_balance = 0 | |
+ else: | |
+ balance = int(current_entry[0].data) | |
+ LOGGER.info('DEBUG: balance retrieved is {} '.format(balance)) | |
+ interest = int(float(balance) * (float(amount) / 100.0)) | |
+ LOGGER.info('DEBUG: interest is {} '.format(interest)) | |
+ new_balance = int(interest) + int(balance) | |
+ LOGGER.info('DEBUG: new_balance calculated is {} '.format(new_balance)) | |
+ | |
+ state_data = str(new_balance).encode() | |
+ addresses = context.set_state({wallet_key: state_data}) | |
+ | |
+ if len(addresses) < 1: | |
+ raise InternalError("State Error") | |
+ | |
def _make_withdraw(self, context, amount, from_key): | |
wallet_key = self._get_wallet_key(from_key) | |
LOGGER.info('Got the key {} and the wallet key {} '.format(from_key, wallet_key)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment