diff --git a/CONTRIBUTING_CORE.md b/CONTRIBUTING_CORE.md new file mode 100644 index 00000000..f7b5544a --- /dev/null +++ b/CONTRIBUTING_CORE.md @@ -0,0 +1,117 @@ +# CORE TEAM CONTRIBUTION GUIDE + +# 1. Transparency + +1.1 Use work logs for reflection of work done, aswell as telling your peers about changes that may affect their own tasks + +1.2 A work log SHOULD be submitted after a "unit of work" is complete. + +1.2.1 A "unit of work" should not span more than one full day's worth of work. + +1.2.2 A "unit of work" should be small enough that the log entries give useful insight. + +1.3 Individual logs are reviewed in weekly meetings + + + +1.4 Work log format is defined in []() + +1.5 Link to issue/MR in bullet point where appropriate + +1.6 + + +# 2. Code hygiene + +2.1 Keep function names and variable names short + +2.2 Keep code files, functions and test fixtures short + +2.3 The less magic the better. Recombinable and replaceable is king + +2.4 Group imports by `standard`, `external`, `local`, `test` - in that order + +2.5 Only auto-import when necessary, and always with a minimum of side-effects + +2.6 Use custom errors. Let them bubble up + +2.7 No logs in tight loops + +2.8 Keep executable main routine minimal. Pass variables (do not use globals) in main business logic function + +2.9 Test coverage MUST be kept higher than 90% after changes + +2.10 Docstrings. Always. Always! + + +# 3. Versioning + +3.1 Use [Semantic Versioning](https://semver.org/) + +3.2 When merging code, explicit dependencies SHOULD NOT use pre-release version + + +# 4. Issues + +4.1 Issue title should use [Convention Commit structure](https://www.conventionalcommits.org/en/v1.0.0-beta.2/) + +4.2 Issues need proper problem statement + +4.2.1. What is the current state + +4.2.2. If current state is not behaving as expected, what was the expected state + +4.2.3. What is the desired new state. + +4.3 Issues need proper resolution statement + +4.3.1. Bullet point list of short sentences describing practical steps to reach desired state + +4.3.2. Builet point list of external resources informing the issue and resolution + +4.4 Tasks needs to be appropriately labelled using GROUP labels. + + +# 5. Code submission + +5.1 A branch and new MR is always created BEFORE THE WORK STARTS + +5.2 An MR should solve ONE SINGLE PART of a problem + +5.3 Every MR should have at least ONE ISSUE associated with it. Ideally issue can be closed when MR is merged + +5.4 MRs should not be open for more than one week (during normal operation periods) + +5.5 MR should ideally not be longer than 400 lines of changes of logic + +5.6 MRs that MOVE or DELETE code should not CHANGE that same code in a single MR. Scope MOVEs and DELETEs in separate commits (or even better, separate MRs) for transparency + + +# 6. Code reviews + +6.1 At least one peer review before merge + +6.2 If MR is too long, evaluate whether this affects the quality of the review negatively. If it does, expect to be asked to split it up + +6.3 Evaluate changes against associated issues' problem statement and proposed resolution steps. If there is a mismatch, either MR needs to change or issue needs to be amended accordingly + +6.4 Make sure all technical debt introduced by MR is documented in issues. Add them according to criteria in section ISSUES if not + +6.5 If CI is not working, reviewer MUST make sure code builds and runs + +6.6 Behave! + +6.6.1 Don't be a jerk + +6.6.2 Don't block needlessly + +6.6.3 Say please diff --git a/apps/cic-cache/MANIFEST.in b/apps/cic-cache/MANIFEST.in index 2630c39c..e05afcd9 100644 --- a/apps/cic-cache/MANIFEST.in +++ b/apps/cic-cache/MANIFEST.in @@ -1 +1 @@ -include *requirements.txt cic_cache/data/config/* +include *requirements.txt cic_cache/data/config/* cic_cache/db/migrations/default/* cic_cache/db/migrations/default/versions/* diff --git a/apps/cic-cache/cic_cache/cli/arg.py b/apps/cic-cache/cic_cache/cli/arg.py index 2d4e6e8a..43126eb2 100644 --- a/apps/cic-cache/cic_cache/cli/arg.py +++ b/apps/cic-cache/cic_cache/cli/arg.py @@ -14,7 +14,7 @@ class ArgumentParser(BaseArgumentParser): if local_arg_flags & CICFlag.CELERY: self.add_argument('-q', '--celery-queue', dest='celery_queue', type=str, default='cic-cache', help='Task queue') if local_arg_flags & CICFlag.SYNCER: - self.add_argument('--offset', type=int, default=0, help='Start block height for initial history sync') + self.add_argument('--offset', type=int, help='Start block height for initial history sync') self.add_argument('--no-history', action='store_true', dest='no_history', help='Skip initial history sync') if local_arg_flags & CICFlag.CHAIN: self.add_argument('-r', '--registry-address', type=str, dest='registry_address', help='CIC registry contract address') diff --git a/apps/cic-cache/cic_cache/data/config/cic.ini b/apps/cic-cache/cic_cache/data/config/cic.ini index b4275352..d2382746 100644 --- a/apps/cic-cache/cic_cache/data/config/cic.ini +++ b/apps/cic-cache/cic_cache/data/config/cic.ini @@ -1,4 +1,4 @@ [cic] registry_address = trust_address = -health_modules = cic_eth.check.db,cic_eth.check.redis,cic_eth.check.signer,cic_eth.check.gas +health_modules = diff --git a/apps/cic-cache/cic_cache/data/config/database.ini b/apps/cic-cache/cic_cache/data/config/database.ini index e32e62f3..3c20e4ec 100644 --- a/apps/cic-cache/cic_cache/data/config/database.ini +++ b/apps/cic-cache/cic_cache/data/config/database.ini @@ -3,7 +3,8 @@ engine = driver = host = port = -name = cic-cache +#name = cic-cache +prefix = user = password = debug = 0 diff --git a/apps/cic-cache/cic_cache/db/__init__.py b/apps/cic-cache/cic_cache/db/__init__.py index b2bb01b9..73a5eefa 100644 --- a/apps/cic-cache/cic_cache/db/__init__.py +++ b/apps/cic-cache/cic_cache/db/__init__.py @@ -9,21 +9,26 @@ from .list import ( tag_transaction, add_tag, ) +from cic_cache.db.models.base import SessionBase logg = logging.getLogger() -def dsn_from_config(config): +def dsn_from_config(config, name): scheme = config.get('DATABASE_ENGINE') if config.get('DATABASE_DRIVER') != None: scheme += '+{}'.format(config.get('DATABASE_DRIVER')) + database_name = name + if config.get('DATABASE_PREFIX'): + database_name = '{}_{}'.format(config.get('DATABASE_PREFIX'), database_name) dsn = '' if config.get('DATABASE_ENGINE') == 'sqlite': + SessionBase.poolable = False dsn = '{}:///{}'.format( scheme, - config.get('DATABASE_NAME'), + database_name, ) else: @@ -33,7 +38,7 @@ def dsn_from_config(config): config.get('DATABASE_PASSWORD'), config.get('DATABASE_HOST'), config.get('DATABASE_PORT'), - config.get('DATABASE_NAME'), + database_name, ) logg.debug('parsed dsn from config: {}'.format(dsn)) return dsn diff --git a/apps/cic-cache/cic_cache/runnable/daemons/query.py b/apps/cic-cache/cic_cache/runnable/daemons/query.py index d7edf6ab..b6b9eca1 100644 --- a/apps/cic-cache/cic_cache/runnable/daemons/query.py +++ b/apps/cic-cache/cic_cache/runnable/daemons/query.py @@ -5,7 +5,11 @@ import re import base64 # external imports -from hexathon import add_0x +from hexathon import ( + add_0x, + strip_0x, + ) +from chainlib.encode import TxHexNormalizer # local imports from cic_cache.cache import ( @@ -16,27 +20,72 @@ from cic_cache.cache import ( logg = logging.getLogger(__name__) #logg = logging.getLogger() -re_transactions_all_bloom = r'/tx/(\d+)?/?(\d+)/?' +re_transactions_all_bloom = r'/tx/?(\d+)?/?(\d+)?/?(\d+)?/?(\d+)?/?' re_transactions_account_bloom = r'/tx/user/((0x)?[a-fA-F0-9]+)(/(\d+)(/(\d+))?)?/?' -re_transactions_all_data = r'/txa/(\d+)?/?(\d+)/?' +re_transactions_all_data = r'/txa/?(\d+)?/?(\d+)?/?(\d+)?/?(\d+)?/?' +re_transactions_account_data = r'/txa/user/((0x)?[a-fA-F0-9]+)(/(\d+)(/(\d+))?)?/?' +re_default_limit = r'/defaultlimit/?' DEFAULT_LIMIT = 100 +tx_normalize = TxHexNormalizer() + +def parse_query_account(r): + address = strip_0x(r[1]) + #address = tx_normalize.wallet_address(address) + limit = DEFAULT_LIMIT + g = r.groups() + if len(g) > 3: + limit = int(r[4]) + if limit == 0: + limit = DEFAULT_LIMIT + offset = 0 + if len(g) > 4: + offset = int(r[6]) + + logg.debug('account query is address {} offset {} limit {}'.format(address, offset, limit)) + + return (address, offset, limit,) + + +# r is an re.Match +def parse_query_any(r): + limit = DEFAULT_LIMIT + offset = 0 + block_offset = None + block_end = None + if r.lastindex != None: + if r.lastindex > 0: + limit = int(r[1]) + if r.lastindex > 1: + offset = int(r[2]) + if r.lastindex > 2: + block_offset = int(r[3]) + if r.lastindex > 3: + block_end = int(r[4]) + if block_end < block_offset: + raise ValueError('cart before the horse, dude') + + logg.debug('data query is offset {} limit {} block_offset {} block_end {}'.format(offset, limit, block_offset, block_end)) + + return (offset, limit, block_offset, block_end,) + + +def process_default_limit(session, env): + r = re.match(re_default_limit, env.get('PATH_INFO')) + if not r: + return None + + return ('application/json', str(DEFAULT_LIMIT).encode('utf-8'),) + def process_transactions_account_bloom(session, env): r = re.match(re_transactions_account_bloom, env.get('PATH_INFO')) if not r: return None + logg.debug('match account bloom') - address = r[1] - if r[2] == None: - address = add_0x(address) - offset = 0 - if r.lastindex > 2: - offset = r[4] - limit = DEFAULT_LIMIT - if r.lastindex > 4: - limit = r[6] + (address, offset, limit,) = parse_query_account(r) c = BloomCache(session) (lowest_block, highest_block, bloom_filter_block, bloom_filter_tx) = c.load_transactions_account(address, offset, limit) @@ -59,13 +108,9 @@ def process_transactions_all_bloom(session, env): r = re.match(re_transactions_all_bloom, env.get('PATH_INFO')) if not r: return None + logg.debug('match all bloom') - offset = DEFAULT_LIMIT - if r.lastindex > 0: - offset = r[1] - limit = 0 - if r.lastindex > 1: - limit = r[2] + (limit, offset, block_offset, block_end,) = parse_query_any(r) c = BloomCache(session) (lowest_block, highest_block, bloom_filter_block, bloom_filter_tx) = c.load_transactions(offset, limit) @@ -88,17 +133,16 @@ def process_transactions_all_data(session, env): r = re.match(re_transactions_all_data, env.get('PATH_INFO')) if not r: return None - if env.get('HTTP_X_CIC_CACHE_MODE') != 'all': - return None + #if env.get('HTTP_X_CIC_CACHE_MODE') != 'all': + # return None + logg.debug('match all data') logg.debug('got data request {}'.format(env)) - block_offset = r[1] - block_end = r[2] - if int(r[2]) < int(r[1]): - raise ValueError('cart before the horse, dude') + + (offset, limit, block_offset, block_end) = parse_query_any(r) c = DataCache(session) - (lowest_block, highest_block, tx_cache) = c.load_transactions_with_data(0, 0, block_offset, block_end, oldest=True) # oldest needs to be settable + (lowest_block, highest_block, tx_cache) = c.load_transactions_with_data(offset, limit, block_offset, block_end, oldest=True) # oldest needs to be settable for r in tx_cache: r['date_block'] = r['date_block'].timestamp() @@ -113,3 +157,30 @@ def process_transactions_all_data(session, env): j = json.dumps(o) return ('application/json', j.encode('utf-8'),) + + +def process_transactions_account_data(session, env): + r = re.match(re_transactions_account_data, env.get('PATH_INFO')) + if not r: + return None + logg.debug('match account data') + #if env.get('HTTP_X_CIC_CACHE_MODE') != 'all': + # return None + + (address, offset, limit,) = parse_query_account(r) + + c = DataCache(session) + (lowest_block, highest_block, tx_cache) = c.load_transactions_account_with_data(address, offset, limit) + + for r in tx_cache: + r['date_block'] = r['date_block'].timestamp() + + o = { + 'low': lowest_block, + 'high': highest_block, + 'data': tx_cache, + } + + j = json.dumps(o) + + return ('application/json', j.encode('utf-8'),) diff --git a/apps/cic-cache/cic_cache/runnable/daemons/server.py b/apps/cic-cache/cic_cache/runnable/daemons/server.py index 58542d03..8704619c 100644 --- a/apps/cic-cache/cic_cache/runnable/daemons/server.py +++ b/apps/cic-cache/cic_cache/runnable/daemons/server.py @@ -12,21 +12,20 @@ import cic_cache.cli from cic_cache.db import dsn_from_config from cic_cache.db.models.base import SessionBase from cic_cache.runnable.daemons.query import ( + process_default_limit, process_transactions_account_bloom, + process_transactions_account_data, process_transactions_all_bloom, process_transactions_all_data, ) +import cic_cache.cli logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() -rootdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) -dbdir = os.path.join(rootdir, 'cic_cache', 'db') -migrationsdir = os.path.join(dbdir, 'migrations') -# process args -arg_flags = cic_cache.cli.argflag_std_base -local_arg_flags = cic_cache.cli.argflag_local_task +arg_flags = cic_cache.cli.argflag_std_read +local_arg_flags = cic_cache.cli.argflag_local_sync | cic_cache.cli.argflag_local_task argparser = cic_cache.cli.ArgumentParser(arg_flags) argparser.process_local_flags(local_arg_flags) args = argparser.parse_args() @@ -35,7 +34,7 @@ args = argparser.parse_args() config = cic_cache.cli.Config.from_args(args, arg_flags, local_arg_flags) # connect to database -dsn = dsn_from_config(config) +dsn = dsn_from_config(config, 'cic_cache') SessionBase.connect(dsn, config.true('DATABASE_DEBUG')) @@ -47,9 +46,11 @@ def application(env, start_response): session = SessionBase.create_session() for handler in [ + process_transactions_account_data, + process_transactions_account_bloom, process_transactions_all_data, process_transactions_all_bloom, - process_transactions_account_bloom, + process_default_limit, ]: r = None try: diff --git a/apps/cic-cache/cic_cache/runnable/daemons/tasker.py b/apps/cic-cache/cic_cache/runnable/daemons/tasker.py index 9e990302..f8778e92 100644 --- a/apps/cic-cache/cic_cache/runnable/daemons/tasker.py +++ b/apps/cic-cache/cic_cache/runnable/daemons/tasker.py @@ -3,6 +3,7 @@ import logging import os import sys import argparse +import tempfile # third-party imports import celery @@ -28,7 +29,7 @@ args = argparser.parse_args() config = cic_cache.cli.Config.from_args(args, arg_flags, local_arg_flags) # connect to database -dsn = dsn_from_config(config) +dsn = dsn_from_config(config, 'cic_cache') SessionBase.connect(dsn) # set up celery diff --git a/apps/cic-cache/cic_cache/runnable/daemons/tracker.py b/apps/cic-cache/cic_cache/runnable/daemons/tracker.py index 16ed1f4b..0094b14e 100644 --- a/apps/cic-cache/cic_cache/runnable/daemons/tracker.py +++ b/apps/cic-cache/cic_cache/runnable/daemons/tracker.py @@ -50,7 +50,7 @@ args = argparser.parse_args() config = cic_cache.cli.Config.from_args(args, arg_flags, local_arg_flags) # connect to database -dsn = dsn_from_config(config) +dsn = dsn_from_config(config, 'cic_cache') SessionBase.connect(dsn, debug=config.true('DATABASE_DEBUG')) # set up rpc @@ -95,10 +95,10 @@ def main(): syncer_backends = SQLBackend.resume(chain_spec, block_offset) if len(syncer_backends) == 0: - initial_block_start = config.get('SYNCER_OFFSET') - initial_block_offset = block_offset + initial_block_start = int(config.get('SYNCER_OFFSET')) + initial_block_offset = int(block_offset) if config.get('SYNCER_NO_HISTORY'): - initial_block_start = block_offset + initial_block_start = initial_block_offset initial_block_offset += 1 syncer_backends.append(SQLBackend.initial(chain_spec, initial_block_offset, start_block_height=initial_block_start)) logg.info('found no backends to resume, adding initial sync from history start {} end {}'.format(initial_block_start, initial_block_offset)) diff --git a/apps/cic-cache/cic_cache/version.py b/apps/cic-cache/cic_cache/version.py index 814fc8e0..25a3de36 100644 --- a/apps/cic-cache/cic_cache/version.py +++ b/apps/cic-cache/cic_cache/version.py @@ -5,7 +5,7 @@ version = ( 0, 2, 1, - 'alpha.2', + 'alpha.3', ) version_object = semver.VersionInfo( diff --git a/apps/cic-cache/config/celery.ini b/apps/cic-cache/config/celery.ini deleted file mode 100644 index 6136b166..00000000 --- a/apps/cic-cache/config/celery.ini +++ /dev/null @@ -1,3 +0,0 @@ -[celery] -broker_url = redis:/// -result_url = redis:/// diff --git a/apps/cic-cache/config/cic.ini b/apps/cic-cache/config/cic.ini deleted file mode 100644 index 48c32fad..00000000 --- a/apps/cic-cache/config/cic.ini +++ /dev/null @@ -1,3 +0,0 @@ -[cic] -registry_address = -trust_address = diff --git a/apps/cic-cache/config/config.ini b/apps/cic-cache/config/config.ini new file mode 100644 index 00000000..e69de29b diff --git a/apps/cic-cache/config/database.ini b/apps/cic-cache/config/database.ini deleted file mode 100644 index 7c5836a2..00000000 --- a/apps/cic-cache/config/database.ini +++ /dev/null @@ -1,9 +0,0 @@ -[database] -NAME=cic_cache -USER=postgres -PASSWORD= -HOST=localhost -PORT=5432 -ENGINE=postgresql -DRIVER=psycopg2 -DEBUG=0 diff --git a/apps/cic-cache/config/docker/celery.ini b/apps/cic-cache/config/docker/celery.ini deleted file mode 100644 index 98c5012f..00000000 --- a/apps/cic-cache/config/docker/celery.ini +++ /dev/null @@ -1,3 +0,0 @@ -[celery] -broker_url = redis://localhost:63379 -result_url = redis://localhost:63379 diff --git a/apps/cic-cache/config/docker/cic.ini b/apps/cic-cache/config/docker/cic.ini deleted file mode 100644 index af112724..00000000 --- a/apps/cic-cache/config/docker/cic.ini +++ /dev/null @@ -1,3 +0,0 @@ -[cic] -registry_address = -trust_address = 0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C diff --git a/apps/cic-cache/config/docker/database.ini b/apps/cic-cache/config/docker/database.ini deleted file mode 100644 index db1f5086..00000000 --- a/apps/cic-cache/config/docker/database.ini +++ /dev/null @@ -1,9 +0,0 @@ -[database] -NAME=cic_cache -USER=grassroots -PASSWORD= -HOST=localhost -PORT=63432 -ENGINE=postgresql -DRIVER=psycopg2 -DEBUG=0 diff --git a/apps/cic-cache/config/docker/syncer.ini b/apps/cic-cache/config/docker/syncer.ini deleted file mode 100644 index 255dc00f..00000000 --- a/apps/cic-cache/config/docker/syncer.ini +++ /dev/null @@ -1,4 +0,0 @@ -[syncer] -loop_interval = 1 -offset = 0 -no_history = 0 diff --git a/apps/cic-cache/config/test/bancor.ini b/apps/cic-cache/config/test/bancor.ini deleted file mode 100644 index c1591774..00000000 --- a/apps/cic-cache/config/test/bancor.ini +++ /dev/null @@ -1,2 +0,0 @@ -[bancor] -dir = diff --git a/apps/cic-cache/config/test/cic.ini b/apps/cic-cache/config/test/cic.ini index d985ae3e..59a093c5 100644 --- a/apps/cic-cache/config/test/cic.ini +++ b/apps/cic-cache/config/test/cic.ini @@ -1,4 +1,3 @@ [cic] registry_address = -chain_spec = trust_address = diff --git a/apps/cic-cache/config/test/database.ini b/apps/cic-cache/config/test/database.ini index 113c1fa4..00832932 100644 --- a/apps/cic-cache/config/test/database.ini +++ b/apps/cic-cache/config/test/database.ini @@ -1,5 +1,5 @@ [database] -NAME=cic-cache-test +PREFIX=cic-cache-test USER=postgres PASSWORD= HOST=localhost diff --git a/apps/cic-cache/config/test/eth.ini b/apps/cic-cache/config/test/eth.ini deleted file mode 100644 index 77a00315..00000000 --- a/apps/cic-cache/config/test/eth.ini +++ /dev/null @@ -1,5 +0,0 @@ -[eth] -#ws_provider = ws://localhost:8546 -#ttp_provider = http://localhost:8545 -provider = http://localhost:8545 -#chain_id = diff --git a/apps/cic-cache/doc/openapi/server.yml b/apps/cic-cache/doc/openapi/server.yml index 79d323a1..09889c76 100644 --- a/apps/cic-cache/doc/openapi/server.yml +++ b/apps/cic-cache/doc/openapi/server.yml @@ -1,4 +1,4 @@ -openapi: "3.0.3" +openapi: "3.0.2" info: title: Grassroots Economics CIC Cache description: Cache of processed transaction data from Ethereum blockchain and worker queues @@ -9,17 +9,34 @@ info: email: will@grassecon.org license: name: GPLv3 - version: 0.1.0 + version: 0.2.0 paths: - /tx/{offset}/{limit}: - description: Bloom filter for batch of latest transactions + /defaultlimit: + summary: The default limit value of result sets. + get: + tags: + - transactions + description: + Retrieve default limit + operationId: limit.default + responses: + 200: + description: Limit query successful + content: + application/json: + schema: + $ref: "#/components/schemas/Limit" + + /tx: + summary: Bloom filter for batch of latest transactions + description: Generate a bloom filter of the latest transactions in the cache. The number of maximum number of transactions returned is returned by the `/defaultlimit` API call. get: tags: - transactions description: Retrieve transactions - operationId: tx.get + operationId: tx.get.latest responses: 200: description: Transaction query successful. @@ -29,27 +46,153 @@ paths: $ref: "#/components/schemas/BlocksBloom" - parameters: - - name: offset - in: path - schema: - type: integer - format: int32 - - name: limit - in: path - schema: - type: integer - format: int32 - - - /tx/{address}/{offset}/{limit}: - description: Bloom filter for batch of latest transactions by account + /tx/{limit}: + summary: Bloom filter for batch of latest transactions + description: Generate a bloom filter of the latest transactions in the cache. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. get: tags: - transactions description: Retrieve transactions - operationId: tx.get + operationId: tx.get.latest.limit + responses: + 200: + description: Transaction query successful. Results are ordered from newest to oldest. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + + + /tx/{limit}/{offset}: + summary: Bloom filter for batch of latest transactions + description: Generate a bloom filter of the latest transactions in the cache. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.latest.range + responses: + 200: + description: Transaction query successful. Results are ordered from newest to oldest. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + + + /tx/{limit}/{offset}/{block_offset}: + summary: Bloom filter for batch of transactions since a particular block. + description: Generate a bloom filter of the latest transactions since a particular block in the cache. The block parameter is inclusive. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.latest.range.block.offset + responses: + 200: + description: Transaction query successful. Results are ordered from oldest to newest. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_offset + in: path + required: true + schema: + type: integer + format: int32 + + + /tx/{limit}/{offset}/{block_offset}/{block_end}: + summary: Bloom filter for batch of transactions within a particular block range. + description: Generate a bloom filter of the latest transactions within a particular block range in the cache. The block parameters are inclusive. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.latest.range.block.range + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_end + in: path + required: true + schema: + type: integer + format: int32 + + + /tx/{address}: + summary: Bloom filter for batch of latest transactions by account. + description: Generate a bloom filter of the latest transactions where a specific account is the spender or beneficiary. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.user responses: 200: description: Transaction query successful. @@ -58,6 +201,30 @@ paths: schema: $ref: "#/components/schemas/BlocksBloom" + parameters: + - name: address + in: path + required: true + schema: + type: string + + + /tx/{address}/{limit}: + summary: Bloom filter for batch of latest transactions by account. + description: Generate a bloom filter of the latest transactions where a specific account is the spender or beneficiary. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.user.limit + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" parameters: - name: address @@ -65,26 +232,317 @@ paths: required: true schema: type: string - - name: offset - in: path - schema: - type: integer - format: int32 - name: limit in: path + required: true schema: type: integer format: int32 + + /tx/{address}/{limit}/{offset}: + summary: Bloom filter for batch of latest transactions by account + description: Generate a bloom filter of the latest transactions where a specific account is the spender or beneficiary. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: tx.get.user.range + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/BlocksBloom" + + parameters: + - name: address + in: path + required: true + schema: + type: string + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + + + /txa: + summary: Cached data for latest transactions. + description: Return data entries of the latest transactions in the cache. The number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.latest + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + + /txa/{limit}: + summary: Cached data for latest transactions. + description: Return data entries of the latest transactions in the cache. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.latest.limit + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + + + /txa/{limit}/{offset}: + summary: Cached data for latest transactions. + description: Return data entries of the latest transactions in the cache. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.latest.range + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + + + /txa/{limit}/{offset}/{block_offset}: + summary: Cached data for transactions since a particular block. + description: Return cached data entries of transactions since a particular block. The block parameter is inclusive. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.latest.range.block.offset + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_offset + in: path + required: true + schema: + type: integer + format: int32 + + /txa/{limit}/{offset}/{block_offset}/{block_end}: + summary: Cached data for transactions within a particular block range. + description: Return cached data entries of transactions within a particular block range in the cache. The block parameters are inclusive. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.latest.range.block.range + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_offset + in: path + required: true + schema: + type: integer + format: int32 + - name: block_end + in: path + required: true + schema: + type: integer + format: int32 + + + /txa/{address}: + summary: Cached data for batch of latest transactions by account. + description: Return cached data of the latest transactions where a specific account is the spender or beneficiary. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.user + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: address + in: path + required: true + schema: + type: string + + + /txa/{address}/{limit}: + summary: Cached data for batch of latest transactions by account. + description: Return cached data of the latest transactions where a specific account is the spender or beneficiary. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.user.limit + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: address + in: path + required: true + schema: + type: string + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + + + /txa/{address}/{limit}/{offset}: + summary: Cached data for batch of latest transactions by account. + description: Return cached data of the latest transactions where a specific account is the spender or beneficiary. If `limit` is 0, the number of maximum number of transactions returned is returned by the `/defaultlimit` API call. + get: + tags: + - transactions + description: + Retrieve transactions + operationId: txa.get.user.range + responses: + 200: + description: Transaction query successful. + content: + application/json: + schema: + $ref: "#/components/schemas/TransactionList" + + parameters: + - name: address + in: path + required: true + schema: + type: string + - name: limit + in: path + required: true + schema: + type: integer + format: int32 + - name: offset + in: path + required: true + schema: + type: integer + format: int32 + + components: schemas: + Limit: + type: integer + format: int32 BlocksBloom: type: object properties: low: - type: int + type: integer format: int32 description: The lowest block number included in the filter + high: + type: integer + format: int32 + description: The highest block number included in the filter block_filter: type: string format: byte @@ -97,6 +555,89 @@ components: type: string description: Hashing algorithm (currently only using sha256) filter_rounds: - type: int + type: integer format: int32 description: Number of hash rounds used to create the filter + TransactionList: + type: object + properties: + low: + type: integer + format: int32 + description: The lowest block number included in the result set + high: + type: integer + format: int32 + description: The highest block number included in the filter + data: + type: array + description: Cached transaction data + items: + $ref: "#/components/schemas/Transaction" + Transaction: + type: object + properties: + block_number: + type: integer + format: int64 + description: Block number transaction was included in. + tx_hash: + type: string + description: Transaction hash, in hex. + date_block: + type: integer + format: int32 + description: Block timestamp. + sender: + type: string + description: Spender address, in hex. + recipient: + type: string + description: Beneficiary address, in hex. + from_value: + type: integer + format: int64 + description: Value deducted from spender's balance. + to_value: + type: integer + format: int64 + description: Value added to beneficiary's balance. + source_token: + type: string + description: Network address of token in which `from_value` is denominated. + destination_token: + type: string + description: Network address of token in which `to_value` is denominated. + success: + type: boolean + description: Network consensus state on whether the transaction was successful or not. + tx_type: + type: string + enum: + - erc20.faucet + - faucet.give_to + + examples: + data_last: + summary: Get the latest cached transactions, using the server's default limit. + value: "/txa" + + data_limit: + summary: Get the last 42 cached transactions. + value: "/txa/42" + + data_range: + summary: Get the next 42 cached transactions, starting from the 13th (zero-indexed). + value: "/txa/42/13" + + data_range_block_offset: + summary: Get the next 42 cached transactions, starting from block 1337 (inclusive). + value: "/txa/42/0/1337" + + data_range_block_offset: + summary: Get the next 42 cached transactions within blocks 1337 and 1453 (inclusive). + value: "/txa/42/0/1337/1453" + + data_range_block_range: + summary: Get the next 42 cached transactions after the 13th, within blocks 1337 and 1453 (inclusive). + value: "/txa/42/13/1337/1453" diff --git a/apps/cic-cache/docker/Dockerfile b/apps/cic-cache/docker/Dockerfile index bc7c0dd0..c36b1301 100644 --- a/apps/cic-cache/docker/Dockerfile +++ b/apps/cic-cache/docker/Dockerfile @@ -4,9 +4,9 @@ FROM $DOCKER_REGISTRY/cic-base-images:python-3.8.6-dev-e8eb2ee2 COPY requirements.txt . -ARG EXTRA_PIP_INDEX_URL="https://pip.grassrootseconomics.net" +ARG EXTRA_PIP_INDEX_URL=https://pip.grassrootseconomics.net ARG EXTRA_PIP_ARGS="" -ARG PIP_INDEX_URL="https://pypi.org/simple" +ARG PIP_INDEX_URL=https://pypi.org/simple RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ pip install --index-url $PIP_INDEX_URL \ @@ -14,14 +14,9 @@ RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ -r requirements.txt -COPY . . -RUN python setup.py install - -# ini files in config directory defines the configurable parameters for the application -# they can all be overridden by environment variables -# to generate a list of environment variables from configuration, use: confini-dump -z (executable provided by confini package) -#COPY config/ /usr/local/etc/cic-cache/ +COPY . . +RUN pip install . --extra-index-url $EXTRA_PIP_INDEX_URL # for db migrations COPY ./aux/wait-for-it/wait-for-it.sh ./ diff --git a/apps/cic-cache/docker/db.sh b/apps/cic-cache/docker/db.sh index 002cea14..a5aac6f0 100644 --- a/apps/cic-cache/docker/db.sh +++ b/apps/cic-cache/docker/db.sh @@ -2,5 +2,5 @@ set -e >&2 echo executing database migration -python scripts/migrate.py -c /usr/local/etc/cic-cache --migrations-dir /usr/local/share/cic-cache/alembic -vv +python scripts/migrate_cic_cache.py --migrations-dir /usr/local/share/cic-cache/alembic -vv set +e diff --git a/apps/cic-cache/requirements.txt b/apps/cic-cache/requirements.txt index 8f47f9b5..1ccd4db2 100644 --- a/apps/cic-cache/requirements.txt +++ b/apps/cic-cache/requirements.txt @@ -1,14 +1,15 @@ alembic==1.4.2 -confini>=0.3.6rc4,<0.5.0 +confini~=0.5.3 uwsgi==2.0.19.1 -moolb~=0.1.1b2 -cic-eth-registry~=0.6.1a1 +moolb~=0.2.0 +cic-eth-registry~=0.6.6 SQLAlchemy==1.3.20 semver==2.13.0 psycopg2==2.8.6 celery==4.4.7 redis==3.5.3 -chainsyncer[sql]>=0.0.6a3,<0.1.0 -erc20-faucet>=0.3.2a2, <0.4.0 -chainlib-eth>=0.0.9a14,<0.1.0 -eth-address-index>=0.2.3a4,<0.3.0 +chainsyncer[sql]~=0.0.7 +erc20-faucet~=0.3.2 +chainlib-eth~=0.0.15 +eth-address-index~=0.2.4 +okota~=0.2.5 diff --git a/apps/cic-cache/scripts/migrate.py b/apps/cic-cache/scripts/migrate_cic_cache.py similarity index 56% rename from apps/cic-cache/scripts/migrate.py rename to apps/cic-cache/scripts/migrate_cic_cache.py index 458b1ff2..9b9296c2 100644 --- a/apps/cic-cache/scripts/migrate.py +++ b/apps/cic-cache/scripts/migrate_cic_cache.py @@ -1,54 +1,55 @@ -#!/usr/bin/python +#!/usr/bin/python3 + +# standard imports import os import argparse import logging import re +# external imports import alembic from alembic.config import Config as AlembicConfig import confini +# local imports from cic_cache.db import dsn_from_config +import cic_cache.cli logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() # BUG: the dbdir doesn't work after script install -rootdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +rootdir = os.path.dirname(os.path.dirname(os.path.realpath(cic_cache.__file__))) dbdir = os.path.join(rootdir, 'cic_cache', 'db') -migrationsdir = os.path.join(dbdir, 'migrations') +default_migrations_dir = os.path.join(dbdir, 'migrations') configdir = os.path.join(rootdir, 'cic_cache', 'data', 'config') #config_dir = os.path.join('/usr/local/etc/cic-cache') -argparser = argparse.ArgumentParser() -argparser.add_argument('-c', type=str, help='config file') -argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') -argparser.add_argument('--migrations-dir', dest='migrations_dir', default=migrationsdir, type=str, help='path to alembic migrations directory') +arg_flags = cic_cache.cli.argflag_std_base +local_arg_flags = cic_cache.cli.argflag_local_sync +argparser = cic_cache.cli.ArgumentParser(arg_flags) +argparser.process_local_flags(local_arg_flags) argparser.add_argument('--reset', action='store_true', help='downgrade before upgrading') -argparser.add_argument('-f', action='store_true', help='force action') -argparser.add_argument('-v', action='store_true', help='be verbose') -argparser.add_argument('-vv', action='store_true', help='be more verbose') +argparser.add_argument('-f', '--force', action='store_true', help='force action') +argparser.add_argument('--migrations-dir', dest='migrations_dir', default=default_migrations_dir, type=str, help='migrations directory') args = argparser.parse_args() -if args.vv: - logging.getLogger().setLevel(logging.DEBUG) -elif args.v: - logging.getLogger().setLevel(logging.INFO) +extra_args = { + 'reset': None, + 'force': None, + 'migrations_dir': None, + } +# process config +config = cic_cache.cli.Config.from_args(args, arg_flags, local_arg_flags, extra_args=extra_args) -config = confini.Config(configdir, args.env_prefix) -config.process() -config.censor('PASSWORD', 'DATABASE') -config.censor('PASSWORD', 'SSL') -logg.debug('config:\n{}'.format(config)) - -migrations_dir = os.path.join(args.migrations_dir, config.get('DATABASE_ENGINE')) +migrations_dir = os.path.join(config.get('_MIGRATIONS_DIR'), config.get('DATABASE_ENGINE', 'default')) if not os.path.isdir(migrations_dir): logg.debug('migrations dir for engine {} not found, reverting to default'.format(config.get('DATABASE_ENGINE'))) migrations_dir = os.path.join(args.migrations_dir, 'default') # connect to database -dsn = dsn_from_config(config) +dsn = dsn_from_config(config, 'cic_cache') logg.info('using migrations dir {}'.format(migrations_dir)) diff --git a/apps/cic-cache/setup.cfg b/apps/cic-cache/setup.cfg index f8393c92..99055d22 100644 --- a/apps/cic-cache/setup.cfg +++ b/apps/cic-cache/setup.cfg @@ -1,6 +1,7 @@ [metadata] name = cic-cache description = CIC Cache API and server +version = 0.3.0a2 author = Louis Holbrook author_email = dev@holbrook.no url = https://gitlab.com/grassrootseconomics/cic-eth @@ -34,7 +35,7 @@ packages = cic_cache.runnable.daemons cic_cache.runnable.daemons.filters scripts = - ./scripts/migrate.py + ./scripts/migrate_cic_cache.py [options.entry_points] console_scripts = diff --git a/apps/cic-cache/setup.py b/apps/cic-cache/setup.py index 84d9731a..81d76924 100644 --- a/apps/cic-cache/setup.py +++ b/apps/cic-cache/setup.py @@ -1,38 +1,39 @@ from setuptools import setup -import configparser +# import configparser import os -import time -from cic_cache.version import ( - version_object, - version_string - ) +# import time -class PleaseCommitFirstError(Exception): - pass - -def git_hash(): - import subprocess - git_diff = subprocess.run(['git', 'diff'], capture_output=True) - if len(git_diff.stdout) > 0: - raise PleaseCommitFirstError() - git_hash = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output=True) - git_hash_brief = git_hash.stdout.decode('utf-8')[:8] - return git_hash_brief - -version_string = str(version_object) - -try: - version_git = git_hash() - version_string += '+build.{}'.format(version_git) -except FileNotFoundError: - time_string_pair = str(time.time()).split('.') - version_string += '+build.{}{:<09d}'.format( - time_string_pair[0], - int(time_string_pair[1]), - ) -print('final version string will be {}'.format(version_string)) +# from cic_cache.version import ( +# version_object, +# version_string +# ) +# +# class PleaseCommitFirstError(Exception): +# pass +# +# def git_hash(): +# import subprocess +# git_diff = subprocess.run(['git', 'diff'], capture_output=True) +# if len(git_diff.stdout) > 0: +# raise PleaseCommitFirstError() +# git_hash = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output=True) +# git_hash_brief = git_hash.stdout.decode('utf-8')[:8] +# return git_hash_brief +# +# version_string = str(version_object) +# +# try: +# version_git = git_hash() +# version_string += '+build.{}'.format(version_git) +# except FileNotFoundError: +# time_string_pair = str(time.time()).split('.') +# version_string += '+build.{}{:<09d}'.format( +# time_string_pair[0], +# int(time_string_pair[1]), +# ) +# print('final version string will be {}'.format(version_string)) requirements = [] f = open('requirements.txt', 'r') @@ -52,9 +53,8 @@ while True: test_requirements.append(l.rstrip()) f.close() - setup( - version=version_string, + # version=version_string, install_requires=requirements, tests_require=test_requirements, ) diff --git a/apps/cic-cache/test_requirements.txt b/apps/cic-cache/test_requirements.txt index a539ecaa..b8ff25eb 100644 --- a/apps/cic-cache/test_requirements.txt +++ b/apps/cic-cache/test_requirements.txt @@ -7,4 +7,4 @@ pytest-celery==0.0.0a1 eth_tester==0.5.0b3 py-evm==0.3.0a20 sarafu-faucet~=0.0.7a1 -erc20-transfer-authorization>=0.3.5a1,<0.4.0 +erc20-transfer-authorization~=0.3.6 diff --git a/apps/cic-cache/tests/conftest.py b/apps/cic-cache/tests/conftest.py index 478c261f..8a708ff2 100644 --- a/apps/cic-cache/tests/conftest.py +++ b/apps/cic-cache/tests/conftest.py @@ -6,6 +6,7 @@ import datetime # external imports import pytest import moolb +from chainlib.encode import TxHexNormalizer # local imports from cic_cache import db @@ -42,6 +43,8 @@ def txs( list_tokens, ): + tx_normalize = TxHexNormalizer() + session = init_database tx_number = 13 @@ -54,10 +57,10 @@ def txs( tx_hash_first, list_defaults['block'], tx_number, - list_actors['alice'], - list_actors['bob'], - list_tokens['foo'], - list_tokens['foo'], + tx_normalize.wallet_address(list_actors['alice']), + tx_normalize.wallet_address(list_actors['bob']), + tx_normalize.executable_address(list_tokens['foo']), + tx_normalize.executable_address(list_tokens['foo']), 1024, 2048, True, @@ -74,10 +77,10 @@ def txs( tx_hash_second, list_defaults['block']-1, tx_number, - list_actors['diane'], - list_actors['alice'], - list_tokens['foo'], - list_tokens['foo'], + tx_normalize.wallet_address(list_actors['diane']), + tx_normalize.wallet_address(list_actors['alice']), + tx_normalize.executable_address(list_tokens['foo']), + tx_normalize.wallet_address(list_tokens['foo']), 1024, 2048, False, @@ -103,6 +106,8 @@ def more_txs( session = init_database + tx_normalize = TxHexNormalizer() + tx_number = 666 tx_hash = '0x' + os.urandom(32).hex() tx_signed = '0x' + os.urandom(128).hex() @@ -115,10 +120,10 @@ def more_txs( tx_hash, list_defaults['block']+2, tx_number, - list_actors['alice'], - list_actors['diane'], - list_tokens['bar'], - list_tokens['bar'], + tx_normalize.wallet_address(list_actors['alice']), + tx_normalize.wallet_address(list_actors['diane']), + tx_normalize.executable_address(list_tokens['bar']), + tx_normalize.executable_address(list_tokens['bar']), 2048, 4096, False, diff --git a/apps/cic-cache/tests/fixtures_config.py b/apps/cic-cache/tests/fixtures_config.py index 17a41c93..b566914b 100644 --- a/apps/cic-cache/tests/fixtures_config.py +++ b/apps/cic-cache/tests/fixtures_config.py @@ -14,7 +14,8 @@ logg = logging.getLogger(__file__) @pytest.fixture(scope='session') def load_config(): config_dir = os.path.join(root_dir, 'config/test') - conf = confini.Config(config_dir, 'CICTEST') + schema_config_dir = os.path.join(root_dir, 'cic_cache', 'data', 'config') + conf = confini.Config(schema_config_dir, 'CICTEST', override_dirs=config_dir) conf.process() logg.debug('config {}'.format(conf)) return conf diff --git a/apps/cic-cache/tests/fixtures_database.py b/apps/cic-cache/tests/fixtures_database.py index f5ff610a..a0faa639 100644 --- a/apps/cic-cache/tests/fixtures_database.py +++ b/apps/cic-cache/tests/fixtures_database.py @@ -24,11 +24,15 @@ def database_engine( if load_config.get('DATABASE_ENGINE') == 'sqlite': SessionBase.transactional = False SessionBase.poolable = False + name = 'cic_cache' + database_name = name + if load_config.get('DATABASE_PREFIX'): + database_name = '{}_{}'.format(load_config.get('DATABASE_PREFIX'), database_name) try: - os.unlink(load_config.get('DATABASE_NAME')) + os.unlink(database_name) except FileNotFoundError: pass - dsn = dsn_from_config(load_config) + dsn = dsn_from_config(load_config, name) SessionBase.connect(dsn, debug=load_config.true('DATABASE_DEBUG')) return dsn diff --git a/apps/cic-cache/tests/test_api.py b/apps/cic-cache/tests/test_api.py index 02561bca..618fff7c 100644 --- a/apps/cic-cache/tests/test_api.py +++ b/apps/cic-cache/tests/test_api.py @@ -14,7 +14,7 @@ def test_api_all_data( ): env = { - 'PATH_INFO': '/txa/410000/420000', + 'PATH_INFO': '/txa/100/0/410000/420000', 'HTTP_X_CIC_CACHE_MODE': 'all', } j = process_transactions_all_data(init_database, env) @@ -23,7 +23,7 @@ def test_api_all_data( assert len(o['data']) == 2 env = { - 'PATH_INFO': '/txa/420000/410000', + 'PATH_INFO': '/txa/100/0/420000/410000', 'HTTP_X_CIC_CACHE_MODE': 'all', } diff --git a/apps/cic-cache/tests/test_cache.py b/apps/cic-cache/tests/test_cache.py index a5fdcc0d..589db37f 100644 --- a/apps/cic-cache/tests/test_cache.py +++ b/apps/cic-cache/tests/test_cache.py @@ -6,6 +6,7 @@ import json # external imports import pytest +from chainlib.encode import TxHexNormalizer # local imports from cic_cache import db @@ -62,6 +63,8 @@ def test_cache_ranges( session = init_database + tx_normalize = TxHexNormalizer() + oldest = list_defaults['block'] - 1 mid = list_defaults['block'] newest = list_defaults['block'] + 2 @@ -100,32 +103,39 @@ def test_cache_ranges( assert b[1] == mid # now check when supplying account - b = c.load_transactions_account(list_actors['alice'], 0, 100) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account(account, 0, 100) assert b[0] == oldest assert b[1] == newest - b = c.load_transactions_account(list_actors['bob'], 0, 100) + account = tx_normalize.wallet_address(list_actors['bob']) + b = c.load_transactions_account(account, 0, 100) assert b[0] == mid assert b[1] == mid - b = c.load_transactions_account(list_actors['diane'], 0, 100) + account = tx_normalize.wallet_address(list_actors['diane']) + b = c.load_transactions_account(account, 0, 100) assert b[0] == oldest assert b[1] == newest # add block filter to the mix - b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account(account, 0, 100, block_offset=list_defaults['block']) assert b[0] == mid assert b[1] == newest - b = c.load_transactions_account(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account(account, 0, 100, block_offset=list_defaults['block']) assert b[0] == mid assert b[1] == newest - b = c.load_transactions_account(list_actors['bob'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['bob']) + b = c.load_transactions_account(account, 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) assert b[0] == mid assert b[1] == mid - b = c.load_transactions_account(list_actors['diane'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['diane']) + b = c.load_transactions_account(account, 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) assert b[0] == oldest assert b[1] == oldest @@ -140,6 +150,8 @@ def test_cache_ranges_data( session = init_database + tx_normalize = TxHexNormalizer() + oldest = list_defaults['block'] - 1 mid = list_defaults['block'] newest = list_defaults['block'] + 2 @@ -203,7 +215,8 @@ def test_cache_ranges_data( assert b[2][1]['tx_hash'] == more_txs[1] # now check when supplying account - b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account_with_data(account, 0, 100) assert b[0] == oldest assert b[1] == newest assert len(b[2]) == 3 @@ -211,13 +224,15 @@ def test_cache_ranges_data( assert b[2][1]['tx_hash'] == more_txs[1] assert b[2][2]['tx_hash'] == more_txs[2] - b = c.load_transactions_account_with_data(list_actors['bob'], 0, 100) + account = tx_normalize.wallet_address(list_actors['bob']) + b = c.load_transactions_account_with_data(account, 0, 100) assert b[0] == mid assert b[1] == mid assert len(b[2]) == 1 assert b[2][0]['tx_hash'] == more_txs[1] - b = c.load_transactions_account_with_data(list_actors['diane'], 0, 100) + account = tx_normalize.wallet_address(list_actors['diane']) + b = c.load_transactions_account_with_data(account, 0, 100) assert b[0] == oldest assert b[1] == newest assert len(b[2]) == 2 @@ -225,27 +240,31 @@ def test_cache_ranges_data( assert b[2][1]['tx_hash'] == more_txs[2] # add block filter to the mix - b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account_with_data(account, 0, 100, block_offset=list_defaults['block']) assert b[0] == mid assert b[1] == newest assert len(b[2]) == 2 assert b[2][0]['tx_hash'] == more_txs[0] assert b[2][1]['tx_hash'] == more_txs[1] - b = c.load_transactions_account_with_data(list_actors['alice'], 0, 100, block_offset=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['alice']) + b = c.load_transactions_account_with_data(account, 0, 100, block_offset=list_defaults['block']) assert b[0] == mid assert b[1] == newest assert len(b[2]) == 2 assert b[2][0]['tx_hash'] == more_txs[0] assert b[2][1]['tx_hash'] == more_txs[1] - b = c.load_transactions_account_with_data(list_actors['bob'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['bob']) + b = c.load_transactions_account_with_data(account, 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) assert b[0] == mid assert b[1] == mid assert len(b[2]) == 1 assert b[2][0]['tx_hash'] == more_txs[1] - b = c.load_transactions_account_with_data(list_actors['diane'], 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) + account = tx_normalize.wallet_address(list_actors['diane']) + b = c.load_transactions_account_with_data(account, 0, 100, block_offset=list_defaults['block'] - 1, block_limit=list_defaults['block']) assert b[0] == oldest assert b[1] == oldest assert len(b[2]) == 1 diff --git a/apps/cic-cache/tests/test_query.py b/apps/cic-cache/tests/test_query.py index fcc06e2f..ac2b1e35 100644 --- a/apps/cic-cache/tests/test_query.py +++ b/apps/cic-cache/tests/test_query.py @@ -82,7 +82,7 @@ def test_query_regex( [ ('alice', None, None, [(420000, 13), (419999, 42)]), ('alice', None, 1, [(420000, 13)]), - ('alice', 1, None, [(419999, 42)]), # 420000 == list_defaults['block'] + ('alice', 1, 1, [(419999, 42)]), # 420000 == list_defaults['block'] ('alice', 2, None, []), # 420000 == list_defaults['block'] ], ) @@ -107,10 +107,11 @@ def test_query_process_txs_account( path_info = '/tx/user/0x' + strip_0x(actor) if query_offset != None: path_info += '/' + str(query_offset) - if query_limit != None: - if query_offset == None: - path_info += '/0' - path_info += '/' + str(query_limit) + if query_limit == None: + query_limit = 100 + path_info += '/' + str(query_limit) + if query_offset == None: + path_info += '/0' env = { 'PATH_INFO': path_info, } @@ -192,7 +193,7 @@ def test_query_process_txs_bloom( @pytest.mark.parametrize( 'query_block_start, query_block_end, query_match_count', [ - (None, 42, 0), + (1, 42, 0), (420000, 420001, 1), (419999, 419999, 1), # matches are inclusive (419999, 420000, 2), @@ -211,7 +212,7 @@ def test_query_process_txs_data( query_match_count, ): - path_info = '/txa' + path_info = '/txa/100/0' if query_block_start != None: path_info += '/' + str(query_block_start) if query_block_end != None: @@ -227,4 +228,5 @@ def test_query_process_txs_data( assert r != None o = json.loads(r[1]) + logg.debug('oo {}'.format(o)) assert len(o['data']) == query_match_count diff --git a/apps/cic-eth-aux/erc20-demurrage-token/requirements.txt b/apps/cic-eth-aux/erc20-demurrage-token/requirements.txt index 618aae32..09de874e 100644 --- a/apps/cic-eth-aux/erc20-demurrage-token/requirements.txt +++ b/apps/cic-eth-aux/erc20-demurrage-token/requirements.txt @@ -1,5 +1,5 @@ celery==4.4.7 -erc20-demurrage-token~=0.0.5a3 -cic-eth-registry~=0.6.1a6 -chainlib~=0.0.9rc1 -cic_eth~=0.12.4a11 +erc20-demurrage-token~=0.0.6 +cic-eth-registry~=0.6.3 +chainlib~=0.0.14 +cic_eth~=0.12.6 diff --git a/apps/cic-eth-aux/erc20-demurrage-token/setup.cfg b/apps/cic-eth-aux/erc20-demurrage-token/setup.cfg index aeddf926..7d48d07c 100644 --- a/apps/cic-eth-aux/erc20-demurrage-token/setup.cfg +++ b/apps/cic-eth-aux/erc20-demurrage-token/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = cic-eth-aux-erc20-demurrage-token -version = 0.0.2a7 +version = 0.0.3 description = cic-eth tasks supporting erc20 demurrage token author = Louis Holbrook author_email = dev@holbrook.no diff --git a/apps/cic-eth/admin_requirements.txt b/apps/cic-eth/admin_requirements.txt index cd9fcc74..7649b394 100644 --- a/apps/cic-eth/admin_requirements.txt +++ b/apps/cic-eth/admin_requirements.txt @@ -1,5 +1,4 @@ SQLAlchemy==1.3.20 -cic-eth-registry>=0.6.1a6,<0.7.0 -hexathon~=0.0.1a8 -chainqueue>=0.0.4a6,<0.1.0 -eth-erc20>=0.1.2a2,<0.2.0 +hexathon~=0.1.0 +chainqueue~=0.0.6a4 +eth-erc20~=0.1.5 diff --git a/apps/cic-eth/cic_eth/api/admin.py b/apps/cic-eth/cic_eth/api/admin.py index 33bc2079..0edaa9e7 100644 --- a/apps/cic-eth/cic_eth/api/admin.py +++ b/apps/cic-eth/cic_eth/api/admin.py @@ -123,7 +123,7 @@ class AdminApi: return s_lock.apply_async() - def tag_account(self, tag, address_hex, chain_spec): + def tag_account(self, chain_spec, tag, address): """Persistently associate an address with a plaintext tag. Some tags are known by the system and is used to resolve addresses to use for certain transactions. @@ -138,7 +138,7 @@ class AdminApi: 'cic_eth.eth.account.set_role', [ tag, - address_hex, + address, chain_spec.asdict(), ], queue=self.queue, @@ -146,6 +146,30 @@ class AdminApi: return s_tag.apply_async() + def get_tag_account(self, chain_spec, tag=None, address=None): + if address != None: + s_tag = celery.signature( + 'cic_eth.eth.account.role', + [ + address, + chain_spec.asdict(), + ], + queue=self.queue, + ) + + else: + s_tag = celery.signature( + 'cic_eth.eth.account.role_account', + [ + tag, + chain_spec.asdict(), + ], + queue=self.queue, + ) + + return s_tag.apply_async() + + def have_account(self, address_hex, chain_spec): s_have = celery.signature( 'cic_eth.eth.account.have', @@ -503,7 +527,7 @@ class AdminApi: queue=self.queue, ) t = s.apply_async() - role = t.get() + role = t.get()[0][1] if role != None: tx['sender_description'] = role @@ -556,7 +580,7 @@ class AdminApi: queue=self.queue, ) t = s.apply_async() - role = t.get() + role = t.get()[0][1] if role != None: tx['recipient_description'] = role diff --git a/apps/cic-eth/cic_eth/api/api_task.py b/apps/cic-eth/cic_eth/api/api_task.py index b00f1c45..39e67fce 100644 --- a/apps/cic-eth/cic_eth/api/api_task.py +++ b/apps/cic-eth/cic_eth/api/api_task.py @@ -512,7 +512,7 @@ class Api(ApiBase): :param password: Password to encode the password with in the backend (careful, you will have to remember it) :type password: str :param register: Register the new account in accounts index backend - :type password: bool + :type register: bool :returns: uuid of root task :rtype: celery.Task """ diff --git a/apps/cic-eth/cic_eth/check/gas.py b/apps/cic-eth/cic_eth/check/gas.py index 4add981b..56fe23ed 100644 --- a/apps/cic-eth/cic_eth/check/gas.py +++ b/apps/cic-eth/cic_eth/check/gas.py @@ -12,8 +12,9 @@ from cic_eth.db.models.base import SessionBase from cic_eth.db.enum import LockEnum from cic_eth.error import LockedError from cic_eth.admin.ctrl import check_lock +from cic_eth.eth.gas import have_gas_minimum -logg = logging.getLogger().getChild(__name__) +logg = logging.getLogger(__name__) def health(*args, **kwargs): @@ -31,18 +32,15 @@ def health(*args, **kwargs): return True gas_provider = AccountRole.get_address('GAS_GIFTER', session=session) + min_gas = int(config.get('ETH_GAS_HOLDER_MINIMUM_UNITS')) * int(config.get('ETH_GAS_GIFTER_REFILL_BUFFER')) + if config.get('ETH_MIN_FEE_PRICE'): + min_gas *= int(config.get('ETH_MIN_FEE_PRICE')) + + r = have_gas_minimum(chain_spec, gas_provider, min_gas, session=session) + session.close() + + if not r: + logg.error('EEK! gas gifter has balance {}, below minimum {}'.format(r, min_gas)) - rpc = RPCConnection.connect(chain_spec, 'default') - o = balance(gas_provider) - r = rpc.do(o) - try: - r = int(r, 16) - except TypeError: - r = int(r) - gas_min = int(config.get('ETH_GAS_GIFTER_MINIMUM_BALANCE')) - if r < gas_min: - logg.error('EEK! gas gifter has balance {}, below minimum {}'.format(r, gas_min)) - return False - - return True + return r diff --git a/apps/cic-eth/cic_eth/check/start.py b/apps/cic-eth/cic_eth/check/start.py new file mode 100644 index 00000000..720c80e0 --- /dev/null +++ b/apps/cic-eth/cic_eth/check/start.py @@ -0,0 +1,18 @@ +# external imports +from chainlib.chain import ChainSpec + +# local imports +from cic_eth.admin.ctrl import check_lock +from cic_eth.enum import LockEnum +from cic_eth.error import LockedError + + +def health(*args, **kwargs): + config = kwargs['config'] + chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) + + try: + check_lock(None, chain_spec.asdict(), LockEnum.START) + except LockedError as e: + return False + return True diff --git a/apps/cic-eth/cic_eth/cli/arg.py b/apps/cic-eth/cic_eth/cli/arg.py index 91fcc071..acdbc632 100644 --- a/apps/cic-eth/cic_eth/cli/arg.py +++ b/apps/cic-eth/cic_eth/cli/arg.py @@ -16,16 +16,22 @@ class ArgumentParser(BaseArgumentParser): self.add_argument('--redis-port', dest='redis_port', type=int, help='redis host to use for task submission') self.add_argument('--redis-db', dest='redis_db', type=int, help='redis db to use') if local_arg_flags & CICFlag.REDIS_CALLBACK: - self.add_argument('--redis-host-callback', dest='redis_host_callback', default='localhost', type=str, help='redis host to use for callback') - self.add_argument('--redis-port-callback', dest='redis_port_callback', default=6379, type=int, help='redis port to use for callback') + self.add_argument('--redis-host-callback', dest='redis_host_callback', type=str, help='redis host to use for callback (defaults to redis host)') + self.add_argument('--redis-port-callback', dest='redis_port_callback', type=int, help='redis port to use for callback (defaults to redis port)') self.add_argument('--redis-timeout', default=20.0, type=float, help='Redis callback timeout') if local_arg_flags & CICFlag.CELERY: + self.add_argument('--celery-scheme', type=str, help='Celery broker scheme (defaults to "redis")') + self.add_argument('--celery-host', type=str, help='Celery broker host (defaults to redis host)') + self.add_argument('--celery-port', type=str, help='Celery broker port (defaults to redis port)') + self.add_argument('--celery-db', type=int, help='Celery broker db (defaults to redis db)') + self.add_argument('--celery-result-scheme', type=str, help='Celery result backend scheme (defaults to celery broker scheme)') + self.add_argument('--celery-result-host', type=str, help='Celery result backend host (defaults to celery broker host)') + self.add_argument('--celery-result-port', type=str, help='Celery result backend port (defaults to celery broker port)') + self.add_argument('--celery-result-db', type=int, help='Celery result backend db (defaults to celery broker db)') + self.add_argument('--celery-no-result', action='store_true', help='Disable the Celery results backend') self.add_argument('-q', '--celery-queue', dest='celery_queue', type=str, default='cic-eth', help='Task queue') if local_arg_flags & CICFlag.SYNCER: - self.add_argument('--offset', type=int, default=0, help='Start block height for initial history sync') + self.add_argument('--offset', type=int, help='Start block height for initial history sync') self.add_argument('--no-history', action='store_true', dest='no_history', help='Skip initial history sync') if local_arg_flags & CICFlag.CHAIN: self.add_argument('-r', '--registry-address', type=str, dest='registry_address', help='CIC registry contract address') - - - diff --git a/apps/cic-eth/cic_eth/cli/base.py b/apps/cic-eth/cic_eth/cli/base.py index b8b3ef2f..71108b29 100644 --- a/apps/cic-eth/cic_eth/cli/base.py +++ b/apps/cic-eth/cic_eth/cli/base.py @@ -24,8 +24,8 @@ class CICFlag(enum.IntEnum): # sync - nibble 4 SYNCER = 4096 - -argflag_local_task = CICFlag.CELERY +argflag_local_base = argflag_std_base | Flag.CHAIN_SPEC +argflag_local_task = CICFlag.CELERY argflag_local_taskcallback = argflag_local_task | CICFlag.REDIS | CICFlag.REDIS_CALLBACK argflag_local_chain = CICFlag.CHAIN argflag_local_sync = CICFlag.SYNCER | CICFlag.CHAIN diff --git a/apps/cic-eth/cic_eth/cli/config.py b/apps/cic-eth/cic_eth/cli/config.py index 8bfadc67..7dfc5d75 100644 --- a/apps/cic-eth/cic_eth/cli/config.py +++ b/apps/cic-eth/cic_eth/cli/config.py @@ -1,12 +1,18 @@ # standard imports import os import logging +import urllib.parse +import copy # external imports from chainlib.eth.cli import ( Config as BaseConfig, Flag, ) +from urlybird.merge import ( + urlhostmerge, + urlmerge, + ) # local imports from .base import CICFlag @@ -40,6 +46,7 @@ class Config(BaseConfig): if local_arg_flags & CICFlag.CHAIN: local_args_override['CIC_REGISTRY_ADDRESS'] = getattr(args, 'registry_address') + if local_arg_flags & CICFlag.CELERY: local_args_override['CELERY_QUEUE'] = getattr(args, 'celery_queue') @@ -49,15 +56,71 @@ class Config(BaseConfig): config.dict_override(local_args_override, 'local cli args') - if local_arg_flags & CICFlag.REDIS_CALLBACK: - config.add(getattr(args, 'redis_host_callback'), '_REDIS_HOST_CALLBACK') - config.add(getattr(args, 'redis_port_callback'), '_REDIS_PORT_CALLBACK') - + local_celery_args_override = {} if local_arg_flags & CICFlag.CELERY: + hostport = urlhostmerge( + None, + config.get('REDIS_HOST'), + config.get('REDIS_PORT'), + ) + db = getattr(args, 'redis_db', None) + if db != None: + db = str(db) + + redis_url = ( + 'redis', + hostport, + db, + ) + + + celery_config_url = urllib.parse.urlsplit(config.get('CELERY_BROKER_URL')) + hostport = urlhostmerge( + celery_config_url[1], + getattr(args, 'celery_host', None), + getattr(args, 'celery_port', None), + ) + db = getattr(args, 'redis_db', None) + if db != None: + db = str(db) + celery_arg_url = ( + getattr(args, 'celery_scheme', None), + hostport, + db, + ) + + celery_url = urlmerge(redis_url, celery_config_url, celery_arg_url) + celery_url_string = urllib.parse.urlunsplit(celery_url) + local_celery_args_override['CELERY_BROKER_URL'] = celery_url_string + if not getattr(args, 'celery_no_result'): + local_celery_args_override['CELERY_RESULT_URL'] = config.get('CELERY_RESULT_URL') + if local_celery_args_override['CELERY_RESULT_URL'] == None: + local_celery_args_override['CELERY_RESULT_URL'] = local_celery_args_override['CELERY_BROKER_URL'] + celery_config_url = urllib.parse.urlsplit(local_celery_args_override['CELERY_RESULT_URL']) + hostport = urlhostmerge( + celery_config_url[1], + getattr(args, 'celery_result_host', None), + getattr(args, 'celery_result_port', None), + ) + celery_arg_url = ( + getattr(args, 'celery_result_scheme', None), + hostport, + getattr(args, 'celery_result_db', None), + ) + celery_url = urlmerge(celery_config_url, celery_arg_url) + logg.debug('celery url {} {}'.format(celery_config_url, celery_url)) + celery_url_string = urllib.parse.urlunsplit(celery_url) + local_celery_args_override['CELERY_RESULT_URL'] = celery_url_string config.add(config.true('CELERY_DEBUG'), 'CELERY_DEBUG', exists_ok=True) + config.dict_override(local_celery_args_override, 'local celery cli args') + + if local_arg_flags & CICFlag.REDIS_CALLBACK: + redis_host_callback = getattr(args, 'redis_host_callback', config.get('REDIS_HOST')) + redis_port_callback = getattr(args, 'redis_port_callback', config.get('REDIS_PORT')) + config.add(redis_host_callback, '_REDIS_HOST_CALLBACK') + config.add(redis_port_callback, '_REDIS_PORT_CALLBACK') + logg.debug('config loaded:\n{}'.format(config)) return config - - diff --git a/apps/cic-eth/cic_eth/data/config/celery.ini b/apps/cic-eth/cic_eth/data/config/celery.ini index f2ad10ab..5b68ee98 100644 --- a/apps/cic-eth/cic_eth/data/config/celery.ini +++ b/apps/cic-eth/cic_eth/data/config/celery.ini @@ -1,5 +1,5 @@ [celery] -broker_url = redis://localhost:6379 +broker_url = result_url = queue = cic-eth debug = 0 diff --git a/apps/cic-eth/cic_eth/data/config/cic.ini b/apps/cic-eth/cic_eth/data/config/cic.ini index 103566ff..7d36d459 100644 --- a/apps/cic-eth/cic_eth/data/config/cic.ini +++ b/apps/cic-eth/cic_eth/data/config/cic.ini @@ -2,5 +2,5 @@ registry_address = trust_address = default_token_symbol = -health_modules = cic_eth.check.db,cic_eth.check.redis,cic_eth.check.signer,cic_eth.check.gas +health_modules = cic_eth.check.db,cic_eth.check.redis,cic_eth.check.signer,cic_eth.check.gas,cic_eth.check.start run_dir = /run diff --git a/apps/cic-eth/cic_eth/data/config/eth.ini b/apps/cic-eth/cic_eth/data/config/eth.ini index fd58e918..2de2d1bd 100644 --- a/apps/cic-eth/cic_eth/data/config/eth.ini +++ b/apps/cic-eth/cic_eth/data/config/eth.ini @@ -1,2 +1,6 @@ [eth] -gas_gifter_minimum_balance = 10000000000000000000000 +gas_holder_minimum_units = 180000 +gas_holder_refill_units = 15 +gas_holder_refill_threshold = 3 +gas_gifter_refill_buffer = 3 +min_fee_price = 1 diff --git a/apps/cic-eth/cic_eth/db/migrations/default/versions/75d4767b3031_lock.py b/apps/cic-eth/cic_eth/db/migrations/default/versions/75d4767b3031_lock.py index dec5cf52..5c2936d8 100644 --- a/apps/cic-eth/cic_eth/db/migrations/default/versions/75d4767b3031_lock.py +++ b/apps/cic-eth/cic_eth/db/migrations/default/versions/75d4767b3031_lock.py @@ -23,7 +23,7 @@ def upgrade(): op.create_table( 'lock', sa.Column('id', sa.Integer, primary_key=True), - sa.Column("address", sa.String(42), nullable=True), + sa.Column("address", sa.String, nullable=True), sa.Column('blockchain', sa.String), sa.Column("flags", sa.BIGINT(), nullable=False, default=0), sa.Column("date_created", sa.DateTime, nullable=False, default=datetime.datetime.utcnow), diff --git a/apps/cic-eth/cic_eth/db/migrations/default/versions/c91cafc3e0c1_add_gas_cache.py b/apps/cic-eth/cic_eth/db/migrations/default/versions/c91cafc3e0c1_add_gas_cache.py new file mode 100644 index 00000000..cc1b0e09 --- /dev/null +++ b/apps/cic-eth/cic_eth/db/migrations/default/versions/c91cafc3e0c1_add_gas_cache.py @@ -0,0 +1,31 @@ +"""Add gas cache + +Revision ID: c91cafc3e0c1 +Revises: aee12aeb47ec +Create Date: 2021-10-28 20:45:34.239865 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'c91cafc3e0c1' +down_revision = 'aee12aeb47ec' +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'gas_cache', + sa.Column('id', sa.Integer, primary_key=True), + sa.Column("address", sa.String, nullable=False), + sa.Column("tx_hash", sa.String, nullable=True), + sa.Column("method", sa.String, nullable=True), + sa.Column("value", sa.BIGINT(), nullable=False), + ) + + +def downgrade(): + op.drop_table('gas_cache') diff --git a/apps/cic-eth/cic_eth/db/models/gas_cache.py b/apps/cic-eth/cic_eth/db/models/gas_cache.py new file mode 100644 index 00000000..af514e1e --- /dev/null +++ b/apps/cic-eth/cic_eth/db/models/gas_cache.py @@ -0,0 +1,27 @@ +# standard imports +import logging + +# external imports +from sqlalchemy import Column, String, NUMERIC + +# local imports +from .base import SessionBase + +logg = logging.getLogger(__name__) + + +class GasCache(SessionBase): + """Provides gas budget cache for token operations + """ + __tablename__ = 'gas_cache' + + address = Column(String()) + tx_hash = Column(String()) + method = Column(String()) + value = Column(NUMERIC()) + + def __init__(self, address, method, value, tx_hash): + self.address = address + self.tx_hash = tx_hash + self.method = method + self.value = value diff --git a/apps/cic-eth/cic_eth/db/models/nonce.py b/apps/cic-eth/cic_eth/db/models/nonce.py index 280e94ca..71f404f6 100644 --- a/apps/cic-eth/cic_eth/db/models/nonce.py +++ b/apps/cic-eth/cic_eth/db/models/nonce.py @@ -12,7 +12,7 @@ from cic_eth.error import ( IntegrityError, ) -logg = logging.getLogger() +logg = logging.getLogger(__name__) class Nonce(SessionBase): @@ -21,7 +21,7 @@ class Nonce(SessionBase): __tablename__ = 'nonce' nonce = Column(Integer) - address_hex = Column(String(42)) + address_hex = Column(String(40)) @staticmethod diff --git a/apps/cic-eth/cic_eth/db/models/role.py b/apps/cic-eth/cic_eth/db/models/role.py index 9343f8d4..a9a12a65 100644 --- a/apps/cic-eth/cic_eth/db/models/role.py +++ b/apps/cic-eth/cic_eth/db/models/role.py @@ -24,8 +24,22 @@ class AccountRole(SessionBase): tag = Column(Text) address_hex = Column(String(42)) - - # TODO: + + @staticmethod + def all(session=None): + session = SessionBase.bind_session(session) + + pairs = [] + + q = session.query(AccountRole.tag, AccountRole.address_hex) + for r in q.all(): + pairs.append((r[1], r[0]),) + + SessionBase.release_session(session) + + return pairs + + @staticmethod def get_address(tag, session): """Get Ethereum address matching the given tag diff --git a/apps/cic-eth/cic_eth/enum.py b/apps/cic-eth/cic_eth/enum.py index 2ce3eccd..3cc85ff8 100644 --- a/apps/cic-eth/cic_eth/enum.py +++ b/apps/cic-eth/cic_eth/enum.py @@ -69,9 +69,12 @@ class StatusEnum(enum.IntEnum): class LockEnum(enum.IntEnum): """ STICKY: When set, reset is not possible + INIT: When set, startup is possible without second level sanity checks (e.g. gas gifter balance) + START: When set, startup is not possible, regardless of state CREATE: Disable creation of accounts SEND: Disable sending to network QUEUE: Disable queueing new or modified transactions + QUERY: Disable all queue state and transaction queries """ STICKY=1 INIT=2 @@ -79,7 +82,8 @@ class LockEnum(enum.IntEnum): SEND=8 QUEUE=16 QUERY=32 - ALL=int(0xfffffffffffffffe) + START=int(0x80000000) + ALL=int(0x7ffffffe) def status_str(v, bits_only=False): diff --git a/apps/cic-eth/cic_eth/error.py b/apps/cic-eth/cic_eth/error.py index 7e8dbfa6..9c0689a8 100644 --- a/apps/cic-eth/cic_eth/error.py +++ b/apps/cic-eth/cic_eth/error.py @@ -64,8 +64,10 @@ class LockedError(Exception): class SeppukuError(Exception): """Exception base class for all errors that should cause system shutdown - """ + def __init__(self, message, lockdown=False): + self.message = message + self.lockdown = lockdown class SignerError(SeppukuError): diff --git a/apps/cic-eth/cic_eth/eth/account.py b/apps/cic-eth/cic_eth/eth/account.py index 9f4916c7..5b83421f 100644 --- a/apps/cic-eth/cic_eth/eth/account.py +++ b/apps/cic-eth/cic_eth/eth/account.py @@ -136,7 +136,7 @@ def register(self, account_address, chain_spec_dict, writer_address=None): # Generate and sign transaction rpc_signer = RPCConnection.connect(chain_spec, 'signer') nonce_oracle = CustodialTaskNonceOracle(writer_address, self.request.root_id, session=session) #, default_nonce) - gas_oracle = self.create_gas_oracle(rpc, AccountRegistry.gas) + gas_oracle = self.create_gas_oracle(rpc, code_callback=AccountRegistry.gas) account_registry = AccountsIndex(chain_spec, signer=rpc_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) (tx_hash_hex, tx_signed_raw_hex) = account_registry.add(account_registry_address, writer_address, account_address, tx_format=TxFormat.RLP_SIGNED) rpc_signer.disconnect() @@ -192,7 +192,7 @@ def gift(self, account_address, chain_spec_dict): # Generate and sign transaction rpc_signer = RPCConnection.connect(chain_spec, 'signer') nonce_oracle = CustodialTaskNonceOracle(account_address, self.request.root_id, session=session) #, default_nonce) - gas_oracle = self.create_gas_oracle(rpc, MinterFaucet.gas) + gas_oracle = self.create_gas_oracle(rpc, code_callback=MinterFaucet.gas) faucet = Faucet(chain_spec, signer=rpc_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) (tx_hash_hex, tx_signed_raw_hex) = faucet.give_to(faucet_address, account_address, account_address, tx_format=TxFormat.RLP_SIGNED) rpc_signer.disconnect() @@ -266,19 +266,46 @@ def set_role(self, tag, address, chain_spec_dict): @celery_app.task(bind=True, base=BaseTask) def role(self, address, chain_spec_dict): - """Return account role for address + """Return account role for address and/or role :param account: Account to check :type account: str, 0x-hex - :param chain_str: Chain spec string representation - :type chain_str: str + :param chain_spec_dict: Chain spec dict representation + :type chain_spec_dict: dict :returns: Account, or None if not exists :rtype: Varies """ session = self.create_session() role_tag = AccountRole.role_for(address, session=session) session.close() - return role_tag + return [(address, role_tag,)] + + +@celery_app.task(bind=True, base=BaseTask) +def role_account(self, role_tag, chain_spec_dict): + """Return address for role. + + If the role parameter is None, will return addresses for all roles. + + :param role_tag: Role to match + :type role_tag: str + :param chain_spec_dict: Chain spec dict representation + :type chain_spec_dict: dict + :returns: List with a single account/tag pair for a single tag, or a list of account and tag pairs for all tags + :rtype: list + """ + session = self.create_session() + + pairs = None + if role_tag != None: + addr = AccountRole.get_address(role_tag, session=session) + pairs = [(addr, role_tag,)] + else: + pairs = AccountRole.all(session=session) + + session.close() + + return pairs @celery_app.task(bind=True, base=CriticalSQLAlchemyTask) diff --git a/apps/cic-eth/cic_eth/eth/erc20.py b/apps/cic-eth/cic_eth/eth/erc20.py index 40e2e0e8..0f89436b 100644 --- a/apps/cic-eth/cic_eth/eth/erc20.py +++ b/apps/cic-eth/cic_eth/eth/erc20.py @@ -10,6 +10,9 @@ from chainlib.eth.tx import ( TxFormat, unpack, ) +from chainlib.eth.contract import ( + ABIContractEncoder, + ) from cic_eth_registry import CICRegistry from cic_eth_registry.erc20 import ERC20Token from hexathon import ( @@ -19,7 +22,7 @@ from hexathon import ( from chainqueue.error import NotLocalTxError from eth_erc20 import ERC20 from chainqueue.sql.tx import cache_tx_dict -from okota.token_index import to_identifier +from okota.token_index.index import to_identifier # local imports from cic_eth.db.models.base import SessionBase @@ -31,10 +34,8 @@ from cic_eth.error import ( YouAreBrokeError, ) from cic_eth.queue.tx import register_tx -from cic_eth.eth.gas import ( - create_check_gas_task, - MaxGasOracle, - ) +from cic_eth.eth.gas import create_check_gas_task +from cic_eth.eth.util import CacheGasOracle from cic_eth.ext.address import translate_address from cic_eth.task import ( CriticalSQLAlchemyTask, @@ -45,13 +46,14 @@ from cic_eth.task import ( from cic_eth.eth.nonce import CustodialTaskNonceOracle from cic_eth.encode import tx_normalize from cic_eth.eth.trust import verify_proofs +from cic_eth.error import SignerError celery_app = celery.current_app logg = logging.getLogger() -@celery_app.task(base=CriticalWeb3Task) -def balance(tokens, holder_address, chain_spec_dict): +@celery_app.task(bind=True, base=CriticalWeb3Task) +def balance(self, tokens, holder_address, chain_spec_dict): """Return token balances for a list of tokens for given address :param tokens: Token addresses @@ -70,8 +72,9 @@ def balance(tokens, holder_address, chain_spec_dict): for t in tokens: address = t['address'] logg.debug('address {} {}'.format(address, holder_address)) + gas_oracle = self.create_gas_oracle(rpc, min_price=self.min_fee_price) token = ERC20Token(chain_spec, rpc, add_0x(address)) - c = ERC20(chain_spec) + c = ERC20(chain_spec, gas_oracle=gas_oracle) o = c.balance_of(address, holder_address, sender_address=caller_address) r = rpc.do(o) t['balance_network'] = c.parse_balance(r) @@ -154,8 +157,12 @@ def transfer_from(self, tokens, holder_address, receiver_address, value, chain_s rpc_signer = RPCConnection.connect(chain_spec, 'signer') session = self.create_session() + nonce_oracle = CustodialTaskNonceOracle(holder_address, self.request.root_id, session=session) - gas_oracle = self.create_gas_oracle(rpc, MaxGasOracle.gas) + enc = ABIContractEncoder() + enc.method('transferFrom') + method = enc.get() + gas_oracle = self.create_gas_oracle(rpc, t['address'], method=method, session=session, min_price=self.min_fee_price) c = ERC20(chain_spec, signer=rpc_signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) try: (tx_hash_hex, tx_signed_raw_hex) = c.transfer_from(t['address'], spender_address, holder_address, receiver_address, value, tx_format=TxFormat.RLP_SIGNED) @@ -225,8 +232,12 @@ def transfer(self, tokens, holder_address, receiver_address, value, chain_spec_d rpc_signer = RPCConnection.connect(chain_spec, 'signer') session = self.create_session() + + enc = ABIContractEncoder() + enc.method('transfer') + method = enc.get() + gas_oracle = self.create_gas_oracle(rpc, t['address'], method=method, session=session, min_price=self.min_fee_price) nonce_oracle = CustodialTaskNonceOracle(holder_address, self.request.root_id, session=session) - gas_oracle = self.create_gas_oracle(rpc, MaxGasOracle.gas) c = ERC20(chain_spec, signer=rpc_signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) try: (tx_hash_hex, tx_signed_raw_hex) = c.transfer(t['address'], holder_address, receiver_address, value, tx_format=TxFormat.RLP_SIGNED) @@ -294,8 +305,12 @@ def approve(self, tokens, holder_address, spender_address, value, chain_spec_dic rpc_signer = RPCConnection.connect(chain_spec, 'signer') session = self.create_session() + nonce_oracle = CustodialTaskNonceOracle(holder_address, self.request.root_id, session=session) - gas_oracle = self.create_gas_oracle(rpc, MaxGasOracle.gas) + enc = ABIContractEncoder() + enc.method('approve') + method = enc.get() + gas_oracle = self.create_gas_oracle(rpc, t['address'], method=method, session=session) c = ERC20(chain_spec, signer=rpc_signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) try: (tx_hash_hex, tx_signed_raw_hex) = c.approve(t['address'], holder_address, spender_address, value, tx_format=TxFormat.RLP_SIGNED) diff --git a/apps/cic-eth/cic_eth/eth/gas.py b/apps/cic-eth/cic_eth/eth/gas.py index 435949fc..9b0717c8 100644 --- a/apps/cic-eth/cic_eth/eth/gas.py +++ b/apps/cic-eth/cic_eth/eth/gas.py @@ -41,6 +41,7 @@ from chainqueue.db.models.tx import TxCache from chainqueue.db.models.otx import Otx # local imports +from cic_eth.db.models.gas_cache import GasCache from cic_eth.db.models.role import AccountRole from cic_eth.db.models.base import SessionBase from cic_eth.error import ( @@ -65,17 +66,56 @@ from cic_eth.encode import ( ZERO_ADDRESS_NORMAL, unpack_normal, ) +from cic_eth.error import SeppukuError +from cic_eth.eth.util import MAXIMUM_FEE_UNITS celery_app = celery.current_app logg = logging.getLogger() -MAXIMUM_FEE_UNITS = 8000000 -class MaxGasOracle: +@celery_app.task(base=CriticalSQLAlchemyTask) +def apply_gas_value_cache(address, method, value, tx_hash): + return apply_gas_value_cache_local(address, method, value, tx_hash) - def gas(code=None): - return MAXIMUM_FEE_UNITS + +def apply_gas_value_cache_local(address, method, value, tx_hash, session=None): + address = tx_normalize.executable_address(address) + tx_hash = tx_normalize.tx_hash(tx_hash) + value = int(value) + + session = SessionBase.bind_session(session) + q = session.query(GasCache) + q = q.filter(GasCache.address==address) + q = q.filter(GasCache.method==method) + o = q.first() + + if o == None: + o = GasCache(address, method, value, tx_hash) + elif value > o.value: + o.value = value + o.tx_hash = strip_0x(tx_hash) + + session.add(o) + session.commit() + + SessionBase.release_session(session) + + +def have_gas_minimum(chain_spec, address, min_gas, session=None, rpc=None): + if rpc == None: + rpc = RPCConnection.connect(chain_spec, 'default') + o = balance(add_0x(address)) + r = rpc.do(o) + try: + r = int(r) + except ValueError: + r = strip_0x(r) + r = int(r, 16) + logg.debug('have gas minimum {} have gas {} minimum is {}'.format(address, r, min_gas)) + if r < min_gas: + return False + return True def create_check_gas_task(tx_signed_raws_hex, chain_spec, holder_address, gas=None, tx_hashes_hex=None, queue=None): @@ -357,6 +397,13 @@ def refill_gas(self, recipient_address, chain_spec_dict): # set up evm RPC connection rpc = RPCConnection.connect(chain_spec, 'default') + # check the gas balance of the gifter + if not have_gas_minimum(chain_spec, gas_provider, self.safe_gas_refill_amount): + raise SeppukuError('Noooooooooooo; gas gifter {} is broke!'.format(gas_provider)) + + if not have_gas_minimum(chain_spec, gas_provider, self.safe_gas_gifter_balance): + logg.error('Gas gifter {} gas balance is below the safe level to operate!'.format(gas_provider)) + # set up transaction builder nonce_oracle = CustodialTaskNonceOracle(gas_provider, self.request.root_id, session=session) gas_oracle = self.create_gas_oracle(rpc) diff --git a/apps/cic-eth/cic_eth/eth/util.py b/apps/cic-eth/cic_eth/eth/util.py new file mode 100644 index 00000000..6720c669 --- /dev/null +++ b/apps/cic-eth/cic_eth/eth/util.py @@ -0,0 +1,54 @@ +# standard imports +import logging + +# external imports +from chainlib.eth.gas import RPCGasOracle +from hexathon import strip_0x + +# local imports +from cic_eth.db.models.gas_cache import GasCache +from cic_eth.encode import tx_normalize +from cic_eth.db.models.base import SessionBase + +MAXIMUM_FEE_UNITS = 8000000 + +logg = logging.getLogger(__name__) + + +class MaxGasOracle(RPCGasOracle): + + def get_fee_units(self, code=None): + return MAXIMUM_FEE_UNITS + + +class CacheGasOracle(MaxGasOracle): + """Returns a previously recorded value for fee unit expenditure for a contract call, if it exists. Otherwise returns max units. + + :todo: instead of max units, connect a pluggable gas heuristics engine. + """ + + def __init__(self, conn, address, method=None, session=None, min_price=None, id_generator=None): + super(CacheGasOracle, self).__init__(conn, code_callback=self.get_fee_units, min_price=min_price, id_generator=id_generator) + self.value = None + self.address = address + self.method = method + + address = tx_normalize.executable_address(address) + session = SessionBase.bind_session(session) + q = session.query(GasCache) + q = q.filter(GasCache.address==address) + if method != None: + method = strip_0x(method) + q = q.filter(GasCache.method==method) + o = q.first() + if o != None: + self.value = int(o.value) + + SessionBase.release_session(session) + + + def get_fee_units(self, code=None): + if self.value != None: + logg.debug('found stored gas unit value {} for address {} method {}'.format(self.value, self.address, self.method)) + return self.value + return super(CacheGasOracle, self).get_fee_units(code=code) diff --git a/apps/cic-eth/cic_eth/pytest/fixtures_config.py b/apps/cic-eth/cic_eth/pytest/fixtures_config.py index 27d7c9cc..e44078f9 100644 --- a/apps/cic-eth/cic_eth/pytest/fixtures_config.py +++ b/apps/cic-eth/cic_eth/pytest/fixtures_config.py @@ -8,15 +8,14 @@ import confini script_dir = os.path.dirname(os.path.realpath(__file__)) root_dir = os.path.dirname(os.path.dirname(script_dir)) +config_dir = os.path.join(root_dir, 'cic_eth', 'data', 'config') logg = logging.getLogger(__name__) @pytest.fixture(scope='session') def load_config(): - config_dir = os.environ.get('CONFINI_DIR') - if config_dir == None: - config_dir = os.path.join(root_dir, 'config/test') - conf = confini.Config(config_dir, 'CICTEST') + override_config_dir = os.path.join(root_dir, 'config', 'test') + conf = confini.Config(config_dir, 'CICTEST', override_dirs=[override_config_dir]) conf.process() logg.debug('config {}'.format(conf)) return conf diff --git a/apps/cic-eth/cic_eth/queue/balance.py b/apps/cic-eth/cic_eth/queue/balance.py index f2f87fb5..8c48ab91 100644 --- a/apps/cic-eth/cic_eth/queue/balance.py +++ b/apps/cic-eth/cic_eth/queue/balance.py @@ -72,7 +72,7 @@ def __balance_incoming_compatible(token_address, receiver_address): status_compare = dead() q = q.filter(Otx.status.op('&')(status_compare)==0) # TODO: this can change the result for the recipient if tx is later obsoleted and resubmission is delayed. - q = q.filter(Otx.status.op('&')(StatusBits.IN_NETWORK)==StatusBits.IN_NETWORK) + #q = q.filter(Otx.status.op('&')(StatusBits.IN_NETWORK)==StatusBits.IN_NETWORK) q = q.filter(TxCache.destination_token_address==token_address) delta = 0 for r in q.all(): diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/__init__.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/__init__.py index d2a9823b..8b63297a 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/filters/__init__.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/__init__.py @@ -3,3 +3,4 @@ from .tx import TxFilter from .gas import GasFilter from .register import RegistrationFilter from .transferauth import TransferAuthFilter +from .token import TokenFilter diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py new file mode 100644 index 00000000..e1cb54f5 --- /dev/null +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py @@ -0,0 +1,63 @@ +# standard imports +import logging + +# external imports +from eth_erc20 import ERC20 +from chainlib.eth.contract import ( + ABIContractEncoder, + ABIContractType, + ) +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.address import is_same_address +from chainlib.eth.error import RequestMismatchException +from cic_eth_registry import CICRegistry +from cic_eth_registry.erc20 import ERC20Token +from eth_token_index import TokenUniqueSymbolIndex +import celery + +# local imports +from .base import SyncFilter + +logg = logging.getLogger(__name__) + + +class TokenFilter(SyncFilter): + + def __init__(self, chain_spec, queue, call_address=ZERO_ADDRESS): + self.queue = queue + self.chain_spec = chain_spec + self.caller_address = call_address + + + def filter(self, conn, block, tx, db_session=None): + if not tx.payload: + return (None, None) + + try: + r = ERC20.parse_transfer_request(tx.payload) + except RequestMismatchException: + return (None, None) + + token_address = tx.inputs[0] + token = ERC20Token(self.chain_spec, conn, token_address) + + registry = CICRegistry(self.chain_spec, conn) + r = registry.by_name(token.symbol, sender_address=self.caller_address) + if is_same_address(r, ZERO_ADDRESS): + return None + + enc = ABIContractEncoder() + enc.method('transfer') + method = enc.get() + + s = celery.signature( + 'cic_eth.eth.gas.apply_gas_value_cache', + [ + token_address, + method, + tx.gas_used, + tx.hash, + ], + queue=self.queue, + ) + return s.apply_async() diff --git a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py index 7e3a22a8..d2751e4a 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/tasker.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/tasker.py @@ -67,7 +67,10 @@ from cic_eth.registry import ( connect_declarator, connect_token_registry, ) -from cic_eth.task import BaseTask +from cic_eth.task import ( + BaseTask, + CriticalWeb3Task, + ) logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -76,18 +79,18 @@ arg_flags = cic_eth.cli.argflag_std_read local_arg_flags = cic_eth.cli.argflag_local_task argparser = cic_eth.cli.ArgumentParser(arg_flags) argparser.process_local_flags(local_arg_flags) -#argparser.add_argument('--default-token-symbol', dest='default_token_symbol', type=str, help='Symbol of default token to use') argparser.add_argument('--trace-queue-status', default=None, dest='trace_queue_status', action='store_true', help='set to perist all queue entry status changes to storage') argparser.add_argument('--aux-all', action='store_true', help='include tasks from all submodules from the aux module path') +argparser.add_argument('--min-fee-price', dest='min_fee_price', type=int, help='set minimum fee price for transactions, in wei') argparser.add_argument('--aux', action='append', type=str, default=[], help='add single submodule from the aux module path') args = argparser.parse_args() # process config extra_args = { -# 'default_token_symbol': 'CIC_DEFAULT_TOKEN_SYMBOL', 'aux_all': None, 'aux': None, 'trace_queue_status': 'TASKS_TRACE_QUEUE_STATUS', + 'min_fee_price': 'ETH_MIN_FEE_PRICE', } config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags) @@ -215,6 +218,7 @@ def main(): argv.append('-n') argv.append(config.get('CELERY_QUEUE')) + # TODO: More elegant way of setting queue-wide settings BaseTask.default_token_symbol = default_token_symbol BaseTask.default_token_address = default_token_address default_token = ERC20Token(chain_spec, conn, add_0x(BaseTask.default_token_address)) @@ -222,6 +226,14 @@ def main(): BaseTask.default_token_decimals = default_token.decimals BaseTask.default_token_name = default_token.name BaseTask.trusted_addresses = trusted_addresses + CriticalWeb3Task.safe_gas_refill_amount = int(config.get('ETH_GAS_HOLDER_MINIMUM_UNITS')) * int(config.get('ETH_GAS_HOLDER_REFILL_UNITS')) + CriticalWeb3Task.safe_gas_threshold_amount = int(config.get('ETH_GAS_HOLDER_MINIMUM_UNITS')) * int(config.get('ETH_GAS_HOLDER_REFILL_THRESHOLD')) + CriticalWeb3Task.safe_gas_gifter_balance = int(config.get('ETH_GAS_HOLDER_MINIMUM_UNITS')) * int(config.get('ETH_GAS_GIFTER_REFILL_BUFFER')) + if config.get('ETH_MIN_FEE_PRICE'): + BaseTask.min_fee_price = int(config.get('ETH_MIN_FEE_PRICE')) + CriticalWeb3Task.safe_gas_threshold_amount *= BaseTask.min_fee_price + CriticalWeb3Task.safe_gas_refill_amount *= BaseTask.min_fee_price + CriticalWeb3Task.safe_gas_gifter_balance *= BaseTask.min_fee_price BaseTask.run_dir = config.get('CIC_RUN_DIR') logg.info('default token set to {} {}'.format(BaseTask.default_token_symbol, BaseTask.default_token_address)) diff --git a/apps/cic-eth/cic_eth/runnable/daemons/tracker.py b/apps/cic-eth/cic_eth/runnable/daemons/tracker.py index 5117537c..680eb796 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/tracker.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/tracker.py @@ -36,6 +36,7 @@ from cic_eth.runnable.daemons.filters import ( TxFilter, RegistrationFilter, TransferAuthFilter, + TokenFilter, ) from cic_eth.stat import init_chain_stat from cic_eth.registry import ( @@ -99,10 +100,10 @@ def main(): syncer_backends = SQLBackend.resume(chain_spec, block_offset) if len(syncer_backends) == 0: - initial_block_start = config.get('SYNCER_OFFSET') - initial_block_offset = block_offset + initial_block_start = int(config.get('SYNCER_OFFSET')) + initial_block_offset = int(block_offset) if config.true('SYNCER_NO_HISTORY'): - initial_block_start = block_offset + initial_block_start = initial_block_offset initial_block_offset += 1 syncer_backends.append(SQLBackend.initial(chain_spec, initial_block_offset, start_block_height=initial_block_start)) logg.info('found no backends to resume, adding initial sync from history start {} end {}'.format(initial_block_start, initial_block_offset)) @@ -154,6 +155,8 @@ def main(): gas_filter = GasFilter(chain_spec, config.get('CELERY_QUEUE')) + token_gas_cache_filter = TokenFilter(chain_spec, config.get('CELERY_QUEUE')) + #transfer_auth_filter = TransferAuthFilter(registry, chain_spec, config.get('_CELERY_QUEUE')) i = 0 @@ -163,6 +166,7 @@ def main(): syncer.add_filter(registration_filter) # TODO: the two following filter functions break the filter loop if return uuid. Pro: less code executed. Con: Possibly unintuitive flow break syncer.add_filter(tx_filter) + syncer.add_filter(token_gas_cache_filter) #syncer.add_filter(transfer_auth_filter) for cf in callback_filters: syncer.add_filter(cf) diff --git a/apps/cic-eth/cic_eth/runnable/tag.py b/apps/cic-eth/cic_eth/runnable/tag.py index b4bdd83a..7231c7ab 100644 --- a/apps/cic-eth/cic_eth/runnable/tag.py +++ b/apps/cic-eth/cic_eth/runnable/tag.py @@ -8,6 +8,7 @@ import re # external imports import cic_eth.cli from chainlib.chain import ChainSpec +from chainlib.eth.address import is_address from xdg.BaseDirectory import xdg_config_home # local imports @@ -21,12 +22,18 @@ logg = logging.getLogger() arg_flags = cic_eth.cli.argflag_std_base | cic_eth.cli.Flag.UNSAFE | cic_eth.cli.Flag.CHAIN_SPEC local_arg_flags = cic_eth.cli.argflag_local_taskcallback argparser = cic_eth.cli.ArgumentParser(arg_flags) -argparser.add_positional('tag', type=str, help='address tag') -argparser.add_positional('address', type=str, help='address') +argparser.add_argument('--set', action='store_true', help='sets the given tag') +argparser.add_argument('--tag', type=str, help='operate on the given tag') +argparser.add_positional('address', required=False, type=str, help='address associated with tag') argparser.process_local_flags(local_arg_flags) args = argparser.parse_args() -config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags) +extra_args = { + 'set': None, + 'tag': None, + 'address': None, + } +config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags, extra_args=extra_args) celery_app = cic_eth.cli.CeleryApp.from_config(config) @@ -39,7 +46,17 @@ api = AdminApi(None) def main(): - admin_api.tag_account(args.tag, args.address, chain_spec) + if config.get('_ADDRESS') != None and not is_address(config.get('_ADDRESS')): + sys.stderr.write('Invalid address {}'.format(config.get('_ADDRESS'))) + sys.exit(1) + + if config.get('_SET'): + admin_api.tag_account(chain_spec, config.get('_TAG'), config.get('_ADDRESS')) + else: + t = admin_api.get_tag_account(chain_spec, tag=config.get('_TAG'), address=config.get('_ADDRESS')) + r = t.get() + for v in r: + sys.stdout.write('{}\t{}\n'.format(v[1], v[0])) if __name__ == '__main__': diff --git a/apps/cic-eth/cic_eth/runnable/transfer.py b/apps/cic-eth/cic_eth/runnable/transfer.py index 4bbaa306..67c8314c 100644 --- a/apps/cic-eth/cic_eth/runnable/transfer.py +++ b/apps/cic-eth/cic_eth/runnable/transfer.py @@ -18,7 +18,7 @@ from cic_eth.api import Api logging.basicConfig(level=logging.WARNING) logg = logging.getLogger('create_account_script') -arg_flags = cic_eth.cli.argflag_std_base +arg_flags = cic_eth.cli.argflag_local_base local_arg_flags = cic_eth.cli.argflag_local_taskcallback argparser = cic_eth.cli.ArgumentParser(arg_flags) argparser.add_argument('--token-symbol', dest='token_symbol', type=str, help='Token symbol') diff --git a/apps/cic-eth/cic_eth/runnable/view.py b/apps/cic-eth/cic_eth/runnable/view.py index 5bd9c0a1..bdc40c33 100644 --- a/apps/cic-eth/cic_eth/runnable/view.py +++ b/apps/cic-eth/cic_eth/runnable/view.py @@ -16,9 +16,14 @@ import confini import celery from chainlib.chain import ChainSpec from chainlib.eth.connection import EthHTTPConnection -from hexathon import add_0x +from hexathon import ( + add_0x, + strip_0x, + uniform as hex_uniform, + ) # local imports +import cic_eth.cli from cic_eth.api.admin import AdminApi from cic_eth.db.enum import ( StatusEnum, @@ -31,59 +36,35 @@ logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() default_format = 'terminal' -default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic') -argparser = argparse.ArgumentParser() -argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)') -argparser.add_argument('-r', '--registry-address', dest='r', type=str, help='CIC registry address') +arg_flags = cic_eth.cli.argflag_std_base +local_arg_flags = cic_eth.cli.argflag_local_taskcallback +argparser = cic_eth.cli.ArgumentParser(arg_flags) argparser.add_argument('-f', '--format', dest='f', default=default_format, type=str, help='Output format') -argparser.add_argument('--status-raw', dest='status_raw', action='store_true', help='Output status bit enum names only') -argparser.add_argument('-c', type=str, default=default_config_dir, help='config root to use') -argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='chain spec') -argparser.add_argument('-q', type=str, default='cic-eth', help='celery queue to submit transaction tasks to') -argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') -argparser.add_argument('-v', action='store_true', help='Be verbose') -argparser.add_argument('-vv', help='be more verbose', action='store_true') argparser.add_argument('query', type=str, help='Transaction, transaction hash, account or "lock"') +argparser.process_local_flags(local_arg_flags) args = argparser.parse_args() -if args.v == True: - logging.getLogger().setLevel(logging.INFO) -elif args.vv == True: - logging.getLogger().setLevel(logging.DEBUG) - -config_dir = os.path.join(args.c) -os.makedirs(config_dir, 0o777, True) -config = confini.Config(config_dir, args.env_prefix) -config.process() -args_override = { - 'ETH_PROVIDER': getattr(args, 'p'), - 'CIC_CHAIN_SPEC': getattr(args, 'i'), - 'CIC_REGISTRY_ADDRESS': getattr(args, 'r'), +extra_args = { + 'f': '_FORMAT', + 'query': '_QUERY', } -# override args -config.dict_override(args_override, 'cli args') -config.censor('PASSWORD', 'DATABASE') -config.censor('PASSWORD', 'SSL') -logg.debug('config loaded from {}:\n{}'.format(config_dir, config)) +config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags, extra_args=extra_args) -try: - config.add(add_0x(args.query), '_QUERY', True) -except: - config.add(args.query, '_QUERY', True) +celery_app = cic_eth.cli.CeleryApp.from_config(config) +queue = config.get('CELERY_QUEUE') -celery_app = celery.Celery(broker=config.get('CELERY_BROKER_URL'), backend=config.get('CELERY_RESULT_URL')) +chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) -queue = args.q +# connect to celery +celery_app = cic_eth.cli.CeleryApp.from_config(config) -chain_spec = ChainSpec.from_chain_str(config.get('CIC_CHAIN_SPEC')) +# set up rpc +rpc = cic_eth.cli.RPC.from_config(config) #, use_signer=True) +conn = rpc.get_default() -rpc = EthHTTPConnection(args.p) - -#registry_address = config.get('CIC_REGISTRY_ADDRESS') - -admin_api = AdminApi(rpc) +admin_api = AdminApi(conn) t = admin_api.registry() registry_address = t.get() @@ -113,7 +94,7 @@ def render_tx(o, **kwargs): for v in o.get('status_log', []): d = datetime.datetime.fromisoformat(v[0]) - e = status_str(v[1], args.status_raw) + e = status_str(v[1], config.get('_RAW')) content += '{}: {}\n'.format(d, e) return content @@ -154,20 +135,24 @@ def render_lock(o, **kwargs): def main(): txs = [] renderer = render_tx - if len(config.get('_QUERY')) > 66: - #registry = connect_registry(rpc, chain_spec, registry_address) - #admin_api.tx(chain_spec, tx_raw=config.get('_QUERY'), registry=registry, renderer=renderer) - admin_api.tx(chain_spec, tx_raw=config.get('_QUERY'), renderer=renderer) - elif len(config.get('_QUERY')) > 42: - #registry = connect_registry(rpc, chain_spec, registry_address) - #admin_api.tx(chain_spec, tx_hash=config.get('_QUERY'), registry=registry, renderer=renderer) - admin_api.tx(chain_spec, tx_hash=config.get('_QUERY'), renderer=renderer) - elif len(config.get('_QUERY')) == 42: - #registry = connect_registry(rpc, chain_spec, registry_address) - txs = admin_api.account(chain_spec, config.get('_QUERY'), include_recipient=False, renderer=render_account) + query = config.get('_QUERY') + try: + query = hex_uniform(strip_0x(query)) + except TypeError: + pass + except ValueError: + pass + + if len(query) > 64: + admin_api.tx(chain_spec, tx_raw=query, renderer=renderer) + elif len(query) > 40: + admin_api.tx(chain_spec, tx_hash=query, renderer=renderer) + + elif len(query) == 40: + txs = admin_api.account(chain_spec, query, include_recipient=False, renderer=render_account) renderer = render_account - elif len(config.get('_QUERY')) >= 4 and config.get('_QUERY')[:4] == 'lock': + elif len(query) >= 4 and query[:4] == 'lock': t = admin_api.get_lock() txs = t.get() renderer = render_lock @@ -175,7 +160,7 @@ def main(): r = renderer(txs) sys.stdout.write(r + '\n') else: - raise ValueError('cannot parse argument {}'.format(config.get('_QUERY'))) + raise ValueError('cannot parse argument {}'.format(query)) if __name__ == '__main__': diff --git a/apps/cic-eth/cic_eth/task.py b/apps/cic-eth/cic_eth/task.py index 149e1800..da70307a 100644 --- a/apps/cic-eth/cic_eth/task.py +++ b/apps/cic-eth/cic_eth/task.py @@ -17,6 +17,7 @@ from cic_eth_registry.error import UnknownContractError # local imports from cic_eth.error import SeppukuError from cic_eth.db.models.base import SessionBase +from cic_eth.eth.util import CacheGasOracle, MaxGasOracle #logg = logging.getLogger().getChild(__name__) logg = logging.getLogger() @@ -24,19 +25,46 @@ logg = logging.getLogger() celery_app = celery.current_app + class BaseTask(celery.Task): session_func = SessionBase.create_session call_address = ZERO_ADDRESS trusted_addresses = [] - create_nonce_oracle = RPCNonceOracle - create_gas_oracle = RPCGasOracle + min_fee_price = 1 + min_fee_limit = 30000 default_token_address = None default_token_symbol = None default_token_name = None default_token_decimals = None run_dir = '/run' + + def create_gas_oracle(self, conn, address=None, *args, **kwargs): + x = None + if address is None: + x = RPCGasOracle( + conn, + code_callback=kwargs.get('code_callback', self.get_min_fee_limit), + min_price=self.min_fee_price, + id_generator=kwargs.get('id_generator'), + ) + else: + + x = MaxGasOracle(conn) + x.code_callback = x.get_fee_units + + return x + + + def get_min_fee_limit(self, code): + return self.min_fee_limit + + + def get_min_fee_limit(self, code): + return self.min_fee_limit + + def create_session(self): return BaseTask.session_func() @@ -59,7 +87,7 @@ class BaseTask(celery.Task): ) s.apply_async() - + class CriticalTask(BaseTask): retry_jitter = True retry_backoff = True @@ -71,26 +99,25 @@ class CriticalSQLAlchemyTask(CriticalTask): sqlalchemy.exc.DatabaseError, sqlalchemy.exc.TimeoutError, sqlalchemy.exc.ResourceClosedError, - ) + ) class CriticalWeb3Task(CriticalTask): autoretry_for = ( ConnectionError, ) - safe_gas_threshold_amount = 2000000000 * 60000 * 3 - safe_gas_refill_amount = safe_gas_threshold_amount * 5 + safe_gas_threshold_amount = 60000 * 3 + safe_gas_refill_amount = safe_gas_threshold_amount * 5 + safe_gas_gifter_balance = safe_gas_threshold_amount * 5 * 100 -class CriticalSQLAlchemyAndWeb3Task(CriticalTask): +class CriticalSQLAlchemyAndWeb3Task(CriticalWeb3Task): autoretry_for = ( sqlalchemy.exc.DatabaseError, sqlalchemy.exc.TimeoutError, ConnectionError, sqlalchemy.exc.ResourceClosedError, ) - safe_gas_threshold_amount = 2000000000 * 60000 * 3 - safe_gas_refill_amount = safe_gas_threshold_amount * 5 class CriticalSQLAlchemyAndSignerTask(CriticalTask): @@ -98,15 +125,12 @@ class CriticalSQLAlchemyAndSignerTask(CriticalTask): sqlalchemy.exc.DatabaseError, sqlalchemy.exc.TimeoutError, sqlalchemy.exc.ResourceClosedError, - ) + ) -class CriticalWeb3AndSignerTask(CriticalTask): +class CriticalWeb3AndSignerTask(CriticalWeb3Task): autoretry_for = ( ConnectionError, ) - safe_gas_threshold_amount = 2000000000 * 60000 * 3 - safe_gas_refill_amount = safe_gas_threshold_amount * 5 - @celery_app.task() def check_health(self): diff --git a/apps/cic-eth/config/test/accounts.ini b/apps/cic-eth/config/test/accounts.ini deleted file mode 100644 index 819a83bc..00000000 --- a/apps/cic-eth/config/test/accounts.ini +++ /dev/null @@ -1,2 +0,0 @@ -[accounts] -writer_address = diff --git a/apps/cic-eth/config/test/bancor.ini b/apps/cic-eth/config/test/bancor.ini deleted file mode 100644 index 961c0e6b..00000000 --- a/apps/cic-eth/config/test/bancor.ini +++ /dev/null @@ -1,2 +0,0 @@ -[bancor] -dir = tests/testdata/bancor diff --git a/apps/cic-eth/config/test/celery.ini b/apps/cic-eth/config/test/celery.ini index 1f12b77f..30a519c7 100644 --- a/apps/cic-eth/config/test/celery.ini +++ b/apps/cic-eth/config/test/celery.ini @@ -1,5 +1,3 @@ [celery] broker_url = filesystem:// result_url = filesystem:// -#broker_url = redis:// -#result_url = redis:// diff --git a/apps/cic-eth/config/test/chain.ini b/apps/cic-eth/config/test/chain.ini deleted file mode 100644 index 9fda0988..00000000 --- a/apps/cic-eth/config/test/chain.ini +++ /dev/null @@ -1,2 +0,0 @@ -[chain] -spec = diff --git a/apps/cic-eth/config/test/cic.ini b/apps/cic-eth/config/test/cic.ini deleted file mode 100644 index d985ae3e..00000000 --- a/apps/cic-eth/config/test/cic.ini +++ /dev/null @@ -1,4 +0,0 @@ -[cic] -registry_address = -chain_spec = -trust_address = diff --git a/apps/cic-eth/config/test/dispatcher.ini b/apps/cic-eth/config/test/dispatcher.ini deleted file mode 100644 index f7b270a5..00000000 --- a/apps/cic-eth/config/test/dispatcher.ini +++ /dev/null @@ -1,2 +0,0 @@ -[dispatcher] -loop_interval = 0.1 diff --git a/apps/cic-eth/config/test/eth.ini b/apps/cic-eth/config/test/eth.ini deleted file mode 100644 index 4c9edda1..00000000 --- a/apps/cic-eth/config/test/eth.ini +++ /dev/null @@ -1,8 +0,0 @@ -[eth] -#ws_provider = ws://localhost:8546 -#ttp_provider = http://localhost:8545 -provider = http://localhost:8545 -gas_provider_address = -#chain_id = -abi_dir = -faucet_giver_address = diff --git a/apps/cic-eth/config/test/signer.ini b/apps/cic-eth/config/test/signer.ini index 0fa1f015..2e1cbc00 100644 --- a/apps/cic-eth/config/test/signer.ini +++ b/apps/cic-eth/config/test/signer.ini @@ -1,5 +1,2 @@ [signer] -socket_path = /run/crypto-dev-signer/jsonrpc.ipc -secret = deedbeef -database_name = signer_test -dev_keys_path = +provider = /run/crypto-dev-signer/jsonrpc.ipc diff --git a/apps/cic-eth/config/test/ssl.ini b/apps/cic-eth/config/test/ssl.ini deleted file mode 100644 index bd494729..00000000 --- a/apps/cic-eth/config/test/ssl.ini +++ /dev/null @@ -1,6 +0,0 @@ -[SSL] -enable_client = false -cert_file = -key_file = -password = -ca_file = diff --git a/apps/cic-eth/config/test/syncer.ini b/apps/cic-eth/config/test/syncer.ini deleted file mode 100644 index 97236743..00000000 --- a/apps/cic-eth/config/test/syncer.ini +++ /dev/null @@ -1,2 +0,0 @@ -[SYNCER] -loop_interval = 1 diff --git a/apps/cic-eth/docker/Dockerfile b/apps/cic-eth/docker/Dockerfile index 854f7306..d9f74c41 100644 --- a/apps/cic-eth/docker/Dockerfile +++ b/apps/cic-eth/docker/Dockerfile @@ -11,13 +11,6 @@ ARG EXTRA_PIP_INDEX_URL=https://pip.grassrootseconomics.net ARG EXTRA_PIP_ARGS="" ARG PIP_INDEX_URL=https://pypi.org/simple -RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ - pip install --index-url $PIP_INDEX_URL \ - --pre \ - --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ - cic-eth-aux-erc20-demurrage-token~=0.0.2a7 - - COPY *requirements.txt ./ RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ pip install --index-url $PIP_INDEX_URL \ @@ -25,7 +18,7 @@ RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ -r requirements.txt \ -r services_requirements.txt \ - -r admin_requirements.txt + -r admin_requirements.txt COPY . . RUN python setup.py install @@ -40,8 +33,6 @@ RUN chmod 755 *.sh # # they can all be overridden by environment variables # # to generate a list of environment variables from configuration, use: confini-dump -z (executable provided by confini package) #COPY config/ /usr/local/etc/cic-eth/ -COPY cic_eth/db/migrations/ /usr/local/share/cic-eth/alembic/ -COPY crypto_dev_signer_config/ /usr/local/etc/crypto-dev-signer/ # TODO this kind of code sharing across projects should be discouraged...can we make util a library? #COPY util/liveness/health.sh /usr/local/bin/health.sh @@ -66,9 +57,8 @@ ENTRYPOINT [] ## # they can all be overridden by environment variables ## # to generate a list of environment variables from configuration, use: confini-dump -z (executable provided by confini package) #COPY config/ /usr/local/etc/cic-eth/ -#COPY cic_eth/db/migrations/ /usr/local/share/cic-eth/alembic/ -#COPY crypto_dev_signer_config/ /usr/local/etc/crypto-dev-signer/ -#COPY scripts/ scripts/ +COPY cic_eth/db/migrations/ /usr/local/share/cic-eth/alembic/ +#COPY scripts/ scripts/ # ## TODO this kind of code sharing across projects should be discouraged...can we make util a library? ##COPY util/liveness/health.sh /usr/local/bin/health.sh diff --git a/apps/cic-eth/requirements.txt b/apps/cic-eth/requirements.txt index 9f7c0fd6..d6b4d323 100644 --- a/apps/cic-eth/requirements.txt +++ b/apps/cic-eth/requirements.txt @@ -1,10 +1,9 @@ -cic-types~=0.2.1a7 celery==4.4.7 -chainlib-eth>=0.0.10a16,<0.1.0 semver==2.13.0 -crypto-dev-signer>=0.4.15rc2,<0.5.0 - -setuptools >= 21.0.0 - +chainlib-eth~=0.0.15 +urlybird~=0.0.1 +cic-eth-registry~=0.6.6 +cic-types~=0.2.1a8 +cic-eth-aux-erc20-demurrage-token~=0.0.3 fastapi[all]==0.70.1 uvicorn[standard]<0.16.0 \ No newline at end of file diff --git a/apps/cic-eth/services_requirements.txt b/apps/cic-eth/services_requirements.txt index d8737146..bc0219d3 100644 --- a/apps/cic-eth/services_requirements.txt +++ b/apps/cic-eth/services_requirements.txt @@ -1,16 +1,15 @@ -chainqueue>=0.0.6a1,<0.1.0 -chainsyncer[sql]>=0.0.7a3,<0.1.0 +chainqueue~=0.0.6a4 +chainsyncer[sql]~=0.0.7 alembic==1.4.2 -confini>=0.3.6rc4,<0.5.0 +confini~=0.5.3 redis==3.5.3 -hexathon~=0.0.1a8 +hexathon~=0.1.0 pycryptodome==3.10.1 liveness~=0.0.1a7 -eth-address-index>=0.2.4a1,<0.3.0 -eth-accounts-index>=0.1.2a3,<0.2.0 -cic-eth-registry>=0.6.1a6,<0.7.0 -erc20-faucet>=0.3.2a2,<0.4.0 -erc20-transfer-authorization>=0.3.5a2,<0.4.0 -sarafu-faucet>=0.0.7a2,<0.1.0 -moolb~=0.1.1b2 -okota>=0.2.4a6,<0.3.0 +eth-address-index~=0.2.4 +eth-accounts-index~=0.1.2 +erc20-faucet~=0.3.2 +erc20-transfer-authorization~=0.3.6 +sarafu-faucet~=0.0.7 +moolb~=0.2.0 +okota~=0.2.5 diff --git a/apps/cic-eth/setup.cfg b/apps/cic-eth/setup.cfg index 22fd50d2..965d1625 100644 --- a/apps/cic-eth/setup.cfg +++ b/apps/cic-eth/setup.cfg @@ -1,7 +1,7 @@ [metadata] name = cic-eth #version = attr: cic_eth.version.__version_string__ -version = 0.12.4a13 +version = 0.12.7 description = CIC Network Ethereum interaction author = Louis Holbrook author_email = dev@holbrook.no diff --git a/apps/cic-eth/test_requirements.txt b/apps/cic-eth/test_requirements.txt index 67462190..fdbdcb21 100644 --- a/apps/cic-eth/test_requirements.txt +++ b/apps/cic-eth/test_requirements.txt @@ -6,4 +6,5 @@ pytest-redis==2.0.0 redis==3.5.3 eth-tester==0.5.0b3 py-evm==0.3.0a20 -eth-erc20~=0.1.2a2 +eth-erc20~=0.1.5 +erc20-transfer-authorization~=0.3.6 diff --git a/apps/cic-eth/tests/filters/test_token_filter.py b/apps/cic-eth/tests/filters/test_token_filter.py new file mode 100644 index 00000000..b94797ca --- /dev/null +++ b/apps/cic-eth/tests/filters/test_token_filter.py @@ -0,0 +1,98 @@ +# external imports +from eth_erc20 import ERC20 +from chainlib.connection import RPCConnection +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.gas import ( + Gas, + OverrideGasOracle, + ) +from chainlib.eth.tx import ( + TxFormat, + receipt, + raw, + unpack, + Tx, + ) +from chainlib.eth.block import ( + Block, + block_latest, + block_by_number, + ) +from chainlib.eth.address import is_same_address +from chainlib.eth.contract import ABIContractEncoder +from hexathon import strip_0x +from eth_token_index import TokenUniqueSymbolIndex + +# local imports +from cic_eth.runnable.daemons.filters.token import TokenFilter +from cic_eth.db.models.gas_cache import GasCache +from cic_eth.db.models.base import SessionBase + + +def test_filter_gas( + default_chain_spec, + init_database, + eth_rpc, + eth_signer, + contract_roles, + agent_roles, + token_roles, + foo_token, + token_registry, + register_lookups, + register_tokens, + celery_session_worker, + cic_registry, + ): + + rpc = RPCConnection.connect(default_chain_spec, 'default') + nonce_oracle = RPCNonceOracle(token_roles['FOO_TOKEN_OWNER'], eth_rpc) + gas_oracle = OverrideGasOracle(price=1000000000, limit=1000000) + c = ERC20(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle) + (tx_hash_hex, tx_signed_raw_hex) = c.transfer(foo_token, token_roles['FOO_TOKEN_OWNER'], agent_roles['ALICE'], 100, tx_format=TxFormat.RLP_SIGNED) + o = raw(tx_signed_raw_hex) + eth_rpc.do(o) + o = receipt(tx_hash_hex) + rcpt = eth_rpc.do(o) + assert rcpt['status'] == 1 + + fltr = TokenFilter(default_chain_spec, queue=None, call_address=agent_roles['ALICE']) + + o = block_latest() + r = eth_rpc.do(o) + o = block_by_number(r, include_tx=False) + r = eth_rpc.do(o) + block = Block(r) + block.txs = [tx_hash_hex] + + tx_signed_raw_bytes = bytes.fromhex(strip_0x(tx_signed_raw_hex)) + tx_src = unpack(tx_signed_raw_bytes, default_chain_spec) + tx = Tx(tx_src, block=block) + tx.apply_receipt(rcpt) + t = fltr.filter(eth_rpc, block, tx, db_session=init_database) + assert t.get() == None + + nonce_oracle = RPCNonceOracle(contract_roles['CONTRACT_DEPLOYER'], eth_rpc) + c = TokenUniqueSymbolIndex(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle) + (tx_hash_hex_register, o) = c.register(token_registry, contract_roles['CONTRACT_DEPLOYER'], foo_token) + eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1 + + t = fltr.filter(eth_rpc, block, tx, db_session=init_database) + r = t.get_leaf() + assert t.successful() + + q = init_database.query(GasCache) + q = q.filter(GasCache.tx_hash==strip_0x(tx_hash_hex)) + o = q.first() + + assert is_same_address(o.address, strip_0x(foo_token)) + assert o.value > 0 + + enc = ABIContractEncoder() + enc.method('transfer') + method = enc.get() + + assert o.method == method diff --git a/apps/cic-eth/tests/task/api/test_admin.py b/apps/cic-eth/tests/task/api/test_admin.py index 363fb8d7..dcfe2011 100644 --- a/apps/cic-eth/tests/task/api/test_admin.py +++ b/apps/cic-eth/tests/task/api/test_admin.py @@ -103,11 +103,11 @@ def test_tag_account( api = AdminApi(eth_rpc, queue=None) - t = api.tag_account('foo', agent_roles['ALICE'], default_chain_spec) + t = api.tag_account(default_chain_spec, 'foo', agent_roles['ALICE']) t.get() - t = api.tag_account('bar', agent_roles['BOB'], default_chain_spec) + t = api.tag_account(default_chain_spec, 'bar', agent_roles['BOB']) t.get() - t = api.tag_account('bar', agent_roles['CAROL'], default_chain_spec) + t = api.tag_account(default_chain_spec, 'bar', agent_roles['CAROL']) t.get() assert AccountRole.get_address('foo', init_database) == tx_normalize.wallet_address(agent_roles['ALICE']) @@ -288,7 +288,6 @@ def test_fix_nonce( init_database.commit() - logg.debug('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!') txs = get_nonce_tx_local(default_chain_spec, 3, agent_roles['ALICE'], session=init_database) ks = txs.keys() assert len(ks) == 2 diff --git a/apps/cic-eth/tests/task/api/test_app_noncritical.py b/apps/cic-eth/tests/task/api/test_app_noncritical.py index a7a3177b..4e8e7eed 100644 --- a/apps/cic-eth/tests/task/api/test_app_noncritical.py +++ b/apps/cic-eth/tests/task/api/test_app_noncritical.py @@ -191,11 +191,17 @@ def test_tokens( break api_param = str(uuid.uuid4()) + fp = os.path.join(CallbackTask.mmap_path, api_param) + f = open(fp, 'wb+') + f.write(b'\x00') + f.close() + api = Api(str(default_chain_spec), queue=None, callback_param=api_param, callback_task='cic_eth.pytest.mock.callback.test_callback') t = api.tokens(['BAR'], proof=[[bar_token_declaration]]) r = t.get() logg.debug('rr {} {}'.format(r, t.children)) + while True: fp = os.path.join(CallbackTask.mmap_path, api_param) try: diff --git a/apps/cic-eth/tests/task/test_task_account.py b/apps/cic-eth/tests/task/test_task_account.py index d1c1c3d2..58f1b499 100644 --- a/apps/cic-eth/tests/task/test_task_account.py +++ b/apps/cic-eth/tests/task/test_task_account.py @@ -141,9 +141,57 @@ def test_role_task( ) t = s.apply_async() r = t.get() - assert r == 'foo' + assert r[0][0] == address + assert r[0][1] == 'foo' +def test_get_role_task( + init_database, + celery_session_worker, + default_chain_spec, + ): + address_foo = '0x' + os.urandom(20).hex() + role_foo = AccountRole.set('foo', address_foo) + init_database.add(role_foo) + + address_bar = '0x' + os.urandom(20).hex() + role_bar = AccountRole.set('bar', address_bar) + init_database.add(role_bar) + + init_database.commit() + + s = celery.signature( + 'cic_eth.eth.account.role_account', + [ + 'bar', + default_chain_spec.asdict(), + ], + queue=None, + ) + t = s.apply_async() + r = t.get() + assert r[0][0] == address_bar + assert r[0][1] == 'bar' + + s = celery.signature( + 'cic_eth.eth.account.role_account', + [ + None, + default_chain_spec.asdict(), + ], + queue=None, + ) + t = s.apply_async() + r = t.get() + x_tags = ['foo', 'bar'] + x_addrs = [address_foo, address_bar] + + for v in r: + x_addrs.remove(v[0]) + x_tags.remove(v[1]) + + assert len(x_tags) == 0 + assert len(x_addrs) == 0 def test_gift( init_database, diff --git a/apps/cic-eth/tests/task/test_task_gas.py b/apps/cic-eth/tests/task/test_task_gas.py index f69a2dd2..63e09667 100644 --- a/apps/cic-eth/tests/task/test_task_gas.py +++ b/apps/cic-eth/tests/task/test_task_gas.py @@ -35,10 +35,26 @@ from hexathon import strip_0x from cic_eth.eth.gas import cache_gas_data from cic_eth.error import OutOfGasError from cic_eth.queue.tx import queue_create +from cic_eth.task import BaseTask logg = logging.getLogger() +def test_task_gas_limit( + eth_rpc, + eth_signer, + default_chain_spec, + agent_roles, + celery_session_worker, + ): + rpc = RPCConnection.connect(default_chain_spec, 'default') + gas_oracle = BaseTask().create_gas_oracle(rpc) + c = Gas(default_chain_spec, signer=eth_signer, gas_oracle=gas_oracle) + (tx_hash_hex, o) = c.create(agent_roles['ALICE'], agent_roles['BOB'], 10, tx_format=TxFormat.RLP_SIGNED) + tx = unpack(bytes.fromhex(strip_0x(o)), default_chain_spec) + assert (tx['gas'], BaseTask.min_fee_price) + + def test_task_check_gas_ok( default_chain_spec, eth_rpc, diff --git a/apps/cic-eth/tests/unit/queue/test_balances.py b/apps/cic-eth/tests/unit/queue/test_balances.py index ffadd080..dccaa4c3 100644 --- a/apps/cic-eth/tests/unit/queue/test_balances.py +++ b/apps/cic-eth/tests/unit/queue/test_balances.py @@ -143,7 +143,7 @@ def test_incoming_balance( 'converters': [], } b = balance_incoming([token_data], recipient, default_chain_spec.asdict()) - assert b[0]['balance_incoming'] == 0 + assert b[0]['balance_incoming'] == 1000 otx.readysend(session=init_database) init_database.flush() @@ -152,8 +152,8 @@ def test_incoming_balance( otx.sent(session=init_database) init_database.commit() - b = balance_incoming([token_data], recipient, default_chain_spec.asdict()) - assert b[0]['balance_incoming'] == 1000 + #b = balance_incoming([token_data], recipient, default_chain_spec.asdict()) + #assert b[0]['balance_incoming'] == 1000 otx.success(block=1024, session=init_database) init_database.commit() diff --git a/apps/cic-eth/tools_requirements.txt b/apps/cic-eth/tools_requirements.txt index 77eece3f..831d6acc 100644 --- a/apps/cic-eth/tools_requirements.txt +++ b/apps/cic-eth/tools_requirements.txt @@ -1,7 +1,5 @@ -crypto-dev-signer>=0.4.15rc2,<=0.4.15 -chainqueue>=0.0.5a3,<0.1.0 -cic-eth-registry>=0.6.1a6,<0.7.0 +chainqueue~=0.0.6a4 redis==3.5.3 -hexathon~=0.0.1a8 +hexathon~=0.1.0 pycryptodome==3.10.1 pyxdg==0.27 diff --git a/apps/cic-notify/.config/test/database.ini b/apps/cic-notify/.config/test/database.ini index bd32185b..3341956d 100644 --- a/apps/cic-notify/.config/test/database.ini +++ b/apps/cic-notify/.config/test/database.ini @@ -1,10 +1,9 @@ -[DATABASE] -user = postgres -password = -host = localhost -port = 5432 -name = /tmp/cic-notify.db -#engine = postgresql -#driver = psycopg2 -engine = sqlite -driver = pysqlite +[database] +name=cic_notify_test +user= +password= +host=localhost +port= +engine=sqlite +driver=pysqlite +debug=0 diff --git a/apps/cic-notify/.coveragerc b/apps/cic-notify/.coveragerc new file mode 100644 index 00000000..ed07bae5 --- /dev/null +++ b/apps/cic-notify/.coveragerc @@ -0,0 +1,7 @@ +[report] +omit = + venv/* + scripts/* + cic_notify/db/migrations/* + cic_notify/runnable/* + cic_notify/version.py \ No newline at end of file diff --git a/apps/cic-notify/cic_notify/api.py b/apps/cic-notify/cic_notify/api.py index fb27f7bd..a8cfc573 100644 --- a/apps/cic-notify/cic_notify/api.py +++ b/apps/cic-notify/cic_notify/api.py @@ -3,6 +3,7 @@ import logging import re # third-party imports +import cic_notify.tasks.sms.db from celery.app.control import Inspect import celery @@ -13,45 +14,16 @@ app = celery.current_app logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() -sms_tasks_matcher = r"^(cic_notify.tasks.sms)(\.\w+)?" - - -re_q = r'^cic-notify' -def get_sms_queue_tasks(app, task_prefix='cic_notify.tasks.sms.'): - host_queues = [] - - i = Inspect(app=app) - qs = i.active_queues() - for host in qs.keys(): - for q in qs[host]: - if re.match(re_q, q['name']): - host_queues.append((host, q['name'],)) - - task_prefix_len = len(task_prefix) - queue_tasks = [] - for (host, queue) in host_queues: - i = Inspect(app=app, destination=[host]) - for tasks in i.registered_tasks().values(): - for task in tasks: - if len(task) >= task_prefix_len and task[:task_prefix_len] == task_prefix: - queue_tasks.append((queue, task,)) - - return queue_tasks - class Api: - # TODO: Implement callback strategy - def __init__(self, queue=None): + def __init__(self, queue: any = 'cic-notify'): """ :param queue: The queue on which to execute notification tasks :type queue: str """ self.queue = queue - self.sms_tasks = get_sms_queue_tasks(app) - logg.debug('sms tasks {}'.format(self.sms_tasks)) - - def sms(self, message, recipient): + def sms(self, message: str, recipient: str): """This function chains all sms tasks in order to send a message, log and persist said data to disk :param message: The message to be sent to the recipient. :type message: str @@ -60,24 +32,9 @@ class Api: :return: a celery Task :rtype: Celery.Task """ - signatures = [] - for q in self.sms_tasks: - - if not self.queue: - queue = q[0] - else: - queue = self.queue - - signature = celery.signature( - q[1], - [ - message, - recipient, - ], - queue=queue, - ) - signatures.append(signature) - - t = celery.group(signatures)() - - return t + s_send = celery.signature('cic_notify.tasks.sms.africastalking.send', [message, recipient], queue=self.queue) + s_log = celery.signature('cic_notify.tasks.sms.log.log', [message, recipient], queue=self.queue) + s_persist_notification = celery.signature( + 'cic_notify.tasks.sms.db.persist_notification', [message, recipient], queue=self.queue) + signatures = [s_send, s_log, s_persist_notification] + return celery.group(signatures)() diff --git a/apps/cic-notify/cic_notify/db/migrations/default/alembic.ini b/apps/cic-notify/cic_notify/db/migrations/default/alembic.ini index 5b6e42b2..6a36ec5a 100644 --- a/apps/cic-notify/cic_notify/db/migrations/default/alembic.ini +++ b/apps/cic-notify/cic_notify/db/migrations/default/alembic.ini @@ -2,7 +2,7 @@ [alembic] # path to migration scripts -script_location = migrations +script_location = . # template used to generate migration files # file_template = %%(rev)s_%%(slug)s @@ -27,28 +27,17 @@ script_location = migrations # sourceless = false # version location specification; this defaults -# to migrations/versions. When using multiple version +# to ./versions. When using multiple version # directories, initial revisions must be specified with --version-path -# version_locations = %(here)s/bar %(here)s/bat migrations/versions +# version_locations = %(here)s/bar %(here)s/bat ./versions # the output encoding used when revision files # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = postgres+psycopg2://postgres@localhost/cic-notify +sqlalchemy.url = driver://user:pass@localhost/dbname -[post_write_hooks] -# post_write_hooks defines scripts or Python functions that are run -# on newly generated revision scripts. See the documentation for further -# detail and examples - -# format using "black" - use the console_scripts runner, against the "black" entrypoint -# hooks=black -# black.type=console_scripts -# black.entrypoint=black -# black.options=-l 79 - # Logging configuration [loggers] keys = root,sqlalchemy,alembic diff --git a/apps/cic-notify/cic_notify/db/migrations/default/env.py b/apps/cic-notify/cic_notify/db/migrations/default/env.py index 70518a2e..00d90657 100644 --- a/apps/cic-notify/cic_notify/db/migrations/default/env.py +++ b/apps/cic-notify/cic_notify/db/migrations/default/env.py @@ -11,7 +11,7 @@ config = context.config # Interpret the config file for Python logging. # This line sets up loggers basically. -fileConfig(config.config_file_name) +fileConfig(config.config_file_name, disable_existing_loggers=True) # add your model's MetaData object here # for 'autogenerate' support @@ -56,11 +56,14 @@ def run_migrations_online(): and associate a connection with the context. """ - connectable = engine_from_config( - config.get_section(config.config_ini_section), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) + connectable = context.config.attributes.get("connection", None) + + if connectable is None: + connectable = engine_from_config( + context.config.get_section(context.config.config_ini_section), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) with connectable.connect() as connection: context.configure( diff --git a/apps/cic-notify/cic_notify/tasks/sms/log.py b/apps/cic-notify/cic_notify/tasks/sms/log.py index 05a6444a..c123f0b3 100644 --- a/apps/cic-notify/cic_notify/tasks/sms/log.py +++ b/apps/cic-notify/cic_notify/tasks/sms/log.py @@ -7,7 +7,7 @@ import celery celery_app = celery.current_app logg = celery_app.log.get_default_logger() -local_logg = logging.getLogger(__name__) +local_logg = logging.getLogger() @celery_app.task diff --git a/apps/cic-notify/cic_notify/version.py b/apps/cic-notify/cic_notify/version.py index 02dcb38f..5eace095 100644 --- a/apps/cic-notify/cic_notify/version.py +++ b/apps/cic-notify/cic_notify/version.py @@ -9,7 +9,7 @@ import semver logg = logging.getLogger() -version = (0, 4, 0, 'alpha.11') +version = (0, 4, 0, 'alpha.12') version_object = semver.VersionInfo( major=version[0], diff --git a/apps/cic-notify/requirements.txt b/apps/cic-notify/requirements.txt index 662c6e15..fa6a6ad0 100644 --- a/apps/cic-notify/requirements.txt +++ b/apps/cic-notify/requirements.txt @@ -1,4 +1,4 @@ -confini>=0.3.6rc4,<0.5.0 +confini~=0.5.1 africastalking==1.2.3 SQLAlchemy==1.3.20 alembic==1.4.2 diff --git a/apps/cic-notify/test_requirements.txt b/apps/cic-notify/test_requirements.txt index 5a2ab673..7217efc1 100644 --- a/apps/cic-notify/test_requirements.txt +++ b/apps/cic-notify/test_requirements.txt @@ -1,5 +1,9 @@ -pytest~=6.0.1 -pytest-celery~=0.0.0a1 -pytest-mock~=3.3.1 -pysqlite3~=0.4.3 -pytest-cov==2.10.1 +Faker==11.1.0 +faker-e164==0.1.0 +pytest==6.2.5 +pytest-celery~=0.0.0 +pytest-mock==3.6.1 +pysqlite3~=0.4.6 +pytest-cov==3.0.0 +pytest-alembic==0.7.0 +requests-mock==1.9.3 diff --git a/apps/cic-notify/tests/__init__.py b/apps/cic-notify/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/apps/cic-notify/tests/cic_notify/db/migrations/test_migrations.py b/apps/cic-notify/tests/cic_notify/db/migrations/test_migrations.py new file mode 100644 index 00000000..9cef5dc5 --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/db/migrations/test_migrations.py @@ -0,0 +1,28 @@ +import pytest + + +def test_single_head_revision(alembic_runner): + heads = alembic_runner.heads + head_count = len(heads) + assert head_count == 1 + + +def test_upgrade(alembic_runner): + try: + alembic_runner.migrate_up_to("head") + except RuntimeError: + pytest.fail('Failed to upgrade to the head revision.') + + +def test_up_down_consistency(alembic_runner): + try: + for revision in alembic_runner.history.revisions: + alembic_runner.migrate_up_to(revision) + except RuntimeError: + pytest.fail('Failed to upgrade through each revision individually.') + + try: + for revision in reversed(alembic_runner.history.revisions): + alembic_runner.migrate_down_to(revision) + except RuntimeError: + pytest.fail('Failed to downgrade through each revision individually.') diff --git a/apps/cic-notify/tests/cic_notify/db/models/test_notification.py b/apps/cic-notify/tests/cic_notify/db/models/test_notification.py new file mode 100644 index 00000000..f18b59bc --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/db/models/test_notification.py @@ -0,0 +1,27 @@ +# standard imports + +# external imports +from faker import Faker +from faker_e164.providers import E164Provider + +# local imports +from cic_notify.db.enum import NotificationStatusEnum, NotificationTransportEnum +from cic_notify.db.models.notification import Notification + + +# test imports +from tests.helpers.phone import phone_number + + +def test_notification(init_database): + message = 'Hello world' + recipient = phone_number() + notification = Notification(NotificationTransportEnum.SMS, recipient, message) + init_database.add(notification) + init_database.commit() + + notification = init_database.query(Notification).get(1) + assert notification.status == NotificationStatusEnum.UNKNOWN + assert notification.recipient == recipient + assert notification.message == message + assert notification.transport == NotificationTransportEnum.SMS diff --git a/apps/cic-notify/tests/cic_notify/db/test_db.py b/apps/cic-notify/tests/cic_notify/db/test_db.py new file mode 100644 index 00000000..6d382cdf --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/db/test_db.py @@ -0,0 +1,38 @@ +# standard imports +import os + +# third-party imports + +# local imports +from cic_notify.db import dsn_from_config + + +def test_dsn_from_config(load_config): + """ + """ + # test dsn for other db formats + overrides = { + 'DATABASE_PASSWORD': 'password', + 'DATABASE_DRIVER': 'psycopg2', + 'DATABASE_ENGINE': 'postgresql' + } + load_config.dict_override(dct=overrides, dct_description='Override values to test different db formats.') + + scheme = f'{load_config.get("DATABASE_ENGINE")}+{load_config.get("DATABASE_DRIVER")}' + + dsn = dsn_from_config(load_config) + assert dsn == f"{scheme}://{load_config.get('DATABASE_USER')}:{load_config.get('DATABASE_PASSWORD')}@{load_config.get('DATABASE_HOST')}:{load_config.get('DATABASE_PORT')}/{load_config.get('DATABASE_NAME')}" + + # undoes overrides to revert engine and drivers to sqlite + overrides = { + 'DATABASE_PASSWORD': '', + 'DATABASE_DRIVER': 'pysqlite', + 'DATABASE_ENGINE': 'sqlite' + } + load_config.dict_override(dct=overrides, dct_description='Override values to test different db formats.') + + # test dsn for sqlite engine + dsn = dsn_from_config(load_config) + scheme = f'{load_config.get("DATABASE_ENGINE")}+{load_config.get("DATABASE_DRIVER")}' + assert dsn == f'{scheme}:///{load_config.get("DATABASE_NAME")}' + diff --git a/apps/cic-notify/tests/cic_notify/tasks/sms/test_africastalking_tasks.py b/apps/cic-notify/tests/cic_notify/tasks/sms/test_africastalking_tasks.py new file mode 100644 index 00000000..e6cf1811 --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/tasks/sms/test_africastalking_tasks.py @@ -0,0 +1,75 @@ +# standard imports +import logging +import os + +# external imports +import pytest +import requests_mock + + +# local imports +from cic_notify.error import NotInitializedError, AlreadyInitializedError, NotificationSendError +from cic_notify.tasks.sms.africastalking import AfricasTalkingNotifier + +# test imports +from tests.helpers.phone import phone_number + + +def test_africas_talking_notifier(africastalking_response, caplog): + caplog.set_level(logging.DEBUG) + with pytest.raises(NotInitializedError) as error: + AfricasTalkingNotifier() + assert str(error.value) == '' + + api_key = os.urandom(24).hex() + sender_id = 'bar' + username = 'sandbox' + AfricasTalkingNotifier.initialize(username, api_key, sender_id) + africastalking_notifier = AfricasTalkingNotifier() + assert africastalking_notifier.sender_id == sender_id + assert africastalking_notifier.initiated is True + + with pytest.raises(AlreadyInitializedError) as error: + AfricasTalkingNotifier.initialize(username, api_key, sender_id) + assert str(error.value) == '' + + with requests_mock.Mocker(real_http=False) as request_mocker: + message = 'Hello world.' + recipient = phone_number() + africastalking_response.get('SMSMessageData').get('Recipients')[0]['number'] = recipient + request_mocker.register_uri(method='POST', + headers={'content-type': 'application/json'}, + json=africastalking_response, + url='https://api.sandbox.africastalking.com/version1/messaging', + status_code=200) + africastalking_notifier.send(message, recipient) + assert f'Africastalking response sender-id {africastalking_response}' in caplog.text + africastalking_notifier.sender_id = None + africastalking_notifier.send(message, recipient) + assert f'africastalking response no-sender-id {africastalking_response}' in caplog.text + with pytest.raises(NotificationSendError) as error: + status = 'InvalidPhoneNumber' + status_code = 403 + africastalking_response.get('SMSMessageData').get('Recipients')[0]['status'] = status + africastalking_response.get('SMSMessageData').get('Recipients')[0]['statusCode'] = status_code + + request_mocker.register_uri(method='POST', + headers={'content-type': 'application/json'}, + json=africastalking_response, + url='https://api.sandbox.africastalking.com/version1/messaging', + status_code=200) + africastalking_notifier.send(message, recipient) + assert str(error.value) == f'Sending notification failed due to: {status}' + with pytest.raises(NotificationSendError) as error: + recipients = [] + status = 'InsufficientBalance' + africastalking_response.get('SMSMessageData')['Recipients'] = recipients + africastalking_response.get('SMSMessageData')['Message'] = status + + request_mocker.register_uri(method='POST', + headers={'content-type': 'application/json'}, + json=africastalking_response, + url='https://api.sandbox.africastalking.com/version1/messaging', + status_code=200) + africastalking_notifier.send(message, recipient) + assert str(error.value) == f'Unexpected number of recipients: {len(recipients)}. Status: {status}' diff --git a/apps/cic-notify/tests/cic_notify/tasks/sms/test_db_tasks.py b/apps/cic-notify/tests/cic_notify/tasks/sms/test_db_tasks.py new file mode 100644 index 00000000..f6b296d3 --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/tasks/sms/test_db_tasks.py @@ -0,0 +1,26 @@ +# standard imports + +# external imports +import celery + +# local imports +from cic_notify.db.enum import NotificationStatusEnum, NotificationTransportEnum +from cic_notify.db.models.notification import Notification + +# test imports +from tests.helpers.phone import phone_number + + +def test_persist_notification(celery_session_worker, init_database): + message = 'Hello world.' + recipient = phone_number() + s_persist_notification = celery.signature( + 'cic_notify.tasks.sms.db.persist_notification', (message, recipient) + ) + s_persist_notification.apply_async().get() + + notification = Notification.session.query(Notification).filter_by(recipient=recipient).first() + assert notification.status == NotificationStatusEnum.UNKNOWN + assert notification.recipient == recipient + assert notification.message == message + assert notification.transport == NotificationTransportEnum.SMS \ No newline at end of file diff --git a/apps/cic-notify/tests/cic_notify/tasks/sms/test_log_tasks.py b/apps/cic-notify/tests/cic_notify/tasks/sms/test_log_tasks.py new file mode 100644 index 00000000..3403ebc0 --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/tasks/sms/test_log_tasks.py @@ -0,0 +1,21 @@ +# standard imports +import logging + +# external imports +import celery + +# local imports + +# test imports +from tests.helpers.phone import phone_number + + +def test_log(caplog, celery_session_worker): + message = 'Hello world.' + recipient = phone_number() + caplog.set_level(logging.INFO) + s_log = celery.signature( + 'cic_notify.tasks.sms.log.log', [message, recipient] + ) + s_log.apply_async().get() + assert f'message to {recipient}: {message}' in caplog.text diff --git a/apps/cic-notify/tests/cic_notify/test_api.py b/apps/cic-notify/tests/cic_notify/test_api.py new file mode 100644 index 00000000..b8e7dd4f --- /dev/null +++ b/apps/cic-notify/tests/cic_notify/test_api.py @@ -0,0 +1,24 @@ +# standard imports + +# external imports +import celery + +# local imports +from cic_notify.api import Api + +# test imports +from tests.helpers.phone import phone_number + + +def test_api(celery_session_worker, mocker): + mocked_group = mocker.patch('celery.group') + message = 'Hello world.' + recipient = phone_number() + s_send = celery.signature('cic_notify.tasks.sms.africastalking.send', [message, recipient], queue=None) + s_log = celery.signature('cic_notify.tasks.sms.log.log', [message, recipient], queue=None) + s_persist_notification = celery.signature( + 'cic_notify.tasks.sms.db.persist_notification', [message, recipient], queue=None) + signatures = [s_send, s_log, s_persist_notification] + api = Api(queue=None) + api.sms(message, recipient) + mocked_group.assert_called_with(signatures) diff --git a/apps/cic-notify/tests/conftest.py b/apps/cic-notify/tests/conftest.py index 4817ea3e..0bfd5865 100644 --- a/apps/cic-notify/tests/conftest.py +++ b/apps/cic-notify/tests/conftest.py @@ -1,31 +1,13 @@ # standard imports -import sys -import os -import pytest import logging # third party imports -import confini - -script_dir = os.path.dirname(os.path.realpath(__file__)) -root_dir = os.path.dirname(script_dir) -sys.path.insert(0, root_dir) # local imports -from cic_notify.db.models.base import SessionBase -#from transport.notification import AfricastalkingNotification -# fixtures -from tests.fixtures_config import * -from tests.fixtures_celery import * -from tests.fixtures_database import * +# test imports -logg = logging.getLogger() - - -#@pytest.fixture(scope='session') -#def africastalking_notification( -# load_config, -# ): -# return AfricastalkingNotificationTransport(load_config) -# +from .fixtures.celery import * +from .fixtures.config import * +from .fixtures.database import * +from .fixtures.result import * diff --git a/apps/cic-notify/tests/fixtures_celery.py b/apps/cic-notify/tests/fixtures/celery.py similarity index 88% rename from apps/cic-notify/tests/fixtures_celery.py rename to apps/cic-notify/tests/fixtures/celery.py index 20cfc444..7cee6ae1 100644 --- a/apps/cic-notify/tests/fixtures_celery.py +++ b/apps/cic-notify/tests/fixtures/celery.py @@ -37,12 +37,6 @@ def celery_config(): shutil.rmtree(rq) -@pytest.fixture(scope='session') -def celery_worker_parameters(): - return { -# 'queues': ('cic-notify'), - } - @pytest.fixture(scope='session') def celery_enable_logging(): return True diff --git a/apps/cic-notify/tests/fixtures/config.py b/apps/cic-notify/tests/fixtures/config.py new file mode 100644 index 00000000..b6baa570 --- /dev/null +++ b/apps/cic-notify/tests/fixtures/config.py @@ -0,0 +1,32 @@ +# standard imports +import os +import logging + +# external imports +import pytest +from confini import Config + +logg = logging.getLogger(__file__) + + +fixtures_dir = os.path.dirname(__file__) +root_directory = os.path.dirname(os.path.dirname(fixtures_dir)) + + +@pytest.fixture(scope='session') +def alembic_config(): + migrations_directory = os.path.join(root_directory, 'cic_notify', 'db', 'migrations', 'default') + file = os.path.join(migrations_directory, 'alembic.ini') + return { + 'file': file, + 'script_location': migrations_directory + } + + +@pytest.fixture(scope='session') +def load_config(): + config_directory = os.path.join(root_directory, '.config/test') + config = Config(default_dir=config_directory) + config.process() + logg.debug('config loaded\n{}'.format(config)) + return config diff --git a/apps/cic-notify/tests/fixtures/database.py b/apps/cic-notify/tests/fixtures/database.py new file mode 100644 index 00000000..490542d7 --- /dev/null +++ b/apps/cic-notify/tests/fixtures/database.py @@ -0,0 +1,54 @@ +# standard imports +import os + +# third-party imports +import pytest +import alembic +from alembic.config import Config as AlembicConfig + +# local imports +from cic_notify.db import dsn_from_config +from cic_notify.db.models.base import SessionBase, create_engine + +from .config import root_directory + + +@pytest.fixture(scope='session') +def alembic_engine(load_config): + data_source_name = dsn_from_config(load_config) + return create_engine(data_source_name) + + +@pytest.fixture(scope='session') +def database_engine(load_config): + if load_config.get('DATABASE_ENGINE') == 'sqlite': + try: + os.unlink(load_config.get('DATABASE_NAME')) + except FileNotFoundError: + pass + dsn = dsn_from_config(load_config) + SessionBase.connect(dsn) + return dsn + + +@pytest.fixture(scope='function') +def init_database(load_config, database_engine): + db_directory = os.path.join(root_directory, 'cic_notify', 'db') + migrations_directory = os.path.join(db_directory, 'migrations', load_config.get('DATABASE_ENGINE')) + if not os.path.isdir(migrations_directory): + migrations_directory = os.path.join(db_directory, 'migrations', 'default') + + session = SessionBase.create_session() + + alembic_config = AlembicConfig(os.path.join(migrations_directory, 'alembic.ini')) + alembic_config.set_main_option('sqlalchemy.url', database_engine) + alembic_config.set_main_option('script_location', migrations_directory) + + alembic.command.downgrade(alembic_config, 'base') + alembic.command.upgrade(alembic_config, 'head') + + yield session + session.commit() + session.close() + + diff --git a/apps/cic-notify/tests/fixtures/result.py b/apps/cic-notify/tests/fixtures/result.py new file mode 100644 index 00000000..ba8c7841 --- /dev/null +++ b/apps/cic-notify/tests/fixtures/result.py @@ -0,0 +1,24 @@ +# standard imports + +# external imports +import pytest + + +# local imports + +# test imports + +@pytest.fixture(scope="function") +def africastalking_response(): + return { + "SMSMessageData": { + "Message": "Sent to 1/1 Total Cost: KES 0.8000", + "Recipients": [{ + "statusCode": 101, + "number": "+254711XXXYYY", + "status": "Success", + "cost": "KES 0.8000", + "messageId": "ATPid_SampleTxnId123" + }] + } + } diff --git a/apps/cic-notify/tests/fixtures_config.py b/apps/cic-notify/tests/fixtures_config.py deleted file mode 100644 index 723fff1c..00000000 --- a/apps/cic-notify/tests/fixtures_config.py +++ /dev/null @@ -1,20 +0,0 @@ -# standard imports -import os -import logging - -# third-party imports -import pytest -import confini - -script_dir = os.path.dirname(os.path.realpath(__file__)) -root_dir = os.path.dirname(script_dir) -logg = logging.getLogger(__file__) - - -@pytest.fixture(scope='session') -def load_config(): - config_dir = os.path.join(root_dir, '.config/test') - conf = confini.Config(config_dir, 'CICTEST') - conf.process() - logg.debug('config {}'.format(conf)) - return conf diff --git a/apps/cic-notify/tests/fixtures_database.py b/apps/cic-notify/tests/fixtures_database.py deleted file mode 100644 index eb3aad69..00000000 --- a/apps/cic-notify/tests/fixtures_database.py +++ /dev/null @@ -1,48 +0,0 @@ -# standard imports -import os - -# third-party imports -import pytest -import alembic -from alembic.config import Config as AlembicConfig - -# local imports -from cic_notify.db import SessionBase -from cic_notify.db import dsn_from_config - - -@pytest.fixture(scope='session') -def database_engine( - load_config, - ): - dsn = dsn_from_config(load_config) - SessionBase.connect(dsn) - return dsn - - -@pytest.fixture(scope='function') -def init_database( - load_config, - database_engine, - ): - - rootdir = os.path.dirname(os.path.dirname(__file__)) - dbdir = os.path.join(rootdir, 'cic_notify', 'db') - migrationsdir = os.path.join(dbdir, 'migrations', load_config.get('DATABASE_ENGINE')) - if not os.path.isdir(migrationsdir): - migrationsdir = os.path.join(dbdir, 'migrations', 'default') - - session = SessionBase.create_session() - - ac = AlembicConfig(os.path.join(migrationsdir, 'alembic.ini')) - ac.set_main_option('sqlalchemy.url', database_engine) - ac.set_main_option('script_location', migrationsdir) - - alembic.command.downgrade(ac, 'base') - alembic.command.upgrade(ac, 'head') - - yield session - session.commit() - session.close() - - diff --git a/apps/cic-notify/tests/helpers/phone.py b/apps/cic-notify/tests/helpers/phone.py new file mode 100644 index 00000000..ff4761d3 --- /dev/null +++ b/apps/cic-notify/tests/helpers/phone.py @@ -0,0 +1,16 @@ +# standard imports + +# external imports +from faker import Faker +from faker_e164.providers import E164Provider + +# local imports + +# test imports + +fake = Faker() +fake.add_provider(E164Provider) + + +def phone_number() -> str: + return fake.e164('KE') \ No newline at end of file diff --git a/apps/cic-notify/tests/test_sms.py b/apps/cic-notify/tests/test_sms.py deleted file mode 100644 index 6019ab5f..00000000 --- a/apps/cic-notify/tests/test_sms.py +++ /dev/null @@ -1,34 +0,0 @@ -# standard imports -import json - -# third party imports -import pytest -import celery - -# local imports -from cic_notify.tasks.sms import db -from cic_notify.tasks.sms import log - -def test_log_notification( - celery_session_worker, - ): - - recipient = '+25412121212' - content = 'bar' - s_log = celery.signature('cic_notify.tasks.sms.log.log') - t = s_log.apply_async(args=[recipient, content]) - - r = t.get() - - -def test_db_notification( - init_database, - celery_session_worker, - ): - - recipient = '+25412121213' - content = 'foo' - s_db = celery.signature('cic_notify.tasks.sms.db.persist_notification') - t = s_db.apply_async(args=[recipient, content]) - - r = t.get() diff --git a/apps/cic-signer/cic_signer/data/config/database.ini b/apps/cic-signer/cic_signer/data/config/database.ini new file mode 100644 index 00000000..2416ddaf --- /dev/null +++ b/apps/cic-signer/cic_signer/data/config/database.ini @@ -0,0 +1,10 @@ +[database] +engine = postgres +driver = psycopg2 +host = localhost +port = 5432 +name = cic_signer +user = +password = +debug = 0 +pool_size = 0 diff --git a/apps/cic-signer/cic_signer/data/config/signer.ini b/apps/cic-signer/cic_signer/data/config/signer.ini new file mode 100644 index 00000000..8b0c82ff --- /dev/null +++ b/apps/cic-signer/cic_signer/data/config/signer.ini @@ -0,0 +1,3 @@ +[signer] +provider = +secret = diff --git a/apps/cic-signer/requirements.txt b/apps/cic-signer/requirements.txt index 4a2e9bf3..f940881c 100644 --- a/apps/cic-signer/requirements.txt +++ b/apps/cic-signer/requirements.txt @@ -1 +1,2 @@ funga-eth[sql]>=0.5.1a1,<0.6.0 +chainlib-eth>=0.0.10a18 diff --git a/apps/cic-signer/scripts/sweep.py b/apps/cic-signer/scripts/sweep.py new file mode 100644 index 00000000..5b4f1513 --- /dev/null +++ b/apps/cic-signer/scripts/sweep.py @@ -0,0 +1,128 @@ +# standard imports +import os +import logging +import uuid +import random +import sys + +# external imports +from chainlib.chain import ChainSpec +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.gas import ( + balance, + Gas, + ) +from hexathon import ( + add_0x, + strip_0x, + ) +from chainlib.eth.connection import EthHTTPSignerConnection +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.sql import SQLKeystore +from chainlib.cli.wallet import Wallet +from chainlib.eth.address import AddressChecksum +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.gas import OverrideGasOracle +from chainlib.eth.address import ( + is_checksum_address, + to_checksum_address, + ) +from chainlib.eth.tx import ( + TxFormat, + ) +import chainlib.eth.cli + + +script_dir = os.path.dirname(os.path.realpath(__file__)) +config_dir = os.path.join(script_dir, '..', 'cic_signer', 'data', 'config') + +logging.basicConfig(level=logging.WARNING) +logg = logging.getLogger() + +arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.WALLET +argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +args = argparser.parse_args() + +config = chainlib.eth.cli.Config.from_args(args, arg_flags, base_config_dir=config_dir) + +# set up rpc +chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) + + +# connect to database +dsn = 'postgresql://{}:{}@{}:{}/{}'.format( + config.get('DATABASE_USER'), + config.get('DATABASE_PASSWORD'), + config.get('DATABASE_HOST'), + config.get('DATABASE_PORT'), + config.get('DATABASE_NAME'), + ) +logg.info('using dsn {}'.format(dsn)) + +keystore = SQLKeystore(dsn, symmetric_key=bytes.fromhex(config.get('SIGNER_SECRET'))) +wallet = Wallet(EIP155Signer, keystore=keystore, checksummer=AddressChecksum) + +rpc = chainlib.eth.cli.Rpc(wallet=wallet) +conn = rpc.connect_by_config(config) + +wallet.init() + +def main(): + if config.get('_RECIPIENT') == None: + sys.stderr.write('Missing sink address\n') + sys.exit(1) + + sink_address = config.get('_RECIPIENT') + if config.get('_UNSAFE'): + sink_address = to_checksum_address(sink_address) + if not is_checksum_address(sink_address): + sys.stderr.write('Invalid sink address {}\n'.format(sink_address)) + sys.exit(1) + + if (config.get('_RPC_SEND')): + verify_string = random.randbytes(4).hex() + verify_string_check = input("\033[;31m*** WARNING! WARNING! WARNING! ***\033[;39m\nThis action will transfer all remaining gas from all accounts in custodial care to account {}. To confirm, please enter the string: {}\n".format(config.get('_RECIPIENT'), verify_string)) + if verify_string != verify_string_check: + sys.stderr.write('Verify string mismatch. Aborting!\n') + sys.exit(1) + + signer = rpc.get_signer() + + gas_oracle = rpc.get_gas_oracle() + gas_pair = gas_oracle.get_fee() + gas_price = gas_pair[0] + gas_limit = 21000 + gas_cost = gas_price * gas_limit + gas_oracle = OverrideGasOracle(price=gas_price, limit=gas_limit) + logg.info('using gas price {}'.format(gas_price)) + + for r in keystore.list(): + account = r[0] + + o = balance(add_0x(account)) + r = conn.do(o) + account_balance = 0 + try: + r = strip_0x(r) + account_balance = int(r, 16) + except ValueError: + account_balance = int(r) + + transfer_amount = account_balance - gas_cost + if transfer_amount <= 0: + logg.warning('address {} has balance {} which is less than gas cost {}, skipping'.format(account, account_balance, gas_cost)) + continue + + nonce_oracle = RPCNonceOracle(account, conn) + c = Gas(chain_spec, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle, signer=signer) + tx_hash_hex = None + if (config.get('_RPC_SEND')): + (tx_hash_hex, o) = c.create(account, config.get('_RECIPIENT'), transfer_amount) + r = conn.do(o) + else: + (tx_hash_hex, o) = c.create(account, config.get('_RECIPIENT'), transfer_amount, tx_format=TxFormat.RLP_SIGNED) + + logg.info('address {} balance {} net transfer {} tx {}'.format(account, account_balance, transfer_amount, tx_hash_hex)) + +if __name__ == '__main__': + main() diff --git a/apps/cic-ussd/.coveragerc b/apps/cic-ussd/.coveragerc index a579887e..32961fd2 100644 --- a/apps/cic-ussd/.coveragerc +++ b/apps/cic-ussd/.coveragerc @@ -3,4 +3,5 @@ omit = venv/* scripts/* cic_ussd/db/migrations/* - cic_ussd/runnable/* \ No newline at end of file + cic_ussd/runnable/* + cic_ussd/version.py \ No newline at end of file diff --git a/apps/cic-ussd/cic_ussd/account/guardianship.py b/apps/cic-ussd/cic_ussd/account/guardianship.py new file mode 100644 index 00000000..32bcfaff --- /dev/null +++ b/apps/cic-ussd/cic_ussd/account/guardianship.py @@ -0,0 +1,22 @@ +# standard imports + +# external imports + +# local imports + +class Guardianship: + guardians: list = [] + + @classmethod + def load_system_guardians(cls, guardians_file: str): + with open(guardians_file, 'r') as system_guardians: + cls.guardians = [line.strip() for line in system_guardians] + + def is_system_guardian(self, phone_number: str): + """ + :param phone_number: + :type phone_number: + :return: + :rtype: + """ + return phone_number in self.guardians diff --git a/apps/cic-ussd/cic_ussd/account/statement.py b/apps/cic-ussd/cic_ussd/account/statement.py index df0e1b02..ce8e156b 100644 --- a/apps/cic-ussd/cic_ussd/account/statement.py +++ b/apps/cic-ussd/cic_ussd/account/statement.py @@ -13,7 +13,6 @@ from cic_types.condiments import MetadataPointer from cic_ussd.account.chain import Chain from cic_ussd.account.transaction import from_wei from cic_ussd.cache import cache_data_key, get_cached_data -from cic_ussd.translation import translation_for logg = logging.getLogger(__name__) @@ -97,17 +96,3 @@ def query_statement(blockchain_address: str, limit: int = 9): callback_param=blockchain_address ) cic_eth_api.list(address=blockchain_address, limit=limit) - - -def statement_transaction_set(preferred_language: str, transaction_reprs: list): - """ - :param preferred_language: - :type preferred_language: - :param transaction_reprs: - :type transaction_reprs: - :return: - :rtype: - """ - if not transaction_reprs: - return translation_for('helpers.no_transaction_history', preferred_language) - return ''.join(f'{transaction_repr}\n' for transaction_repr in transaction_reprs) diff --git a/apps/cic-ussd/cic_ussd/account/tokens.py b/apps/cic-ussd/cic_ussd/account/tokens.py index 4ffeb57b..288b4d9c 100644 --- a/apps/cic-ussd/cic_ussd/account/tokens.py +++ b/apps/cic-ussd/cic_ussd/account/tokens.py @@ -15,7 +15,6 @@ from cic_ussd.cache import cache_data, cache_data_key, get_cached_data from cic_ussd.error import CachedDataNotFoundError, SeppukuError from cic_ussd.metadata.tokens import query_token_info, query_token_metadata from cic_ussd.processor.util import wait_for_cache -from cic_ussd.translation import translation_for logg = logging.getLogger(__file__) @@ -326,16 +325,3 @@ def set_active_token(blockchain_address: str, token_symbol: str): cache_data(key=key, data=token_symbol) -def token_list_set(preferred_language: str, token_data_reprs: list): - """ - :param preferred_language: - :type preferred_language: - :param token_data_reprs: - :type token_data_reprs: - :return: - :rtype: - """ - if not token_data_reprs: - return translation_for('helpers.no_tokens_list', preferred_language) - return ''.join(f'{token_data_repr}\n' for token_data_repr in token_data_reprs) - diff --git a/apps/cic-ussd/cic_ussd/cache.py b/apps/cic-ussd/cic_ussd/cache.py index 2faa46d9..8e9b8a38 100644 --- a/apps/cic-ussd/cic_ussd/cache.py +++ b/apps/cic-ussd/cic_ussd/cache.py @@ -14,7 +14,7 @@ class Cache: store: Redis = None -def cache_data(key: str, data: str): +def cache_data(key: str, data: [bytes, float, int, str]): """ :param key: :type key: @@ -55,5 +55,6 @@ def cache_data_key(identifier: Union[list, bytes], salt: MetadataPointer): hash_object.update(identity) else: hash_object.update(identifier) - hash_object.update(salt.value.encode(encoding="utf-8")) + if salt != MetadataPointer.NONE: + hash_object.update(salt.value.encode(encoding="utf-8")) return hash_object.digest().hex() diff --git a/apps/cic-ussd/cic_ussd/db/models/account.py b/apps/cic-ussd/cic_ussd/db/models/account.py index 505f822b..47d0fff9 100644 --- a/apps/cic-ussd/cic_ussd/db/models/account.py +++ b/apps/cic-ussd/cic_ussd/db/models/account.py @@ -63,10 +63,7 @@ class Account(SessionBase): def remove_guardian(self, phone_number: str): set_guardians = self.guardians.split(',') set_guardians.remove(phone_number) - if len(set_guardians) > 1: - self.guardians = ','.join(set_guardians) - else: - self.guardians = set_guardians[0] + self.guardians = ','.join(set_guardians) def get_guardians(self) -> list: return self.guardians.split(',') if self.guardians else [] @@ -171,7 +168,7 @@ class Account(SessionBase): return check_password_hash(password, self.password_hash) -def create(chain_str: str, phone_number: str, session: Session): +def create(chain_str: str, phone_number: str, session: Session, preferred_language: str): """ :param chain_str: :type chain_str: @@ -179,12 +176,14 @@ def create(chain_str: str, phone_number: str, session: Session): :type phone_number: :param session: :type session: + :param preferred_language: + :type preferred_language: :return: :rtype: """ api = Api(callback_task='cic_ussd.tasks.callback_handler.account_creation_callback', callback_queue='cic-ussd', - callback_param='', + callback_param=preferred_language, chain_str=chain_str) task_uuid = api.create_account().id TaskTracker.add(session=session, task_uuid=task_uuid) diff --git a/apps/cic-ussd/cic_ussd/db/ussd_menu.json b/apps/cic-ussd/cic_ussd/db/ussd_menu.json index a176f952..1419eacf 100644 --- a/apps/cic-ussd/cic_ussd/db/ussd_menu.json +++ b/apps/cic-ussd/cic_ussd/db/ussd_menu.json @@ -2,417 +2,441 @@ "ussd_menu": { "1": { "description": "Entry point for users to select their preferred language.", - "display_key": "ussd.kenya.initial_language_selection", + "display_key": "ussd.initial_language_selection", "name": "initial_language_selection", "parent": null }, "2": { "description": "Entry point for users to enter a pin to secure their account.", - "display_key": "ussd.kenya.initial_pin_entry", + "display_key": "ussd.initial_pin_entry", "name": "initial_pin_entry", "parent": null }, "3": { "description": "Pin confirmation entry menu.", - "display_key": "ussd.kenya.initial_pin_confirmation", + "display_key": "ussd.initial_pin_confirmation", "name": "initial_pin_confirmation", "parent": "initial_pin_entry" }, "4": { "description": "The signup process has been initiated and the account is being created.", - "display_key": "ussd.kenya.account_creation_prompt", + "display_key": "ussd.account_creation_prompt", "name": "account_creation_prompt", "parent": null }, "5": { "description": "Entry point for activated users.", - "display_key": "ussd.kenya.start", + "display_key": "ussd.start", "name": "start", "parent": null }, "6": { "description": "Given name entry menu.", - "display_key": "ussd.kenya.enter_given_name", + "display_key": "ussd.enter_given_name", "name": "enter_given_name", "parent": "metadata_management" }, "7": { "description": "Family name entry menu.", - "display_key": "ussd.kenya.enter_family_name", + "display_key": "ussd.enter_family_name", "name": "enter_family_name", "parent": "metadata_management" }, "8": { "description": "Gender entry menu.", - "display_key": "ussd.kenya.enter_gender", + "display_key": "ussd.enter_gender", "name": "enter_gender", "parent": "metadata_management" }, "9": { "description": "Age entry menu.", - "display_key": "ussd.kenya.enter_gender", + "display_key": "ussd.enter_gender", "name": "enter_gender", "parent": "metadata_management" }, "10": { "description": "Location entry menu.", - "display_key": "ussd.kenya.enter_location", + "display_key": "ussd.enter_location", "name": "enter_location", "parent": "metadata_management" }, "11": { "description": "Products entry menu.", - "display_key": "ussd.kenya.enter_products", + "display_key": "ussd.enter_products", "name": "enter_products", "parent": "metadata_management" }, "12": { "description": "Entry point for activated users.", - "display_key": "ussd.kenya.start", + "display_key": "ussd.start", "name": "start", "parent": null }, "13": { "description": "Send Token recipient entry.", - "display_key": "ussd.kenya.enter_transaction_recipient", + "display_key": "ussd.enter_transaction_recipient", "name": "enter_transaction_recipient", "parent": "start" }, "14": { "description": "Send Token amount prompt menu.", - "display_key": "ussd.kenya.enter_transaction_amount", + "display_key": "ussd.enter_transaction_amount", "name": "enter_transaction_amount", "parent": "start" }, "15": { "description": "Pin entry for authorization to send token.", - "display_key": "ussd.kenya.transaction_pin_authorization", + "display_key": "ussd.transaction_pin_authorization", "name": "transaction_pin_authorization", "parent": "start" }, "16": { "description": "Manage account menu.", - "display_key": "ussd.kenya.account_management", + "display_key": "ussd.account_management", "name": "account_management", "parent": "start" }, "17": { "description": "Manage metadata menu.", - "display_key": "ussd.kenya.metadata_management", + "display_key": "ussd.metadata_management", "name": "metadata_management", "parent": "start" }, "18": { "description": "Manage user's preferred language menu.", - "display_key": "ussd.kenya.select_preferred_language", + "display_key": "ussd.select_preferred_language", "name": "select_preferred_language", "parent": "account_management" }, "19": { "description": "Retrieve mini-statement menu.", - "display_key": "ussd.kenya.mini_statement_pin_authorization", + "display_key": "ussd.mini_statement_pin_authorization", "name": "mini_statement_pin_authorization", "parent": "account_management" }, "20": { "description": "Manage user's pin menu.", - "display_key": "ussd.kenya.enter_current_pin", + "display_key": "ussd.enter_current_pin", "name": "enter_current_pin", "parent": "account_management" }, "21": { "description": "New pin entry menu.", - "display_key": "ussd.kenya.enter_new_pin", + "display_key": "ussd.enter_new_pin", "name": "enter_new_pin", "parent": "account_management" }, "22": { "description": "Pin entry menu.", - "display_key": "ussd.kenya.display_metadata_pin_authorization", + "display_key": "ussd.display_metadata_pin_authorization", "name": "display_metadata_pin_authorization", "parent": "start" }, "23": { "description": "Exit menu.", - "display_key": "ussd.kenya.exit", + "display_key": "ussd.exit", "name": "exit", "parent": null }, "24": { "description": "Invalid menu option.", - "display_key": "ussd.kenya.exit_invalid_menu_option", + "display_key": "ussd.exit_invalid_menu_option", "name": "exit_invalid_menu_option", "parent": null }, "25": { "description": "Pin policy violation.", - "display_key": "ussd.kenya.exit_invalid_pin", + "display_key": "ussd.exit_invalid_pin", "name": "exit_invalid_pin", "parent": null }, "26": { "description": "Pin mismatch. New pin and the new pin confirmation do not match", - "display_key": "ussd.kenya.exit_pin_mismatch", + "display_key": "ussd.exit_pin_mismatch", "name": "exit_pin_mismatch", "parent": null }, "27": { "description": "Ussd pin blocked Menu", - "display_key": "ussd.kenya.exit_pin_blocked", + "display_key": "ussd.exit_pin_blocked", "name": "exit_pin_blocked", "parent": null }, "28": { "description": "Key params missing in request.", - "display_key": "ussd.kenya.exit_invalid_request", + "display_key": "ussd.exit_invalid_request", "name": "exit_invalid_request", "parent": null }, "29": { "description": "The user did not select a choice.", - "display_key": "ussd.kenya.exit_invalid_input", + "display_key": "ussd.exit_invalid_input", "name": "exit_invalid_input", "parent": null }, "30": { "description": "Exit following unsuccessful transaction due to insufficient account balance.", - "display_key": "ussd.kenya.exit_insufficient_balance", + "display_key": "ussd.exit_insufficient_balance", "name": "exit_insufficient_balance", "parent": null }, "31": { "description": "Exit following a successful transaction.", - "display_key": "ussd.kenya.exit_successful_transaction", + "display_key": "ussd.exit_successful_transaction", "name": "exit_successful_transaction", "parent": null }, "32": { "description": "End of a menu flow.", - "display_key": "ussd.kenya.complete", + "display_key": "ussd.complete", "name": "complete", "parent": null }, "33": { "description": "Pin entry menu to view account balances.", - "display_key": "ussd.kenya.account_balances_pin_authorization", + "display_key": "ussd.account_balances_pin_authorization", "name": "account_balances_pin_authorization", "parent": "account_management" }, "34": { "description": "Pin entry menu to view account statement.", - "display_key": "ussd.kenya.account_statement_pin_authorization", + "display_key": "ussd.account_statement_pin_authorization", "name": "account_statement_pin_authorization", "parent": "account_management" }, "35": { "description": "Menu to display account balances.", - "display_key": "ussd.kenya.account_balances", + "display_key": "ussd.account_balances", "name": "account_balances", "parent": "account_management" }, "36": { "description": "Menu to display first set of transactions in statement.", - "display_key": "ussd.kenya.first_transaction_set", + "display_key": "ussd.first_transaction_set", "name": "first_transaction_set", - "parent": null + "parent": "account_management" }, "37": { "description": "Menu to display middle set of transactions in statement.", - "display_key": "ussd.kenya.middle_transaction_set", + "display_key": "ussd.middle_transaction_set", "name": "middle_transaction_set", "parent": null }, "38": { "description": "Menu to display last set of transactions in statement.", - "display_key": "ussd.kenya.last_transaction_set", + "display_key": "ussd.last_transaction_set", "name": "last_transaction_set", "parent": null }, "39": { "description": "Menu to instruct users to call the office.", - "display_key": "ussd.kenya.help", + "display_key": "ussd.help", "name": "help", "parent": null }, "40": { "description": "Menu to display a user's entire profile", - "display_key": "ussd.kenya.display_user_metadata", + "display_key": "ussd.display_user_metadata", "name": "display_user_metadata", "parent": "metadata_management" }, "41": { "description": "The recipient is not in the system", - "display_key": "ussd.kenya.exit_invalid_recipient", + "display_key": "ussd.exit_invalid_recipient", "name": "exit_invalid_recipient", "parent": null }, "42": { "description": "Pin entry menu for changing name data.", - "display_key": "ussd.kenya.name_edit_pin_authorization", + "display_key": "ussd.name_edit_pin_authorization", "name": "name_edit_pin_authorization", "parent": "metadata_management" }, "43": { "description": "Pin entry menu for changing gender data.", - "display_key": "ussd.kenya.gender_edit_pin_authorization", + "display_key": "ussd.gender_edit_pin_authorization", "name": "gender_edit_pin_authorization", "parent": "metadata_management" }, "44": { "description": "Pin entry menu for changing location data.", - "display_key": "ussd.kenya.location_edit_pin_authorization", + "display_key": "ussd.location_edit_pin_authorization", "name": "location_edit_pin_authorization", "parent": "metadata_management" }, "45": { "description": "Pin entry menu for changing products data.", - "display_key": "ussd.kenya.products_edit_pin_authorization", + "display_key": "ussd.products_edit_pin_authorization", "name": "products_edit_pin_authorization", "parent": "metadata_management" }, "46": { "description": "Pin confirmation for pin change.", - "display_key": "ussd.kenya.new_pin_confirmation", + "display_key": "ussd.new_pin_confirmation", "name": "new_pin_confirmation", "parent": "metadata_management" }, "47": { "description": "Year of birth entry menu.", - "display_key": "ussd.kenya.enter_date_of_birth", + "display_key": "ussd.enter_date_of_birth", "name": "enter_date_of_birth", "parent": "metadata_management" }, "48": { "description": "Pin entry menu for changing year of birth data.", - "display_key": "ussd.kenya.dob_edit_pin_authorization", + "display_key": "ussd.dob_edit_pin_authorization", "name": "dob_edit_pin_authorization", "parent": "metadata_management" }, "49": { "description": "Menu to display first set of tokens in the account's token list.", - "display_key": "ussd.kenya.first_account_tokens_set", + "display_key": "ussd.first_account_tokens_set", "name": "first_account_tokens_set", - "parent": null + "parent": "start" }, "50": { "description": "Menu to display middle set of tokens in the account's token list.", - "display_key": "ussd.kenya.middle_account_tokens_set", + "display_key": "ussd.middle_account_tokens_set", "name": "middle_account_tokens_set", "parent": null }, "51": { "description": "Menu to display last set of tokens in the account's token list.", - "display_key": "ussd.kenya.last_account_tokens_set", + "display_key": "ussd.last_account_tokens_set", "name": "last_account_tokens_set", "parent": null }, "52": { "description": "Pin entry menu for setting an active token.", - "display_key": "ussd.kenya.token_selection_pin_authorization", + "display_key": "ussd.token_selection_pin_authorization", "name": "token_selection_pin_authorization", - "parent": null + "parent": "first_account_tokens_set" }, "53": { "description": "Exit following a successful active token setting.", - "display_key": "ussd.kenya.exit_successful_token_selection", + "display_key": "ussd.exit_successful_token_selection", "name": "exit_successful_token_selection", "parent": null }, "54": { "description": "Pin management menu for operations related to an account's pin.", - "display_key": "ussd.kenya.pin_management", + "display_key": "ussd.pin_management", "name": "pin_management", "parent": "start" }, "55": { "description": "Phone number entry for account whose pin is being reset.", - "display_key": "ussd.kenya.reset_guarded_pin", + "display_key": "ussd.reset_guarded_pin", "name": "reset_guarded_pin", "parent": "pin_management" }, "56": { "description": "Pin entry for initiating request to reset an account's pin.", - "display_key": "ussd.kenya.reset_guarded_pin_authorization", + "display_key": "ussd.reset_guarded_pin_authorization", "name": "reset_guarded_pin_authorization", "parent": "pin_management" }, "57": { "description": "Exit menu following successful pin reset initiation.", - "display_key": "ussd.kenya.exit_pin_reset_initiated_success", + "display_key": "ussd.exit_pin_reset_initiated_success", "name": "exit_pin_reset_initiated_success", "parent": "pin_management" }, "58": { "description": "Exit menu in the event that an account is not a set guardian.", - "display_key": "ussd.kenya.exit_not_authorized_for_pin_reset", + "display_key": "ussd.exit_not_authorized_for_pin_reset", "name": "exit_not_authorized_for_pin_reset", "parent": "pin_management" }, "59": { "description": "Pin guard menu for handling guardianship operations.", - "display_key": "ussd.kenya.guard_pin", + "display_key": "ussd.guard_pin", "name": "guard_pin", "parent": "pin_management" }, "60": { "description": "Pin entry to display a list of set guardians.", - "display_key": "ussd.kenya.guardian_list_pin_authorization", + "display_key": "ussd.guardian_list_pin_authorization", "name": "guardian_list_pin_authorization", "parent": "guard_pin" }, "61": { "description": "Menu to display list of set guardians.", - "display_key": "ussd.kenya.guardian_list", + "display_key": "ussd.guardian_list", "name": "guardian_list", "parent": "guard_pin" }, "62": { "description": "Phone number entry to add an account as a guardian to reset pin.", - "display_key": "ussd.kenya.add_guardian", + "display_key": "ussd.add_guardian", "name": "add_guardian", "parent": "guard_pin" }, "63": { "description": "Pin entry to confirm addition of an account as a guardian.", - "display_key": "ussd.kenya.add_guardian_pin_authorization", + "display_key": "ussd.add_guardian_pin_authorization", "name": "add_guardian_pin_authorization", "parent": "guard_pin" }, "64": { "description": "Exit menu when an account is successfully added as pin reset guardian.", - "display_key": "ussd.kenya.exit_guardian_addition_success", + "display_key": "ussd.exit_guardian_addition_success", "name": "exit_guardian_addition_success", "parent": "guard_pin" }, "65": { "description": "Phone number entry to remove an account as a guardian to reset pin.", - "display_key": "ussd.kenya.remove_guardian", + "display_key": "ussd.remove_guardian", "name": "remove_guardian", "parent": "guard_pin" }, "66": { "description": "Pin entry to confirm removal of an account as a guardian.", - "display_key": "ussd.kenya.remove_guardian_pin_authorization", + "display_key": "ussd.remove_guardian_pin_authorization", "name": "remove_guardian_pin_authorization", "parent": "guard_pin" }, "67": { "description": "Exit menu when an account is successfully removed as pin reset guardian.", - "display_key": "ussd.kenya.exit_guardian_removal_success", + "display_key": "ussd.exit_guardian_removal_success", "name": "exit_guardian_removal_success", "parent": "guard_pin" }, "68": { - "description": "Exit menu when invalid phone number entry for guardian addition. ", - "display_key": "ussd.kenya.exit_invalid_guardian_addition", + "description": "Exit menu when invalid phone number entry for guardian addition.", + "display_key": "ussd.exit_invalid_guardian_addition", "name": "exit_invalid_guardian_addition", "parent": "guard_pin" }, "69": { - "description": "Exit menu when invalid phone number entry for guardian removal. ", - "display_key": "ussd.kenya.exit_invalid_guardian_removal", + "description": "Exit menu when invalid phone number entry for guardian removal.", + "display_key": "ussd.exit_invalid_guardian_removal", "name": "exit_invalid_guardian_removal", "parent": "guard_pin" + }, + "70": { + "description": "Menu to display middle set of languages to select.", + "display_key": "ussd.initial_middle_language_set", + "name": "initial_middle_language_set", + "parent": null + }, + "71": { + "description": "Menu to display last set of languages to select.", + "display_key": "ussd.initial_last_language_set", + "name": "initial_last_language_set", + "parent": null + }, + "72": { + "description": "Menu to display middle set of languages to select.", + "display_key": "ussd.middle_language_set", + "name": "middle_language_set", + "parent": null + }, + "73": { + "description": "Menu to display last set of languages to select.", + "display_key": "ussd.last_language_set", + "name": "last_language_set", + "parent": null } } } \ No newline at end of file diff --git a/apps/cic-ussd/cic_ussd/metadata/__init__.py b/apps/cic-ussd/cic_ussd/metadata/__init__.py index a5deea10..16ed66a8 100644 --- a/apps/cic-ussd/cic_ussd/metadata/__init__.py +++ b/apps/cic-ussd/cic_ussd/metadata/__init__.py @@ -7,3 +7,4 @@ from .custom import CustomMetadata from .person import PersonMetadata from .phone import PhonePointerMetadata from .preferences import PreferencesMetadata +from .tokens import TokenMetadata diff --git a/apps/cic-ussd/cic_ussd/processor/menu.py b/apps/cic-ussd/cic_ussd/processor/menu.py index 5346a35d..31a734d9 100644 --- a/apps/cic-ussd/cic_ussd/processor/menu.py +++ b/apps/cic-ussd/cic_ussd/processor/menu.py @@ -19,34 +19,33 @@ from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.account.statement import ( get_cached_statement, parse_statement_transactions, - query_statement, - statement_transaction_set -) + query_statement) from cic_ussd.account.tokens import (create_account_tokens_list, get_active_token_symbol, get_cached_token_data, get_cached_token_symbol_list, get_cached_token_data_list, - parse_token_list, - token_list_set) + parse_token_list) from cic_ussd.account.transaction import from_wei, to_wei -from cic_ussd.cache import cache_data_key, cache_data +from cic_ussd.cache import cache_data_key, cache_data, get_cached_data from cic_ussd.db.models.account import Account from cic_ussd.metadata import PersonMetadata from cic_ussd.phone_number import Support -from cic_ussd.processor.util import parse_person_metadata +from cic_ussd.processor.util import parse_person_metadata, ussd_menu_list, wait_for_session_data from cic_ussd.session.ussd_session import save_session_data +from cic_ussd.state_machine.logic.language import preferred_langauge_from_selection from cic_ussd.translation import translation_for from sqlalchemy.orm.session import Session -logg = logging.getLogger(__name__) +logg = logging.getLogger(__file__) class MenuProcessor: def __init__(self, account: Account, display_key: str, menu_name: str, session: Session, ussd_session: dict): self.account = account self.display_key = display_key - self.identifier = bytes.fromhex(self.account.blockchain_address) + if account: + self.identifier = bytes.fromhex(self.account.blockchain_address) self.menu_name = menu_name self.session = session self.ussd_session = ussd_session @@ -89,36 +88,29 @@ class MenuProcessor: :rtype: """ cached_statement = get_cached_statement(self.account.blockchain_address) - transaction_sets = [] - if cached_statement: - statement = json.loads(cached_statement) - statement_transactions = parse_statement_transactions(statement) - transaction_sets = [statement_transactions[tx:tx + 3] for tx in range(0, len(statement_transactions), 3)] + preferred_language = get_cached_preferred_language(self.account.blockchain_address) if not preferred_language: preferred_language = i18n.config.get('fallback') - no_transaction_history = statement_transaction_set(preferred_language, transaction_sets) - first_transaction_set = no_transaction_history - middle_transaction_set = no_transaction_history - last_transaction_set = no_transaction_history - if transaction_sets: - first_transaction_set = statement_transaction_set(preferred_language, transaction_sets[0]) - if len(transaction_sets) >= 2: - middle_transaction_set = statement_transaction_set(preferred_language, transaction_sets[1]) - if len(transaction_sets) >= 3: - last_transaction_set = statement_transaction_set(preferred_language, transaction_sets[2]) - if self.display_key == 'ussd.kenya.first_transaction_set': + statement_list = [] + if cached_statement: + statement_list = parse_statement_transactions(statement=json.loads(cached_statement)) + + fallback = translation_for('helpers.no_transaction_history', preferred_language) + transaction_sets = ussd_menu_list(fallback=fallback, menu_list=statement_list, split=3) + + if self.display_key == 'ussd.first_transaction_set': return translation_for( - self.display_key, preferred_language, first_transaction_set=first_transaction_set + self.display_key, preferred_language, first_transaction_set=transaction_sets[0] ) - if self.display_key == 'ussd.kenya.middle_transaction_set': + if self.display_key == 'ussd.middle_transaction_set': return translation_for( - self.display_key, preferred_language, middle_transaction_set=middle_transaction_set + self.display_key, preferred_language, middle_transaction_set=transaction_sets[1] ) - if self.display_key == 'ussd.kenya.last_transaction_set': + if self.display_key == 'ussd.last_transaction_set': return translation_for( - self.display_key, preferred_language, last_transaction_set=last_transaction_set + self.display_key, preferred_language, last_transaction_set=transaction_sets[2] ) def add_guardian_pin_authorization(self): @@ -129,7 +121,7 @@ class MenuProcessor: preferred_language = get_cached_preferred_language(self.account.blockchain_address) if not preferred_language: preferred_language = i18n.config.get('fallback') - set_guardians = self.account.get_guardians() + set_guardians = self.account.get_guardians()[:3] if set_guardians: guardians_list = '' guardians_list_header = translation_for('helpers.guardians_list_header', preferred_language) @@ -145,36 +137,30 @@ class MenuProcessor: def account_tokens(self) -> str: cached_token_data_list = get_cached_token_data_list(self.account.blockchain_address) token_data_list = parse_token_list(cached_token_data_list) - token_list_sets = [token_data_list[tds:tds + 3] for tds in range(0, len(token_data_list), 3)] + preferred_language = get_cached_preferred_language(self.account.blockchain_address) if not preferred_language: preferred_language = i18n.config.get('fallback') - no_token_list = token_list_set(preferred_language, []) - first_account_tokens_set = no_token_list - middle_account_tokens_set = no_token_list - last_account_tokens_set = no_token_list - if token_list_sets: - data = { - 'account_tokens_list': cached_token_data_list - } - save_session_data(data=data, queue='cic-ussd', session=self.session, ussd_session=self.ussd_session) - first_account_tokens_set = token_list_set(preferred_language, token_list_sets[0]) - if len(token_list_sets) >= 2: - middle_account_tokens_set = token_list_set(preferred_language, token_list_sets[1]) - if len(token_list_sets) >= 3: - last_account_tokens_set = token_list_set(preferred_language, token_list_sets[2]) - if self.display_key == 'ussd.kenya.first_account_tokens_set': + fallback = translation_for('helpers.no_tokens_list', preferred_language) + token_list_sets = ussd_menu_list(fallback=fallback, menu_list=token_data_list, split=3) + + data = { + 'account_tokens_list': cached_token_data_list + } + save_session_data(data=data, queue='cic-ussd', session=self.session, ussd_session=self.ussd_session) + + if self.display_key == 'ussd.first_account_tokens_set': return translation_for( - self.display_key, preferred_language, first_account_tokens_set=first_account_tokens_set + self.display_key, preferred_language, first_account_tokens_set=token_list_sets[0] ) - if self.display_key == 'ussd.kenya.middle_account_tokens_set': + if self.display_key == 'ussd.middle_account_tokens_set': return translation_for( - self.display_key, preferred_language, middle_account_tokens_set=middle_account_tokens_set + self.display_key, preferred_language, middle_account_tokens_set=token_list_sets[1] ) - if self.display_key == 'ussd.kenya.last_account_tokens_set': + if self.display_key == 'ussd.last_account_tokens_set': return translation_for( - self.display_key, preferred_language, last_account_tokens_set=last_account_tokens_set + self.display_key, preferred_language, last_account_tokens_set=token_list_sets[2] ) def help(self) -> str: @@ -222,7 +208,7 @@ class MenuProcessor: remaining_attempts = 3 remaining_attempts -= self.account.failed_pin_attempts retry_pin_entry = translation_for( - 'ussd.kenya.retry_pin_entry', preferred_language, remaining_attempts=remaining_attempts + 'ussd.retry_pin_entry', preferred_language, remaining_attempts=remaining_attempts ) return translation_for( f'{self.display_key}.retry', preferred_language, retry_pin_entry=retry_pin_entry @@ -238,6 +224,38 @@ class MenuProcessor: guardian = Account.get_by_phone_number(guardian_phone_number, self.session) return guardian.standard_metadata_id() + def language(self): + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cached_system_languages = get_cached_data(key) + language_list: list = json.loads(cached_system_languages) + + if self.account: + preferred_language = get_cached_preferred_language(self.account.blockchain_address) + else: + preferred_language = i18n.config.get('fallback') + + fallback = translation_for('helpers.no_language_list', preferred_language) + language_list_sets = ussd_menu_list(fallback=fallback, menu_list=language_list, split=3) + + if self.display_key in ['ussd.initial_language_selection', 'ussd.select_preferred_language']: + return translation_for( + self.display_key, preferred_language, first_language_set=language_list_sets[0] + ) + + if 'middle_language_set' in self.display_key: + return translation_for( + self.display_key, preferred_language, middle_language_set=language_list_sets[1] + ) + + if 'last_language_set' in self.display_key: + return translation_for( + self.display_key, preferred_language, last_language_set=language_list_sets[2] + ) + + def account_creation_prompt(self): + preferred_language = preferred_langauge_from_selection(self.ussd_session.get('user_input')) + return translation_for(self.display_key, preferred_language) + def reset_guarded_pin_authorization(self): guarded_account_information = self.guarded_account_metadata() return self.pin_authorization(guarded_account_information=guarded_account_information) @@ -381,8 +399,9 @@ class MenuProcessor: ) def exit_invalid_menu_option(self): - preferred_language = get_cached_preferred_language(self.account.blockchain_address) - if not preferred_language: + if self.account: + preferred_language = get_cached_preferred_language(self.account.blockchain_address) + else: preferred_language = i18n.config.get('fallback') return translation_for(self.display_key, preferred_language, support_phone=Support.phone_number) @@ -390,7 +409,7 @@ class MenuProcessor: preferred_language = get_cached_preferred_language(self.account.blockchain_address) if not preferred_language: preferred_language = i18n.config.get('fallback') - return translation_for('ussd.kenya.exit_pin_blocked', preferred_language, support_phone=Support.phone_number) + return translation_for('ussd.exit_pin_blocked', preferred_language, support_phone=Support.phone_number) def exit_successful_token_selection(self) -> str: selected_token = self.ussd_session.get('data').get('selected_token') @@ -398,7 +417,7 @@ class MenuProcessor: preferred_language = get_cached_preferred_language(self.account.blockchain_address) if not preferred_language: preferred_language = i18n.config.get('fallback') - return translation_for(self.display_key,preferred_language,token_symbol=token_symbol) + return translation_for(self.display_key, preferred_language, token_symbol=token_symbol) def exit_successful_transaction(self): """ @@ -445,6 +464,9 @@ def response(account: Account, display_key: str, menu_name: str, session: Sessio """ menu_processor = MenuProcessor(account, display_key, menu_name, session, ussd_session) + if menu_name == 'account_creation_prompt': + return menu_processor.account_creation_prompt() + if menu_name == 'start': return menu_processor.start_menu() @@ -502,6 +524,9 @@ def response(account: Account, display_key: str, menu_name: str, session: Sessio if 'account_tokens_set' in menu_name: return menu_processor.account_tokens() + if 'language' in menu_name: + return menu_processor.language() + if menu_name == 'display_user_metadata': return menu_processor.person_metadata() @@ -515,5 +540,4 @@ def response(account: Account, display_key: str, menu_name: str, session: Sessio return menu_processor.exit_successful_token_selection() preferred_language = get_cached_preferred_language(account.blockchain_address) - return translation_for(display_key, preferred_language) diff --git a/apps/cic-ussd/cic_ussd/processor/ussd.py b/apps/cic-ussd/cic_ussd/processor/ussd.py index 8e383d9b..a07bf4df 100644 --- a/apps/cic-ussd/cic_ussd/processor/ussd.py +++ b/apps/cic-ussd/cic_ussd/processor/ussd.py @@ -8,7 +8,7 @@ from sqlalchemy.orm.session import Session from tinydb.table import Document # local imports -from cic_ussd.db.models.account import Account, create +from cic_ussd.db.models.account import Account from cic_ussd.db.models.base import SessionBase from cic_ussd.db.models.ussd_session import UssdSession from cic_ussd.menu.ussd_menu import UssdMenu @@ -16,7 +16,6 @@ from cic_ussd.processor.menu import response from cic_ussd.processor.util import latest_input, resume_last_ussd_session from cic_ussd.session.ussd_session import create_or_update_session, persist_ussd_session from cic_ussd.state_machine import UssdStateMachine -from cic_ussd.translation import translation_for from cic_ussd.validator import is_valid_response @@ -36,9 +35,6 @@ def handle_menu(account: Account, session: Session) -> Document: last_ussd_session = UssdSession.last_ussd_session(account.phone_number, session) if last_ussd_session: return resume_last_ussd_session(last_ussd_session.state) - - elif not account.has_preferred_language(): - return UssdMenu.find_by_name('initial_language_selection') else: return UssdMenu.find_by_name('initial_pin_entry') @@ -71,16 +67,13 @@ def get_menu(account: Account, return UssdMenu.find_by_name(state) -def handle_menu_operations(chain_str: str, - external_session_id: str, +def handle_menu_operations(external_session_id: str, phone_number: str, queue: str, service_code: str, session, user_input: str): """ - :param chain_str: - :type chain_str: :param external_session_id: :type external_session_id: :param phone_number: @@ -100,10 +93,38 @@ def handle_menu_operations(chain_str: str, account: Account = Account.get_by_phone_number(phone_number, session) if account: return handle_account_menu_operations(account, external_session_id, queue, session, service_code, user_input) - create(chain_str, phone_number, session) - menu = UssdMenu.find_by_name('account_creation_prompt') - preferred_language = i18n.config.get('fallback') - create_or_update_session( + else: + return handle_no_account_menu_operations( + account, external_session_id, phone_number, queue, session, service_code, user_input) + + +def handle_no_account_menu_operations(account: Optional[Account], + external_session_id: str, + phone_number: str, + queue: str, + session: Session, + service_code: str, + user_input: str): + """ + :param account: + :type account: + :param external_session_id: + :type external_session_id: + :param phone_number: + :type phone_number: + :param queue: + :type queue: + :param session: + :type session: + :param service_code: + :type service_code: + :param user_input: + :type user_input: + :return: + :rtype: + """ + menu = UssdMenu.find_by_name('initial_language_selection') + ussd_session = create_or_update_session( external_session_id=external_session_id, msisdn=phone_number, service_code=service_code, @@ -111,7 +132,20 @@ def handle_menu_operations(chain_str: str, session=session, user_input=user_input) persist_ussd_session(external_session_id, queue) - return translation_for('ussd.kenya.account_creation_prompt', preferred_language) + last_ussd_session: UssdSession = UssdSession.last_ussd_session(phone_number, session) + if last_ussd_session: + if not user_input: + menu = resume_last_ussd_session(last_ussd_session.state) + else: + session = SessionBase.bind_session(session) + state = next_state(account, session, user_input, last_ussd_session.to_json()) + menu = UssdMenu.find_by_name(state) + + return response(account=account, + display_key=menu.get('display_key'), + menu_name=menu.get('name'), + session=session, + ussd_session=ussd_session.to_json()) def handle_account_menu_operations(account: Account, @@ -152,15 +186,12 @@ def handle_account_menu_operations(account: Account, if last_ussd_session: ussd_session = create_or_update_session( external_session_id, phone_number, service_code, user_input, menu.get('name'), session, - last_ussd_session.data - ) + last_ussd_session.data) else: ussd_session = create_or_update_session( - external_session_id, phone_number, service_code, user_input, menu.get('name'), session, None - ) + external_session_id, phone_number, service_code, user_input, menu.get('name'), session, {}) menu_response = response( - account, menu.get('display_key'), menu.get('name'), session, ussd_session.to_json() - ) + account, menu.get('display_key'), menu.get('name'), session, ussd_session.to_json()) if not is_valid_response(menu_response): raise ValueError(f'Invalid response: {response}') persist_ussd_session(external_session_id, queue) diff --git a/apps/cic-ussd/cic_ussd/processor/util.py b/apps/cic-ussd/cic_ussd/processor/util.py index b2baf261..28c50c63 100644 --- a/apps/cic-ussd/cic_ussd/processor/util.py +++ b/apps/cic-ussd/cic_ussd/processor/util.py @@ -3,7 +3,7 @@ import datetime import json import logging import time -from typing import Union +from typing import List, Union # external imports from cic_types.condiments import MetadataPointer @@ -21,9 +21,7 @@ logg = logging.getLogger(__file__) def latest_input(user_input: str) -> str: """ :param user_input: - :type user_input: :return: - :rtype: """ return user_input.split('*')[-1] @@ -85,6 +83,27 @@ def resume_last_ussd_session(last_state: str) -> Document: return UssdMenu.find_by_name(last_state) +def ussd_menu_list(fallback: str, menu_list: list, split: int = 3) -> List[str]: + """ + :param fallback: + :type fallback: + :param menu_list: + :type menu_list: + :param split: + :type split: + :return: + :rtype: + """ + menu_list_sets = [menu_list[item:item + split] for item in range(0, len(menu_list), split)] + menu_list_reprs = [] + for i in range(split): + try: + menu_list_reprs.append(''.join(f'{list_set_item}\n' for list_set_item in menu_list_sets[i]).rstrip('\n')) + except IndexError: + menu_list_reprs.append(fallback) + return menu_list_reprs + + def wait_for_cache(identifier: Union[list, bytes], resource_name: str, salt: MetadataPointer, interval: int = 1, max_retry: int = 5): """ :param identifier: @@ -132,17 +151,28 @@ def wait_for_session_data(resource_name: str, session_data_key: str, ussd_sessio :return: :rtype: """ - session_data = ussd_session.get('data').get(session_data_key) - counter = 0 - while session_data is None: - logg.debug(f'Waiting for: {resource_name}. Checking after: {interval} ...') + data = ussd_session.get('data') + data_poller = 0 + while not data: + logg.debug(f'Waiting for data object on ussd session: {ussd_session.get("external_session_id")}') + logg.debug(f'Data poller at: {data_poller}. Checking again after: {interval} secs...') time.sleep(interval) - counter += 1 - session_data = ussd_session.get('data').get(session_data_key) - if session_data is not None: - logg.debug(f'{resource_name} now available.') + data_poller += 1 + if data: + logg.debug(f'Data object found, proceeding to poll for: {session_data_key}') break - else: - if counter == max_retry: - logg.debug(f'Could not find: {resource_name} within: {max_retry}') + if data: + session_data_poller = 0 + session_data = data.get(session_data_key) + while not session_data_key: + logg.debug( + f'Session data poller at: {data_poller} with max retry at: {max_retry}. Checking again after: {interval} secs...') + time.sleep(interval) + session_data_poller += 1 + + if session_data: + logg.debug(f'{resource_name} now available.') break + + elif session_data_poller >= max_retry: + logg.debug(f'Could not find data object within: {max_retry}') diff --git a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_tasker.py b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_tasker.py index 14c577c1..95ff193b 100644 --- a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_tasker.py +++ b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_tasker.py @@ -20,6 +20,7 @@ from cic_ussd.db import dsn_from_config from cic_ussd.db.models.base import SessionBase from cic_ussd.phone_number import Support from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession +from cic_ussd.translation import generate_locale_files from cic_ussd.validator import validate_presence logging.basicConfig(level=logging.WARNING) @@ -83,6 +84,10 @@ if key_file_path: validate_presence(path=key_file_path) Signer.key_file_path = key_file_path +generate_locale_files(locale_dir=config.get('LOCALE_PATH'), + schema_file_path=config.get('SCHEMA_FILE_PATH'), + translation_builder_path=config.get('LOCALE_FILE_BUILDERS')) + # set up translations i18n.load_path.append(config.get('LOCALE_PATH')) i18n.set('fallback', config.get('LOCALE_FALLBACK')) diff --git a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py index 8e3f4268..6a53efbc 100644 --- a/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py +++ b/apps/cic-ussd/cic_ussd/runnable/daemons/cic_user_ussd_server.py @@ -18,6 +18,7 @@ from cic_types.ext.metadata.signer import Signer # local imports from cic_ussd.account.chain import Chain +from cic_ussd.account.guardianship import Guardianship from cic_ussd.account.tokens import query_default_token from cic_ussd.cache import cache_data, cache_data_key, Cache from cic_ussd.db import dsn_from_config @@ -33,7 +34,7 @@ from cic_ussd.processor.ussd import handle_menu_operations from cic_ussd.runnable.server_base import exportable_parser, logg from cic_ussd.session.ussd_session import UssdSession as InMemoryUssdSession from cic_ussd.state_machine import UssdStateMachine -from cic_ussd.translation import translation_for +from cic_ussd.translation import generate_locale_files, Languages, translation_for from cic_ussd.validator import check_ip, check_request_content_length, validate_phone_number, validate_presence args = exportable_parser.parse_args() @@ -56,10 +57,6 @@ SessionBase.connect(data_source_name, pool_size=int(config.get('DATABASE_POOL_SIZE')), debug=config.true('DATABASE_DEBUG')) -# set up translations -i18n.load_path.append(config.get('LOCALE_PATH')) -i18n.set('fallback', config.get('LOCALE_FALLBACK')) - # set Fernet key PasswordEncoder.set_key(config.get('APP_PASSWORD_PEPPER')) @@ -121,6 +118,22 @@ valid_service_codes = config.get('USSD_SERVICE_CODE').split(",") E164Format.region = config.get('E164_REGION') Support.phone_number = config.get('OFFICE_SUPPORT_PHONE') +validate_presence(config.get('SYSTEM_GUARDIANS_FILE')) +Guardianship.load_system_guardians(config.get('SYSTEM_GUARDIANS_FILE')) + +generate_locale_files(locale_dir=config.get('LOCALE_PATH'), + schema_file_path=config.get('SCHEMA_FILE_PATH'), + translation_builder_path=config.get('LOCALE_FILE_BUILDERS')) + +# set up translations +i18n.load_path.append(config.get('LOCALE_PATH')) +i18n.set('fallback', config.get('LOCALE_FALLBACK')) + +validate_presence(config.get('LANGUAGES_FILE')) +Languages.load_languages_dict(config.get('LANGUAGES_FILE')) +languages = Languages() +languages.cache_system_languages() + def application(env, start_response): """Loads python code for application to be accessible over web server @@ -175,7 +188,7 @@ def application(env, start_response): if service_code not in valid_service_codes: response = translation_for( - 'ussd.kenya.invalid_service_code', + 'ussd.invalid_service_code', i18n.config.get('fallback'), valid_service_code=valid_service_codes[0] ) @@ -189,9 +202,7 @@ def application(env, start_response): return [] logg.debug('session {} started for {}'.format(external_session_id, phone_number)) - response = handle_menu_operations( - chain_str, external_session_id, phone_number, args.q, service_code, session, user_input - ) + response = handle_menu_operations(external_session_id, phone_number, args.q, service_code, session, user_input) response_bytes, headers = with_content_headers(headers, response) start_response('200 OK,', headers) session.commit() diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/account.py b/apps/cic-ussd/cic_ussd/state_machine/logic/account.py index 13bc13c0..53e64882 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/account.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/account.py @@ -11,46 +11,20 @@ from cic_types.models.person import get_contact_data_from_vcard, generate_vcard_ # local imports from cic_ussd.account.chain import Chain -from cic_ussd.account.maps import gender, language +from cic_ussd.account.maps import gender from cic_ussd.account.metadata import get_cached_preferred_language -from cic_ussd.db.models.account import Account +from cic_ussd.db.models.account import Account, create from cic_ussd.db.models.base import SessionBase from cic_ussd.error import MetadataNotFoundError from cic_ussd.metadata import PersonMetadata from cic_ussd.session.ussd_session import save_session_data +from cic_ussd.state_machine.logic.language import preferred_langauge_from_selection from cic_ussd.translation import translation_for from sqlalchemy.orm.session import Session logg = logging.getLogger(__file__) -def change_preferred_language(state_machine_data: Tuple[str, dict, Account, Session]): - """ - :param state_machine_data: - :type state_machine_data: - :return: - :rtype: - """ - user_input, ussd_session, account, session = state_machine_data - r_user_input = language().get(user_input) - session = SessionBase.bind_session(session) - account.preferred_language = r_user_input - session.add(account) - session.flush() - SessionBase.release_session(session) - - preferences_data = { - 'preferred_language': r_user_input - } - - s = celery.signature( - 'cic_ussd.tasks.metadata.add_preferences_metadata', - [account.blockchain_address, preferences_data], - queue='cic-ussd' - ) - return s.apply_async() - - def update_account_status_to_active(state_machine_data: Tuple[str, dict, Account, Session]): """This function sets user's account to active. :param state_machine_data: A tuple containing user input, a ussd session and user object. @@ -245,3 +219,16 @@ def edit_user_metadata_attribute(state_machine_data: Tuple[str, dict, Account, S [blockchain_address, parsed_person_metadata] ) s_edit_person_metadata.apply_async(queue='cic-ussd') + + +def process_account_creation(state_machine_data: Tuple[str, dict, Account, Session]): + """ + :param state_machine_data: + :type state_machine_data: + :return: + :rtype: + """ + user_input, ussd_session, account, session = state_machine_data + preferred_language = preferred_langauge_from_selection(user_input=user_input) + chain_str = Chain.spec.__str__() + create(chain_str, ussd_session.get('msisdn'), session, preferred_language) diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/language.py b/apps/cic-ussd/cic_ussd/state_machine/logic/language.py new file mode 100644 index 00000000..0fb66ec1 --- /dev/null +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/language.py @@ -0,0 +1,95 @@ +# standard imports +import json +from typing import Tuple + +# external imports +import celery +import i18n +from cic_types.condiments import MetadataPointer +from sqlalchemy.orm.session import Session + +# local imports +from cic_ussd.cache import cache_data_key, get_cached_data +from cic_ussd.db.models.account import Account +from cic_ussd.processor.util import wait_for_cache, wait_for_session_data +from cic_ussd.session.ussd_session import save_session_data +from cic_ussd.translation import Languages + + +def is_valid_language_selection(state_machine_data: Tuple[str, dict, Account, Session]): + """ + :param state_machine_data: + :type state_machine_data: + :return: + :rtype: + """ + user_input, ussd_session, account, session = state_machine_data + + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cached_system_languages = get_cached_data(key) + language_list = json.loads(cached_system_languages) + + if not language_list: + wait_for_cache(identifier='system:languages'.encode('utf-8'), resource_name='Languages list', salt=MetadataPointer.NONE) + + if user_input in ['00', '11', '22']: + return False + user_input = int(user_input) + return user_input <= len(language_list) + + +def change_preferred_language(state_machine_data: Tuple[str, dict, Account, Session]): + """ + :param state_machine_data: + :type state_machine_data: + :return: + :rtype: + """ + process_language_selection(state_machine_data=state_machine_data) + user_input, ussd_session, account, session = state_machine_data + wait_for_session_data(resource_name='Preferred language', session_data_key='preferred_language', ussd_session=ussd_session) + preferred_language = ussd_session.get('data').get('preferred_language') + preferences_data = { + 'preferred_language': preferred_language + } + + s = celery.signature( + 'cic_ussd.tasks.metadata.add_preferences_metadata', + [account.blockchain_address, preferences_data], + queue='cic-ussd' + ) + return s.apply_async() + + +def process_language_selection(state_machine_data: Tuple[str, dict, Account, Session]): + """ + :param state_machine_data: + :type state_machine_data: + :return: + :rtype: + """ + user_input, ussd_session, account, session = state_machine_data + preferred_language = preferred_langauge_from_selection(user_input=user_input) + data = { + 'preferred_language': preferred_language + } + save_session_data(queue='cic-ussd', session=session, data=data, ussd_session=ussd_session) + + +def preferred_langauge_from_selection(user_input: str): + """ + :param user_input: + :type user_input: + :return: + :rtype: + """ + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cached_system_languages = get_cached_data(key) + language_list = json.loads(cached_system_languages) + user_input = int(user_input) + selected_language = language_list[user_input - 1] + preferred_language = i18n.config.get('fallback') + for key, value in Languages.languages_dict.items(): + if selected_language[3:] == value: + preferred_language = key + return preferred_language diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/pin_guard.py b/apps/cic-ussd/cic_ussd/state_machine/logic/pin_guard.py index 4e87e473..9c26003e 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/pin_guard.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/pin_guard.py @@ -9,9 +9,11 @@ from phonenumbers.phonenumberutil import NumberParseException from sqlalchemy.orm.session import Session # local imports +from cic_ussd.account.guardianship import Guardianship from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.db.models.account import Account from cic_ussd.db.models.base import SessionBase +from cic_ussd.notifications import Notifier from cic_ussd.phone_number import process_phone_number, E164Format from cic_ussd.session.ussd_session import save_session_data from cic_ussd.translation import translation_for @@ -82,8 +84,10 @@ def is_valid_guardian_addition(state_machine_data: Tuple[str, dict, Account, Ses preferred_language = i18n.config.get('fallback') is_valid_account = Account.get_by_phone_number(phone_number, session) is not None + guardianship = Guardianship() + is_system_guardian = guardianship.is_system_guardian(phone_number) is_initiator = phone_number == account.phone_number - is_existent_guardian = phone_number in account.get_guardians() + is_existent_guardian = phone_number in account.get_guardians() or is_system_guardian failure_reason = '' if not is_valid_account: @@ -100,7 +104,7 @@ def is_valid_guardian_addition(state_machine_data: Tuple[str, dict, Account, Ses session_data['failure_reason'] = failure_reason save_session_data('cic-ussd', session, session_data, ussd_session) - return phone_number is not None and is_valid_account and not is_existent_guardian and not is_initiator + return phone_number is not None and is_valid_account and not is_existent_guardian and not is_initiator and not is_system_guardian def add_pin_guardian(state_machine_data: Tuple[str, dict, Account, Session]): @@ -130,6 +134,9 @@ def is_set_pin_guardian(account: Account, checked_number: str, preferred_languag is_set_guardian = checked_number in set_guardians is_initiator = checked_number == account.phone_number + guardianship = Guardianship() + is_system_guardian = guardianship.is_system_guardian(checked_number) + if not is_set_guardian: failure_reason = translation_for('helpers.error.is_not_existent_guardian', preferred_language) @@ -141,7 +148,7 @@ def is_set_pin_guardian(account: Account, checked_number: str, preferred_languag session_data['failure_reason'] = failure_reason save_session_data('cic-ussd', session, session_data, ussd_session) - return is_set_guardian and not is_initiator + return (is_set_guardian or is_system_guardian) and not is_initiator def is_dialers_pin_guardian(state_machine_data: Tuple[str, dict, Account, Session]): @@ -193,8 +200,20 @@ def initiate_pin_reset(state_machine_data: Tuple[str, dict, Account, Session]): save_session_data('cic-ussd', session, session_data, ussd_session) guarded_account_phone_number = session_data.get('guarded_account_phone_number') guarded_account = Account.get_by_phone_number(guarded_account_phone_number, session) + if quorum_count >= guarded_account.guardian_quora: guarded_account.reset_pin(session) logg.debug(f'Reset initiated for: {guarded_account.phone_number}') session_data['quorum_count'] = 0 save_session_data('cic-ussd', session, session_data, ussd_session) + + preferred_language = get_cached_preferred_language(guarded_account.blockchain_address) + if not preferred_language: + preferred_language = i18n.config.get('fallback') + + notifier = Notifier() + notifier.send_sms_notification( + key='sms.pin_reset_initiated', + phone_number=guarded_account.phone_number, + preferred_language=preferred_language, + pin_initiator=account.standard_metadata_id()) diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py b/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py index 7f4c2398..f8887813 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/tokens.py @@ -23,7 +23,7 @@ def is_valid_token_selection(state_machine_data: Tuple[str, dict, Account, Sessi account_tokens_list = session_data.get('account_tokens_list') if not account_tokens_list: wait_for_session_data('Account token list', session_data_key='account_tokens_list', ussd_session=ussd_session) - if user_input not in ['00', '22']: + if user_input not in ['00', '11', '22']: try: user_input = int(user_input) return user_input <= len(account_tokens_list) diff --git a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py index 08bd56fa..827e1b46 100644 --- a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py +++ b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py @@ -32,14 +32,14 @@ celery_app = celery.current_app @celery_app.task(bind=True, base=CriticalSQLAlchemyTask) -def account_creation_callback(self, result: str, url: str, status_code: int): +def account_creation_callback(self, result: str, param: str, status_code: int): """This function defines a task that creates a user and :param self: Reference providing access to the callback task instance. :type self: celery.Task :param result: The blockchain address for the created account :type result: str - :param url: URL provided to callback task in cic-eth should http be used for callback. - :type url: str + :param param: URL provided to callback task in cic-eth should http be used for callback. + :type param: str :param status_code: The status of the task to create an account :type status_code: int """ @@ -69,6 +69,15 @@ def account_creation_callback(self, result: str, url: str, status_code: int): set_active_token(blockchain_address=result, token_symbol=token_symbol) queue = self.request.delivery_info.get('routing_key') + preferences_data = {"preferred_language": param} + # temporarily caching selected language + key = cache_data_key(bytes.fromhex(result), MetadataPointer.PREFERENCES) + cache_data(key, json.dumps(preferences_data)) + s_preferences_metadata = celery.signature( + 'cic_ussd.tasks.metadata.add_preferences_metadata', [result, preferences_data], queue=queue + ) + s_preferences_metadata.apply_async() + s_phone_pointer = celery.signature( 'cic_ussd.tasks.metadata.add_phone_pointer', [result, phone_number], queue=queue ) diff --git a/apps/cic-ussd/cic_ussd/translation.py b/apps/cic-ussd/cic_ussd/translation.py index e512a5b3..b85edacc 100644 --- a/apps/cic-ussd/cic_ussd/translation.py +++ b/apps/cic-ussd/cic_ussd/translation.py @@ -1,9 +1,56 @@ """ This module is responsible for translation of ussd menu text based on a user's set preferred language. """ +# standard imports +import json + import i18n +import os +from pathlib import Path from typing import Optional +# external imports +from cic_translations.processor import generate_translation_files, parse_csv +from cic_types.condiments import MetadataPointer + +# local imports +from cic_ussd.cache import cache_data, cache_data_key +from cic_ussd.validator import validate_presence + + +def generate_locale_files(locale_dir: str, schema_file_path: str, translation_builder_path: str): + """""" + translation_builder_files = os.listdir(translation_builder_path) + for file in translation_builder_files: + props = Path(file) + if props.suffix == '.csv': + parsed_csv = parse_csv(os.path.join(translation_builder_path, file)) + generate_translation_files( + parsed_csv=parsed_csv, + schema_file_path=schema_file_path, + translation_file_type=props.stem, + translation_file_path=locale_dir + ) + + +class Languages: + languages_dict: dict = None + + @classmethod + def load_languages_dict(cls, languages_file: str): + with open(languages_file, "r") as languages_file: + cls.languages_dict = json.load(languages_file) + + def cache_system_languages(self): + system_languages: list = list(self.languages_dict.values()) + languages_list = [] + for i in range(len(system_languages)): + language = f'{i + 1}. {system_languages[i]}' + languages_list.append(language) + + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cache_data(key, json.dumps(languages_list)) + def translation_for(key: str, preferred_language: Optional[str] = None, **kwargs) -> str: """ diff --git a/apps/cic-ussd/cic_ussd/version.py b/apps/cic-ussd/cic_ussd/version.py index c12829b9..9ccea412 100644 --- a/apps/cic-ussd/cic_ussd/version.py +++ b/apps/cic-ussd/cic_ussd/version.py @@ -1,7 +1,7 @@ # standard imports import semver -version = (0, 3, 1, 'alpha.6') +version = (0, 3, 1, 'alpha.7') version_object = semver.VersionInfo( major=version[0], diff --git a/apps/cic-ussd/config/app.ini b/apps/cic-ussd/config/app.ini index 4c5e19db..ebd5613c 100644 --- a/apps/cic-ussd/config/app.ini +++ b/apps/cic-ussd/config/app.ini @@ -11,3 +11,6 @@ transitions=transitions/ host = port = ssl = + +[system] +guardians_file = var/lib/sys/guardians.txt diff --git a/apps/cic-ussd/config/test/app.ini b/apps/cic-ussd/config/test/app.ini index 1aa92a72..e6f8218e 100644 --- a/apps/cic-ussd/config/test/app.ini +++ b/apps/cic-ussd/config/test/app.ini @@ -6,3 +6,6 @@ password_pepper=QYbzKff6NhiQzY3ygl2BkiKOpER8RE/Upqs/5aZWW+I= [machine] states=states/ transitions=transitions/ + +[system] +guardians_file = var/lib/sys/guardians.txt diff --git a/apps/cic-ussd/config/test/chain.ini b/apps/cic-ussd/config/test/chain.ini index 6f64b7f1..13e8ae65 100644 --- a/apps/cic-ussd/config/test/chain.ini +++ b/apps/cic-ussd/config/test/chain.ini @@ -1,2 +1,2 @@ [chain] -spec = 'evm:foo:1:bar' +spec = evm:foo:1:bar diff --git a/apps/cic-ussd/config/test/translations.ini b/apps/cic-ussd/config/test/translations.ini index f32c64ed..9676f435 100644 --- a/apps/cic-ussd/config/test/translations.ini +++ b/apps/cic-ussd/config/test/translations.ini @@ -1,3 +1,10 @@ [locale] fallback=sw -path=var/lib/locale/ +path= +file_builders=var/lib/sys/ + +[schema] +file_path = data/schema + +[languages] +file = var/lib/sys/languages.json diff --git a/apps/cic-ussd/config/translations.ini b/apps/cic-ussd/config/translations.ini index f32c64ed..5cf2ba19 100644 --- a/apps/cic-ussd/config/translations.ini +++ b/apps/cic-ussd/config/translations.ini @@ -1,3 +1,10 @@ [locale] fallback=sw path=var/lib/locale/ +file_builders=var/lib/sys/ + +[schema] +file_path = /usr/local/lib/python3.8/site-packages/cic_translations/data/schema + +[languages] +file = var/lib/sys/languages.json diff --git a/apps/cic-ussd/docker/Dockerfile b/apps/cic-ussd/docker/Dockerfile index a7a611b0..634c4299 100644 --- a/apps/cic-ussd/docker/Dockerfile +++ b/apps/cic-ussd/docker/Dockerfile @@ -14,19 +14,12 @@ ARG EXTRA_PIP_INDEX_URL=https://pip.grassrootseconomics.net ARG EXTRA_PIP_ARGS="" ARG PIP_INDEX_URL=https://pypi.org/simple -RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ - pip install --index-url $PIP_INDEX_URL \ - --pre \ - --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ - cic-eth-aux-erc20-demurrage-token~=0.0.2a7 - - COPY *requirements.txt ./ RUN --mount=type=cache,mode=0755,target=/root/.cache/pip \ pip install --index-url $PIP_INDEX_URL \ --pre \ --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ - -r requirements.txt + -r requirements.txt COPY . . diff --git a/apps/cic-ussd/requirements.txt b/apps/cic-ussd/requirements.txt index 5dbd7aed..83b70b5c 100644 --- a/apps/cic-ussd/requirements.txt +++ b/apps/cic-ussd/requirements.txt @@ -4,10 +4,12 @@ billiard==3.6.4.0 bcrypt==3.2.0 celery==4.4.7 cffi==1.14.6 -cic-eth~=0.12.5a1 -cic-notify~=0.4.0a11 -cic-types~=0.2.1a7 -confini>=0.3.6rc4,<0.5.0 +cic-eth~=0.12.7 +cic-notify~=0.4.0a12 +cic-translations~=0.0.3 +cic-types~=0.2.1a8 +confini~=0.5.2 +cic-eth-aux-erc20-demurrage-token~=0.0.3 phonenumbers==8.12.12 psycopg2==2.8.6 python-i18n[YAML]==0.3.9 diff --git a/apps/cic-ussd/states/account_management_states.json b/apps/cic-ussd/states/account_management_states.json index ea9027a2..de71959a 100644 --- a/apps/cic-ussd/states/account_management_states.json +++ b/apps/cic-ussd/states/account_management_states.json @@ -13,5 +13,7 @@ "products_edit_pin_authorization", "account_balances_pin_authorization", "account_statement_pin_authorization", - "account_balances" + "account_balances", + "middle_language_set", + "last_language_set" ] \ No newline at end of file diff --git a/apps/cic-ussd/states/signup_states.json b/apps/cic-ussd/states/signup_states.json index 1e6722d2..cd1a5732 100644 --- a/apps/cic-ussd/states/signup_states.json +++ b/apps/cic-ussd/states/signup_states.json @@ -2,6 +2,8 @@ "start", "scan_data", "initial_language_selection", + "initial_middle_language_set", + "initial_last_language_set", "initial_pin_entry", "initial_pin_confirmation", "change_preferred_language" diff --git a/apps/cic-ussd/test_requirements.txt b/apps/cic-ussd/test_requirements.txt index e893b546..6843b112 100644 --- a/apps/cic-ussd/test_requirements.txt +++ b/apps/cic-ussd/test_requirements.txt @@ -1,12 +1,12 @@ -cic-eth[services]~=0.12.4a13 -Faker==8.1.2 +cic-eth[services]~=0.12.7 +Faker==11.1.0 faker-e164==0.1.0 -pytest==6.2.4 -pytest-alembic==0.2.5 +pytest==6.2.5 +pytest-alembic==0.7.0 pytest-celery==0.0.0a1 -pytest-cov==2.10.1 -pytest-mock==3.3.1 +pytest-cov==3.0.0 +pytest-mock==3.6.1 pytest-ordering==0.6 -pytest-redis==2.0.0 -requests-mock==1.8.0 -tavern==1.14.2 +pytest-redis==2.3.0 +requests-mock==1.9.3 +tavern==1.18.0 diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_balance.py b/apps/cic-ussd/tests/cic_ussd/account/test_balance.py index fcfb8383..e80fc265 100644 --- a/apps/cic-ussd/tests/cic_ussd/account/test_balance.py +++ b/apps/cic-ussd/tests/cic_ussd/account/test_balance.py @@ -2,10 +2,16 @@ # external imports import pytest +from cic_types.condiments import MetadataPointer # local imports -from cic_ussd.account.balance import calculate_available_balance, get_balances, get_cached_available_balance +from cic_ussd.account.balance import (calculate_available_balance, + get_balances, + get_cached_adjusted_balance, + get_cached_available_balance) from cic_ussd.account.chain import Chain +from cic_ussd.account.tokens import get_cached_token_data_list +from cic_ussd.cache import cache_data_key, get_cached_data from cic_ussd.error import CachedDataNotFoundError # test imports @@ -57,19 +63,45 @@ def test_calculate_available_balance(activated_account, 'balance_outgoing': balance_outgoing, 'balance_incoming': balance_incoming } - assert calculate_available_balance(balances) == available_balance + assert calculate_available_balance(balances, 6) == available_balance def test_get_cached_available_balance(activated_account, balances, cache_balances, cache_default_token_data, - load_chain_spec): - cached_available_balance = get_cached_available_balance(activated_account.blockchain_address) - available_balance = calculate_available_balance(balances[0]) + load_chain_spec, + token_symbol): + identifier = [bytes.fromhex(activated_account.blockchain_address), token_symbol.encode('utf-8')] + cached_available_balance = get_cached_available_balance(6, identifier) + available_balance = calculate_available_balance(balances[0], 6) assert cached_available_balance == available_balance address = blockchain_address() with pytest.raises(CachedDataNotFoundError) as error: - cached_available_balance = get_cached_available_balance(address) + identifier = [bytes.fromhex(address), token_symbol.encode('utf-8')] + key = cache_data_key(identifier=identifier, salt=MetadataPointer.BALANCES) + cached_available_balance = get_cached_available_balance(6, identifier) assert cached_available_balance is None - assert str(error.value) == f'No cached available balance for address: {address}' + assert str(error.value) == f'No cached available balance at {key}' + + +def test_get_cached_adjusted_balance(activated_account, cache_adjusted_balances, token_symbol): + identifier = bytes.fromhex(activated_account.blockchain_address) + balances_identifier = [identifier, token_symbol.encode('utf-8')] + key = cache_data_key(balances_identifier, MetadataPointer.BALANCES_ADJUSTED) + adjusted_balances = get_cached_data(key) + assert get_cached_adjusted_balance(balances_identifier) == adjusted_balances + + +def test_get_account_tokens_balance(activated_account, + cache_token_data_list, + celery_session_worker, + load_chain_spec, + load_config, + mock_async_balance_api_query, + token_symbol): + blockchain_address = activated_account.blockchain_address + chain_str = Chain.spec.__str__() + get_balances(blockchain_address, chain_str, token_symbol, asynchronous=True) + assert mock_async_balance_api_query.get('address') == blockchain_address + assert mock_async_balance_api_query.get('token_symbol') == token_symbol diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_guardianship.py b/apps/cic-ussd/tests/cic_ussd/account/test_guardianship.py new file mode 100644 index 00000000..ccfbed48 --- /dev/null +++ b/apps/cic-ussd/tests/cic_ussd/account/test_guardianship.py @@ -0,0 +1,21 @@ +# standard imports +import os + +# external imports + +# local imports +from cic_ussd.account.guardianship import Guardianship + +# test imports +from tests.fixtures.config import root_directory + + +def test_guardianship(load_config, setup_guardianship): + guardians_file = os.path.join(root_directory, load_config.get('SYSTEM_GUARDIANS_FILE')) + with open(guardians_file, 'r') as system_guardians: + guardians = [line.strip() for line in system_guardians] + assert Guardianship.guardians == guardians + + guardianship = Guardianship() + assert guardianship.is_system_guardian(Guardianship.guardians[0]) is True + assert guardianship.is_system_guardian('+254712345678') is False diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_statement.py b/apps/cic-ussd/tests/cic_ussd/account/test_statement.py index 7fb5a7bc..5c6b4167 100644 --- a/apps/cic-ussd/tests/cic_ussd/account/test_statement.py +++ b/apps/cic-ussd/tests/cic_ussd/account/test_statement.py @@ -11,8 +11,7 @@ from cic_ussd.account.statement import (filter_statement_transactions, generate, get_cached_statement, parse_statement_transactions, - query_statement, - statement_transaction_set) + query_statement) from cic_ussd.account.transaction import transaction_actors from cic_ussd.cache import cache_data_key, get_cached_data @@ -74,12 +73,3 @@ def test_query_statement(blockchain_address, limit, load_chain_spec, activated_a query_statement(blockchain_address, limit) assert mock_transaction_list_query.get('address') == blockchain_address assert mock_transaction_list_query.get('limit') == limit - - -def test_statement_transaction_set(cache_default_token_data, load_chain_spec, preferences, set_locale_files, statement): - parsed_transactions = parse_statement_transactions(statement) - preferred_language = preferences.get('preferred_language') - transaction_set = statement_transaction_set(preferred_language, parsed_transactions) - transaction_set.startswith('Sent') - transaction_set = statement_transaction_set(preferred_language, []) - transaction_set.startswith('No') diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_tokens.py b/apps/cic-ussd/tests/cic_ussd/account/test_tokens.py index ca7e9cb5..4215c89b 100644 --- a/apps/cic-ussd/tests/cic_ussd/account/test_tokens.py +++ b/apps/cic-ussd/tests/cic_ussd/account/test_tokens.py @@ -1,17 +1,80 @@ # standard imports +import hashlib import json # external imports import pytest +from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.chain import Chain -from cic_ussd.account.tokens import get_cached_default_token, get_default_token_symbol, query_default_token +from cic_ussd.account.tokens import (collate_token_metadata, + create_account_tokens_list, + get_active_token_symbol, + get_default_token_symbol, + get_cached_default_token, + get_cached_token_data, + get_cached_token_data_list, + get_cached_token_symbol_list, + hashed_token_proof, + handle_token_symbol_list, + order_account_tokens_list, + parse_token_list, + process_token_data, + query_default_token, + query_token_data, + remove_from_account_tokens_list, + set_active_token) +from cic_ussd.cache import cache_data, cache_data_key, get_cached_data +from cic_ussd.error import CachedDataNotFoundError # test imports +def test_collate_token_metadata(token_meta_symbol, token_proof_symbol): + description = token_proof_symbol.get('description') + issuer = token_proof_symbol.get('issuer') + location = token_meta_symbol.get('location') + contact = token_meta_symbol.get('contact') + data = { + 'description': description, + 'issuer': issuer, + 'location': location, + 'contact': contact + } + assert collate_token_metadata(token_proof_symbol, token_meta_symbol) == data + + +def test_create_account_tokens_list(activated_account, + cache_balances, + cache_token_data, + cache_token_symbol_list, + init_cache): + create_account_tokens_list(activated_account.blockchain_address) + key = cache_data_key(bytes.fromhex(activated_account.blockchain_address), MetadataPointer.TOKEN_DATA_LIST) + cached_data_list = json.loads(get_cached_data(key)) + data = get_cached_token_data_list(activated_account.blockchain_address) + assert cached_data_list == data + + +def test_get_active_token_symbol(activated_account, set_active_token, valid_recipient): + identifier = bytes.fromhex(activated_account.blockchain_address) + key = cache_data_key(identifier=identifier, salt=MetadataPointer.TOKEN_ACTIVE) + active_token_symbol = get_cached_data(key) + assert active_token_symbol == get_active_token_symbol(activated_account.blockchain_address) + with pytest.raises(CachedDataNotFoundError) as error: + get_active_token_symbol(valid_recipient.blockchain_address) + assert str(error.value) == 'No active token set.' + + +def test_get_cached_token_data(activated_account, cache_token_data, token_symbol): + identifier = [bytes.fromhex(activated_account.blockchain_address), token_symbol.encode('utf-8')] + key = cache_data_key(identifier, MetadataPointer.TOKEN_DATA) + token_data = json.loads(get_cached_data(key)) + assert token_data == get_cached_token_data(activated_account.blockchain_address, token_symbol) + + def test_get_cached_default_token(cache_default_token_data, default_token_data, load_chain_spec): chain_str = Chain.spec.__str__() cached_default_token = get_cached_default_token(chain_str) @@ -27,6 +90,84 @@ def test_get_default_token_symbol_from_api(default_token_data, load_chain_spec, assert default_token_symbol == default_token_data['symbol'] +def test_get_cached_token_data_list(activated_account, cache_token_data_list): + blockchain_address = activated_account.blockchain_address + key = cache_data_key(identifier=bytes.fromhex(blockchain_address), salt=MetadataPointer.TOKEN_DATA_LIST) + token_symbols_list = json.loads(get_cached_data(key)) + assert token_symbols_list == get_cached_token_data_list(blockchain_address) + + +def test_get_cached_token_symbol_list(activated_account, cache_token_symbol_list): + blockchain_address = activated_account.blockchain_address + key = cache_data_key(identifier=bytes.fromhex(blockchain_address), salt=MetadataPointer.TOKEN_SYMBOLS_LIST) + token_symbols_list = json.loads(get_cached_data(key)) + assert token_symbols_list == get_cached_token_symbol_list(blockchain_address) + + +def test_hashed_token_proof(token_proof_symbol): + hash_object = hashlib.new("sha256") + token_proof = json.dumps(token_proof_symbol) + hash_object.update(token_proof.encode('utf-8')) + assert hash_object.digest().hex() == hashed_token_proof(token_proof_symbol) + + +def test_handle_token_symbol_list(activated_account, init_cache): + handle_token_symbol_list(activated_account.blockchain_address, 'GFT') + cached_token_symbol_list = get_cached_token_symbol_list(activated_account.blockchain_address) + assert len(cached_token_symbol_list) == 1 + handle_token_symbol_list(activated_account.blockchain_address, 'DET') + cached_token_symbol_list = get_cached_token_symbol_list(activated_account.blockchain_address) + assert len(cached_token_symbol_list) == 2 + + +def test_order_account_tokens_list(activated_account, token_list_entries): + identifier = bytes.fromhex(activated_account.blockchain_address) + last_sent_token_key = cache_data_key(identifier=identifier, salt=MetadataPointer.TOKEN_LAST_SENT) + cache_data(last_sent_token_key, 'FII') + + last_received_token_key = cache_data_key(identifier=identifier, salt=MetadataPointer.TOKEN_LAST_RECEIVED) + cache_data(last_received_token_key, 'DET') + + ordered_list = order_account_tokens_list(token_list_entries, identifier) + assert ordered_list == [ + { + 'name': 'Fee', + 'symbol': 'FII', + 'issuer': 'Foo', + 'contact': { + 'phone': '+254712345678' + }, + 'location': 'Fum', + 'balance': 50.0 + }, + { + 'name': 'Demurrage Token', + 'symbol': 'DET', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org'}, + 'location': 'Fum', + 'balance': 49.99 + }, + { + 'name': 'Giftable Token', + 'symbol': 'GFT', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org'}, + 'location': 'Fum', + 'balance': 60.0 + } + ] + + +def test_parse_token_list(token_list_entries): + parsed_token_list = ['1. FII 50.0', '2. GFT 60.0', '3. DET 49.99'] + assert parsed_token_list == parse_token_list(token_list_entries) + + def test_query_default_token(default_token_data, load_chain_spec, mock_sync_default_token_api_query): chain_str = Chain.spec.__str__() queried_default_token_data = query_default_token(chain_str) @@ -40,3 +181,38 @@ def test_get_default_token_symbol_from_cache(cache_default_token_data, default_t default_token_symbol = get_default_token_symbol() assert default_token_symbol is not None assert default_token_symbol == default_token_data.get('symbol') + + +def test_remove_from_account_tokens_list(token_list_entries): + assert remove_from_account_tokens_list(token_list_entries, 'GFT') == ([{ + 'name': 'Giftable Token', + 'symbol': 'GFT', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org' + }, + 'location': 'Fum', + 'balance': 60.0 + }], + [ + { + 'name': 'Fee', + 'symbol': 'FII', + 'issuer': 'Foo', + 'contact': {'phone': '+254712345678'}, + 'location': 'Fum', + 'balance': 50.0 + }, + { + 'name': 'Demurrage Token', + 'symbol': 'DET', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org' + }, + 'location': 'Fum', + 'balance': 49.99 + } + ]) diff --git a/apps/cic-ussd/tests/cic_ussd/account/test_transaction.py b/apps/cic-ussd/tests/cic_ussd/account/test_transaction.py index 45d7a105..c2d2c9df 100644 --- a/apps/cic-ussd/tests/cic_ussd/account/test_transaction.py +++ b/apps/cic-ussd/tests/cic_ussd/account/test_transaction.py @@ -1,5 +1,4 @@ # standard imports -from decimal import Decimal # external imports import pytest @@ -37,11 +36,11 @@ def test_aux_transaction_data(preferences, set_locale_files, transactions_list): @pytest.mark.parametrize("value, expected_result", [ - (50000000, Decimal('50.00')), - (100000, Decimal('0.10')) + (50000000, 50.0), + (100000, 0.1) ]) def test_from_wei(cache_default_token_data, expected_result, value): - assert from_wei(value) == expected_result + assert from_wei(6, value) == expected_result @pytest.mark.parametrize("value, expected_result", [ @@ -49,7 +48,7 @@ def test_from_wei(cache_default_token_data, expected_result, value): (0.10, 100000) ]) def test_to_wei(cache_default_token_data, expected_result, value): - assert to_wei(value) == expected_result + assert to_wei(6, value) == expected_result @pytest.mark.parametrize("decimals, value, expected_result", [ @@ -108,8 +107,8 @@ def test_outgoing_transaction_processor(activated_account, activated_account.blockchain_address, valid_recipient.blockchain_address) - outgoing_tx_processor.transfer(amount, token_symbol) + outgoing_tx_processor.transfer(amount, 6, token_symbol) assert mock_transfer_api.get('from_address') == activated_account.blockchain_address assert mock_transfer_api.get('to_address') == valid_recipient.blockchain_address - assert mock_transfer_api.get('value') == to_wei(amount) + assert mock_transfer_api.get('value') == to_wei(6, amount) assert mock_transfer_api.get('token_symbol') == token_symbol diff --git a/apps/cic-ussd/tests/cic_ussd/db/models/test_account.py b/apps/cic-ussd/tests/cic_ussd/db/models/test_account.py index 6baa9b31..2426787d 100644 --- a/apps/cic-ussd/tests/cic_ussd/db/models/test_account.py +++ b/apps/cic-ussd/tests/cic_ussd/db/models/test_account.py @@ -90,7 +90,7 @@ def test_standard_metadata_id(activated_account, cache_person_metadata, pending_ def test_account_create(init_cache, init_database, load_chain_spec, mock_account_creation_task_result, task_uuid): chain_str = Chain.spec.__str__() - create(chain_str, phone_number(), init_database) + create(chain_str, phone_number(), init_database, 'en') assert len(init_database.query(TaskTracker).all()) == 1 account_creation_data = get_cached_data(task_uuid) assert json.loads(account_creation_data).get('status') == AccountStatus.PENDING.name diff --git a/apps/cic-ussd/tests/cic_ussd/metadata/test_base.py b/apps/cic-ussd/tests/cic_ussd/metadata/test_base.py index f3ad8e62..38937ec1 100644 --- a/apps/cic-ussd/tests/cic_ussd/metadata/test_base.py +++ b/apps/cic-ussd/tests/cic_ussd/metadata/test_base.py @@ -23,7 +23,7 @@ def test_ussd_metadata_handler(activated_account, setup_metadata_signer): identifier = bytes.fromhex(strip_0x(activated_account.blockchain_address)) cic_type = MetadataPointer.PERSON - metadata_client = UssdMetadataHandler(cic_type, identifier) + metadata_client = UssdMetadataHandler(cic_type=cic_type, identifier=identifier) assert metadata_client.cic_type == cic_type assert metadata_client.engine == 'pgp' assert metadata_client.identifier == identifier diff --git a/apps/cic-ussd/tests/cic_ussd/metadata/test_tokens_meta.py b/apps/cic-ussd/tests/cic_ussd/metadata/test_tokens_meta.py new file mode 100644 index 00000000..fb8aae79 --- /dev/null +++ b/apps/cic-ussd/tests/cic_ussd/metadata/test_tokens_meta.py @@ -0,0 +1,72 @@ +# standard imports +import json + +# external imports +import pytest +import requests_mock +from cic_types.condiments import MetadataPointer +from requests.exceptions import HTTPError + +# local imports +from cic_ussd.cache import cache_data_key, get_cached_data +from cic_ussd.metadata import TokenMetadata +from cic_ussd.metadata.tokens import token_metadata_handler, query_token_metadata, query_token_info + + +# test imports + + +def test_token_metadata_handler(activated_account, + init_cache, + setup_metadata_request_handler, + setup_metadata_signer, + token_meta_symbol, + token_symbol): + with requests_mock.Mocker(real_http=False) as request_mocker: + with pytest.raises(HTTPError) as error: + metadata_client = TokenMetadata(identifier=b'foo', cic_type=MetadataPointer.TOKEN_META_SYMBOL) + reason = 'Not Found' + status_code = 401 + request_mocker.register_uri('GET', metadata_client.url, status_code=status_code, reason=reason) + token_metadata_handler(metadata_client) + assert str(error.value) == f'Client Error: {status_code}, reason: {reason}' + + identifier = token_symbol.encode('utf-8') + metadata_client = TokenMetadata(identifier, cic_type=MetadataPointer.TOKEN_META_SYMBOL) + request_mocker.register_uri('GET', metadata_client.url, json=token_meta_symbol, status_code=200, reason='OK') + token_metadata_handler(metadata_client) + key = cache_data_key(identifier, MetadataPointer.TOKEN_META_SYMBOL) + cached_token_meta_symbol = get_cached_data(key) + assert json.loads(cached_token_meta_symbol) == token_meta_symbol + + +def test_query_token_metadata(init_cache, + setup_metadata_request_handler, + setup_metadata_signer, + token_meta_symbol, + token_proof_symbol, + token_symbol): + with requests_mock.Mocker(real_http=False) as request_mocker: + identifier = token_symbol.encode('utf-8') + metadata_client = TokenMetadata(identifier, cic_type=MetadataPointer.TOKEN_META_SYMBOL) + request_mocker.register_uri('GET', metadata_client.url, json=token_meta_symbol, status_code=200, reason='OK') + query_token_metadata(identifier) + key = cache_data_key(identifier, MetadataPointer.TOKEN_META_SYMBOL) + cached_token_meta_symbol = get_cached_data(key) + assert json.loads(cached_token_meta_symbol) == token_meta_symbol + + +def test_query_token_info(init_cache, + setup_metadata_request_handler, + setup_metadata_signer, + token_meta_symbol, + token_proof_symbol, + token_symbol): + with requests_mock.Mocker(real_http=False) as request_mocker: + identifier = token_symbol.encode('utf-8') + metadata_client = TokenMetadata(identifier, cic_type=MetadataPointer.TOKEN_PROOF_SYMBOL) + request_mocker.register_uri('GET', metadata_client.url, json=token_proof_symbol, status_code=200, reason='OK') + query_token_info(identifier) + key = cache_data_key(identifier, MetadataPointer.TOKEN_PROOF_SYMBOL) + cached_token_proof_symbol = get_cached_data(key) + assert json.loads(cached_token_proof_symbol) == token_proof_symbol diff --git a/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py b/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py index f5ec7f19..cd9cd365 100644 --- a/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py +++ b/apps/cic-ussd/tests/cic_ussd/processor/test_menu.py @@ -1,6 +1,6 @@ # standard imports import json -import datetime +import os # external imports from cic_types.condiments import MetadataPointer @@ -10,195 +10,464 @@ from cic_ussd.account.balance import get_cached_available_balance from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.account.statement import ( get_cached_statement, - parse_statement_transactions, - statement_transaction_set + parse_statement_transactions ) -from cic_ussd.account.tokens import get_default_token_symbol +from cic_ussd.account.tokens import (get_active_token_symbol, + get_cached_token_data) from cic_ussd.account.transaction import from_wei, to_wei -from cic_ussd.cache import cache_data, cache_data_key -from cic_ussd.menu.ussd_menu import UssdMenu +from cic_ussd.cache import cache_data, cache_data_key, get_cached_data from cic_ussd.metadata import PersonMetadata from cic_ussd.phone_number import Support -from cic_ussd.processor.menu import response -from cic_ussd.processor.util import parse_person_metadata +from cic_ussd.processor.menu import response, MenuProcessor +from cic_ussd.processor.util import parse_person_metadata, ussd_menu_list from cic_ussd.translation import translation_for # test imports - -def test_menu_processor(activated_account, - balances, - cache_balances, - cache_default_token_data, - cache_preferences, - cache_person_metadata, - cache_statement, - celery_session_worker, - generic_ussd_session, - init_database, - load_chain_spec, - load_support_phone, - load_ussd_menu, - mock_get_adjusted_balance, - mock_sync_balance_api_query, - mock_transaction_list_query, - valid_recipient): - preferred_language = get_cached_preferred_language(activated_account.blockchain_address) - available_balance = get_cached_available_balance(activated_account.blockchain_address) - token_symbol = get_default_token_symbol() - with_available_balance = 'ussd.kenya.account_balances.available_balance' - with_fees = 'ussd.kenya.account_balances.with_fees' - ussd_menu = UssdMenu.find_by_name('account_balances') - name = ussd_menu.get('name') - resp = response(activated_account, 'ussd.kenya.account_balances', name, init_database, generic_ussd_session) +def test_account_balance(activated_account, cache_balances, cache_preferences, cache_token_data, + generic_ussd_session, init_database, set_active_token): + """blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + decimals = token_data.get("decimals") + identifier = bytes.fromhex(blockchain_address) + balances_identifier = [identifier, token_symbol.encode('utf-8')] + available_balance = get_cached_available_balance(decimals, balances_identifier) + with_available_balance = 'ussd.account_balances.available_balance' + resp = response(activated_account, with_available_balance, with_available_balance[5:], init_database, + generic_ussd_session) assert resp == translation_for(with_available_balance, preferred_language, available_balance=available_balance, token_symbol=token_symbol) - identifier = bytes.fromhex(activated_account.blockchain_address) - key = cache_data_key(identifier, MetadataPointer.BALANCES_ADJUSTED) + with_fees = 'ussd.account_balances.with_fees' + key = cache_data_key(balances_identifier, MetadataPointer.BALANCES_ADJUSTED) adjusted_balance = 45931650.64654012 cache_data(key, json.dumps(adjusted_balance)) - resp = response(activated_account, 'ussd.kenya.account_balances', name, init_database, generic_ussd_session) - tax_wei = to_wei(int(available_balance)) - int(adjusted_balance) - tax = from_wei(int(tax_wei)) + resp = response(activated_account, with_fees, with_fees[5:], init_database, generic_ussd_session) + tax_wei = to_wei(decimals, int(available_balance)) - int(adjusted_balance) + tax = from_wei(decimals, int(tax_wei)) assert resp == translation_for(key=with_fees, preferred_language=preferred_language, available_balance=available_balance, tax=tax, - token_symbol=token_symbol) + token_symbol=token_symbol)""" + pass - cached_statement = get_cached_statement(activated_account.blockchain_address) - statement = json.loads(cached_statement) - statement_transactions = parse_statement_transactions(statement) - transaction_sets = [statement_transactions[tx:tx + 3] for tx in range(0, len(statement_transactions), 3)] - first_transaction_set = [] - middle_transaction_set = [] - last_transaction_set = [] - if transaction_sets: - first_transaction_set = statement_transaction_set(preferred_language, transaction_sets[0]) - if len(transaction_sets) >= 2: - middle_transaction_set = statement_transaction_set(preferred_language, transaction_sets[1]) - if len(transaction_sets) >= 3: - last_transaction_set = statement_transaction_set(preferred_language, transaction_sets[2]) - display_key = 'ussd.kenya.first_transaction_set' - ussd_menu = UssdMenu.find_by_name('first_transaction_set') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) +def test_account_statement(activated_account, + cache_preferences, + cache_statement, + generic_ussd_session, + init_database, + set_active_token, + set_locale_files): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + cached_statement = get_cached_statement(blockchain_address) + statement_list = parse_statement_transactions(statement=json.loads(cached_statement)) + first_transaction_set = 'ussd.first_transaction_set' + middle_transaction_set = 'ussd.middle_transaction_set' + last_transaction_set = 'ussd.last_transaction_set' + fallback = translation_for('helpers.no_transaction_history', preferred_language) + transaction_sets = ussd_menu_list(fallback=fallback, menu_list=statement_list, split=3) + resp = response(activated_account, first_transaction_set, first_transaction_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(first_transaction_set, preferred_language, first_transaction_set=transaction_sets[0]) + resp = response(activated_account, middle_transaction_set, middle_transaction_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(middle_transaction_set, preferred_language, + middle_transaction_set=transaction_sets[1]) + resp = response(activated_account, last_transaction_set, last_transaction_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(last_transaction_set, preferred_language, last_transaction_set=transaction_sets[2]) - assert resp == translation_for(display_key, preferred_language, first_transaction_set=first_transaction_set) - display_key = 'ussd.kenya.middle_transaction_set' - ussd_menu = UssdMenu.find_by_name('middle_transaction_set') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) +def test_add_guardian_pin_authorization(activated_account, + cache_preferences, + guardian_account, + generic_ussd_session, + init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + add_guardian_pin_authorization = 'ussd.add_guardian_pin_authorization' + activated_account.add_guardian(guardian_account.phone_number) + init_database.flush() + generic_ussd_session['external_session_id'] = os.urandom(20).hex() + generic_ussd_session['msisdn'] = guardian_account.phone_number + generic_ussd_session['data'] = {'guardian_phone_number': guardian_account.phone_number} + generic_ussd_session['state'] = 'add_guardian_pin_authorization' + resp = response(activated_account, + add_guardian_pin_authorization, + add_guardian_pin_authorization[5:], + init_database, + generic_ussd_session) + assert resp == translation_for(f'{add_guardian_pin_authorization}.first', preferred_language, + guardian_information=guardian_account.standard_metadata_id()) - assert resp == translation_for(display_key, preferred_language, middle_transaction_set=middle_transaction_set) - display_key = 'ussd.kenya.last_transaction_set' - ussd_menu = UssdMenu.find_by_name('last_transaction_set') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) +def test_guardian_list(activated_account, + cache_preferences, + generic_ussd_session, + guardian_account, + init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + guardians_list = 'ussd.guardian_list' + guardians_list_header = translation_for('helpers.guardians_list_header', preferred_language) + guardian_information = guardian_account.standard_metadata_id() + guardians = guardians_list_header + '\n' + f'{guardian_information}\n' + activated_account.add_guardian(guardian_account.phone_number) + init_database.flush() + resp = response(activated_account, guardians_list, guardians_list[5:], init_database, generic_ussd_session) + assert resp == translation_for(guardians_list, preferred_language, guardians_list=guardians) + guardians = translation_for('helpers.no_guardians_list', preferred_language) + identifier = bytes.fromhex(guardian_account.blockchain_address) + key = cache_data_key(identifier, MetadataPointer.PREFERENCES) + cache_data(key, json.dumps({'preferred_language': preferred_language})) + resp = response(guardian_account, guardians_list, guardians_list[5:], init_database, generic_ussd_session) + assert resp == translation_for(guardians_list, preferred_language, guardians_list=guardians) - assert resp == translation_for(display_key, preferred_language, last_transaction_set=last_transaction_set) - display_key = 'ussd.kenya.display_user_metadata' - ussd_menu = UssdMenu.find_by_name('display_user_metadata') - name = ussd_menu.get('name') - identifier = bytes.fromhex(activated_account.blockchain_address) +def test_account_tokens(activated_account, cache_token_data_list, celery_session_worker, generic_ussd_session, + init_cache, init_database): + """blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + cached_token_data_list = get_cached_token_data_list(blockchain_address) + token_data_list = ['1. GFT 50.0'] + fallback = translation_for('helpers.no_tokens_list', preferred_language) + token_list_sets = ussd_menu_list(fallback=fallback, menu_list=token_data_list, split=3) + first_account_tokens_set = 'ussd.first_account_tokens_set' + middle_account_tokens_set = 'ussd.middle_account_tokens_set' + last_account_tokens_set = 'ussd.last_account_tokens_set' + resp = response(activated_account, first_account_tokens_set, first_account_tokens_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(first_account_tokens_set, preferred_language, + first_account_tokens_set=token_list_sets[0]) + assert generic_ussd_session.get('data').get('account_tokens_list') == cached_token_data_list + resp = response(activated_account, middle_account_tokens_set, middle_account_tokens_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(middle_account_tokens_set, preferred_language, + middle_account_tokens_set=token_list_sets[1]) + resp = response(activated_account, last_account_tokens_set, last_account_tokens_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(last_account_tokens_set, preferred_language, + last_account_tokens_set=token_list_sets[2])""" + pass + + +def test_help(activated_account, cache_preferences, generic_ussd_session, init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + help = 'ussd.help' + resp = response(activated_account, help, help[5:], init_database, generic_ussd_session) + assert resp == translation_for(help, preferred_language, support_phone=Support.phone_number) + + +def test_person_data(activated_account, cache_person_metadata, cache_preferences, cached_ussd_session, + generic_ussd_session, init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + identifier = bytes.fromhex(blockchain_address) + display_user_metadata = 'ussd.display_user_metadata' person_metadata = PersonMetadata(identifier) cached_person_metadata = person_metadata.get_cached_metadata() - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == parse_person_metadata(cached_person_metadata, display_key, preferred_language) + resp = response(activated_account, display_user_metadata, display_user_metadata[5:], init_database, + generic_ussd_session) + assert resp == parse_person_metadata(cached_person_metadata, display_user_metadata, preferred_language) - display_key = 'ussd.kenya.account_balances_pin_authorization' - ussd_menu = UssdMenu.find_by_name('account_balances_pin_authorization') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == translation_for(f'{display_key}.first', preferred_language) - activated_account.failed_pin_attempts = 1 - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - retry_pin_entry = translation_for('ussd.kenya.retry_pin_entry', preferred_language, remaining_attempts=2) - assert resp == translation_for(f'{display_key}.retry', preferred_language, retry_pin_entry=retry_pin_entry) - activated_account.failed_pin_attempts = 0 +def test_guarded_account_metadata(activated_account, generic_ussd_session, init_database): + reset_guarded_pin_authorization = 'ussd.reset_guarded_pin_authorization' + generic_ussd_session['data'] = {'guarded_account_phone_number': activated_account.phone_number} + menu_processor = MenuProcessor(activated_account, reset_guarded_pin_authorization, + reset_guarded_pin_authorization[5:], init_database, generic_ussd_session) + assert menu_processor.guarded_account_metadata() == activated_account.standard_metadata_id() - display_key = 'ussd.kenya.start' - ussd_menu = UssdMenu.find_by_name('start') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == translation_for(display_key, + +def test_guardian_metadata(activated_account, generic_ussd_session, guardian_account, init_database): + add_guardian_pin_authorization = 'ussd.add_guardian_pin_authorization' + generic_ussd_session['data'] = {'guardian_phone_number': guardian_account.phone_number} + menu_processor = MenuProcessor(activated_account, add_guardian_pin_authorization, + add_guardian_pin_authorization[5:], init_database, generic_ussd_session) + assert menu_processor.guardian_metadata() == guardian_account.standard_metadata_id() + + +def test_language(activated_account, cache_preferences, generic_ussd_session, init_database, load_languages): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + initial_language_selection = 'ussd.initial_language_selection' + select_preferred_language = 'ussd.select_preferred_language' + initial_middle_language_set = 'ussd.initial_middle_language_set' + middle_language_set = 'ussd.middle_language_set' + initial_last_language_set = 'ussd.initial_last_language_set' + last_language_set = 'ussd.last_language_set' + + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cached_system_languages = get_cached_data(key) + language_list: list = json.loads(cached_system_languages) + + fallback = translation_for('helpers.no_language_list', preferred_language) + language_list_sets = ussd_menu_list(fallback=fallback, menu_list=language_list, split=3) + + resp = response(activated_account, initial_language_selection, initial_language_selection[5:], init_database, + generic_ussd_session) + assert resp == translation_for(initial_language_selection, preferred_language, + first_language_set=language_list_sets[0]) + + resp = response(activated_account, select_preferred_language, select_preferred_language[5:], init_database, + generic_ussd_session) + assert resp == translation_for(select_preferred_language, preferred_language, + first_language_set=language_list_sets[0]) + + resp = response(activated_account, initial_middle_language_set, initial_middle_language_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(initial_middle_language_set, preferred_language, + middle_language_set=language_list_sets[1]) + + resp = response(activated_account, initial_last_language_set, initial_last_language_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(initial_last_language_set, preferred_language, + last_language_set=language_list_sets[2]) + + resp = response(activated_account, middle_language_set, middle_language_set[5:], init_database, + generic_ussd_session) + assert resp == translation_for(middle_language_set, preferred_language, middle_language_set=language_list_sets[1]) + + resp = response(activated_account, last_language_set, last_language_set[5:], init_database, generic_ussd_session) + assert resp == translation_for(last_language_set, preferred_language, last_language_set=language_list_sets[2]) + + +def test_account_creation_prompt(activated_account, cache_preferences, generic_ussd_session, init_database, + load_languages): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + user_input = '' + if preferred_language == 'en': + user_input = '1' + elif preferred_language == 'sw': + user_input = '2' + account_creation_prompt = 'ussd.account_creation_prompt' + generic_ussd_session['user_input'] = user_input + resp = response(activated_account, account_creation_prompt, account_creation_prompt[5:], init_database, + generic_ussd_session) + assert resp == translation_for(account_creation_prompt, preferred_language) + + +def test_reset_guarded_pin_authorization(activated_account, cache_preferences, generic_ussd_session, guardian_account, + init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + reset_guarded_pin_authorization = 'ussd.reset_guarded_pin_authorization' + generic_ussd_session['external_session_id'] = os.urandom(20).hex() + generic_ussd_session['msisdn'] = guardian_account.phone_number + generic_ussd_session['data'] = {'guarded_account_phone_number': activated_account.phone_number} + resp = response(activated_account, + reset_guarded_pin_authorization, + reset_guarded_pin_authorization[5:], + init_database, + generic_ussd_session) + assert resp == translation_for(f'{reset_guarded_pin_authorization}.first', preferred_language, + guarded_account_information=activated_account.phone_number) + + +def test_start(activated_account, cache_balances, cache_preferences, cache_token_data, cache_token_data_list, + cache_token_symbol_list, celery_session_worker, generic_ussd_session, init_database, load_chain_spec, + mock_sync_balance_api_query, set_active_token): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + decimals = token_data.get("decimals") + identifier = bytes.fromhex(blockchain_address) + balances_identifier = [identifier, token_symbol.encode('utf-8')] + available_balance = get_cached_available_balance(decimals, balances_identifier) + start = 'ussd.start' + resp = response(activated_account, start, start[5:], init_database, generic_ussd_session) + assert resp == translation_for(start, preferred_language, account_balance=available_balance, account_token_name=token_symbol) - display_key = 'ussd.kenya.start' - ussd_menu = UssdMenu.find_by_name('start') - name = ussd_menu.get('name') - older_timestamp = (activated_account.created - datetime.timedelta(days=35)) - activated_account.created = older_timestamp - init_database.flush() - response(activated_account, display_key, name, init_database, generic_ussd_session) - assert mock_get_adjusted_balance['timestamp'] == int((datetime.datetime.now() - datetime.timedelta(days=30)).timestamp()) - display_key = 'ussd.kenya.transaction_pin_authorization' - ussd_menu = UssdMenu.find_by_name('transaction_pin_authorization') - name = ussd_menu.get('name') +def test_token_selection_pin_authorization(activated_account, cache_preferences, cache_token_data, generic_ussd_session, + init_database, set_active_token): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + token_selection_pin_authorization = 'ussd.token_selection_pin_authorization' + generic_ussd_session['data'] = {'selected_token': token_data} + resp = response(activated_account, + token_selection_pin_authorization, + token_selection_pin_authorization[5:], + init_database, + generic_ussd_session) + token_name = token_data.get('name') + token_symbol = token_data.get('symbol') + token_issuer = token_data.get('issuer') + token_contact = token_data.get('contact') + token_location = token_data.get('location') + data = f'{token_name} ({token_symbol})\n{token_issuer}\n{token_contact}\n{token_location}\n' + assert resp == translation_for(f'{token_selection_pin_authorization}.first', preferred_language, + token_data=data) + + +def test_transaction_pin_authorization(activated_account, cache_preferences, cache_token_data, generic_ussd_session, + init_database, set_active_token, valid_recipient): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + decimals = token_data.get("decimals") + transaction_pin_authorization = 'ussd.transaction_pin_authorization' generic_ussd_session['data'] = { 'recipient_phone_number': valid_recipient.phone_number, 'transaction_amount': '15' } - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) + resp = response(activated_account, transaction_pin_authorization, transaction_pin_authorization[5:], init_database, + generic_ussd_session) user_input = generic_ussd_session.get('data').get('transaction_amount') - transaction_amount = to_wei(value=int(user_input)) + transaction_amount = to_wei(decimals, int(user_input)) tx_recipient_information = valid_recipient.standard_metadata_id() tx_sender_information = activated_account.standard_metadata_id() - assert resp == translation_for(f'{display_key}.first', + assert resp == translation_for(f'{transaction_pin_authorization}.first', preferred_language, recipient_information=tx_recipient_information, - transaction_amount=from_wei(transaction_amount), + transaction_amount=from_wei(decimals, transaction_amount), token_symbol=token_symbol, sender_information=tx_sender_information) - display_key = 'ussd.kenya.exit_insufficient_balance' - ussd_menu = UssdMenu.find_by_name('exit_insufficient_balance') - name = ussd_menu.get('name') + +def test_guardian_exits(activated_account, cache_preferences, cache_token_data, generic_ussd_session, guardian_account, + init_database, set_active_token): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + generic_ussd_session['data'] = {'guardian_phone_number': guardian_account.phone_number} + # testing exit guardian addition success + exit_guardian_addition_success = 'ussd.exit_guardian_addition_success' + resp = response(activated_account, exit_guardian_addition_success, exit_guardian_addition_success[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_guardian_addition_success, preferred_language, + guardian_information=guardian_account.standard_metadata_id()) + + # testing exit guardian removal success + exit_guardian_removal_success = 'ussd.exit_guardian_removal_success' + resp = response(activated_account, exit_guardian_removal_success, exit_guardian_removal_success[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_guardian_removal_success, preferred_language, + guardian_information=guardian_account.standard_metadata_id()) + + generic_ussd_session['data'] = {'failure_reason': 'foo'} + # testing exit invalid guardian addition + exit_invalid_guardian_addition = 'ussd.exit_invalid_guardian_addition' + resp = response(activated_account, exit_invalid_guardian_addition, exit_invalid_guardian_addition[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_invalid_guardian_addition, preferred_language, error_exit='foo') + + # testing exit invalid guardian removal + exit_invalid_guardian_removal = 'ussd.exit_invalid_guardian_removal' + resp = response(activated_account, exit_invalid_guardian_removal, exit_invalid_guardian_removal[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_invalid_guardian_removal, preferred_language, error_exit='foo') + + +def test_exit_pin_reset_initiated_success(activated_account, cache_preferences, generic_ussd_session, init_database): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + exit_pin_reset_initiated_success = 'ussd.exit_pin_reset_initiated_success' + generic_ussd_session['data'] = {'guarded_account_phone_number': activated_account.phone_number} + resp = response(activated_account, exit_pin_reset_initiated_success, exit_pin_reset_initiated_success[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_pin_reset_initiated_success, + preferred_language, + guarded_account_information=activated_account.standard_metadata_id()) + + +def test_exit_insufficient_balance(activated_account, cache_balances, cache_preferences, cache_token_data, + generic_ussd_session, init_database, set_active_token, valid_recipient): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + decimals = token_data.get("decimals") + identifier = bytes.fromhex(blockchain_address) + balances_identifier = [identifier, token_symbol.encode('utf-8')] + available_balance = get_cached_available_balance(decimals, balances_identifier) + tx_recipient_information = valid_recipient.standard_metadata_id() + exit_insufficient_balance = 'ussd.exit_insufficient_balance' generic_ussd_session['data'] = { 'recipient_phone_number': valid_recipient.phone_number, 'transaction_amount': '85' } transaction_amount = generic_ussd_session.get('data').get('transaction_amount') - transaction_amount = to_wei(value=int(transaction_amount)) - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == translation_for(display_key, + transaction_amount = to_wei(decimals, int(transaction_amount)) + resp = response(activated_account, exit_insufficient_balance, exit_insufficient_balance[5:], init_database, + generic_ussd_session) + assert resp == translation_for(exit_insufficient_balance, preferred_language, - amount=from_wei(transaction_amount), + amount=from_wei(decimals, transaction_amount), token_symbol=token_symbol, recipient_information=tx_recipient_information, token_balance=available_balance) - display_key = 'ussd.kenya.exit_invalid_menu_option' - ussd_menu = UssdMenu.find_by_name('exit_invalid_menu_option') - name = ussd_menu.get('name') - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == translation_for(display_key, preferred_language, support_phone=Support.phone_number) - display_key = 'ussd.kenya.exit_successful_transaction' - ussd_menu = UssdMenu.find_by_name('exit_successful_transaction') - name = ussd_menu.get('name') +def test_exit_invalid_menu_option(activated_account, cache_preferences, generic_ussd_session, init_database, + load_support_phone): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + exit_invalid_menu_option = 'ussd.exit_invalid_menu_option' + resp = response(activated_account, exit_invalid_menu_option, exit_invalid_menu_option[5:], init_database, + generic_ussd_session) + assert resp == translation_for(exit_invalid_menu_option, preferred_language, support_phone=Support.phone_number) + + +def test_exit_pin_blocked(activated_account, cache_preferences, generic_ussd_session, init_database, + load_support_phone): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + exit_pin_blocked = 'ussd.exit_pin_blocked' + resp = response(activated_account, exit_pin_blocked, exit_pin_blocked[5:], init_database, generic_ussd_session) + assert resp == translation_for(exit_pin_blocked, preferred_language, support_phone=Support.phone_number) + + +def test_exit_successful_token_selection(activated_account, cache_preferences, cache_token_data, generic_ussd_session, + init_database, set_active_token): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + exit_successful_token_selection = 'ussd.exit_successful_token_selection' + generic_ussd_session['data'] = {'selected_token': token_data} + resp = response(activated_account, exit_successful_token_selection, exit_successful_token_selection[5:], + init_database, generic_ussd_session) + assert resp == translation_for(exit_successful_token_selection, preferred_language, token_symbol=token_symbol) + + +def test_exit_successful_transaction(activated_account, cache_preferences, cache_token_data, generic_ussd_session, + init_database, set_active_token, valid_recipient): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + preferred_language = get_cached_preferred_language(blockchain_address) + decimals = token_data.get("decimals") + tx_recipient_information = valid_recipient.standard_metadata_id() + tx_sender_information = activated_account.standard_metadata_id() + exit_successful_transaction = 'ussd.exit_successful_transaction' generic_ussd_session['data'] = { 'recipient_phone_number': valid_recipient.phone_number, 'transaction_amount': '15' } transaction_amount = generic_ussd_session.get('data').get('transaction_amount') - transaction_amount = to_wei(value=int(transaction_amount)) - resp = response(activated_account, display_key, name, init_database, generic_ussd_session) - assert resp == translation_for(display_key, + transaction_amount = to_wei(decimals, int(transaction_amount)) + resp = response(activated_account, exit_successful_transaction, exit_successful_transaction[5:], init_database, + generic_ussd_session) + assert resp == translation_for(exit_successful_transaction, preferred_language, - transaction_amount=from_wei(transaction_amount), + transaction_amount=from_wei(decimals, transaction_amount), token_symbol=token_symbol, recipient_information=tx_recipient_information, sender_information=tx_sender_information) diff --git a/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py b/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py index e5bbd52f..b5c68617 100644 --- a/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py +++ b/apps/cic-ussd/tests/cic_ussd/processor/test_ussd.py @@ -10,13 +10,16 @@ from chainlib.hash import strip_0x from cic_types.condiments import MetadataPointer # local imports -from cic_ussd.account.chain import Chain from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.cache import cache_data, cache_data_key, get_cached_data from cic_ussd.db.models.task_tracker import TaskTracker from cic_ussd.menu.ussd_menu import UssdMenu from cic_ussd.metadata import PersonMetadata -from cic_ussd.processor.ussd import get_menu, handle_menu, handle_menu_operations +from cic_ussd.processor.ussd import (get_menu, + handle_menu, + handle_menu_operations) +from cic_ussd.processor.util import ussd_menu_list +from cic_ussd.state_machine.logic.language import preferred_langauge_from_selection from cic_ussd.translation import translation_for # test imports @@ -43,7 +46,7 @@ def test_handle_menu(activated_account, ussd_menu = UssdMenu.find_by_name('exit_pin_blocked') assert menu_resp.get('name') == ussd_menu.get('name') menu_resp = handle_menu(pending_account, init_database) - ussd_menu = UssdMenu.find_by_name('initial_language_selection') + ussd_menu = UssdMenu.find_by_name('initial_pin_entry') assert menu_resp.get('name') == ussd_menu.get('name') identifier = bytes.fromhex(strip_0x(pending_account.blockchain_address)) key = cache_data_key(identifier, MetadataPointer.PREFERENCES) @@ -75,38 +78,62 @@ def test_get_menu(activated_account, assert menu_resp.get('name') == ussd_menu.get('name') -def test_handle_menu_operations(activated_account, - cache_preferences, - celery_session_worker, - generic_ussd_session, - init_database, - init_cache, - load_chain_spec, - load_config, - mock_account_creation_task_result, - persisted_ussd_session, - person_metadata, - set_locale_files, - setup_metadata_request_handler, - setup_metadata_signer, - task_uuid): - # sourcery skip: extract-duplicate-method - chain_str = Chain.spec.__str__() +def test_handle_no_account_menu_operations(celery_session_worker, + init_cache, + init_database, + load_chain_spec, + load_config, + load_languages, + load_ussd_menu, + mock_account_creation_task_result, + pending_account, + persisted_ussd_session, + set_locale_files, + task_uuid): + initial_language_selection = 'ussd.initial_language_selection' phone = phone_number() external_session_id = os.urandom(20).hex() valid_service_codes = load_config.get('USSD_SERVICE_CODE').split(",") preferred_language = i18n.config.get('fallback') - resp = handle_menu_operations(chain_str, external_session_id, phone, None, valid_service_codes[0], init_database, '4444') - assert resp == translation_for('ussd.kenya.account_creation_prompt', preferred_language) + key = cache_data_key('system:languages'.encode('utf-8'), MetadataPointer.NONE) + cached_system_languages = get_cached_data(key) + language_list: list = json.loads(cached_system_languages) + fallback = translation_for('helpers.no_language_list', preferred_language) + language_list_sets = ussd_menu_list(fallback=fallback, menu_list=language_list, split=3) + resp = handle_menu_operations(external_session_id, phone, None, valid_service_codes[0], init_database, '') + assert resp == translation_for(initial_language_selection, preferred_language, + first_language_set=language_list_sets[0]) cached_ussd_session = get_cached_data(external_session_id) ussd_session = json.loads(cached_ussd_session) assert ussd_session['msisdn'] == phone + persisted_ussd_session.external_session_id = external_session_id + persisted_ussd_session.msisdn = phone + persisted_ussd_session.state = initial_language_selection[5:] + init_database.add(persisted_ussd_session) + init_database.commit() + account_creation_prompt = 'ussd.account_creation_prompt' + user_input = '2' + resp = handle_menu_operations(external_session_id, phone, None, valid_service_codes[0], init_database, user_input) + preferred_language = preferred_langauge_from_selection(user_input) + assert resp == translation_for(account_creation_prompt, preferred_language) task_tracker = init_database.query(TaskTracker).filter_by(task_uuid=task_uuid).first() assert task_tracker.task_uuid == task_uuid cached_creation_task_uuid = get_cached_data(task_uuid) creation_task_uuid_data = json.loads(cached_creation_task_uuid) assert creation_task_uuid_data['status'] == 'PENDING' + +def test_handle_account_menu_operations(activated_account, + cache_preferences, + celery_session_worker, + init_database, + load_config, + persisted_ussd_session, + person_metadata, + set_locale_files, + setup_metadata_request_handler, + setup_metadata_signer, ): + valid_service_codes = load_config.get('USSD_SERVICE_CODE').split(",") identifier = bytes.fromhex(strip_0x(activated_account.blockchain_address)) person_metadata_client = PersonMetadata(identifier) with requests_mock.Mocker(real_http=False) as request_mocker: @@ -117,6 +144,5 @@ def test_handle_menu_operations(activated_account, phone = activated_account.phone_number preferred_language = get_cached_preferred_language(activated_account.blockchain_address) persisted_ussd_session.state = 'enter_transaction_recipient' - resp = handle_menu_operations(chain_str, external_session_id, phone, None, valid_service_codes[0], init_database, '1') - assert resp == translation_for('ussd.kenya.enter_transaction_recipient', preferred_language) - + resp = handle_menu_operations(external_session_id, phone, None, valid_service_codes[0], init_database, '1') + assert resp == translation_for('ussd.enter_transaction_recipient', preferred_language) diff --git a/apps/cic-ussd/tests/cic_ussd/processor/test_util.py b/apps/cic-ussd/tests/cic_ussd/processor/test_util.py index 038ad8ca..dbc4d763 100644 --- a/apps/cic-ussd/tests/cic_ussd/processor/test_util.py +++ b/apps/cic-ussd/tests/cic_ussd/processor/test_util.py @@ -10,7 +10,10 @@ from cic_types.models.person import get_contact_data_from_vcard # local imports from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.metadata import PersonMetadata -from cic_ussd.processor.util import latest_input, parse_person_metadata, resume_last_ussd_session +from cic_ussd.processor.util import (latest_input, + parse_person_metadata, + resume_last_ussd_session, + ussd_menu_list) from cic_ussd.translation import translation_for @@ -32,7 +35,7 @@ def test_parse_person_metadata(activated_account, cache_person_metadata, cache_p cached_person_metadata = person_metadata.get_cached_metadata() person_metadata = json.loads(cached_person_metadata) preferred_language = get_cached_preferred_language(activated_account.blockchain_address) - display_key = 'ussd.kenya.display_person_metadata' + display_key = 'ussd.display_person_metadata' parsed_person_metadata = parse_person_metadata(cached_person_metadata, display_key, preferred_language) @@ -60,3 +63,20 @@ def test_parse_person_metadata(activated_account, cache_person_metadata, cache_p ]) def test_resume_last_ussd_session(expected_menu_name, last_state, load_ussd_menu): assert resume_last_ussd_session(last_state).get('name') == expected_menu_name + + +def test_ussd_menu_list(activated_account, cache_preferences, load_ussd_menu, set_locale_files): + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + fallback = translation_for('helpers.no_transaction_history', preferred_language) + menu_list_sets = ['1. FII 50.0', '2. GFT 60.0', '3. DET 49.99'] + split = 3 + menu_list = ussd_menu_list(fallback=fallback, menu_list=menu_list_sets, split=split) + menu_list_sets = [menu_list_sets[item:item + split] for item in range(0, len(menu_list), split)] + menu_list_reprs = [] + for i in range(split): + try: + menu_list_reprs.append(''.join(f'{list_set_item}\n' for list_set_item in menu_list_sets[i]).rstrip('\n')) + except IndexError: + menu_list_reprs.append(fallback) + assert menu_list == menu_list_reprs diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_account_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_account_logic.py index 9e8da5a6..5d696e04 100644 --- a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_account_logic.py +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_account_logic.py @@ -3,8 +3,7 @@ import json # external imports import pytest -import requests_mock -from chainlib.hash import strip_0x + from cic_types.models.person import Person, get_contact_data_from_vcard # local imports @@ -12,9 +11,7 @@ from cic_ussd.cache import get_cached_data from cic_ussd.account.maps import gender from cic_ussd.account.metadata import get_cached_preferred_language from cic_ussd.db.enum import AccountStatus -from cic_ussd.metadata import PreferencesMetadata -from cic_ussd.state_machine.logic.account import (change_preferred_language, - edit_user_metadata_attribute, +from cic_ussd.state_machine.logic.account import (edit_user_metadata_attribute, parse_gender, parse_person_metadata, save_complete_person_metadata, @@ -26,32 +23,6 @@ from cic_ussd.translation import translation_for # test imports -@pytest.mark.parametrize('user_input, expected_preferred_language', [ - ('1', 'en'), - ('2', 'sw') -]) -def test_change_preferred_language(activated_account, - celery_session_worker, - expected_preferred_language, - init_database, - generic_ussd_session, - mock_response, - preferences, - setup_metadata_request_handler, - user_input): - identifier = bytes.fromhex(strip_0x(activated_account.blockchain_address)) - preferences_metadata_client = PreferencesMetadata(identifier) - with requests_mock.Mocker(real_http=False) as requests_mocker: - requests_mocker.register_uri( - 'POST', preferences_metadata_client.url, status_code=200, reason='OK', json=mock_response - ) - state_machine_data = (user_input, generic_ussd_session, activated_account, init_database) - res = change_preferred_language(state_machine_data) - init_database.commit() - assert res.id is not None - assert activated_account.preferred_language == expected_preferred_language - - @pytest.mark.parametrize('user_input', [ '1', '2', diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_language_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_language_logic.py new file mode 100644 index 00000000..a22088ce --- /dev/null +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_language_logic.py @@ -0,0 +1,52 @@ +# standard imports +import json + +# external imports +import requests_mock +from cic_types.condiments import MetadataPointer + +# local imports +from cic_ussd.cache import cache_data_key, get_cached_data +from cic_ussd.metadata import PreferencesMetadata +from cic_ussd.state_machine.logic.language import (change_preferred_language, + is_valid_language_selection, + preferred_langauge_from_selection, + process_language_selection) + +# test imports + + +def test_change_preferred_language(activated_account, + cached_ussd_session, + celery_session_worker, + init_database, + load_languages, + mocker, + setup_metadata_signer, + setup_metadata_request_handler): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + preferences = { + 'preferred_language': 'en' + } + ussd_session['data'] = preferences + mock_add_preferences_metadata = mocker.patch('cic_ussd.tasks.metadata.add_preferences_metadata.apply_async') + with requests_mock.Mocker(real_http=False) as request_mocker: + identifier = bytes.fromhex(activated_account.blockchain_address) + metadata_client = PreferencesMetadata(identifier=identifier) + request_mocker.register_uri('POST', metadata_client.url, status_code=201, reason='CREATED', json=preferences) + state_machine_data = ('1', ussd_session, activated_account, init_database) + change_preferred_language(state_machine_data) + mock_add_preferences_metadata.assert_called_with( + (activated_account.blockchain_address, preferences), {}, queue='cic-ussd') + + +def test_is_valid_language_selection(activated_account, + generic_ussd_session, + init_cache, + init_database, + load_languages): + state_machine_data = ('1', generic_ussd_session, activated_account, init_database) + assert is_valid_language_selection(state_machine_data) is True + state_machine_data = ('12', generic_ussd_session, activated_account, init_database) + assert is_valid_language_selection(state_machine_data) is False diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_menu_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_menu_logic.py index 1cde862c..daf10e36 100644 --- a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_menu_logic.py +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_menu_logic.py @@ -9,7 +9,10 @@ from cic_ussd.state_machine.logic.menu import (menu_one_selected, menu_four_selected, menu_five_selected, menu_six_selected, + menu_nine_selected, menu_zero_zero_selected, + menu_eleven_selected, + menu_twenty_two_selected, menu_ninety_nine_selected) # test imports @@ -29,8 +32,14 @@ def test_menu_selection(init_database, pending_account, persisted_ussd_session): assert menu_five_selected(('e', ussd_session, pending_account, init_database)) is False assert menu_six_selected(('6', ussd_session, pending_account, init_database)) is True assert menu_six_selected(('8', ussd_session, pending_account, init_database)) is False + assert menu_nine_selected(('9', ussd_session, pending_account, init_database)) is True + assert menu_nine_selected(('-', ussd_session, pending_account, init_database)) is False assert menu_zero_zero_selected(('00', ussd_session, pending_account, init_database)) is True assert menu_zero_zero_selected(('/', ussd_session, pending_account, init_database)) is False + assert menu_eleven_selected(('11', ussd_session, pending_account, init_database)) is True + assert menu_eleven_selected(('*', ussd_session, pending_account, init_database)) is False + assert menu_twenty_two_selected(('22', ussd_session, pending_account, init_database)) is True + assert menu_twenty_two_selected(('5', ussd_session, pending_account, init_database)) is False assert menu_ninety_nine_selected(('99', ussd_session, pending_account, init_database)) is True assert menu_ninety_nine_selected(('d', ussd_session, pending_account, init_database)) is False diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_pin_guard_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_pin_guard_logic.py new file mode 100644 index 00000000..f6e439a6 --- /dev/null +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_pin_guard_logic.py @@ -0,0 +1,221 @@ +# standard imports +import json + +# external imports +import requests_mock + +# local imports +from cic_ussd.account.guardianship import Guardianship +from cic_ussd.account.metadata import get_cached_preferred_language +from cic_ussd.cache import cache_data_key, get_cached_data +from cic_ussd.db.models.account import Account +from cic_ussd.metadata import PersonMetadata +from cic_ussd.state_machine.logic.pin_guard import (add_pin_guardian, + is_dialers_pin_guardian, + is_others_pin_guardian, + is_set_pin_guardian, + remove_pin_guardian, + initiate_pin_reset, + save_guardian_to_session_data, + save_guarded_account_session_data, + retrieve_person_metadata, + is_valid_guardian_addition) +from cic_ussd.translation import translation_for + + +def test_save_guardian_to_session_data(activated_account, + cached_ussd_session, + celery_session_worker, + guardian_account, + init_cache, + init_database): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['msisdn'] = activated_account.phone_number + state_machine_data = (guardian_account.phone_number, ussd_session, activated_account, init_database) + save_guardian_to_session_data(state_machine_data) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + assert ussd_session.get('data').get('guardian_phone_number') == guardian_account.phone_number + + +def test_save_guarded_account_session_data(activated_account, + cached_ussd_session, + celery_session_worker, + guardian_account, + init_cache, + init_database): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['msisdn'] = guardian_account.phone_number + state_machine_data = (activated_account.phone_number, ussd_session, guardian_account, init_database) + save_guarded_account_session_data(state_machine_data) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + assert ussd_session.get('data').get('guarded_account_phone_number') == activated_account.phone_number + + +def test_retrieve_person_metadata(activated_account, + cached_ussd_session, + celery_session_worker, + guardian_account, + init_cache, + init_database, + mocker, + person_metadata, + setup_metadata_request_handler, + setup_metadata_signer): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['msisdn'] = activated_account.phone_number + state_machine_data = (guardian_account.phone_number, ussd_session, activated_account, init_database) + mocker_query_person_metadata = mocker.patch('cic_ussd.tasks.metadata.query_person_metadata.apply_async') + with requests_mock.Mocker(real_http=False) as request_mocker: + identifier = bytes.fromhex(activated_account.blockchain_address) + metadata_client = PersonMetadata(identifier) + request_mocker.register_uri('GET', metadata_client.url, json=person_metadata, reason='OK', status_code=200) + retrieve_person_metadata(state_machine_data) + mocker_query_person_metadata.assert_called_with((guardian_account.blockchain_address,), {}, queue='cic-ussd') + + +def test_is_valid_guardian_addition(activated_account, + cache_preferences, + cached_ussd_session, + celery_session_worker, + init_cache, + init_database, + guardian_account, + load_languages, + load_ussd_menu, + set_locale_files, + setup_guardianship): + blockchain_address = activated_account.blockchain_address + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + state_machine_data = (guardian_account.phone_number, ussd_session, activated_account, init_database) + assert is_valid_guardian_addition(state_machine_data) is True + + state_machine_data = (activated_account.phone_number, ussd_session, activated_account, init_database) + assert is_valid_guardian_addition(state_machine_data) is False + + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + preferred_language = get_cached_preferred_language(blockchain_address) + failure_reason = translation_for('helpers.error.is_initiator', preferred_language) + assert ussd_session.get('data').get('failure_reason') == failure_reason + + state_machine_data = (Guardianship.guardians[0], ussd_session, activated_account, init_database) + assert is_valid_guardian_addition(state_machine_data) is False + + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + preferred_language = get_cached_preferred_language(blockchain_address) + failure_reason = translation_for('helpers.error.is_existent_guardian', preferred_language) + assert ussd_session.get('data').get('failure_reason') == failure_reason + + +def test_add_pin_guardian(activated_account, generic_ussd_session, guardian_account, init_database): + generic_ussd_session['data'] = {'guardian_phone_number': guardian_account.phone_number} + state_machine_data = ('', generic_ussd_session, activated_account, init_database) + add_pin_guardian(state_machine_data) + account = Account.get_by_phone_number(activated_account.phone_number, init_database) + assert account.get_guardians()[0] == guardian_account.phone_number + + +def test_is_set_pin_guardian(activated_account, + cache_preferences, + cached_ussd_session, + celery_session_worker, + init_cache, + init_database, + guardian_account, + load_languages, + load_ussd_menu, + set_locale_files, + setup_guardianship): + blockchain_address = activated_account.blockchain_address + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + preferred_language = get_cached_preferred_language(blockchain_address) + assert is_set_pin_guardian(activated_account, guardian_account.phone_number, preferred_language, init_database, + ussd_session) is False + + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + failure_reason = translation_for('helpers.error.is_not_existent_guardian', preferred_language) + assert ussd_session.get('data').get('failure_reason') == failure_reason + + assert is_set_pin_guardian(activated_account, Guardianship.guardians[0], preferred_language, init_database, + ussd_session) is True + + assert is_set_pin_guardian(activated_account, activated_account.phone_number, preferred_language, init_database, + ussd_session) is False + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + failure_reason = translation_for('helpers.error.is_initiator', preferred_language) + assert ussd_session.get('data').get('failure_reason') == failure_reason + + +def test_is_dialers_pin_guardian(activated_account, + cache_preferences, + cached_ussd_session, + celery_session_worker, + init_database, + guardian_account): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + state_machine_data = (guardian_account.phone_number, ussd_session, activated_account, init_database) + assert is_dialers_pin_guardian(state_machine_data) is False + activated_account.add_guardian(guardian_account.phone_number) + init_database.flush() + state_machine_data = (guardian_account.phone_number, ussd_session, activated_account, init_database) + assert is_dialers_pin_guardian(state_machine_data) is True + + +def test_is_others_pin_guardian(activated_account, + cache_preferences, + cached_ussd_session, + celery_session_worker, + init_database, + guardian_account): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + state_machine_data = (activated_account.phone_number, ussd_session, guardian_account, init_database) + assert is_others_pin_guardian(state_machine_data) is False + activated_account.add_guardian(guardian_account.phone_number) + init_database.flush() + state_machine_data = (activated_account.phone_number, ussd_session, guardian_account, init_database) + assert is_others_pin_guardian(state_machine_data) is True + + +def test_remove_pin_guardian(activated_account, generic_ussd_session, guardian_account, init_database): + generic_ussd_session['data'] = {'guardian_phone_number': guardian_account.phone_number} + activated_account.add_guardian(guardian_account.phone_number) + init_database.flush() + assert activated_account.get_guardians()[0] == guardian_account.phone_number + state_machine_data = ('', generic_ussd_session, activated_account, init_database) + remove_pin_guardian(state_machine_data) + assert len(activated_account.get_guardians()) == 0 + + +def test_initiate_pin_reset(activated_account, + cache_preferences, + celery_session_worker, + cached_ussd_session, + guardian_account, + init_cache, + init_database, + load_ussd_menu, + mock_notifier_api, + set_locale_files): + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['data'] = {'guarded_account_phone_number': activated_account.phone_number} + state_machine_data = ('', ussd_session, guardian_account, init_database) + initiate_pin_reset(state_machine_data) + blockchain_address = activated_account.blockchain_address + preferred_language = get_cached_preferred_language(blockchain_address) + message = translation_for('sms.pin_reset_initiated', preferred_language, pin_initiator=guardian_account.standard_metadata_id()) + assert mock_notifier_api.get('message') == message + assert mock_notifier_api.get('recipient') == activated_account.phone_number + diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_sms_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_sms_logic.py index 9d35ce29..a8f23edd 100644 --- a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_sms_logic.py +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_sms_logic.py @@ -23,6 +23,7 @@ def test_upsell_unregistered_recipient(activated_account, load_support_phone, mock_notifier_api, set_locale_files, + set_active_token, valid_recipient): cached_ussd_session.set_data('recipient_phone_number', valid_recipient.phone_number) state_machine_data = ('', cached_ussd_session.to_json(), activated_account, init_database) diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_tokens_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_tokens_logic.py new file mode 100644 index 00000000..ac351267 --- /dev/null +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_tokens_logic.py @@ -0,0 +1,69 @@ +# standard imports +import json + +# external imports +from cic_types.condiments import MetadataPointer + +# local imports +from cic_ussd.cache import cache_data_key, get_cached_data +from cic_ussd.state_machine.logic.tokens import (is_valid_token_selection, + process_token_selection, + set_selected_active_token) +from cic_ussd.account.tokens import get_cached_token_data_list + + +# test imports + + +def test_is_valid_token_selection(activated_account, + cache_token_data_list, + cache_token_symbol_list, + cached_ussd_session, + init_cache, + init_database): + cached_token_data_list = get_cached_token_data_list(activated_account.blockchain_address) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['data'] = {'account_tokens_list': cached_token_data_list} + state_machine_data = ('GFT', ussd_session, activated_account, init_database) + assert is_valid_token_selection(state_machine_data) is True + state_machine_data = ('1', ussd_session, activated_account, init_database) + assert is_valid_token_selection(state_machine_data) is True + state_machine_data = ('3', ussd_session, activated_account, init_database) + assert is_valid_token_selection(state_machine_data) is False + + +def test_process_token_selection(activated_account, + cache_token_data_list, + cache_token_symbol_list, + cached_ussd_session, + celery_session_worker, + init_cache, + init_database): + cached_token_data_list = get_cached_token_data_list(activated_account.blockchain_address) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['data'] = {'account_tokens_list': cached_token_data_list} + state_machine_data = ('GFT', ussd_session, activated_account, init_database) + process_token_selection(state_machine_data) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + assert ussd_session.get('data').get('selected_token').get('symbol') == 'GFT' + + +def test_set_selected_active_token(activated_account, + cache_token_data_list, + cache_token_symbol_list, + cached_ussd_session, + init_cache, + init_database): + cached_token_data_list = get_cached_token_data_list(activated_account.blockchain_address) + ussd_session = get_cached_data(cached_ussd_session.external_session_id) + ussd_session = json.loads(ussd_session) + ussd_session['data'] = {'selected_token': cached_token_data_list[0]} + state_machine_data = ('GFT', ussd_session, activated_account, init_database) + set_selected_active_token(state_machine_data) + identifier = bytes.fromhex(activated_account.blockchain_address) + key = cache_data_key(identifier=identifier, salt=MetadataPointer.TOKEN_ACTIVE) + active_token = get_cached_data(key) + assert active_token == 'GFT' diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_transaction_logic.py b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_transaction_logic.py index d7b894fd..360e5bf7 100644 --- a/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_transaction_logic.py +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/logic/test_transaction_logic.py @@ -3,13 +3,12 @@ import json # external imports import pytest -import requests_mock -from chainlib.hash import strip_0x # local imports +from cic_ussd.account.metadata import get_cached_preferred_language +from cic_ussd.account.tokens import get_active_token_symbol, get_cached_token_data from cic_ussd.account.transaction import to_wei from cic_ussd.cache import get_cached_data -from cic_ussd.metadata import PersonMetadata from cic_ussd.state_machine.logic.transaction import (is_valid_recipient, is_valid_transaction_amount, has_sufficient_balance, @@ -18,7 +17,6 @@ from cic_ussd.state_machine.logic.transaction import (is_valid_recipient, save_recipient_phone_to_session_data, save_transaction_amount_to_session_data) - # test imports @@ -49,17 +47,18 @@ def test_is_valid_transaction_amount(activated_account, amount, expected_result, ]) def test_has_sufficient_balance(activated_account, cache_balances, - cache_default_token_data, + cache_token_data, expected_result, generic_ussd_session, init_database, + set_active_token, value): state_machine_data = (value, generic_ussd_session, activated_account, init_database) assert has_sufficient_balance(state_machine_data=state_machine_data) == expected_result def test_process_transaction_request(activated_account, - cache_default_token_data, + cache_token_data, cached_ussd_session, celery_session_worker, init_cache, @@ -67,7 +66,12 @@ def test_process_transaction_request(activated_account, load_chain_spec, load_config, mock_transfer_api, + set_active_token, valid_recipient): + blockchain_address = activated_account.blockchain_address + token_symbol = get_active_token_symbol(blockchain_address) + token_data = get_cached_token_data(blockchain_address, token_symbol) + decimals = token_data.get("decimals") cached_ussd_session.set_data('recipient_phone_number', valid_recipient.phone_number) cached_ussd_session.set_data('transaction_amount', '50') ussd_session = get_cached_data(cached_ussd_session.external_session_id) @@ -76,7 +80,7 @@ def test_process_transaction_request(activated_account, process_transaction_request(state_machine_data) assert mock_transfer_api['from_address'] == activated_account.blockchain_address assert mock_transfer_api['to_address'] == valid_recipient.blockchain_address - assert mock_transfer_api['value'] == to_wei(50) + assert mock_transfer_api['value'] == to_wei(decimals, 50) assert mock_transfer_api['token_symbol'] == load_config.get('TEST_TOKEN_SYMBOL') diff --git a/apps/cic-ussd/tests/cic_ussd/state_machine/test_state_machine.py b/apps/cic-ussd/tests/cic_ussd/state_machine/test_state_machine.py index ee853735..18c368d5 100644 --- a/apps/cic-ussd/tests/cic_ussd/state_machine/test_state_machine.py +++ b/apps/cic-ussd/tests/cic_ussd/state_machine/test_state_machine.py @@ -6,8 +6,10 @@ def test_state_machine(activated_account_ussd_session, celery_session_worker, init_database, init_state_machine, - pending_account): + load_languages, + pending_account, + set_locale_files): state_machine = UssdStateMachine(activated_account_ussd_session) state_machine.scan_data(('1', activated_account_ussd_session, pending_account, init_database)) assert state_machine.__repr__() == f'' - assert state_machine.state == 'initial_pin_entry' + assert state_machine.state == 'account_creation_prompt' diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py index a0d4676d..9f8a4d49 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_callback_handler.py @@ -4,15 +4,18 @@ import json # external imports import celery import pytest +import requests_mock from chainlib.hash import strip_0x from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.statement import filter_statement_transactions +from cic_ussd.account.tokens import collate_token_metadata from cic_ussd.account.transaction import transaction_actors from cic_ussd.cache import cache_data_key, get_cached_data from cic_ussd.db.models.account import Account from cic_ussd.error import AccountCreationDataNotFound +from cic_ussd.metadata import TokenMetadata # test imports @@ -22,11 +25,13 @@ from tests.helpers.accounts import blockchain_address def test_account_creation_callback(account_creation_data, cache_account_creation_data, celery_session_worker, + cache_default_token_data, custom_metadata, init_cache, init_database, load_chain_spec, mocker, + preferences, setup_metadata_request_handler, setup_metadata_signer): phone_number = account_creation_data.get('phone_number') @@ -48,10 +53,12 @@ def test_account_creation_callback(account_creation_data, cached_account_creation_data = get_cached_data(task_uuid) cached_account_creation_data = json.loads(cached_account_creation_data) assert cached_account_creation_data.get('status') == account_creation_data.get('status') + mock_add_preferences_metadata = mocker.patch('cic_ussd.tasks.metadata.add_preferences_metadata.apply_async') mock_add_phone_pointer = mocker.patch('cic_ussd.tasks.metadata.add_phone_pointer.apply_async') mock_add_custom_metadata = mocker.patch('cic_ussd.tasks.metadata.add_custom_metadata.apply_async') + preferred_language = preferences.get('preferred_language') s_account_creation_callback = celery.signature( - 'cic_ussd.tasks.callback_handler.account_creation_callback', [result, '', 0] + 'cic_ussd.tasks.callback_handler.account_creation_callback', [result, preferred_language, 0] ) s_account_creation_callback.apply_async().get() account = init_database.query(Account).filter_by(phone_number=phone_number).first() @@ -59,6 +66,7 @@ def test_account_creation_callback(account_creation_data, cached_account_creation_data = get_cached_data(task_uuid) cached_account_creation_data = json.loads(cached_account_creation_data) assert cached_account_creation_data.get('status') == 'CREATED' + mock_add_preferences_metadata.assert_called_with((result, preferences), {}, queue='cic-ussd') mock_add_phone_pointer.assert_called_with((result, phone_number), {}, queue='cic-ussd') mock_add_custom_metadata.assert_called_with((result, custom_metadata), {}, queue='cic-ussd') @@ -117,12 +125,46 @@ def test_statement_callback(activated_account, mocker, transactions_list): (activated_account.blockchain_address, sender_transaction), {}, queue='cic-ussd') +def test_token_data_callback(activated_account, + cache_token_data, + cache_token_meta_symbol, + cache_token_proof_symbol, + celery_session_worker, + default_token_data, + init_cache, + token_meta_symbol, + token_symbol): + blockchain_address = activated_account.blockchain_address + identifier = token_symbol.encode('utf-8') + status_code = 1 + with pytest.raises(ValueError) as error: + s_token_data_callback = celery.signature( + 'cic_ussd.tasks.callback_handler.token_data_callback', + [[default_token_data], blockchain_address, status_code]) + s_token_data_callback.apply_async().get() + assert str(error.value) == f'Unexpected status code: {status_code}.' + + token_data_key = cache_data_key([bytes.fromhex(blockchain_address), identifier], MetadataPointer.TOKEN_DATA) + token_meta_key = cache_data_key(identifier, MetadataPointer.TOKEN_META_SYMBOL) + token_info_key = cache_data_key(identifier, MetadataPointer.TOKEN_PROOF_SYMBOL) + token_meta = get_cached_data(token_meta_key) + token_meta = json.loads(token_meta) + token_info = get_cached_data(token_info_key) + token_info = json.loads(token_info) + token_data = collate_token_metadata(token_info=token_info, token_metadata=token_meta) + token_data = {**token_data, **default_token_data} + cached_token_data = json.loads(get_cached_data(token_data_key)) + for key, value in token_data.items(): + assert token_data[key] == cached_token_data[key] + + def test_transaction_balances_callback(activated_account, balances, cache_balances, - cache_default_token_data, + cache_token_data, cache_person_metadata, cache_preferences, + celery_session_worker, load_chain_spec, mocker, preferences, @@ -157,7 +199,16 @@ def test_transaction_balances_callback(activated_account, mocked_chain.assert_called() -def test_transaction_callback(load_chain_spec, mock_async_balance_api_query, transaction_result): +def test_transaction_callback(cache_token_data, + celery_session_worker, + default_token_data, + init_cache, + load_chain_spec, + mock_async_balance_api_query, + token_symbol, + token_meta_symbol, + token_proof_symbol, + transaction_result): status_code = 1 with pytest.raises(ValueError) as error: s_transaction_callback = celery.signature( @@ -166,13 +217,19 @@ def test_transaction_callback(load_chain_spec, mock_async_balance_api_query, tra s_transaction_callback.apply_async().get() assert str(error.value) == f'Unexpected status code: {status_code}.' - status_code = 0 - s_transaction_callback = celery.signature( - 'cic_ussd.tasks.callback_handler.transaction_callback', - [transaction_result, 'transfer', status_code]) - s_transaction_callback.apply_async().get() - recipient_transaction, sender_transaction = transaction_actors(transaction_result) - assert mock_async_balance_api_query.get('address') == recipient_transaction.get('blockchain_address') or sender_transaction.get('blockchain_address') - assert mock_async_balance_api_query.get('token_symbol') == recipient_transaction.get('token_symbol') or sender_transaction.get('token_symbol') + with requests_mock.Mocker(real_http=False) as request_mocker: + identifier = token_symbol.encode('utf-8') + metadata_client = TokenMetadata(identifier, cic_type=MetadataPointer.TOKEN_META_SYMBOL) + request_mocker.register_uri('GET', metadata_client.url, json=token_meta_symbol, status_code=200, reason='OK') + metadata_client = TokenMetadata(identifier, cic_type=MetadataPointer.TOKEN_PROOF_SYMBOL) + request_mocker.register_uri('GET', metadata_client.url, json=token_proof_symbol, status_code=200, reason='OK') + status_code = 0 + s_transaction_callback = celery.signature( + 'cic_ussd.tasks.callback_handler.transaction_callback', + [transaction_result, 'transfer', status_code]) + s_transaction_callback.apply_async().get() + recipient_transaction, sender_transaction = transaction_actors(transaction_result) + assert mock_async_balance_api_query.get('address') == recipient_transaction.get('blockchain_address') or sender_transaction.get('blockchain_address') + assert mock_async_balance_api_query.get('token_symbol') == recipient_transaction.get('token_symbol') or sender_transaction.get('token_symbol') diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_notifications_tasks.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_notifications_tasks.py index cb554d8b..6d6d86a5 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_notifications_tasks.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_notifications_tasks.py @@ -14,13 +14,14 @@ from cic_ussd.translation import translation_for def test_transaction(cache_default_token_data, + cache_token_data, celery_session_worker, load_support_phone, mock_notifier_api, notification_data, set_locale_files): notification_data['transaction_type'] = 'transfer' - amount = from_wei(notification_data.get('token_value')) + amount = from_wei(6, notification_data.get('token_value')) balance = notification_data.get('available_balance') phone_number = notification_data.get('phone_number') preferred_language = notification_data.get('preferred_language') diff --git a/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py b/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py index fcfdef4a..9733d45a 100644 --- a/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py +++ b/apps/cic-ussd/tests/cic_ussd/tasks/test_processor_tasks.py @@ -52,6 +52,11 @@ def test_cache_statement(activated_account, cached_statement = get_cached_data(key) cached_statement = json.loads(cached_statement) assert len(cached_statement) == 1 + + sender_transaction['token_value'] = 60.0 + s_parse_transaction = celery.signature( + 'cic_ussd.tasks.processor.parse_transaction', [sender_transaction]) + result = s_parse_transaction.apply_async().get() s_cache_statement = celery.signature( 'cic_ussd.tasks.processor.cache_statement', [result, activated_account.blockchain_address] ) diff --git a/apps/cic-ussd/tests/cic_ussd/test_notifications.py b/apps/cic-ussd/tests/cic_ussd/test_notifications.py index bfd0b1f6..9e407b8c 100644 --- a/apps/cic-ussd/tests/cic_ussd/test_notifications.py +++ b/apps/cic-ussd/tests/cic_ussd/test_notifications.py @@ -8,8 +8,8 @@ from cic_ussd.notifications import Notifier @pytest.mark.parametrize("key, preferred_language, recipient, expected_message", [ - ("ussd.kenya.exit", "en", "+254712345678", "END Thank you for using the service."), - ("ussd.kenya.exit", "sw", "+254712345678", "END Asante kwa kutumia huduma.") + ("ussd.exit", "en", "+254712345678", "END Thank you for using the service."), + ("ussd.exit", "sw", "+254712345678", "END Asante kwa kutumia huduma.") ]) def test_send_sms_notification(celery_session_worker, expected_message, diff --git a/apps/cic-ussd/tests/cic_ussd/test_translation.py b/apps/cic-ussd/tests/cic_ussd/test_translation.py index a6448dbb..322883b1 100644 --- a/apps/cic-ussd/tests/cic_ussd/test_translation.py +++ b/apps/cic-ussd/tests/cic_ussd/test_translation.py @@ -10,11 +10,11 @@ from cic_ussd.translation import translation_for def test_translation_for(set_locale_files): english_translation = translation_for( - key='ussd.kenya.exit_invalid_request', + key='ussd.exit_invalid_request', preferred_language='en' ) swahili_translation = translation_for( - key='ussd.kenya.exit_invalid_request', + key='ussd.exit_invalid_request', preferred_language='sw' ) assert swahili_translation == 'END Chaguo si sahihi.' diff --git a/apps/cic-ussd/tests/fixtures/account.py b/apps/cic-ussd/tests/fixtures/account.py index 5873e0a4..ab652362 100644 --- a/apps/cic-ussd/tests/fixtures/account.py +++ b/apps/cic-ussd/tests/fixtures/account.py @@ -8,6 +8,7 @@ from cic_types.condiments import MetadataPointer # local imports from cic_ussd.account.chain import Chain +from cic_ussd.account.tokens import set_active_token from cic_ussd.cache import cache_data, cache_data_key from cic_ussd.db.enum import AccountStatus from cic_ussd.db.models.account import Account @@ -36,6 +37,16 @@ def activated_account(init_database, set_fernet_key): return account +@pytest.fixture(scope='function') +def guardian_account(init_database, set_fernet_key): + account = Account(blockchain_address(), phone_number()) + account.create_password('0000') + account.activate_account() + init_database.add(account) + init_database.commit() + return account + + @pytest.fixture(scope='function') def balances(): return [{ @@ -53,13 +64,22 @@ def cache_account_creation_data(init_cache, account_creation_data): @pytest.fixture(scope='function') -def cache_balances(activated_account, balances, init_cache): - identifier = bytes.fromhex(activated_account.blockchain_address) +def cache_balances(activated_account, balances, init_cache, token_symbol): + identifier = [bytes.fromhex(activated_account.blockchain_address), token_symbol.encode('utf-8')] balances = json.dumps(balances[0]) key = cache_data_key(identifier, MetadataPointer.BALANCES) cache_data(key, balances) +@pytest.fixture(scope='function') +def cache_adjusted_balances(activated_account, balances, init_cache, token_symbol): + identifier = bytes.fromhex(activated_account.blockchain_address) + balances_identifier = [identifier, token_symbol.encode('utf-8')] + key = cache_data_key(balances_identifier, MetadataPointer.BALANCES_ADJUSTED) + adjusted_balance = 45931650.64654012 + cache_data(key, adjusted_balance) + + @pytest.fixture(scope='function') def cache_default_token_data(default_token_data, init_cache, load_chain_spec): chain_str = Chain.spec.__str__() @@ -68,6 +88,113 @@ def cache_default_token_data(default_token_data, init_cache, load_chain_spec): cache_data(key, data) +@pytest.fixture(scope='function') +def set_active_token(activated_account, init_cache, token_symbol): + identifier = bytes.fromhex(activated_account.blockchain_address) + key = cache_data_key(identifier, MetadataPointer.TOKEN_ACTIVE) + cache_data(key=key, data=token_symbol) + + +@pytest.fixture(scope='function') +def cache_token_data(activated_account, init_cache, token_data): + identifier = [bytes.fromhex(activated_account.blockchain_address), token_data.get('symbol').encode('utf-8')] + key = cache_data_key(identifier, MetadataPointer.TOKEN_DATA) + cache_data(key=key, data=json.dumps(token_data)) + + +@pytest.fixture(scope='function') +def cache_token_symbol_list(activated_account, init_cache, token_symbol): + identifier = bytes.fromhex(activated_account.blockchain_address) + key = cache_data_key(identifier=identifier, salt=MetadataPointer.TOKEN_SYMBOLS_LIST) + token_symbols_list = [token_symbol] + cache_data(key, json.dumps(token_symbols_list)) + + +@pytest.fixture(scope='function') +def cache_token_data_list(activated_account, init_cache, token_data): + identifier = bytes.fromhex(activated_account.blockchain_address) + key = cache_data_key(identifier, MetadataPointer.TOKEN_DATA_LIST) + token_data_list = [token_data] + cache_data(key, json.dumps(token_data_list)) + + +@pytest.fixture(scope='function') +def token_meta_symbol(): + return { + "contact": { + "phone": "+254700000000", + "email": "info@grassrootseconomics.org" + }, + "country_code": "KE", + "location": "Kilifi", + "name": "GRASSROOTS ECONOMICS" + } + + +@pytest.fixture(scope='function') +def token_proof_symbol(): + return { + "description": "Community support", + "issuer": "Grassroots Economics", + "namespace": "ge", + "proofs": [ + "0x4746540000000000000000000000000000000000000000000000000000000000", + "1f0f0e3e9db80eeaba22a9d4598e454be885855d6048545546fd488bb709dc2f" + ], + "version": 0 + } + + +@pytest.fixture(scope='function') +def token_list_entries(): + return [ + { + 'name': 'Fee', + 'symbol': 'FII', + 'issuer': 'Foo', + 'contact': {'phone': '+254712345678'}, + 'location': 'Fum', + 'balance': 50.0 + }, + { + 'name': 'Giftable Token', + 'symbol': 'GFT', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org' + }, + 'location': 'Fum', + 'balance': 60.0 + }, + { + 'name': 'Demurrage Token', + 'symbol': 'DET', + 'issuer': 'Grassroots Economics', + 'contact': { + 'phone': '+254700000000', + 'email': 'info@grassrootseconomics.org' + }, + 'location': 'Fum', + 'balance': 49.99 + } + ] + + +@pytest.fixture(scope='function') +def cache_token_meta_symbol(token_meta_symbol, token_symbol): + identifier = token_symbol.encode('utf-8') + key = cache_data_key(identifier, MetadataPointer.TOKEN_META_SYMBOL) + cache_data(key, json.dumps(token_meta_symbol)) + + +@pytest.fixture(scope='function') +def cache_token_proof_symbol(token_proof_symbol, token_symbol): + identifier = token_symbol.encode('utf-8') + key = cache_data_key(identifier, MetadataPointer.TOKEN_PROOF_SYMBOL) + cache_data(key, json.dumps(token_proof_symbol)) + + @pytest.fixture(scope='function') def cache_person_metadata(activated_account, init_cache, person_metadata): identifier = bytes.fromhex(activated_account.blockchain_address) @@ -100,10 +227,33 @@ def custom_metadata(): @pytest.fixture(scope='function') def default_token_data(token_symbol): return { - 'symbol': token_symbol, - 'address': blockchain_address(), - 'name': 'Giftable', - 'decimals': 6 + 'symbol': token_symbol, + 'address': '32e860c2a0645d1b7b005273696905f5d6dc5d05', + 'name': 'Giftable Token', + 'decimals': 6, + "converters": [] + } + + +@pytest.fixture(scope='function') +def token_data(): + return { + "description": "Community support", + "issuer": "Grassroots Economics", + "location": "Kilifi", + "contact": { + "phone": "+254700000000", + "email": "info@grassrootseconomics.org" + }, + "decimals": 6, + "name": "Giftable Token", + "symbol": "GFT", + "address": "32e860c2a0645d1b7b005273696905f5d6dc5d05", + "proofs": [ + "0x4746540000000000000000000000000000000000000000000000000000000000", + "1f0f0e3e9db80eeaba22a9d4598e454be885855d6048545546fd488bb709dc2f" + ], + "converters": [] } diff --git a/apps/cic-ussd/tests/fixtures/cache.py b/apps/cic-ussd/tests/fixtures/cache.py index f522689d..edb13e1e 100644 --- a/apps/cic-ussd/tests/fixtures/cache.py +++ b/apps/cic-ussd/tests/fixtures/cache.py @@ -2,14 +2,18 @@ # external imports import pytest +from pytest_redis import factories # local imports from cic_ussd.cache import Cache from cic_ussd.session.ussd_session import UssdSession +redis_test_proc = factories.redis_proc() +redis_db = factories.redisdb('redis_test_proc', decode=True) + @pytest.fixture(scope='function') -def init_cache(redisdb): - Cache.store = redisdb - UssdSession.store = redisdb - return redisdb +def init_cache(redis_db): + Cache.store = redis_db + UssdSession.store = redis_db + return redis_db diff --git a/apps/cic-ussd/tests/fixtures/config.py b/apps/cic-ussd/tests/fixtures/config.py index eab100d8..67ac6187 100644 --- a/apps/cic-ussd/tests/fixtures/config.py +++ b/apps/cic-ussd/tests/fixtures/config.py @@ -10,11 +10,13 @@ from confini import Config # local imports from cic_ussd.account.chain import Chain +from cic_ussd.account.guardianship import Guardianship from cic_ussd.encoder import PasswordEncoder from cic_ussd.files.local_files import create_local_file_data_stores, json_file_parser from cic_ussd.menu.ussd_menu import UssdMenu from cic_ussd.phone_number import E164Format, Support from cic_ussd.state_machine import UssdStateMachine +from cic_ussd.translation import generate_locale_files, Languages from cic_ussd.validator import validate_presence logg = logging.getLogger(__name__) @@ -39,6 +41,14 @@ def init_state_machine(load_config): UssdStateMachine.transitions = json_file_parser(filepath=load_config.get('MACHINE_TRANSITIONS')) +@pytest.fixture(scope='function') +def load_languages(init_cache, load_config): + validate_presence(load_config.get('LANGUAGES_FILE')) + Languages.load_languages_dict(load_config.get('LANGUAGES_FILE')) + languages = Languages() + languages.cache_system_languages() + + @pytest.fixture(scope='function') def load_chain_spec(load_config): chain_spec = ChainSpec.from_chain_str(load_config.get('CHAIN_SPEC')) @@ -75,8 +85,23 @@ def set_fernet_key(load_config): PasswordEncoder.set_key(load_config.get('APP_PASSWORD_PEPPER')) -@pytest.fixture -def set_locale_files(load_config): - validate_presence(load_config.get('LOCALE_PATH')) - i18n.load_path.append(load_config.get('LOCALE_PATH')) +@pytest.fixture(scope='function') +def setup_guardianship(load_config): + guardians_file = os.path.join(root_directory, load_config.get('SYSTEM_GUARDIANS_FILE')) + validate_presence(guardians_file) + Guardianship.load_system_guardians(guardians_file) + + +@pytest.fixture(scope="session") +def set_locale_files(load_config, tmpdir_factory): + tmpdir = tmpdir_factory.mktemp("var") + tmpdir_path = str(tmpdir) + validate_presence(tmpdir_path) + import cic_translations + package_path = cic_translations.__path__ + schema_files = os.path.join(package_path[0], load_config.get("SCHEMA_FILE_PATH")) + generate_locale_files(locale_dir=tmpdir_path, + schema_file_path=schema_files, + translation_builder_path=load_config.get('LOCALE_FILE_BUILDERS')) + i18n.load_path.append(tmpdir_path) i18n.set('fallback', load_config.get('LOCALE_FALLBACK')) diff --git a/apps/cic-ussd/tests/fixtures/transaction.py b/apps/cic-ussd/tests/fixtures/transaction.py index dbdfe901..8e83d227 100644 --- a/apps/cic-ussd/tests/fixtures/transaction.py +++ b/apps/cic-ussd/tests/fixtures/transaction.py @@ -40,6 +40,7 @@ def statement(activated_account): 'blockchain_address': activated_account.blockchain_address, 'token_symbol': 'GFT', 'token_value': 25000000, + 'token_decimals': 6, 'role': 'sender', 'action_tag': 'Sent', 'direction_tag': 'To', @@ -63,7 +64,7 @@ def transaction_result(activated_account, load_config, valid_recipient): 'destination_token_symbol': load_config.get('TEST_TOKEN_SYMBOL'), 'source_token_decimals': 6, 'destination_token_decimals': 6, - 'chain': 'evm:bloxberg:8996' + 'chain': load_config.get('CHAIN_SPEC') } diff --git a/apps/cic-ussd/transitions/language_setting_transitions.json b/apps/cic-ussd/transitions/language_setting_transitions.json index 4ba0fa3a..bba4f496 100644 --- a/apps/cic-ussd/transitions/language_setting_transitions.json +++ b/apps/cic-ussd/transitions/language_setting_transitions.json @@ -1,21 +1,142 @@ [ { "trigger": "scan_data", - "source": "select_preferred_language", + "source": "initial_language_selection", + "dest": "account_creation_prompt", + "after": "cic_ussd.state_machine.logic.account.process_account_creation", + "conditions": "cic_ussd.state_machine.logic.language.is_valid_language_selection" + }, + { + "trigger": "scan_data", + "source": "initial_language_selection", + "dest": "initial_middle_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_eleven_selected" + }, + { + "trigger": "scan_data", + "source": "initial_language_selection", "dest": "exit", - "after": "cic_ussd.state_machine.logic.account.change_preferred_language", - "conditions": "cic_ussd.state_machine.logic.menu.menu_one_selected" + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "initial_middle_language_set", + "dest": "initial_language_selection", + "conditions": "cic_ussd.state_machine.logic.menu.menu_twenty_two_selected" + }, + { + "trigger": "scan_data", + "source": "initial_middle_language_set", + "dest": "initial_last_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_eleven_selected" + }, + { + "trigger": "scan_data", + "source": "initial_middle_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "initial_last_language_set", + "dest": "initial_middle_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_twenty_two_selected" + }, + { + "trigger": "scan_data", + "source": "initial_last_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "initial_middle_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "initial_language_selection", + "dest": "exit_invalid_menu_option" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "exit_invalid_menu_option" + }, + { + "trigger": "scan_data", + "source": "last_language_set", + "dest": "exit_invalid_menu_option" }, { "trigger": "scan_data", "source": "select_preferred_language", "dest": "exit", - "after": "cic_ussd.state_machine.logic.account.change_preferred_language", - "conditions": "cic_ussd.state_machine.logic.menu.menu_two_selected" + "after": "cic_ussd.state_machine.logic.language.change_preferred_language", + "conditions": "cic_ussd.state_machine.logic.language.is_valid_language_selection" + }, + { + "trigger": "scan_data", + "source": "select_preferred_language", + "dest": "middle_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_eleven_selected" + }, + { + "trigger": "scan_data", + "source": "select_preferred_language", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "select_preferred_language", + "conditions": "cic_ussd.state_machine.logic.menu.menu_twenty_two_selected" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "last_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_eleven_selected" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "last_language_set", + "dest": "middle_language_set", + "conditions": "cic_ussd.state_machine.logic.menu.menu_twenty_two_selected" + }, + { + "trigger": "scan_data", + "source": "last_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "exit", + "conditions": "cic_ussd.state_machine.logic.menu.menu_zero_zero_selected" }, { "trigger": "scan_data", "source": "select_preferred_language", "dest": "exit_invalid_menu_option" + }, + { + "trigger": "scan_data", + "source": "middle_language_set", + "dest": "exit_invalid_menu_option" + }, + { + "trigger": "scan_data", + "source": "last_language_set", + "dest": "exit_invalid_menu_option" } ] \ No newline at end of file diff --git a/apps/cic-ussd/transitions/signup_transitions.json b/apps/cic-ussd/transitions/signup_transitions.json index a0b14049..982f6ecc 100644 --- a/apps/cic-ussd/transitions/signup_transitions.json +++ b/apps/cic-ussd/transitions/signup_transitions.json @@ -1,29 +1,4 @@ [ - { - "trigger": "scan_data", - "source": "initial_language_selection", - "dest": "initial_pin_entry", - "after": "cic_ussd.state_machine.logic.account.change_preferred_language", - "conditions": "cic_ussd.state_machine.logic.menu.menu_one_selected" - }, - { - "trigger": "scan_data", - "source": "initial_language_selection", - "dest": "initial_pin_entry", - "after": "cic_ussd.state_machine.logic.account.change_preferred_language", - "conditions": "cic_ussd.state_machine.logic.menu.menu_two_selected" - }, - { - "trigger": "scan_data", - "source": "initial_language_selection", - "dest": "help", - "conditions": "cic_ussd.state_machine.logic.menu.menu_three_selected" - }, - { - "trigger": "scan_data", - "source": "initial_language_selection", - "dest": "exit_invalid_menu_option" - }, { "trigger": "scan_data", "source": "initial_pin_entry", @@ -39,7 +14,6 @@ { "trigger": "scan_data", "source": "initial_pin_confirmation", - "unless": "cic_ussd.state_machine.logic.validator.has_cached_person_metadata", "conditions": "cic_ussd.state_machine.logic.pin.pins_match", "dest": "start", "after": [ diff --git a/apps/cic-ussd/var/lib/locale/helpers.en.yml b/apps/cic-ussd/var/lib/locale/helpers.en.yml deleted file mode 100644 index 70b344df..00000000 --- a/apps/cic-ussd/var/lib/locale/helpers.en.yml +++ /dev/null @@ -1,36 +0,0 @@ -en: - female: |- - Female - from: |- - From - male: |- - Male - not_provided: |- - Not provided - no_transaction_history: |- - No transaction history - no_tokens_list: |- - No tokens to list - other: |- - Other - received: |- - Received - sent: |- - Sent - to: |- - To - guardians_list_header: |- - Walinzi uliowaongeza ni: - no_guardians_list: |- - No guardians set - error: - no_phone_number_provided: |- - No phone number was provided. - no_matching_account: |- - The number provided is not registered. - is_initiator: |- - Phone number cannot be your own. - is_existent_guardian: |- - This phone number is is already added as a guardian. - is_not_existent_guardian: |- - Phone number not set as PIN reset guardian. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/locale/helpers.sw.yml b/apps/cic-ussd/var/lib/locale/helpers.sw.yml deleted file mode 100644 index cace30c9..00000000 --- a/apps/cic-ussd/var/lib/locale/helpers.sw.yml +++ /dev/null @@ -1,36 +0,0 @@ -sw: - female: |- - Mwanamke - from: |- - Kutoka kwa - male: |- - Mwanaume - not_provided: |- - Haijawekwa - no_transaction_history: |- - Hamna ripoti ya matumizi - no_tokens_list: |- - Hamna sarafu nyingine - other: |- - Nyingine - received: |- - Ulipokea - sent: |- - Ulituma - to: |- - Kwa - guardians_list_header: |- - Your set guardians are: - no_guardians_list: |- - Hamna walinzi walioongezwa - error: - no_phone_number_provided: |- - Namabari ya simu haijawekwa. - no_matching_account: |- - Nambari uliyoweka haijasajiliwa. - is_initiator: |- - Nambari yafaa kuwa tofauti na yako. - is_existent_guardian: |- - Namabari hii tayari imeongezwa kama mlinzi wa nambari ya siri. - is_not_existent_guardian: |- - Nambari hii haijaongezwa kama mlinzi wa nambari ya siri. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/locale/sms.en.yml b/apps/cic-ussd/var/lib/locale/sms.en.yml deleted file mode 100644 index 678fbb15..00000000 --- a/apps/cic-ussd/var/lib/locale/sms.en.yml +++ /dev/null @@ -1,11 +0,0 @@ -en: - account_successfully_created: |- - You have been registered on Sarafu Network! To use dial *384*96# on Safaricom and *483*96# on other networks. For help %{support_phone}. - received_tokens: |- - Successfully received %{amount} %{token_symbol} from %{tx_sender_information} %{timestamp} to %{tx_recipient_information}. New balance is %{balance} %{token_symbol}. - sent_tokens: |- - Successfully sent %{amount} %{token_symbol} to %{tx_recipient_information} %{timestamp} from %{tx_sender_information}. New balance is %{balance} %{token_symbol}. - terms: |- - By using the service, you agree to the terms and conditions at http://grassecon.org/tos - upsell_unregistered_recipient: |- - %{tx_sender_information} tried to send you %{token_symbol} but you are not registered. To use dial *384*96# on Safaricom and *483*96# on other networks. For help %{support_phone}. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/locale/sms.sw.yml b/apps/cic-ussd/var/lib/locale/sms.sw.yml deleted file mode 100644 index e2647cf2..00000000 --- a/apps/cic-ussd/var/lib/locale/sms.sw.yml +++ /dev/null @@ -1,11 +0,0 @@ -sw: - account_successfully_created: |- - Umesajiliwa kwa huduma ya Sarafu! Kutumia bonyeza *384*96# Safaricom ama *483*46# kwa utandao tofauti. Kwa Usaidizi %{support_phone}. - received_tokens: |- - Umepokea %{amount} %{token_symbol} kutoka kwa %{tx_sender_information} %{timestamp} ikapokewa na %{tx_recipient_information}. Salio lako ni %{balance} %{token_symbol}. - sent_tokens: |- - Umetuma %{amount} %{token_symbol} kwa %{tx_recipient_information} %{timestamp} kutoka kwa %{tx_sender_information}. Salio lako ni %{balance} %{token_symbol}. - terms: |- - Kwa kutumia hii huduma, umekubali sheria na masharti yafuatayo http://grassecon.org/tos - upsell_unregistered_recipient: |- - %{tx_sender_information} amejaribu kukutumia %{token_symbol} lakini hujasajili. Kutumia bonyeza *384*96# Safaricom ama *483*46# kwa utandao tofauti. Kwa Usaidizi %{support_phone}. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/locale/ussd.en.yml b/apps/cic-ussd/var/lib/locale/ussd.en.yml deleted file mode 100644 index 40985fed..00000000 --- a/apps/cic-ussd/var/lib/locale/ussd.en.yml +++ /dev/null @@ -1,317 +0,0 @@ -en: - kenya: - initial_language_selection: |- - CON Welcome to Sarafu Network - 1. English - 2. Kiswahili - 3. Help - initial_pin_entry: |- - CON Please enter a new four number PIN for your account. - initial_pin_confirmation: |- - CON Enter your four number PIN again - enter_given_name: |- - CON Enter first name - 0. Back - enter_family_name: |- - CON Enter family name - 0. Back - enter_date_of_birth: |- - CON Enter year of birth - 0. Back - enter_gender: |- - CON Enter gender - 1. Male - 2. Female - 3. Other - 0. Back - enter_location: |- - CON Enter your location - 0. Back - enter_products: |- - CON Please enter a product or service you offer - 0. Back - start: |- - CON Balance %{account_balance} %{account_token_name} - 1. Send - 2. My Sarafu - 3. My Account - 4. Help - enter_transaction_recipient: |- - CON Enter phone number - 0. Back - enter_transaction_amount: |- - CON Enter amount - 0. Back - first_account_tokens_set: |- - CON Choose a number or symbol from your balances: - %{first_account_tokens_set} - 11. Next - 00. Exit - middle_account_tokens_set: |- - CON Choose a number or symbol from your balances: - %{middle_account_tokens_set} - 11. Next - 22. Previous - 00. Exit - last_account_tokens_set: |- - CON Choose a number or symbol from your balances: - %{last_account_tokens_set} - 22. Previous - 00. Exit - token_selection_pin_authorization: - first: |- - CON %{token_data} - Enter pin to select: - retry: |- - %{retry_pin_entry} - account_management: |- - CON My account - 1. My profile - 2. Change language - 3. Check balance - 4. Check statement - 5. PIN options - 0. Back - metadata_management: |- - CON My profile - 1. Edit name - 2. Edit gender - 3. Edit age - 4. Edit location - 5. Edit products - 6. View my profile - 0. Back - display_user_metadata: |- - CON Your details are: - Name: %{full_name} - Gender: %{gender} - Age: %{age} - Location: %{location} - You sell: %{products} - 0. Back - select_preferred_language: |- - CON Choose language - 1. English - 2. Kiswahili - 0. Back - retry_pin_entry: |- - CON Incorrect PIN entered, please try again. You have %{remaining_attempts} attempts remaining. - 0. Back - pin_management: |- - CON Pin options - 1. Change PIN - 2. Reset PIN - 3. Guard PIN - 0. Back - enter_current_pin: - first: |- - CON Enter current PIN. - 0. Back - retry: |- - %{retry_pin_entry} - enter_new_pin: |- - CON Enter your new four number PIN - 0. Back - new_pin_confirmation: |- - CON Enter your new four number PIN again - 0. Back - reset_guarded_pin: |- - CON Enter phone number you are the guardian to reset their pin - 0. Back - reset_guarded_pin_authorization: - first: |- - CON Enter YOUR pin to confirm %{guarded_account_information}'s reset - 0. Back - retry: |- - %{retry_pin_entry} - exit_pin_reset_initiated_success: |- - CON Success: You have initiated a PIN reset for %{guarded_account_information} - 0. Back - 9. Exit - exit_not_authorized_for_pin_reset: |- - CON Failure: You are not authorized to reset that PIN. You must be a guardian! - 0. Back - 9. Exit - guard_pin: |- - CON Pin guard - 1. View guardians - 2. Add guardian - 3. Remove guardian - 0. Back - guardian_list_pin_authorization: - first: |- - CON Enter your pin to view set guardians - 0. Back - retry: |- - %{retry_pin_entry} - guardian_list: |- - CON %{guardians_list} - 0. Back - 9. Exit - add_guardian: |- - CON Enter phone number to add as pin reset guardian - 0. Back - add_guardian_pin_authorization: - first: |- - CON Enter your pin to add %{guardian_information} as your PIN reset guardian - 0. Back - retry: |- - %{retry_pin_entry} - exit_guardian_addition_success: |- - CON Success: %{guardian_information} can now reset your PIN - 0. Back - 9. Exit - exit_invalid_guardian_addition: |- - CON %{error_exit} - 0. Back - 9. Exit - remove_guardian: |- - CON Enter phone number to revoke guardianship: - 0. Back - remove_guardian_pin_authorization: - first: |- - CON Enter your pin to remove %{guardian_information} as your PIN reset guardian - 0. Back - retry: |- - %{retry_pin_entry} - exit_guardian_removal_success: |- - CON Success: %{guardian_information} PIN reset guardianship is revoked - 0. Back - 9. Exit - exit_invalid_guardian_removal: |- - CON %{error_exit} - 0. Back - 9. Exit - transaction_pin_authorization: - first: |- - CON %{recipient_information} will receive %{transaction_amount} %{token_symbol} from %{sender_information}. - Please enter your PIN to confirm. - 0. Back - retry: |- - %{retry_pin_entry} - display_metadata_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - account_balances_pin_authorization: - first: |- - CON Please enter your PIN to view balances - 0. Back - retry: |- - %{retry_pin_entry} - account_statement_pin_authorization: - first: |- - CON Please enter your PIN to view statement - 0. Back - retry: |- - %{retry_pin_entry} - name_edit_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - dob_edit_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - gender_edit_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - location_edit_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - products_edit_pin_authorization: - first: |- - CON Please enter your PIN - 0. Back - retry: |- - %{retry_pin_entry} - account_balances: - available_balance: |- - CON Your balances are as follows: - balance: %{available_balance} %{token_symbol} - 0. Back - with_fees: |- - CON Your balances are as follows: - balances: %{available_balance} %{token_symbol} - fees: %{tax} %{token_symbol} - 0. Back - with_rewards: |- - CON Your balances are as follows: - balance: %{available_balance} %{token_symbol} - fees: %{tax} %{token_symbol} - rewards: %{bonus} %{token_symbol} - 0. Back - first_transaction_set: |- - CON %{first_transaction_set} - 1. Next - 00. Exit - middle_transaction_set: |- - CON %{middle_transaction_set} - 1. Next - 2. Previous - 00. Exit - last_transaction_set: |- - CON %{last_transaction_set} - 2. Previous - 00. Exit - exit: |- - END Thank you for using the service. - exit_invalid_request: |- - END Invalid request. - exit_invalid_menu_option: |- - CON Invalid menu option. For help, call %{support_phone}. - 00. Back - 99. Exit - exit_invalid_input: |- - CON Invalid input. Nothing selected - 00. Back - 99. Exit - exit_pin_blocked: |- - END Your PIN has been blocked. For help, please call %{support_phone}. - exit_invalid_pin: |- - END The PIN you have entered is invalid. PIN must consist of 4 digits. For help, call %{support_phone}. - exit_invalid_new_pin: |- - END The PIN you have entered is invalid. PIN must be different from your current PIN. For help, call %{support_phone}. - exit_pin_mismatch: |- - END The new PIN does not match the one you entered. Please try again. For help, call %{support_phone}. - exit_invalid_recipient: |- - CON Recipient's phone number is not registered or is invalid: - 00. Retry - 99. Exit - exit_successful_transaction: |- - CON Your request has been sent. %{recipient_information} will receive %{transaction_amount} %{token_symbol} from %{sender_information}. - 00. Back - 99. Exit - exit_insufficient_balance: |- - CON Payment of %{amount} %{token_symbol} to %{recipient_information} has failed due to insufficent balance. - Your Sarafu-Network balances is: %{token_balance} - 00. Back - 99. Exit - exit_successful_token_selection: |- - CON Success! %{token_symbol} is your active Sarafu. - 00. Back - 99. Exit - invalid_service_code: |- - Please dial %{valid_service_code} to access Sarafu Network - help: |- - CON For assistance call %{support_phone} - 00. Back - 99. Exit - complete: |- - CON Your request has been sent. You will receive an SMS shortly. - 00. Back - 99. Exit - account_creation_prompt: |- - END Your account is being created. You will receive an SMS when your account is ready. diff --git a/apps/cic-ussd/var/lib/locale/ussd.sw.yml b/apps/cic-ussd/var/lib/locale/ussd.sw.yml deleted file mode 100644 index 89bf9e71..00000000 --- a/apps/cic-ussd/var/lib/locale/ussd.sw.yml +++ /dev/null @@ -1,316 +0,0 @@ -sw: - kenya: - initial_language_selection: |- - CON Karibu Sarafu Network - 1. English - 2. Kiswahili - 3. Help - initial_pin_entry: |- - CON Tafadhali weka pin mpya yenye nambari nne kwa akaunti yako - initial_pin_confirmation: |- - CON Weka PIN yako tena - enter_given_name: |- - CON Weka jina lako la kwanza - enter_family_name: |- - CON Weka jina lako la mwisho - 0. Nyuma - enter_date_of_birth: |- - CON Weka mwaka wa kuzaliwa - 0. Nyuma - enter_gender: |- - CON Weka jinsia yako - 1. Mwanaume - 2. Mwanamke - 3. Nyngine - 0. Nyuma - enter_location: |- - CON Weka eneo lako - 0. Nyuma - enter_products: |- - CON Weka bidhaa ama huduma unauza - 0. Nyuma - start: |- - CON Salio %{account_balance} %{account_token_name} - 1. Tuma - 2. Sarafu yangu - 3. Akaunti yangu - 4. Usaidizi - enter_transaction_recipient: |- - CON Weka nambari ya simu - 0. Nyuma - enter_transaction_amount: |- - CON Weka kiwango - 0. Nyuma - first_account_tokens_set: |- - CON Chagua nambari au ishara kutoka kwa salio zako: - %{first_account_tokens_set} - 11. Mbele - 00. Ondoka - middle_account_tokens_set: |- - CON Chagua nambari au ishara kutoka kwa salio zako: - %{middle_account_tokens_set} - 11. Mbele - 22. Nyuma - 00. Ondoka - last_account_tokens_set: |- - CON Chagua nambari au ishara kutoka kwa salio zako: - %{last_account_tokens_set} - 22. Nyuma - 00. Ondoka - token_selection_pin_authorization: - first: |- - CON %{token_data} - Weka nambari ya siri kuchagua: - retry: |- - %{retry_pin_entry} - account_management: |- - CON Akaunti yangu - 1. Wasifu wangu - 2. Chagua lugha utakayotumia - 3. Angalia salio - 4. Angalia taarifa ya matumizi - 5. Mipangilio ya nambari ya siri - 0. Nyuma - metadata_management: |- - CON Wasifu wangu - 1. Weka jina - 2. Weka jinsia - 3. Weka umri - 4. Weka eneo - 5. Weka bidhaa - 6. Angalia wasifu wako - 0. Nyuma - display_user_metadata: |- - CON Wasifu wako una maelezo yafuatayo: - Jina: %{full_name} - Jinsia: %{gender} - Umri: %{age} - Eneo: %{location} - Unauza: %{products} - 0. Nyuma - select_preferred_language: |- - CON Chagua lugha - 1. Kingereza - 2. Kiswahili - 0. Nyuma - retry_pin_entry: |- - CON Nambari uliyoweka si sahihi, jaribu tena. Una majaribio %{remaining_attempts} yaliyobaki. - 0. Nyuma - pin_management: |- - CON Pin options - 1. Badilisha nambari yangu ya siri - 2. Tuma ombili la kubadilisha nambari ya siri - 3. Linda nambari ya siri - 0. Nyuma - enter_current_pin: - first: |- - CON Weka nambari ya siri. - 0. Nyuma - retry: |- - %{retry_pin_entry} - enter_new_pin: |- - CON Weka nambari ya siri mpya - 0. Nyuma - new_pin_confirmation: |- - CON Weka nambari yako ya siri tena - 0. Nyuma - reset_guarded_pin: |- - CON Weka nambari ya simu ili kutuma ombi la kubalisha nambari ya siri. - 0. Nyuma - reset_guarded_pin_authorization: - first: |- - CON Weka nambari YAKO ya siri ili kudhibitisha ombi la kubadilisha nambari ya siri ya %{guarded_account_information}. - 0. Nyuma - retry: |- - %{retry_pin_entry} - exit_pin_reset_initiated_success: |- - CON Ombi lako la kubadili nambari ya siri ya %{guarded_account_information} limetumwa. - 0. Nyuma - 9. Ondoka - exit_not_authorized_for_pin_reset: |- - CON Huruhusiwi kutuma ombi la kubadilisha nambari ya siri. - 0. Nyuma - 9. Ondoka - guard_pin: |- - CON Linda nambari ya siri - 1. Walinzi wa namabari ya siri - 2. Ongeza mlinzi - 3. Ondoa mlinzi - 0. Nyuma - guardian_list_pin_authorization: - first: |- - CON Weka nambari yako ya siri ili kuona walinzi uliowaongeza - 0. Nyuma - retry: |- - %{retry_pin_entry} - guardian_list: |- - CON %{guardians_list} - 0. Nyuma - 9. Ondoka - add_guardian: |- - CON Weka nambari ya simu ili kuongeza mlinzi - 0. Nyuma - add_guardian_pin_authorization: - first: |- - CON Weka nambari YAKO ya siri ili kumwongeza %{guardian_information} kama mlinzi - 0. Nyuma - retry: |- - %{retry_pin_entry} - exit_guardian_addition_success: |- - CON Ombi lako la kumwongeza: %{guardian_information} kama mlinzi limefanikiwa - 0. Nyuma - 9. Ondoka - exit_invalid_guardian_addition: |- - CON %{error_exit} - 0. Nyuma - 9. Ondoka - remove_guardian: |- - CON Weka nambari ya simu ili kuondoa mlinzi - 0. Nyuma - remove_guardian_pin_authorization: - first: |- - CON Weka nambari YAKO ya siri ili kumwondoa %{guardian_information} kama mlinzi - 0. Nyuma - retry: |- - %{retry_pin_entry} - exit_guardian_removal_success: |- - CON Ombi lako la kumwondoa: %{guardian_information} kama mlinzi limefanikiwa - 0. Nyuma - 9. Ondoka - exit_invalid_guardian_removal: |- - CON %{error_exit} - 0. Nyuma - 9. Ondoka - transaction_pin_authorization: - first: |- - CON %{recipient_information} atapokea %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. - Tafadhali weka nambari yako ya siri kudhibitisha. - 0. Nyuma - retry: |- - %{retry_pin_entry} - display_metadata_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - account_balances_pin_authorization: - first: |- - CON Tafadhali weka PIN yako kuona salio. - 0. Nyuma - retry: |- - %{retry_pin_entry} - account_statement_pin_authorization: - first: |- - CON Tafadhali weka PIN yako kuona taarifa ya matumizi. - 0. Nyuma - retry: |- - %{retry_pin_entry} - name_edit_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - dob_edit_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - gender_edit_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - location_edit_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - products_edit_pin_authorization: - first: |- - CON Tafadhali weka PIN yako - 0. Nyuma - retry: |- - %{retry_pin_entry} - account_balances: - available_balance: |- - CON Salio zako ni zifuatazo: - salio: %{available_balance} %{token_symbol} - 0. Nyuma - with_fees: |- - CON Salio zako ni zifuatazo: - salio: %{available_balance} %{token_symbol} - ushuru: %{tax} %{token_symbol} - 0. Nyuma - with_rewards: |- - CON Salio zako ni zifuatazo: - salio: %{available_balance} %{token_symbol} - ushuru: %{tax} %{token_symbol} - tuzo: %{bonus} %{token_symbol} - 0. Nyuma - first_transaction_set: |- - CON %{first_transaction_set} - 1. Mbele - 00. Ondoka - middle_transaction_set: |- - CON %{middle_transaction_set} - 1. Mbele - 2. Nyuma - 00. Ondoka - last_transaction_set: |- - CON %{last_transaction_set} - 2. Nyuma - 00. Ondoka - exit: |- - END Asante kwa kutumia huduma. - exit_invalid_request: |- - END Chaguo si sahihi. - exit_invalid_menu_option: |- - CON Chaguo lako sio sahihi. Kwa usaidizi piga simu %{support_phone} - 00. Nyuma - 99. Ondoka - exit_invalid_input: |- - CON Chaguo lako halipatikani. Hakuna kilichochaguliwa. - 00. Nyuma - 99. Ondoka - exit_pin_blocked: |- - END PIN yako imefungwa. Kwa usaidizi tafadhali piga simu %{support_phone}. - exit_invalid_pin: |- - END PIN uliyobonyeza sio sahihi. PIN lazima iwe na nambari nne. Kwa usaidizi piga simu %{support_phone}. - exit_invalid_new_pin: |- - END PIN uliyobonyeza sio sahihi. PIN lazima iwe tofauti na pin yako ya sasa. Kwa usaidizi piga simu %{support_phone}. - exit_pin_mismatch: |- - END PIN mpya na udhibitisho wa pin mpya hazilingani. Tafadhali jaribu tena. Kwa usaidizi piga simu %{support_phone}. - exit_invalid_recipient: |- - CON Mpokeaji wa nambari hapatikani au sio sahihi. - 00. Jaribu tena - 99. Ondoka - exit_successful_transaction: |- - CON Ombi lako limetumwa. %{recipient_information} atapokea %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. - 00. Nyuma - 99. Ondoka - exit_insufficient_balance: |- - CON Malipo ya %{amount} %{token_symbol} kwa %{recipient_information} halijakamilika kwa sababu salio lako haitoshi. - Akaunti yako ya Sarafu ina salio ifuatayo: %{token_balance} - 00. Nyuma - 99. Ondoka - exit_successful_token_selection: |- - CON Chaguo lako limekamilika, %{token_symbol} ni sarafu itakayotumika. - 00. Nyuma - 99. Ondoka - invalid_service_code: |- - Bonyeza %{valid_service_code} kutumia mtandao wa Sarafu - help: |- - CON Kwa usaidizi piga simu %{support_phone} - 0. Nyuma - 9. Ondoka - complete: |- - CON Ombi lako limetumwa. Utapokea uthibitishaji wa SMS kwa muda mfupi. - 00. Nyuma - 99. Ondoka - account_creation_prompt: |- - END Akaunti yako ya Sarafu inatayarishwa. Utapokea ujumbe wa SMS akaunti yako ikiwa tayari. diff --git a/apps/cic-ussd/var/lib/sys/guardians.txt b/apps/cic-ussd/var/lib/sys/guardians.txt new file mode 100644 index 00000000..8de77269 --- /dev/null +++ b/apps/cic-ussd/var/lib/sys/guardians.txt @@ -0,0 +1 @@ ++254700000000 \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/sys/helpers.csv b/apps/cic-ussd/var/lib/sys/helpers.csv new file mode 100644 index 00000000..5bea05ba --- /dev/null +++ b/apps/cic-ussd/var/lib/sys/helpers.csv @@ -0,0 +1,19 @@ +keys,en,sw +female,Female,Mwanamke +from,From,Kutoka kwa +male,Male,Mwanaume +not_provided,Not provided,Haijawekwa +no_language_list,No language list,Hamna lugha ya kuchagua +no_transaction_history,No transaction history,Hamna ripoti ya matumizi +no_tokens_list,No tokens to list,Hamna sarafu nyingine +other,Other,Nyingine +received,Received,Ulipokea +sent,Sent,Ulituma +to,To,Kwa +guardians_list_header,Your set guardians are:,Walinzi uliowaongeza ni: +no_guardians_list,No guardians set,Hamna walinzi walioongezwa +error.no_phone_number_provided,No phone number was provided.,Namabari ya simu haijawekwa. +error.no_matching_account,The number provided is not registered.,Nambari uliyoweka haijasajiliwa. +error.is_initiator,Phone number cannot be your own.,Nambari yafaa kuwa tofauti na yako. +error.is_existent_guardian,This phone number is is already added as a guardian.,Namabari hii tayari imeongezwa kama mlinzi wa nambari ya siri. +error.is_not_existent_guardian,Phone number not set as PIN reset guardian.,Nambari hii haijaongezwa kama mlinzi wa nambari ya siri. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/sys/languages.json b/apps/cic-ussd/var/lib/sys/languages.json new file mode 100644 index 00000000..ee5dd3c4 --- /dev/null +++ b/apps/cic-ussd/var/lib/sys/languages.json @@ -0,0 +1,9 @@ +{ + "en": "English", + "sw": "Kiswahili", + "kam": "Kamba", + "kik": "Kikiuyu", + "miji": "Mijikenda", + "luo": "Luo", + "bor": "Borana" +} diff --git a/apps/cic-ussd/var/lib/sys/sms.csv b/apps/cic-ussd/var/lib/sys/sms.csv new file mode 100644 index 00000000..1da02cae --- /dev/null +++ b/apps/cic-ussd/var/lib/sys/sms.csv @@ -0,0 +1,7 @@ +keys,en,sw +account_successfully_created,You have been registered on Sarafu Network! To use dial *384*96# on Safaricom and *483*96# on other networks. For help %{support_phone}.,Umesajiliwa kwa huduma ya Sarafu! Kutumia bonyeza *384*96# Safaricom ama *483*46# kwa utandao tofauti. Kwa Usaidizi %{support_phone}. +received_tokens,Successfully received %{amount} %{token_symbol} from %{tx_sender_information} %{timestamp} to %{tx_recipient_information}. New balance is %{balance} %{token_symbol}.,Umepokea %{amount} %{token_symbol} kutoka kwa %{tx_sender_information} %{timestamp} ikapokewa na %{tx_recipient_information}. Salio lako ni %{balance} %{token_symbol}. +sent_tokens,Successfully sent %{amount} %{token_symbol} to %{tx_recipient_information} %{timestamp} from %{tx_sender_information}. New balance is %{balance} %{token_symbol}.,Umetuma %{amount} %{token_symbol} kwa %{tx_recipient_information} %{timestamp} kutoka kwa %{tx_sender_information}. Salio lako ni %{balance} %{token_symbol}. +terms,"By using the service, you agree to the terms and conditions at http://grassecon.org/tos","Kwa kutumia hii huduma, umekubali sheria na masharti yafuatayo http://grassecon.org/tos" +upsell_unregistered_recipient,%{tx_sender_information} tried to send you %{token_symbol} but you are not registered. To use dial *384*96# on Safaricom and *483*96# on other networks. For help %{support_phone}.,%{tx_sender_information} amejaribu kukutumia %{token_symbol} lakini hujasajili. Kutumia bonyeza *384*96# Safaricom ama *483*46# kwa utandao tofauti. Kwa Usaidizi %{support_phone}. +pin_reset_initiated,%{pin_initiator} has sent a request to initiate your PIN reset.,%{pin_initiator} ametuma ombi la kubadilisha PIN yako. \ No newline at end of file diff --git a/apps/cic-ussd/var/lib/sys/ussd.csv b/apps/cic-ussd/var/lib/sys/ussd.csv new file mode 100644 index 00000000..606f3efe --- /dev/null +++ b/apps/cic-ussd/var/lib/sys/ussd.csv @@ -0,0 +1,866 @@ +keys,en,sw,kam,kik,miji,luo,bor +initial_language_selection,"CON Welcome to Sarafu Network +%{first_language_set} + +11. Next +00. Exit","CON Karibu Sarafu Network +%{first_language_set} + +11. Mbele +00. Ondoka","CON Kalivu Network ya Sarafu +1. English +2. Kiswahili +3. Kikamba +3. Help","CON Karibu Sarafu Network +1. Githungu +2. Githweri +3. Uteithio","CON Karibu Sarafu Network +1. Chizungu +2. Chiswahili +3. Avizwa","CON Machiegni e network mar Sarafu +1. Dho Ngere +2. Dho oswayo +3. Kony","CON Karibu Sarafu Network +1. Afaan ferenji +2. Afaan kiswahili +3. Qarqars" +initial_pin_entry,CON Please enter a new four number PIN for your account.,CON Tafadhali weka pin mpya yenye nambari nne kwa akaunti yako,CON Tafadhali ikia pin yumbya ila ina namba inya kinanduni chaku,CON Ekera namba yaku ya thiri njeru ena numba enna.,CON Ika piniyo ya namba Ne kwa akaunti Yakwako.,CON Kiyie to ket namba ni mopondo e akaont ni.,CON Tafadhal pin hareti kekhae ka namba afuri fulaa akaunti kake +initial_pin_confirmation,CON Enter your four number PIN again,CON Weka PIN yako tena,CON Ikia PIN yaku ingi,CON Ekera namba yaku ya thiri ringi,CON Uyira Kwika pin kaheri.,CON Ket namba ni mopondo kendo,CON Mar dibii pin kekhae +enter_given_name,"CON Enter first name +0. Back",CON Weka jina lako la kwanza,CON Ikia isyitwa yaku ya mbee,CON Ekera retwa rwaku ria mbere,CON Ika dzinaro rakwanza.,CON Ket nyingi mokwongo,CON Makhaa kake ka karaa kor +enter_family_name,"CON Enter family name +0. Back","CON Weka jina lako la mwisho +0. Rudi","CON Ikia isyitwa yaku ya muthya +0. Syoka itina","CON Ekera ritwa rwaku ria mwisho +0. Coka thutha","CON Ika dzinaro ra mwisho +0. Uya Nyuma","CON Ket nyingi mogik. +0. Dog chien","CON Makhaa kake ka egee +0. Dhebii" +enter_date_of_birth,"CON Enter year of birth +0. Back","CON Weka mwaka wa kuzaliwa +0. Rudi","CON Ikia mwaka wa kusyawa +0. Syoka itina","CON Ekera mwaka waku wa guciarwo +0. Coka thutha","CON Ika mwaka wakuvyalwa +0. Uya Nyuma","CON Ket iki mar nyuol +0. Dog chien","CON Gan kake ka athdalat kor +0. Dheebi" +enter_gender,"CON Enter gender +1. Male +2. Female +3. Other +0. Back","CON Weka jinsia yako +1. Mwanaume +2. Mwanamke +3. Nyngine +0. Rudi","CON Ikia gender yaku +1. Mundume +2. Mundumuka +3. Ingi +0. Syoka itina","CON We mudurume kana mutumia +1.Mudurume +2. Mutumia +3. Ingi +0. Coka thutha","CON Ika kala Umulume ama Umuche au vingine. +1. Mulume +2. Muche +3. Vinjine +0. Uya Nyuma","CON Ket kit chwech mari +1. Dichuo +2. Dhako +3. Moko +0. Dog chien","CON Athin Dir mo Dubr +1. Dir +2. Dubr +3. Ka dibii +0. Dheebi" +enter_location,"CON Enter your location, +0. Back","CON Weka eneo lako +0. Rudi","CON Ikia utui waku kana location +0. Syoka itina","CON Ekera kuria uumete +0. Coka thutha","CON Ika enero wombolako. +0. Uya nyuma","CON Ket kumaidake +0. Dog chien","CON Fulaa athin kubat kor +0. Dhebii" +enter_products,"CON Please enter a product or service you offer +0. Back","CON Weka bidhaa ama huduma unauza +0. Rudi","CON Ikia syindu kana huduma ila unenganae +0. Syoka itina","CON Ekera indo kana wira uria urendia +0. Coka thutha","CON Ika Viya ama utu uhendao +0. Uya Nyuma","CON Ket gima iuso kata tich mitimo +0. Dog chien","CON Waan gurgurt okan namaa kenit khes khae +0. Dheebi" +start,"CON Balance %{account_balance} %{account_token_name} +1. Send +2. My Sarafu +3. My Account +4. Help","CON Salio %{account_balance} %{account_token_name} +1. Tuma +2. Sarafu yangu +3. Akaunti yangu +4. Usaidizi","CON Mbalansi kana utyalyo %{account_balance} %{account_token_name} +1. Tuma +2. Kinandu chakwa +3. Utethyo","CON Matigari %{account_balance} %{account_token_name} +1. Tuma +2. Akaunti yaku +3. Uteithio","CON Sazo %{account_balance} %{account_token_name} +1. Huma +2. Akaunti yangu +3. Avizwa","CON Ma Odong' %{account_balance} %{account_token_name} +1. Or +2. Akaont na +3. Kony","CON Salio %{account_balance} %{account_token_name} +1. Erg +2. Akaunti khiy +3. Qarqars" +enter_transaction_recipient,"CON Enter phone number +0. Back","CON Weka nambari ya simu +0. Rudi","CON Ikia namba ya simu +0. Syoka itina","CON Ikira namba ya thimu +0. Coka thutha","CON Ika namba yasimu. +0. Uya Nyuma","CON Ket nambani mar simu +0. Dog chien","CON Namba ta simuu kekhai +0. Dheebi" +enter_transaction_amount,"CON Enter amount +0. Back","CON Weka kiwango +0. Rudi","CON Ikia kiwango +0. Syoka itina","CON Ikira muigana +0. Coka thutha","CON Ika chaasi. +0. Uya nyuma","CON Ket giko mari +0. Dog chien","CON kiwango kekhai +0. Dheebi" +first_account_tokens_set,"CON Choose a number or symbol from your balances: +%{first_account_tokens_set} + +0. Back +11. Next +00. Exit","CON Chagua nambari au ishara kutoka kwa salio zako: +%{first_account_tokens_set} + +0. Rudi +11. Mbele +00. Ondoka","Sakua Sarafu: + %{token_list} +99. Thi mbee +00. Syoka itina","Shaghura Sarafu: + %{token_list} +99. Mbere +00. Coka thutha","Tsagula Sarafu: + %{token_list} +99. Enderera +00. Uya Nyuma","Yier Sarafu: + %{token_list} +99. Nyime +00. Dog chien","Chaqui Sarafu: + %{token_list} +99. Dhuur +00. Dheebi" +middle_account_tokens_set,"CON Choose a number or symbol from your balances: +%{middle_account_tokens_set} +11. Next +22. Previous +00. Exit","CON Chagua nambari au ishara kutoka kwa salio zako: +%{middle_account_tokens_set} +11. Mbele +22. Rudi +00. Ondoka","Sakura Sarafu: + %{token_list} +99. Thi mbee +00. Syoka itina","Shaghura Sarafu: + %{token_list} +99. Mbere +00. Cooka thutha","Tsagula Sarafu: + %{token_list} +99. Enderera +00. Uya Nyuma","Yier Sarafu: + %{token_list} +99. Nyime +00. Dog chien","Chaqui Sarafu: + %{token_list} +99. Dhuur +00. Dheebi" +last_account_tokens_set,"CON Choose a number or symbol from your balances: +%{last_account_tokens_set} +22. Previous +00. Exit","CON Chagua nambari au ishara kutoka kwa salio zako: +%{last_account_tokens_set} +22. Rudi +00. Ondoka","Sakura sarafu: + %{token_list} +00. Syoka itina","Sarafu: + %{token_list} +00. Cooka thutha","Tsagula Sarafu: + %{token_list} +00. Uya Nyuma","Yier Sarafu: + %{token_list} +00. Dog chien","Chagua Sarafu: + %{token_list} +00. Dheebi" +token_selection_pin_authorization.first,"CON %{token_data} +Enter pin to select:","CON %{token_data} +Weka nambari ya siri kuchagua: +0. Back","%{token_info} +Sakua kwa kwikia pin yaku: +0. Syoka itina","%{token_info} +Ekera pin yaku gushaghura: +0. Cooka thutha","%{token_info} +Ika piniyo kutsagula Sarafu: +0. Uya Nyuma","%{token_info} +Ket pin ni iyier: +0. Dog chien","%{token_info} +Pin kake khai akh dibii chaguat +0. Dheebi" +token_selection_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +account_management,"CON My account +1. My profile +2. Change language +3. Check balance +4. Check statement +5. PIN options +0. Back","CON Akaunti yangu +1. Wasifu wangu +2. Chagua lugha utakayotumia +3. Angalia salio +4. Angalia taarifa ya matumizi +5. Mipangilio ya nambari ya siri +0. Rudi","CON Kinandu Chakwa +1. Profile/wasifu wakwa +2. Sakua luka ila ukatumiaa +3. Syisya Mbalansi yaku +4. Syisya welesyo wa utumii +5. Chenja namba yaku ya siri +0. Syoka itina","CON Akaunti yakwa +1. Maritwa makwa +2. Shaghura rothiumu ukuhuthira +3. Rora matigari +4. Rora rugano rwa mahuthira +5. Chengia namba ya thiri +0. Coka","CON Akaunti yangu +1. Malagizo Gangu +2. Tsagula Luga Undohumira +3. Lola Sazo +4. Lola tarifa Za Mahumizi +5. Galuza Namba Fitse +0. Uya Nyuma","CON Akaont na +1. Nyanonro mara +2. Yier dhok midwatiyogo +3. Ngi midong go +4. N'gi chal mar akaont +5. Lok nambani mopondo +0. Dog chien","CON Akaunti khiy +1. Wasifu wangu +2. Afaan dubaad chaqui +3. laali balansi +4. Angalia taarifa ya matumizi +5. Gargarch namba +0. Dheebi" +metadata_management,"CON My profile +1. Edit name +2. Edit gender +3. Edit age +4. Edit location +5. Edit products +6. View my profile +0. Back","CON Wasifu wangu +1. Weka jina +2. Weka jinsia +3. Weka umri +4. Weka eneo +5. Weka bidhaa +6. Angalia wasifu wako +0. Rudi","CON Profile/Wasifu wakwa +1. Ikia isyitwa +2. Ikia jinsia/gender yaku +3. Ikia miaka yaku +4. Ikia utui waku +5. Ikia syindu ila utesaa +6. Sisya profile/wasifu waku +0. Syoka itina","CON Maondu maku +1. Ekera ritwa +2. Ekera kana we mundurume kana mutumia +3. Ekera miaka yaku +4. Ekera kuria uikaraga +5. Ikira kiria uendagia +6. Rora maundu maku +0. Coka thutha","CON Malagizo Gangu +1. Ika dzinaro +2. Ika kala umulume ama Umuche +3. Ika umuri +4. Ika eneo +5. Ika Miyo ama viya uguzavyo +6. Lola malagizo Gangu +0. Uya nyuma","CON Wasifu wangu +1. Ket nyingi +2. Ket kit chuech mari +3. Ket iki +4. Ket kumaidake +5. Ket gikmaiuso +6. Ng'i nyanonro mara +0. Dog chien","CON Wasifu wangu +1. Maqa kekhai +2. Naam dira mo dubr +3. Gan kekhai +4. Fulaa itgalt kai +5. Mih kai +6. Angalia wasifu wako +0. Dheebi" +display_user_metadata,"CON Your details are: +Name: %{full_name} +Gender: %{gender} +Age: %{age} +Location: %{location} +You sell: %{products} +0. Back","CON Wasifu wako una maelezo yafuatayo: + Jina: %{full_name} + Jinsia: %{gender} + Umri: %{age} + Eneo: %{location} + Unauza: %{products} +0. Rudi","CON Profile/Wasifu waku wina maelesyo aa: + Isyitwa: %{full_name} + Jinsia yaku/gender: %{gender} + Miaka yaku: %{age} + Utui/location yaku: %{location} + Syindu ila uta: %{products} +0. Syoka itina","CON Maundu maku mena rugano ruru: + Maretwa: %{full_name} + Mutumia kana muthuri: %{gender} + Miaka : %{age} + Kuria uikaraga : %{location} + Kiria uendagia : %{products} +0. Coka thutha","CON Malagizo gako gana moro uthuwizirao: + Dzina: %{full_name} + Umuche ama Mulume: %{gender} + Umuri: %{age} + Umbolako: %{location} + Miyo uguzayo: %{products} +0. Uya nyuma","CON Nyanonro mari en: + Nying: %{full_name} + Kit chuech: %{gender} + Iga: %{age} + Kumidake: %{location} + Gima iuso: %{products} +0. Dog chien","CON Wasifu wako una maelezo yafuatayo: + JinaMakha: %{full_name} + Jinsia: %{gender} + Gan: %{age} + Fulaa : %{location} + Maan gurgurt: %{products} +0. Dheebi" +select_preferred_language,"CON Choose language: +%{first_language_set} + +0. Back +11. Next +00. Exit","CON Chagua lugha: +%{first_language_set} + +0. Rudi +11. Mbele +00. Ondoka","CON Sakua luka +1. Kisungu +2. Kiswahili +3. Kikamba +0. Syoka itina","CON Caghura ruthiomi +1. Githungu +2. Githweri +0. Coka","CON Tsagula Luga +1. Kizungu +2. Kiswahili +0. Uya nyuma","CON Yier dhok +1. Dho Ngere +2. Dho Oswayo +0. Dog chien","CON Chagua lugha +1. Afaan ferenji +2. Afaan kiswahili +0. Dheebi" +retry_pin_entry,"CON Incorrect PIN entered,please try again. You have %{remaining_attempts} attempts remaining. +0. Back","CON Nambari uliyoweka si sahihi, jaribu tena. Una majaribio %{remaining_attempts} yaliyobaki. +0. Rudi","CON Namba ila wekiya iyaile, tata kwikia ingi. Tata mala %{remaining_attempts} nimo matyele. +0. Itina","CON Namba uikirite ti njega, geria ringi.Ni maita %{remaining_attempts} matigarete. +0. Gucoka thutha","CON Nambari fitse urioika seyo, jeza kaheri. Usere Majezo %{remaining_attempts} Gaserego. +0. Uya nyuma","CON Namba miketo oknikare, tem kendo. Idong gi temo di %{remaining_attempts} modong. +0. Chien","CON Namba at keket suninit,laal amalle.Nafaas kaitdheebit %tanaataf +0. Dheebi" +pin_management,"CON Pin options +1. Change PIN +2. Reset PIN +3. Guard PIN +0. Back","CON Pin options +1. Badilisha nambari yangu ya siri +2. Tuma ombili la kubadilisha nambari ya siri +3. Linda nambari ya siri +0. Rudi",,,,, +enter_current_pin.first,"CON Enter current PIN. +0. Back","CON Weka nambari ya siri. +0. Rudi","CON Ikia namba yaku ya siri. +0. Syoka itina","CON Ekera namba ya thiri +0. Coka thutha","CON Ika namba fitse. +0. Uya Nyuma","CON Ket nambani mopondo. +0. Dog chien","CON Namba ka namii imben kekhai +0. Dheebi" +enter_current_pin.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +enter_new_pin,"CON Enter your new four number PIN +0. Back","CON Weka nambari ya siri mpya +0. Rudi","CON Ikia namba yaku ya siri ila yumbya +0. Syoka itina","CON Ekera namba njeru ya thiri +0. coka thutha","CON Ika namba fitse mbisha +0. Uya nyuma","CON Ket namba mopondo maanyien. +0. Dog chien","CON Namba hareti ka namii imben kekhai +0. Dheebi" +new_pin_confirmation,"CON Enter your new four number PIN again +0. Back","CON Weka nambari yako ya siri tena +0. Rudi","CON Ikia namba yaku ya siri ingi +0. Syoka itina","CON Ekera namba yaku ya thiri renge +0. Coka thutha","CON Uyira kuika lwaphiri +0. Uya Nyuma","CON Ket nambani mopondo kendo +0. Dog chien","CON Namba hareti ka namii imben kekhai amalle +0. Dheebi" +reset_guarded_pin,"CON Enter phone number you are the guardian to reset their pin +0. Back","CON Weka nambari ya simu ili kutuma ombi la kubalisha nambari ya siri. +0. Rudi",,,,, +reset_guarded_pin_authorization.first,"CON Enter YOUR pin to confirm %{guarded_account_information}'s reset +0. Back","CON Weka nambari YAKO ya siri ili kudhibitisha ombi la kubadilisha nambari ya siri ya %{guarded_account_information}. +0. Rudi",,,,, +reset_guarded_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},,,,, +exit_pin_reset_initiated_success,"CON Success: You have initiated a PIN reset for %{guarded_account_information} +0. Back +9. Exit","CON Ombi lako la kubadili nambari ya siri ya %{guarded_account_information} limetumwa. +0. Rudi +9. Ondoka",,,,, +exit_not_authorized_for_pin_reset,"CON Failure: You are not authorized to reset that PIN. You must be a guardian! +0. Back +9. Exit","CON Huruhusiwi kutuma ombi la kubadilisha nambari ya siri. +0. Rudi +9. Ondoka",,,,, +guard_pin,"CON Pin guard +1. View guardians +2. Add guardian +3. Remove guardian +0. Back","CON Linda nambari ya siri +1. Walinzi wa namabari ya siri +2. Ongeza mlinzi +3. Ondoa mlinzi +0. Rudi",,,,, +guardian_list_pin_authorization.first,"CON Enter your pin to view set guardians +0. Back","CON Weka nambari yako ya siri ili kuona walinzi uliowaongeza +0. Rudi",,,,, +guardian_list_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},,,,, +guardian_list,"CON %{guardians_list} +0. Back +9. Exit","CON %{guardians_list} +0. Rudi +9. Ondoka",,,,, +add_guardian,"CON Enter phone number to add as pin reset guardian +0. Back","CON Weka nambari ya simu ili kuongeza mlinzi +0. Rudi",,,,, +add_guardian_pin_authorization.first,"CON Enter your pin to add %{guardian_information} as your PIN reset guardian +0. Back","CON Weka nambari YAKO ya siri ili kumwongeza %{guardian_information} kama mlinzi +0. Rudi",,,,, +add_guardian_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},,,,, +exit_guardian_addition_success,"CON Success: %{guardian_information} can now reset your PIN +0. Back +9. Exit","CON Ombi lako la kumwongeza: %{guardian_information} kama mlinzi limefanikiwa +0. Rudi +9. Ondoka",,,,, +exit_invalid_guardian_addition,"CON %{error_exit} +0. Back +9. Exit","CON %{error_exit} +0. Rudi +9. Ondoka",,,,, +remove_guardian,"CON Enter phone number to revoke guardianship: +0. Back","CON Weka nambari ya simu ili kuondoa mlinzi +0. Rudi",,,,, +remove_guardian_pin_authorization.first,"CON Enter your pin to remove %{guardian_information} as your PIN reset guardian +0. Back","CON Weka nambari YAKO ya siri ili kumwondoa %{guardian_information} kama mlinzi +0. Rudi",,,,, +remove_guardian_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},,,,, +exit_guardian_removal_success,"CON Success: %{guardian_information} PIN reset guardianship is revoked +0. Back +9. Exit","CON Ombi lako la kumwondoa: %{guardian_information} kama mlinzi limefanikiwa +0. Rudi +9. Ondoka",,,,, +exit_invalid_guardian_removal,"CON %{error_exit} +0. Back +9. Exit","CON %{error_exit} +0. Rudi +9. Ondoka",,,,, +transaction_pin_authorization.first,"CON %{recipient_information} will receive %{transaction_amount} %{token_symbol} from %{sender_information}. +Please enter your PIN to confirm. +0. Back","CON %{recipient_information} atapokea %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. +Tafadhali weka nambari yako ya siri kudhibitisha. +0. Rudi","CON %{recipient_information} nukwata %{transaction_amount} %{token_symbol} kuma kwa %{sender_information}. +Tafadhali ikia namba yaku ya siri kuvitukithya. +0. Syoka itina","CON %{recipient_information} akuamukira %{transaction_amount} %{token_symbol} kuuma kwa %{sender_information}. +Ekera namba yaku ya thiri kuetekeria. +0. Coka thutha","CON %{recipient_information} atapokea %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. +Unavoywa kuika nambayo fitse kugeluza. +0. Uya nyuma.","CON %{recipient_information} dhiyudo %{transaction_amount} %{token_symbol} kowuok kuom %{sender_information}. +Kiyie to ket nambani mopondo mondo iyie: +0. Dog chien","CON %{recipient_information} in argad%{transaction_amount} %{token_symbol} ir %{sender_information}. +Namba ka namii imben kekhai +0. Dheebi" +transaction_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +display_metadata_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera pin yaku +0. coka thutha","CON Unavoywa kuika namayo fitswe +0. Uya Nyuma","CON Kiyie to ket nambani mopondo +0. Dog chien","CON Namba ka namii imben kekhai +0. Dheebi" +display_metadata_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +account_balances_pin_authorization.first,"CON Please enter your PIN to view balances +0. Back","CON Tafadhali weka PIN yako kuona salio. +0. Rudi","CON Tafadhali ikia PIN yaku kwona utyalo. +0. Syoka itina","CON Ekera pin yaku kuona matigari maku +0. Coka ","CON Unavoywa namba fitswe kulola Sazo. +0. Uya nyuma","CON Kiyie to ket nambani mopondo mondo ine modong' +0. Dog chien","CON Namba ka namii imbeen kekhai ak balansi kake lalt +0. Dheebi" +account_balances_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +account_statement_pin_authorization.first,"CON Please enter your PIN to view statement +0. Back","CON Tafadhali weka PIN yako kuona taarifa ya matumizi. +0. Rudi","CON Tafadhali ikia PIN yaku kwona welesyo wa utumii. +0. Syoka itina","CON Ekera pin yaku kuona rugano rwa mahuthira maku +0. coka thutha","CON Unavoywa namba fitswe kupata maerezo ga mahumizi Gako. +0. Uya Nyuma","CON Kiyie to ket nambani mar siri mondo ine chenro mar tiyo. +0. Dog chien","CON Tafadhali weka PIN yako kuona taarifa ya matumizi. +0. Dheebi" +account_statement_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +name_edit_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera pin yaku +0. coka thutha","CON Unavoywa namba fitse +0. Uya Nyuma","CON Ket nambani mopondo +0. Dog chien","CON Namba ka namii imben kekhai +0. Dheebi" +name_edit_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +dob_edit_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera namba yaku ya thiri +0. Rudi","CON Unavoywa namba fitswe +0. Uya nyuma","CON Kiyie to ket nambani mopondo +0. Dog chien","CON Namba kake ka namii imbeen kekhai +0. Dheebi" +dob_edit_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +gender_edit_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera namba yaku ya thiri +0. coka thutha","CON Unavoywa namba fitswe +0. Uya nyuma","CON Kiyie to ket nambani mopondo +0. Dog chien","CON Namba kake ka namii imbeen kekhai +0. Dheebi" +gender_edit_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +location_edit_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera namba yaku ya thiri +0. Coka thutha","CON Unavoywa namba fitswe +0. Uya nyuma","CON Kiyie to ket nambani mopondo +0. Dog chien","CON Namba kake ka namii imbeen kekhai +0. Dheebi" +location_edit_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +products_edit_pin_authorization.first,"CON Please enter your PIN +0. Back","CON Tafadhali weka PIN yako +0. Rudi","CON Tafadhali ikia PIN yaku +0. Syoka itina","CON Ekera namba yaku ya thiri +0. Coka thutha","CON Unavoywa namba fitswe +0. Uya nyuma","CON Kiyie to ket nambani mopondo +0. Dog chien","CON Namba kake ka namii imbeen kekhai +0. Dheebi" +products_edit_pin_authorization.retry,%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry},%{retry_pin_entry} +account_balances.available_balance,"CON Your balances are as follows: + %{available_balance} %{token_symbol} +0. Back","CON Salio zako ni zifuatazo: + %{available_balance} %{token_symbol} +0. Rudi","CON Utyalo waku ni uu: + utyalo: %{available_balance} %{token_symbol} +0. Syoka itina","CON Matigari maku ni maya: + matigari: %{available_balance} %{token_symbol} +0. coka","CON Masazogo nidzavivyo gatuwiravyo: + Sazo: %{available_balance} %{token_symbol} +0. Uya Nyuma","CON Dong mari en: + Dong: %{available_balance} %{token_symbol} +0. Dog chien","CON Balansi kake akan + salio: %{available_balance} %{token_symbol} +0. Dheebi" +account_balances.with_fees,"CON Your balances are as follows: +balances: %{available_balance} %{token_symbol} +fees: %{tax} %{token_symbol} +0. Back","CON Salio zako ni zifuatazo: + salio: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} +0. Rudi","CON Utyalo waku ni uu: + utyalo: %{available_balance} %{token_symbol} + tax/ushuru: %{tax} %{token_symbol} +0. Syoka itina","CON Matigari maku ni maya: + matigari: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} +0. coka thutha","CON Masazogo ni gatuwirago: + Masazo: %{available_balance} %{token_symbol} + Ushuuru: %{tax} %{token_symbol} +0. Uya nyuma","CON Dong mari en: + Dong: %{available_balance} %{token_symbol} + osuru: %{tax} %{token_symbol} +0. Dog chien","CON Balansi kake akan + salio: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} +0. Dheebi" +account_balances.with_rewards,"CON Your balances are as follows: +balance: %{available_balance} %{token_symbol} +fees: %{tax} %{token_symbol} +rewards: %{bonus} %{token_symbol} +0. Back","CON Salio zako ni zifuatazo: + salio: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} + tuzo: %{bonus} %{token_symbol} +0. Rudi","CON Utyalo waku ni uu: + Utyalo: %{available_balance} %{token_symbol} + Tax/ushuru: %{tax} %{token_symbol} + muthinzio: %{bonus} %{token_symbol} +0. Syoka itina","CON Salio zako ni zifuatazo: + salio: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} + tuzo: %{bonus} %{token_symbol} +0. coka thutha","CON Sazoro ni rituwiraro: + Sazo: %{available_balance} %{token_symbol} + Ushuuru: %{tax} %{token_symbol} + Zawadi: %{bonus} %{token_symbol} +0. Uya Nyuma","CON Dong mari en: + Dong: %{available_balance} %{token_symbol} + osuru: %{tax} %{token_symbol} + mich: %{bonus} %{token_symbol} +0. Dog chien","CON Balansi kake akan + salio: %{available_balance} %{token_symbol} + ushuru: %{tax} %{token_symbol} + tuzo: %{bonus} %{token_symbol} +0. Dheebi" +first_transaction_set,"CON %{first_transaction_set} + +0. Back +11. Next +00. Exit","CON %{first_transaction_set} + +0. Rudi +11. Mbele +00. Ondoka","CON %{first_transaction_set} +1. Mbee +00. Ondoka","CON %{first_transaction_set} +1. Mbere +00. uma","CON %{first_transaction_set} +1. Mbere +00. Uka","CON %{first_transaction_set} +1. Nyime +00. Wuogi","CON %{first_transaction_set} +1. Dhuur +00. Bai" +middle_transaction_set,"CON %{middle_transaction_set} + +11. Next +22. Previous +00. Exit","CON %{middle_transaction_set} + +11. Mbele +22. Rudi +00. Ondoka","CON %{middle_transaction_set} +1. Mbee +2. Itina +00. Ondoka","CON %{middle_transaction_set} +1. Mbere +2. coka thutha +00. Uma","CON %{middle_transaction_set} +1. Mbere +2. Uya nyuma +00. Uka","CON %{middle_transaction_set} +1. Nyime +2. Dog chien +00. Wuogi","CON %{middle_transaction_set} +1. Dhuur +2. Dheebi +00. Bai" +last_transaction_set,"CON %{last_transaction_set} + +22. Previous +00. Exit","CON %{last_transaction_set} + +22. Rudi +00. Ondoka","CON %{last_transaction_set} +2. Itina +00. Ondoka","CON %{last_transaction_set} +2. Coka thutha +00. Uma","CON %{last_transaction_set} +2. Uya Nyuma +00. Uka","CON %{last_transaction_set} +2. Dog chien +00. Wuogi","CON %{last_transaction_set} +2. Dhuur +00. Bai" +exit,END Thank you for using the service.,END Asante kwa kutumia huduma.,END Ni muvea kwa kutumia huduma ii.,END Thegio ni kuhuthira mutabo,END. Namvera kwa mahumizi ga ii huduma.,END Erokamano kuom tiyo kodwa.,END Asante kwa kutumia huduma. +exit_invalid_request,END Invalid request.,END Chaguo si sahihi.,END Usakuo waku uyaile.,END shaguro riaku ti riega,END. Tsagulo karisawa sawa,END Yiero okni kare,END Ka at chaquad suninit +exit_invalid_menu_option,"CON Invalid menu option. For help,call %{support_phone}. +00. Back +99. Exit","CON Chaguo lako sio sahihi. Kwa usaidizi piga simu %{support_phone} +00. Rudi +99. Ondoka","CON Usakuo waku uyaile. Kwa utethyo kuna simu %{support_phone} +00. Itina +99. Ondoka","CON Shaguro riaku ti riega.Kwa uteithio hura %{support_phone} +00. Coka thutha +99. Uma","CON Tsaguloro karisawa sawa. Kuavizwa piga %{support_phone} +00. Uya Nyuma +99. Uka","CON Yiero ni oknikare. Kuom kony go simu e %{support_phone} +00. Dog chien +99. Wuogi","CON Ka at chaqui suninit.qarqarsa simu dai%{support_phone} +00. Dheebi +99. Bai" +exit_invalid_input,"CON Invalid input. Nothing selected +00. Back +99. Exit","CON Chaguo lako halipatikani. Hakuna kilichochaguliwa. +00. Rudi +99. Ondoka","CON Usakuo waku wikwonekana.Vaii kindu kisakue. +00. Itina +99. Ondoka","CON Shaguro riaku ritironekana. Hatiri kindu washaghura. +00. Coka thutha +99. Uma","CON Tsaguloro karipatikana. Kakuna Kutsagurire chochosi. +00. Uya nyuma +99. Uka","CON Yiero ni okyudre. Onge gima iyiero. +00. Dog chien +99. Wuogi","CON Ka at chaguad injirt. oo +00. dheebi +99. Bai" +exit_pin_blocked,"END Your PIN has been blocked. For help, please call %{support_phone}.",END PIN yako imefungwa. Kwa usaidizi tafadhali piga simu %{support_phone}.,END PIN yaku niyavingwa. Kutethwa kuna simu ino %{support_phone}.,END PIN yaku niyahingwo. Kwa uteithio hura thimu %{support_phone}.,END. Namba fitse yakwako ifungwa. Kwa kuavizwa unaangwa upige simu %{support_phone}.,END Nambani mopondo olor. Kuom kony go simu e %{support_phone}.,END Pin kake yahidat. Qarqarsa simu dai %{support_phone}. +exit_invalid_pin,"END The PIN you have entered is invalid. PIN must consist of 4 digits. For help, call %{support_phone}.",END PIN uliyobonyeza sio sahihi. PIN lazima iwe na nambari nne. Kwa usaidizi piga simu %{support_phone}.,END PIN ila wekia iyaile. Ni lasima PIN ithiwe na namba inya. Kutethwa kuna namba ii %{support_phone}.,END PIN iria wekera tii njega. PIN nomoka ikorwo na namba inya. Kwa uteithio hura thimu %{support_phone}.,"END Namba fitse urohofya seyo, kaisawa. Namba fitswe inamalwa ikale na namba nee. Kwa kuvizwa, piga simu%{support_phone}.",END. Namba mopondo miketo oknikare. Nyaka obed gi nembni ang'wen. Kuom kony go simu e %{support_phone}.,END PIN ka at keket suninit. PIN Pin namba afuuri tatatu. Qarqarsa simu dai %{support_phone}. +exit_invalid_new_pin,"END The PIN you have entered is invalid. PIN must be different from your current PIN. For help, call %{support_phone}.",END PIN uliyobonyeza sio sahihi. PIN lazima iwe tofauti na pin yako ya sasa. Kwa usaidizi piga simu %{support_phone}.,END PIN ila wekia iyaile. PIN ni lasima ithiwe tofauti na pin yaku ya oyu. Kutethwa kuna namba ii %{support_phone}.,END PIN uria wekera ti njega. PIN nomohaka ikorwo na namba ndiganu na ya riu . Kwa uteithio hora thimu %{support_phone}.,END Namba fitswe uriohopya siyo ya karakara. Namba fitswe inahenzekana ikale itofauti na uhumirayo vivi. Kwa maavizo piga simu %{support_phone}.,END Namba mopondo miketo oknikare. Nyaka obed mopogore gi nambani mopondo masani. Kuom kony gochi e %{support_phone}.,END PIN ka at keket suninit.Pin kake walinfakaatin.Qarqars simu dai %{support_phone}. +exit_pin_mismatch,"END The new PIN does not match the one you entered. Please try again. For help, call %{support_phone}.",END PIN mpya na udhibitisho wa pin mpya hazilingani. Tafadhali jaribu tena. Kwa usaidizi piga simu %{support_phone}.,END PIN yumbya na uhakikisho wa pin yumbya syivwanene. Tafadhali tata ingi. Kutethwa kuna simu %{support_phone}.,END PIN njeru na pin ya guetekeria shitira hianana . Geria ringi. Kwa uteithio hora thimu %{support_phone}.,END Namba fitse uzdoinjiza kaikara kara na uriyohopya laphiri. Unavoywa ujeze kaheri. Kwa kuavizwa piga simu %{support_phone}.,GIKO. Namba mopondo miketo opogore gi manikuongo keto. Kiyie to ket kendo. Kuom kony gochi e %{support_phone}.,END PIN mpya na udhibitisho wa pin mpya hazilingani. Pin hareti ka at kekeet walinfakan. it dheebi amaale.Qarqars simu dai%{support_phone}. +exit_invalid_recipient,"CON Recipient's phone number is not registered or is invalid: +00. Retry +99. Exit","CON Mpokeaji wa nambari hapatikani au sio sahihi. +00. Jaribu tena +99. Ondoka","CON Mukwati wa namba ndokwatikana kana ii namba iyaile kana ti sahihi. +00. Tata ingi +99. Ondoka","CON Mpokeaji wa nambari hapatikani au sio sahihi.Namba ya mutumirwo ndiranyitikana kana ti njega +00. Geria ringi +99. Uma","CON Muphokezi wa namba kapatikana ama namba kai karakara. +00. Jeza Kaheri +99. Uka","CON Jayuto mar nambani okyudre kata oknikare. +00. Tem kendo +99. Wuogi","CON Mpokeaji wa nambari hapatikani au sio sahihi. +00. +99. Ondoka" +exit_successful_transaction,"CON Your request has been sent. %{recipient_information} will receive %{transaction_amount} %{token_symbol} from %{sender_information}. +00. Back +99. Exit","CON Ombi lako limetumwa. %{recipient_information} atapokea %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. +00. Rudi +99. Ondoka","CON Woni waku niwatumwa. %{recipient_information} nukupokea %{transaction_amount} %{token_symbol} kuma kwa %{sender_information}. +00. Itina +99. Ondoka","CON Mahoya maku nimatomwo. %{recipient_information} akuamukira%{transaction_amount} %{token_symbol} kuma kwa %{sender_information}. +00. Coka +99. Uma","CON Mavoyogo gahumwa. %{recipient_information} undaphokera %{transaction_amount} %{token_symbol} kuombola kwa %{sender_information}. +00. Uya Nyuma +99. Uka","CON Kwayo ni oseor. %{recipient_information} oboyudo %{transaction_amount} %{token_symbol} kowuok kuom %{sender_information}. +00. Dog chien +99. Wuogi","CON Qarqar kake yaergad. %{recipient_information} inargat %{transaction_amount} %{token_symbol} kutoka kwa %{sender_information}. +00. Dheebi +99. Bai" +exit_insufficient_balance,"CON Payment of %{amount} %{token_symbol} to %{recipient_information} has failed due to insufficient balance. +Your Sarafu-Network balances is: %{token_balance} +00. Back +99. Exit","CON Malipo ya %{amount} %{token_symbol} kwa %{recipient_information} halijakamilika kwa sababu salio lako haitoshi. +Akaunti yako ya Sarafu ina salio ifuatayo: %{token_balance} +00. Rudi +99. Ondoka","CON Ndivi ya %{amount} %{token_symbol} kwa %{recipient_information} inavitukithwa nundu utyalyo waku ni munini. +Kinandu chaku cha Sarafu kina utyalo uu: %{token_balance} +00. Itina +99. Ondoka","CON Marehi ma %{amount} %{token_symbol} kwa %{recipient_information} matinarekereka tondu matigari maku matiraigana. +Akaunti yako ya Sarafu ina salio ifuatayo: %{token_balance} +00. Coka +99. Uma","CON Maripho ga %{amount} %{token_symbol} kwa %{recipient_information} Karidzangwe kukamirika Kwaukala sazoro Karitosha. +Akauntiyo vivi ina sazo dza rituwiranavyo: %{token_balance} +00. Uya nyuma +99. Uka","CON Chudo mar %{amount} %{token_symbol} kuom %{recipient_information} okotieki nikech dong ni okrom. +Akaont ni mar Sarafu ni gi dong mar: %{token_balance} +00. Dog chien +99. Wuogi","CON Malipo ka%{amount} %{token_symbol} kwa %{recipient_information} Inkamilikee balansi kake ingau +Akaunti kake balansi akan kabd: %{token_balance} +00. Dheebi +99. Bai" +exit_successful_token_selection,"CON Success! %{token_symbol} is your active Sarafu. +00. Back +99. Exit","CON Chaguo lako limekamilika, %{token_symbol} ni sarafu itakayotumika. +00. Rudi +99. Ondoka",,,,, +invalid_service_code,Please dial %{valid_service_code} to access Sarafu Network,Bonyeza %{valid_service_code} kutumia mtandao wa Sarafu,Vinyia %{valid_service_code} kutumia mutandao wa Sarafu,Hihinya%{valid_service_code} kuhudhira mutabo wa Sarafu,Hofya %{valid_service_code} Kuhumira Mutandao wa sarafu,Dii %{valid_service_code} mondo iti gi Sarafu,Bonyeza %{valid_service_code} kutumia mtandao wa Sarafu +help,"CON For assistance call %{support_phone} +00. Back +99. Exit","CON Kwa usaidizi piga simu %{support_phone} +0. Rudi +9. Ondoka","CON Kwa utethyo kuna simu %{support_phone} +0. Itina +9. Ondoka","CON Kwa uteithio hora thimu %{support_phone} +0. Coka +9. Uma","CON Kwa Kuavizwa piga simu %{support_phone} +0. Uya nyuma +9. Uka","CON Kuom kony go simu e %{support_phone} +0. Dog chien +9. Wuogi","CON Qarqars simu dai%{support_phone} +0. Dheebi +9. Bai" +complete,"CON Your request has been sent. You will receive an SMS shortly. +00. Back +99. Exit","CON Ombi lako limetumwa. Utapokea uthibitishaji wa SMS kwa muda mfupi. +00. Rudi +99. Ondoka","CON Woni waku niwatumwa. Nukwata SMS ya kwonya ivinda ite yasa. +00. Itina +99. Ondoka","CON Mahoya maku nimatomwo. Niukuamukira SMS ya guitikirika ihinda ikuhi . +00. Coka +99. Uma","CON Vyoyoro rihumwa. Undaphokera Uthibitishaji wa SMS kwa muda mufuhi. +00. Uya nyuma +99. Uka","CON Kwayo ni oseor. Iboyudo mesej mar ote ni bang' saa matin. +00. Dog chien +99. Wuogi","CON Qarqars kake yaergad. Utapokea uthibitishaji wa SMS kwa muda mfupi. +00. Dheebi +99. Bai" +account_creation_prompt,END Your account is being created. You will receive an SMS when your account is ready.,END Akaunti yako ya Sarafu inatayarishwa. Utapokea ujumbe wa SMS akaunti yako ikiwa tayari.,END Akaunti yako ya Sarafu yendeye usovwa. Nukwata SMS akaunti yaku yasovwa.,END Akaunti yaku ya Sarafu niiraharirio.Niugutumirwo SMS akauti yaku ya rikio kuharirio,END Akauntiyo ya sarafu idzikoni. Undaphokera ujumbe wa SMS ichikala tayari.,END Akaont ni mar Sarafu iloso. Iboyudo mesej ka akaont ni otieki.,END Akaunti yako ya Sarafu inatayarishwa. Utapokea ujumbe wa SMS akaunti yako ikiwa tayari. +initial_middle_language_set,"CON Choose language: +%{middle_language_set} + +11. Next +22. Previous +00. Exit","CON Chagua lugha: +%{middle_language_set} + +11. Mbele +22. Rudi +00. Ondoka",,,,, +initial_last_language_set,"CON Choose language: +%{last_language_set} + +22. Previous +00. Exit","CON Choose language: +%{last_language_set} + +22. Rudi +00. Ondoka",,,,, +middle_language_set,"CON Choose language: +%{middle_language_set} + +11. Next +22. Previous +00. Exit","CON Chagua lugha: +%{middle_language_set} + +11. Mbele +22. Rudi +00. Ondoka",,,,, +last_language_set,"CON Choose language: + +%{last_language_set} +22. Previous +00. Exit","CON Choose language: +%{last_language_set} + +22. Rudi +00. Ondoka",,,,, \ No newline at end of file diff --git a/apps/contract-migration/2_deploy_contract_instance.sh b/apps/contract-migration/2_deploy_contract_instance.sh index ed8a24dc..1c3c5e85 100644 --- a/apps/contract-migration/2_deploy_contract_instance.sh +++ b/apps/contract-migration/2_deploy_contract_instance.sh @@ -41,17 +41,17 @@ add_pending_tx_hash $r # Deploy transfer authorization contact -advance_nonce -debug_rpc ->&2 echo -e "\033[;96mDeploy transfer authorization contract\033[;39m" -DEV_TRANSFER_AUTHORIZATION_ADDRESS=`erc20-transfer-auth-deploy --nonce $nonce -w $gas_price_arg -y $WALLET_KEY_FILE -i $CHAIN_SPEC -p $RPC_PROVIDER $DEV_DEBUG_FLAG` - - ->&2 echo -e "\033[;96mAdd transfer authorization record to contract registry\033[;39m" -advance_nonce -debug_rpc -r=`eth-contract-registry-set $DEV_WAIT_FLAG $fee_price_arg --nonce $nonce -s -u -y $WALLET_KEY_FILE -e $CIC_REGISTRY_ADDRESS -i $CHAIN_SPEC -p $RPC_PROVIDER $DEV_DEBUG_FLAG --identifier TransferAuthorization $DEV_TRANSFER_AUTHORIZATION_ADDRESS` -add_pending_tx_hash $r +#advance_nonce +#debug_rpc +#>&2 echo -e "\033[;96mDeploy transfer authorization contract\033[;39m" +#DEV_TRANSFER_AUTHORIZATION_ADDRESS=`erc20-transfer-auth-deploy --nonce $nonce -w $gas_price_arg -y $WALLET_KEY_FILE -i $CHAIN_SPEC -p $RPC_PROVIDER $DEV_DEBUG_FLAG` +# +# +#>&2 echo -e "\033[;96mAdd transfer authorization record to contract registry\033[;39m" +#advance_nonce +#debug_rpc +#r=`eth-contract-registry-set $DEV_WAIT_FLAG $fee_price_arg --nonce $nonce -s -u -y $WALLET_KEY_FILE -e $CIC_REGISTRY_ADDRESS -i $CHAIN_SPEC -p $RPC_PROVIDER $DEV_DEBUG_FLAG --identifier TransferAuthorization $DEV_TRANSFER_AUTHORIZATION_ADDRESS` +#add_pending_tx_hash $r # Deploy token index contract @@ -66,6 +66,13 @@ debug_rpc r=`eth-contract-registry-set $DEV_WAIT_FLAG $fee_price_arg --nonce $nonce -s -u -y $WALLET_KEY_FILE -e $CIC_REGISTRY_ADDRESS -i $CHAIN_SPEC -p $RPC_PROVIDER $DEV_DEBUG_FLAG --identifier TokenRegistry $DEV_TOKEN_INDEX_ADDRESS` add_pending_tx_hash $r +# Assign writer for token index +>&2 echo -e "\033[;96mEnable token index writer $DEV_ETH_ACCOUNT_CONTRACT_DEPLOYER to write to accounts index contract at $DEV_TOKEN_INDEX_ADDRESS\033[;39m" +advance_nonce +debug_rpc +r=`eth-accounts-index-writer -s -u -i $CHAIN_SPEC -p $RPC_PROVIDER --nonce $nonce --fee-limit 1000000 -e $DEV_TOKEN_INDEX_ADDRESS $DEV_DEBUG_FLAG $DEV_ETH_ACCOUNT_CONTRACT_DEPLOYER` +add_pending_tx_hash $r + check_wait 2 echo -e "\033[;96mWriting env_reset file\033[;39m" diff --git a/apps/contract-migration/3_deploy_token.sh b/apps/contract-migration/3_deploy_token.sh index 9f1e2270..abfdb7e3 100644 --- a/apps/contract-migration/3_deploy_token.sh +++ b/apps/contract-migration/3_deploy_token.sh @@ -61,7 +61,7 @@ function deploy_token_erc20_demurrage_token() { _deploy_token_defaults "DET" "Demurrage Token" advance_nonce debug_rpc - TOKEN_ADDRESS=`erc20-demurrage-token-deploy --nonce $nonce $fee_price_arg -p $RPC_PROVIDER -y $WALLET_KEY_FILE -i $CHAIN_SPEC --name "$TOKEN_NAME" --symbol $TOKEN_SYMBOL $DEV_DEBUG_FLAG -ww -s` + TOKEN_ADDRESS=`erc20-demurrage-token-deploy --nonce $nonce $fee_price_arg -p $RPC_PROVIDER -y $WALLET_KEY_FILE -i $CHAIN_SPEC --name "$TOKEN_NAME" --symbol $TOKEN_SYMBOL --decimals $TOKEN_DECIMALS $DEV_DEBUG_FLAG -ww -s` } function deploy_accounts_index() { @@ -152,6 +152,12 @@ else deploy_minter_${TOKEN_MINTER_MODE} $TOKEN_ADDRESS fi +>&2 echo -e "\033[;96mTransfer a single token to self to poke the gas cacher\033[;39m" +advance_nonce +debug_rpc +r=`erc20-transfer $DEV_WAIT_FLAG --nonce $nonce $fee_price_arg -p $RPC_PROVIDER -y $WALLET_KEY_FILE -i $CHAIN_SPEC -u $DEV_DEBUG_FLAG -s -e $TOKEN_ADDRESS -a $DEV_ETH_ACCOUNT_CONTRACT_DEPLOYER 1` +add_pending_tx_hash $r + check_wait 3 >&2 echo -e "\033[;96mWriting token metadata and proofs\033[;39m" diff --git a/apps/contract-migration/4_init_custodial.sh b/apps/contract-migration/4_init_custodial.sh index 5dc17d72..6997e910 100644 --- a/apps/contract-migration/4_init_custodial.sh +++ b/apps/contract-migration/4_init_custodial.sh @@ -18,6 +18,7 @@ fi must_address "$CIC_REGISTRY_ADDRESS" "registry" must_eth_rpc + # get required addresses from registries token_index_address=`eth-contract-registry-list -u -i $CHAIN_SPEC -p $RPC_PROVIDER -e $CIC_REGISTRY_ADDRESS $DEV_DEBUG_FLAG --raw TokenRegistry` accounts_index_address=`eth-contract-registry-list -u -i $CHAIN_SPEC -p $RPC_PROVIDER -e $CIC_REGISTRY_ADDRESS $DEV_DEBUG_FLAG --raw AccountRegistry` @@ -28,11 +29,11 @@ REDIS_HOST_CALLBACK=${REDIS_HOST_CALLBACK:-$REDIS_HOST} REDIS_PORT_CALLBACK=${REDIS_PORT_CALLBACK:-$REDIS_PORT} >&2 echo -e "\033[;96mcreate account for gas gifter\033[;39m" gas_gifter=`cic-eth-create --redis-timeout 120 $DEV_DEBUG_FLAG --redis-host-callback $REDIS_HOST_CALLBACK --redis-port-callback $REDIS_PORT_CALLBACK --no-register` -cic-eth-tag -i $CHAIN_SPEC GAS_GIFTER $gas_gifter +cic-eth-tag -i $CHAIN_SPEC --set --tag GAS_GIFTER $gas_gifter >&2 echo -e "\033[;96mcreate account for accounts index writer\033[;39m" accounts_index_writer=`cic-eth-create --redis-timeout 120 $DEV_DEBUG_FLAG --redis-host-callback $REDIS_HOST_CALLBACK --redis-port-callback $REDIS_PORT_CALLBACK --no-register` -cic-eth-tag -i $CHAIN_SPEC ACCOUNT_REGISTRY_WRITER $accounts_index_writer +cic-eth-tag -i $CHAIN_SPEC --set --tag ACCOUNT_REGISTRY_WRITER $accounts_index_writer # Assign system writer for accounts index diff --git a/apps/contract-migration/config.sh b/apps/contract-migration/config.sh index ea69a52b..48097f37 100644 --- a/apps/contract-migration/config.sh +++ b/apps/contract-migration/config.sh @@ -1,6 +1,7 @@ #!/bin/bash set -a +set -e if [ -z $DEV_DATA_DIR ]; then export DEV_DATA_DIR=`mktemp -d` @@ -33,6 +34,7 @@ else fi rm $bash_debug_flag -f ${DEV_DATA_DIR}/env_reset rm $bash_debug_flag -f $noncefile + export SYNCER_OFFSET=`eth-info --raw block` confini-dump --schema-dir ./config --prefix export > ${DEV_DATA_DIR}/env_reset fi @@ -55,4 +57,5 @@ fi # Migration variable processing confini-dump --schema-dir ./config > ${DEV_DATA_DIR}/env_reset +set +e set +a diff --git a/apps/contract-migration/config/config.ini b/apps/contract-migration/config/config.ini index 78edef76..2d8e93a5 100644 --- a/apps/contract-migration/config/config.ini +++ b/apps/contract-migration/config/config.ini @@ -25,3 +25,6 @@ port = [cic] registry_address = trust_address = + +[syncer] +offset = diff --git a/apps/contract-migration/docker/Dockerfile b/apps/contract-migration/docker/Dockerfile index 48f67376..35b22752 100644 --- a/apps/contract-migration/docker/Dockerfile +++ b/apps/contract-migration/docker/Dockerfile @@ -26,16 +26,7 @@ ARG pip_trusted_host=pypi.org RUN pip install --index-url $PIP_INDEX_URL \ --pre \ --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ - -r requirements.txt - -COPY override_requirements.txt . - -RUN pip install --index-url $PIP_INDEX_URL \ - --pre \ - --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ - --force-reinstall \ - --no-cache \ - -r override_requirements.txt + -r requirements.txt RUN pip freeze diff --git a/apps/contract-migration/override_requirements.txt b/apps/contract-migration/override_requirements.txt deleted file mode 100644 index 1b1e760c..00000000 --- a/apps/contract-migration/override_requirements.txt +++ /dev/null @@ -1 +0,0 @@ -chainlib-eth==0.0.10a15 diff --git a/apps/contract-migration/requirements.txt b/apps/contract-migration/requirements.txt index 36ec884d..db889d39 100644 --- a/apps/contract-migration/requirements.txt +++ b/apps/contract-migration/requirements.txt @@ -1,13 +1,12 @@ -cic-eth[tools]==0.12.4a13 -chainlib-eth>=0.0.10a15,<0.1.0 -eth-erc20>=0.1.2a3,<0.2.0 -erc20-demurrage-token>=0.0.5a2,<0.1.0 -eth-address-index>=0.2.4a1,<0.3.0 -cic-eth-registry>=0.6.1a6,<0.7.0 -erc20-transfer-authorization>=0.3.5a2,<0.4.0 -erc20-faucet>=0.3.2a2,<0.4.0 -sarafu-faucet>=0.0.7a2,<0.1.0 -confini>=0.4.2rc3,<1.0.0 -eth-token-index>=0.2.4a1,<=0.3.0 -okota>=0.2.4a15,<0.3.0 -cic-types~=0.2.1a2 \ No newline at end of file +cic-eth[tools]==0.12.7 +cic-types~=0.2.1a8 +chainlib-eth~=0.0.15 +eth-erc20~=0.1.5 +erc20-demurrage-token~=0.0.7 +eth-address-index~=0.2.4 +cic-eth-registry~=0.6.5 +erc20-faucet==0.3.2 +sarafu-faucet==0.0.7 +confini~=0.5.3 +eth-token-index==0.2.4 +okota==0.2.5a1 diff --git a/apps/contract-migration/scripts/proofs.py b/apps/contract-migration/scripts/proofs.py index 2d35d5bf..b65afa43 100644 --- a/apps/contract-migration/scripts/proofs.py +++ b/apps/contract-migration/scripts/proofs.py @@ -18,6 +18,7 @@ from eth_address_declarator.declarator import AddressDeclarator from funga.eth.signer import EIP155Signer from funga.eth.keystore.dict import DictKeystore from hexathon import add_0x, strip_0x +from okota.token_index.index import to_identifier # local imports @@ -109,6 +110,7 @@ if __name__ == '__main__': identifier = bytes.fromhex(hashed_token_proof) token_immutable_proof_writer = MetadataRequestsHandler(cic_type=MetadataPointer.NONE, identifier=identifier) write_metadata(token_immutable_proof_writer, token_proof_data) + logg.debug(f'Writing hashed proof: {hashed_token_proof}') write_to_declarator(contract_address=args.address_declarator, contract_wrapper=contract_wrapper, proof=hashed_token_proof, @@ -116,12 +118,11 @@ if __name__ == '__main__': signer_address=args.signer_address, token_address=args.e) - hashed_token_proof = hash_proof(args.token_symbol.encode('utf-8')) - identifier = bytes.fromhex(hashed_token_proof) - token_immutable_proof_writer = MetadataRequestsHandler(cic_type=MetadataPointer.NONE, identifier=identifier) + hashed_token_proof = to_identifier(args.token_symbol) + logg.debug(f'Writing hashed proof: {hashed_token_proof}') write_to_declarator(contract_address=args.address_declarator, contract_wrapper=contract_wrapper, - proof=identifier, + proof=hashed_token_proof, rpc=rpc, signer_address=args.signer_address, token_address=args.e) diff --git a/apps/data-seeding/cic_eth/import_balance.py b/apps/data-seeding/cic_eth/import_balance.py index 28104341..4d471234 100644 --- a/apps/data-seeding/cic_eth/import_balance.py +++ b/apps/data-seeding/cic_eth/import_balance.py @@ -1,48 +1,37 @@ # standard imports +import argparse +import json +import logging import os import sys -import logging -import time -import argparse -import sys -import re -import hashlib -import csv -import json # external imports import confini -from hexathon import ( - strip_0x, - add_0x, - ) -from chainsyncer.backend.memory import MemBackend -from chainsyncer.driver.head import HeadSyncer -from chainlib.eth.connection import EthHTTPConnection -from chainlib.eth.block import ( - block_latest, - ) -from chainlib.hash import keccak256_string_to_hex +from chainlib.chain import ChainSpec from chainlib.eth.address import to_checksum_address +from chainlib.eth.block import ( + block_latest, +) +from chainlib.eth.connection import EthHTTPConnection +from chainlib.eth.error import ( + RequestMismatchException, +) from chainlib.eth.gas import OverrideGasOracle from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.tx import TxFactory -from chainlib.jsonrpc import JSONRPCRequest -from chainlib.eth.error import ( - EthException, - RequestMismatchException, - ) -from chainlib.chain import ChainSpec -from chainlib.eth.constant import ZERO_ADDRESS -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore -from cic_types.models.person import Person -from eth_erc20 import ERC20 +from chainlib.hash import keccak256_string_to_hex +from chainsyncer.backend.memory import MemBackend +from chainsyncer.driver.head import HeadSyncer from cic_eth.cli.chain import chain_interface +from cic_types.models.person import Person from eth_accounts_index import AccountsIndex from eth_contract_registry import Registry +from eth_erc20 import ERC20 from eth_token_index import TokenUniqueSymbolIndex - +from funga.eth.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer +from hexathon import ( + strip_0x, +) logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() diff --git a/apps/data-seeding/cic_eth/import_users.py b/apps/data-seeding/cic_eth/import_users.py index 1322c638..bf66c382 100644 --- a/apps/data-seeding/cic_eth/import_users.py +++ b/apps/data-seeding/cic_eth/import_users.py @@ -40,7 +40,7 @@ argparser = argparse.ArgumentParser() argparser.add_argument('-c', type=str, help='config override directory') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, help='Chain specification string') argparser.add_argument('-f', action='store_true', help='force clear previous state') -argparser.add_argument('--old-chain-spec', type=str, dest='old_chain_spec', default='evm:oldchain:1', help='chain spec') +argparser.add_argument('--old-chain-spec', type=str, dest='old_chain_spec', default='evm:foo:1:oldchain', help='chain spec') argparser.add_argument('--redis-host', dest='redis_host', type=str, help='redis host to use for task submission') argparser.add_argument('--redis-port', dest='redis_port', type=int, help='redis host to use for task submission') argparser.add_argument('--redis-db', dest='redis_db', type=int, help='redis db to use for task submission and callback') @@ -227,9 +227,10 @@ if __name__ == '__main__': ) os.makedirs(os.path.dirname(filepath), exist_ok=True) - sub_old_chain_str = '{}:{}'.format(old_chain_spec.common_name(), old_chain_spec.network_id()) + sub_old_chain_str = '{}:{}'.format(old_chain_spec.network_id(), old_chain_spec.common_name()) + logg.debug('u id {}'.format(u.identities)) f = open(filepath, 'w') - k = u.identities['evm'][sub_old_chain_str][0] + k = u.identities['evm'][old_chain_spec.fork()][sub_old_chain_str][0] tag_data = {'tags': user_tags[strip_0x(k)]} f.write(json.dumps(tag_data)) f.close() diff --git a/apps/data-seeding/cic_eth/traffic/common/signer.py b/apps/data-seeding/cic_eth/traffic/common/signer.py index fa47b79b..319f6c5b 100644 --- a/apps/data-seeding/cic_eth/traffic/common/signer.py +++ b/apps/data-seeding/cic_eth/traffic/common/signer.py @@ -2,8 +2,8 @@ import logging # external imports -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.dict import DictKeystore logg = logging.getLogger(__name__) diff --git a/apps/data-seeding/cic_ussd/import_balance.py b/apps/data-seeding/cic_ussd/import_balance.py index 6d8b9a2d..3db012e8 100644 --- a/apps/data-seeding/cic_ussd/import_balance.py +++ b/apps/data-seeding/cic_ussd/import_balance.py @@ -9,8 +9,8 @@ from chainlib.chain import ChainSpec from chainlib.eth.address import to_checksum_address from chainlib.eth.connection import EthHTTPConnection from confini import Config -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.dict import DictKeystore # local imports from import_util import BalanceProcessor, get_celery_worker_status @@ -65,7 +65,8 @@ args_override = { 'REDIS_DB': getattr(args, 'redis_db'), 'META_HOST': getattr(args, 'meta_host'), 'META_PORT': getattr(args, 'meta_port'), - 'WALLET_KEY_FILE': getattr(args, 'y') + 'WALLET_KEY_FILE': getattr(args, 'y'), + 'TOKEN_SYMBOL': getattr(args, 'token_symbol'), } config.dict_override(args_override, 'cli flag') config.censor('PASSWORD', 'DATABASE') @@ -110,7 +111,7 @@ def main(): config.get('CIC_REGISTRY_ADDRESS'), signer_address, signer) - ImportTask.balance_processor.init(args.token_symbol) + ImportTask.balance_processor.init(config.get('TOKEN_SYMBOL')) balances = {} accuracy = 10 ** 6 count = 0 diff --git a/apps/data-seeding/config/token.ini b/apps/data-seeding/config/token.ini new file mode 100644 index 00000000..b14d3a08 --- /dev/null +++ b/apps/data-seeding/config/token.ini @@ -0,0 +1,2 @@ +[token] +symbol = diff --git a/apps/data-seeding/config/traffic.ini b/apps/data-seeding/config/traffic.ini new file mode 100644 index 00000000..df89d1aa --- /dev/null +++ b/apps/data-seeding/config/traffic.ini @@ -0,0 +1,4 @@ +[traffic] +#local.noop_traffic = 2 +#local.account = 2 +local.transfer = 2 diff --git a/apps/data-seeding/docker/Dockerfile b/apps/data-seeding/docker/Dockerfile index 9afaa5ee..6a3ebf4e 100644 --- a/apps/data-seeding/docker/Dockerfile +++ b/apps/data-seeding/docker/Dockerfile @@ -1,16 +1,25 @@ -# syntax = docker/dockerfile:1.2 -FROM registry.gitlab.com/grassrootseconomics/cic-base-images:python-3.8.6-dev-5ab8bf45 +ARG DOCKER_REGISTRY="registry.gitlab.com/grassrootseconomics" + +FROM $DOCKER_REGISTRY/cic-base-images:python-3.8.6-dev-e8eb2ee2 WORKDIR /root RUN mkdir -vp /usr/local/etc/cic -COPY package.json \ - package-lock.json \ - ./ +ARG NPM_REPOSITORY=${NPM_REPOSITORY:-https://registry.npmjs.org} +RUN npm config set snyk=false +#RUN npm config set registry={NPM_REPOSITORY} +RUN npm config set registry=${NPM_REPOSITORY} + +# copy the dependencies +COPY package.json package-lock.json ./ +RUN --mount=type=cache,mode=0755,target=/root/.npm \ + npm set cache /root/.npm && \ + npm cache verify && \ + npm ci --verbose -RUN npm ci --production +#RUN npm ci --production --verbose #RUN --mount=type=cache,mode=0755,target=/root/node_modules npm install COPY common/ cic_ussd/common/ @@ -22,9 +31,10 @@ ARG EXTRA_PIP_ARGS="" ARG PIP_INDEX_URL=https://pypi.org/simple RUN pip install --index-url $PIP_INDEX_URL \ + --pre \ --extra-index-url $EXTRA_PIP_INDEX_URL $EXTRA_PIP_ARGS \ -r requirements.txt -COPY . . +COPY . . ENTRYPOINT [ ] diff --git a/apps/data-seeding/eth/import_balance.py b/apps/data-seeding/eth/import_balance.py index 1a9080a9..3c103301 100644 --- a/apps/data-seeding/eth/import_balance.py +++ b/apps/data-seeding/eth/import_balance.py @@ -33,8 +33,8 @@ from chainlib.eth.error import ( RequestMismatchException, ) from chainlib.chain import ChainSpec -from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.dict import DictKeystore +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.dict import DictKeystore from cic_types.models.person import Person from eth_erc20 import ERC20 from cic_eth.cli.chain import chain_interface diff --git a/apps/data-seeding/eth/import_users.py b/apps/data-seeding/eth/import_users.py index f7a54969..dd462f79 100644 --- a/apps/data-seeding/eth/import_users.py +++ b/apps/data-seeding/eth/import_users.py @@ -27,9 +27,9 @@ from cic_types.processor import generate_metadata_pointer from cic_types import MetadataPointer from eth_accounts_index.registry import AccountRegistry from eth_contract_registry import Registry -from crypto_dev_signer.keystore.dict import DictKeystore -from crypto_dev_signer.eth.signer.defaultsigner import ReferenceSigner as EIP155Signer -from crypto_dev_signer.keystore.keyfile import to_dict as to_keyfile_dict +from funga.eth.keystore.dict import DictKeystore +from funga.eth.signer.defaultsigner import EIP155Signer +from funga.eth.keystore.keyfile import to_dict as to_keyfile_dict # local imports from common.dirs import initialize_dirs diff --git a/apps/data-seeding/import_ussd.sh b/apps/data-seeding/import_ussd.sh index 4b9e9550..8dd4c3e9 100755 --- a/apps/data-seeding/import_ussd.sh +++ b/apps/data-seeding/import_ussd.sh @@ -1,5 +1,11 @@ #!/bin/bash +. /tmp/cic/config/env_reset + +OUT_DIR=out +CONFIG_DIR=config +confini-dump --schema-dir $CONFIG_DIR + if [[ -d "$OUT_DIR" ]] then echo -e "\033[;96mfound existing IMPORT DIR cleaning up...\033[;96m" @@ -19,14 +25,14 @@ then fi echo -e "\033[;96mCreating seed data...\033[;96m" -python create_import_users.py -vv -c "$CONFIG" --dir "$OUT_DIR" "$NUMBER_OF_USERS" +python create_import_users.py -vv -c "$CONFIG_DIR" --dir "$OUT_DIR" "$NUMBER_OF_USERS" wait $! echo -e "\033[;96mCheck for running celery workers ...\033[;96m" if [ -f ./cic-ussd-import.pid ]; then echo -e "\033[;96mFound a running worker. Killing ...\033[;96m" - kill -9 $( nohup.out 2> nohup.err < /dev/null & + nohup python cic_ussd/import_balance.py -vv -c "$CONFIG_DIR" -p "$ETH_PROVIDER" -r "$CIC_REGISTRY_ADDRESS" --token-symbol "$TOKEN_SYMBOL" -y "$WALLET_KEY_FILE" "$OUT_DIR" > nohup.out 2> nohup.err < /dev/null & else echo -e "\033[;96mRunning worker with opening balance transactions\033[;96m" TARGET_TX_COUNT=$((NUMBER_OF_USERS*2)) - nohup python cic_ussd/import_balance.py -vv -c "$CONFIG" -p "$ETH_PROVIDER" -r "$CIC_REGISTRY_ADDRESS" --include-balances --token-symbol "$TOKEN_SYMBOL" -y "$WALLET_KEY_FILE" "$OUT_DIR" & + nohup python cic_ussd/import_balance.py -vv -c "$CONFIG_DIR" -p "$ETH_PROVIDER" -r "$CIC_REGISTRY_ADDRESS" --include-balances --token-symbol "$TOKEN_SYMBOL" -y "$WALLET_KEY_FILE" "$OUT_DIR" & fi echo -e "\033[;96mTarget count set to ${TARGET_TX_COUNT}" @@ -53,12 +59,12 @@ done IMPORT_BALANCE_JOB=$(=0.5.1a1,<=0.5.15 +cic-eth[tools]~=0.12.5a11 +cic-types~=0.2.1a8 +funga>=0.5.1 faker==4.17.1 chainsyncer~=0.0.7a3 -chainlib-eth~=0.0.10a10 +chainlib-eth~=0.0.15 eth-address-index~=0.2.4a1 eth-contract-registry~=0.6.3a3 eth-accounts-index~=0.1.2a3 -eth-erc20~=0.1.2a3 +eth-erc20==0.1.4 erc20-faucet~=0.3.2a2 psycopg2==2.8.6 liveness~=0.0.1a7 -confini>=0.4.2rc3,<0.5.0 +confini>=0.5.2 diff --git a/apps/data-seeding/verify.py b/apps/data-seeding/verify.py index fd59df45..ca4a14b7 100644 --- a/apps/data-seeding/verify.py +++ b/apps/data-seeding/verify.py @@ -60,7 +60,7 @@ eth_tests = [ ] phone_tests = [ - 'ussd', +# 'ussd', 'ussd_pins' ] @@ -68,7 +68,11 @@ admin_tests = [ 'local_key', ] -all_tests = eth_tests + custodial_tests + metadata_tests + phone_tests +cache_tests = [ + 'cache_tx_user', + ] + +all_tests = eth_tests + custodial_tests + metadata_tests + phone_tests + cache_tests argparser = argparse.ArgumentParser(description='daemon that monitors transactions in new blocks') argparser.add_argument('-p', '--provider', dest='p', type=str, help='chain rpc provider address') @@ -77,11 +81,14 @@ argparser.add_argument('--old-chain-spec', type=str, dest='old_chain_spec', defa argparser.add_argument('-i', '--chain-spec', type=str, dest='i', help='chain spec') argparser.add_argument('--meta-provider', type=str, dest='meta_provider', default='http://localhost:63380', help='cic-meta url') argparser.add_argument('--ussd-provider', type=str, dest='ussd_provider', default='http://localhost:63315', help='cic-ussd url') +argparser.add_argument('--cache-provider', type=str, dest='cache_provider', default='http://localhost:63313', help='cic-cache url') argparser.add_argument('--skip-custodial', dest='skip_custodial', action='store_true', help='skip all custodial verifications') argparser.add_argument('--skip-ussd', dest='skip_ussd', action='store_true', help='skip all ussd verifications') argparser.add_argument('--skip-metadata', dest='skip_metadata', action='store_true', help='skip all metadata verifications') +argparser.add_argument('--skip-cache', dest='skip_cache', action='store_true', help='skip all cache verifications') argparser.add_argument('--exclude', action='append', type=str, default=[], help='skip specified verification') argparser.add_argument('--include', action='append', type=str, help='include specified verification') +argparser.add_argument('--list-verifications', action='store_true', help='print a list of verification check identifiers') argparser.add_argument('--token-symbol', default='GFT', type=str, dest='token_symbol', help='Token symbol to use for trnsactions') argparser.add_argument('-r', '--registry-address', type=str, dest='r', help='CIC Registry address') argparser.add_argument('--env-prefix', default=os.environ.get('CONFINI_ENV_PREFIX'), dest='env_prefix', type=str, help='environment prefix for variables to overwrite configuration') @@ -115,6 +122,7 @@ config.censor('PASSWORD', 'DATABASE') config.censor('PASSWORD', 'SSL') config.add(args.meta_provider, '_META_PROVIDER', True) config.add(args.ussd_provider, '_USSD_PROVIDER', True) +config.add(args.cache_provider, '_CACHE_PROVIDER', True) token_symbol = args.token_symbol @@ -351,6 +359,24 @@ class Verifier: raise VerifierError(o_retrieved, 'metadata (person)') + def verify_cache_tx_user(self, address, balance=None): + url = os.path.join(config.get('_CACHE_PROVIDER'), 'txa', 'user', address) + req = urllib.request.Request(url) + req.add_header('X_CIC_CACHE_MODE', 'all') + try: + res = urllib.request.urlopen(req) + except urllib.error.HTTPError as e: + raise VerifierError( + '({}) {}'.format(url, e), + 'cache (tx user)', + ) + r = json.load(res) + if len(r['data']) == 0: + raise VerifierError('empty tx list for address {}'.format(address), 'cache (tx user)') + for tx in r['data']: + logg.warning('found tx {} for {} but not checking validity'.format(tx['tx_hash'], address)) + + def verify_metadata_phone(self, address, balance=None): upper_address = strip_0x(address).upper() f = open(os.path.join( @@ -397,11 +423,13 @@ class Verifier: if m != 'CON Welcome': raise VerifierError(response_data, 'ussd') + def verify_ussd_pins(self, address, balance): response_data = send_ussd_request(address, self.data_dir) if response_data[:11] != 'CON Balance' and response_data[:9] != 'CON Salio': raise VerifierError(response_data, 'pins') + def verify(self, address, balance, debug_stem=None): for k in active_tests: diff --git a/apps/helpers/demurrage_token_poke/poke.sh b/apps/helpers/demurrage_token_poke/poke.sh new file mode 100644 index 00000000..30eab7e5 --- /dev/null +++ b/apps/helpers/demurrage_token_poke/poke.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +default_token_address=`eth-contract-registry-list --raw -e $CIC_REGISTRY_ADDRESS DefaultToken` +export TOKEN_ADDRESSES=${TOKEN_ADDRESSES:-$default_token_address} + +IFS="," read -r -a token_addresses <<< $TOKEN_ADDRESSES +export RPC_VERIFY=1 + +for token_address in ${token_addresses[@]}; do + >&2 echo checking token address $token_address + t=`eth-encode --signature demurrageTimestamp -e $token_address --notx` + v=`eth-encode --signature demurrageAmount -e $token_address --notx` + >&2 echo last demurrage apply call for $token_address was value $v at $t + if [ "$?" -eq 0 ]; then + h=`eth-encode --signature applyDemurrage -i $CHAIN_SPEC -y $WALLET_KEY_FILE -e $token_address --fee-limit 8000000 -s -ww` + >&2 echo applied demurrage on $token_address tx hash $h + fi +done diff --git a/docker-compose.build.yml b/docker-compose.build.yml new file mode 100644 index 00000000..11e77fdc --- /dev/null +++ b/docker-compose.build.yml @@ -0,0 +1,52 @@ +version: '3.2' + +services: + cic-cache: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-cache:${TAG:-latest} + build: + context: apps/cic-cache + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-cache:latest + cic-eth: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:${TAG:-latest} + build: + context: apps/cic-eth + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-eth:latest + cic-meta: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-meta:${TAG:-latest} + build: + context: apps/cic-meta + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-meta:latest + cic-notify: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-notify:${TAG:-latest} + build: + context: apps/cic-notify + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-notify:latest + funga-eth: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/funga-eth:${TAG:-latest} + build: + context: apps/cic-signer + dockerfile: Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/funga-eth:latest + cic-ussd: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-ussd:${TAG:-latest} + build: + context: apps/cic-ussd + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-ussd:latest + cic-contract-bootstrap: + image: ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-contract-bootstrap:${TAG:-latest} + build: + context: apps/contract-migration + dockerfile: docker/Dockerfile + cache_from: + - ${IMAGE_BASE_URL:-registry.gitlab.com/grassrootseconomics/cic-internal-integration}/cic-ussd:latest \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 81930c2e..e75a48f4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -153,6 +153,7 @@ services: SIGNER_PROVIDER: ${SIGNER_PROVIDER:-http://cic-signer:8000} SIGNER_SECRET: ${SIGNER_SECRET:-deadbeef} TASKS_TRACE_QUEUE_STATUS: ${TASKS_TRACE_QUEUE_STATUS:-1} + ETH_MIN_FEE_PRICE: ${ETH_MIN_FEE_PRICE:-1000000000} restart: unless-stopped depends_on: - evm @@ -163,13 +164,13 @@ services: - signer-data:/run/crypto-dev-signer - contract-config:/tmp/cic/config/:ro command: - - /bin/bash - - -c - - | - set -a - if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi - set +a - ./start_tasker.sh --aux-all -q cic-eth -vv + - /bin/bash + - -c + - | + set -a + if [[ -f /tmp/cic/config/env_reset ]]; then source /tmp/cic/config/env_reset; fi + set +a + ./start_tasker.sh --aux-all -q cic-eth -vv cic-eth-server: image: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics}/cic-eth:${TAG:-latest} @@ -236,7 +237,7 @@ services: SYNCER_LOOP_INTERVAL: ${SYNCER_LOOP_INTERVAL:-5} SYNCER_NO_HISTORY: ${SYNCER_NO_HISTORY:-1} SYNCER_OFFSET: ${SYNCER_OFFSET:-0} - TASKS_TRANSFER_CALLBACKS: ${TASKS_TRANSFER_CALLBACKS:-"cic-eth:cic_eth.callbacks.noop.noop,cic-ussd:cic_ussd.tasks.callback_handler.transaction_callback"} + TASKS_TRANSFER_CALLBACKS: ${TASKS_TRANSFER_CALLBACKS:-cic-eth:cic_eth.callbacks.noop.noop,cic-ussd:cic_ussd.tasks.callback_handler.transaction_callback} restart: unless-stopped depends_on: - evm @@ -375,7 +376,7 @@ services: SYNCER_LOOP_INTERVAL: ${SYNCER_LOOP_INTERVAL:-5} SYNCER_OFFSET: ${SYNCER_OFFSET:-0} SYNCER_NO_HISTORY: ${SYNCER_NO_HISTORY:-1} - TASKS_TRANSFER_CALLBACKS: ${TASKS_TRANSFER_CALLBACKS:-"cic-eth:cic_eth.callbacks.noop.noop,cic-ussd:cic_ussd.tasks.callback_handler.transaction_callback"} + TASKS_TRANSFER_CALLBACKS: ${TASKS_TRANSFER_CALLBACKS:-cic-eth:cic_eth.callbacks.noop.noop,cic-ussd:cic_ussd.tasks.callback_handler.transaction_callback} restart: unless-stopped depends_on: - evm @@ -529,6 +530,9 @@ services: context: apps/cic-ussd dockerfile: docker/Dockerfile args: + PIP_INDEX_URL: ${PIP_INDEX_URL:-https://pypi.org/simple} + EXTRA_PIP_INDEX_URL: ${EXTRA_PIP_INDEX_URL:-https://pip.grassrootseconomics.net} + EXTRA_PIP_ARGS: $EXTRA_PIP_ARGS DOCKER_REGISTRY: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics} environment: DATABASE_HOST: ${DATABASE_HOST:-postgres} @@ -553,8 +557,8 @@ services: - postgres - redis #- cic-meta-server - - cic-eth-tasker - - cic-cache-tasker + #- cic-eth-tasker + #- cic-cache-tasker volumes: - ./apps/contract-migration/testdata/pgp/:/usr/src/secrets/ command: "/root/start_cic_user_tasker.sh -q cic-ussd -vv" @@ -667,6 +671,7 @@ services: context: apps/data-seeding dockerfile: docker/Dockerfile args: + NPM_REPOSITORY: ${DEV_NPM_REPOSITORY:-https://registry.npmjs.org} DOCKER_REGISTRY: ${DEV_DOCKER_REGISTRY:-registry.gitlab.com/grassrootseconomics} PIP_INDEX_URL: ${PIP_INDEX_URL:-https://pypi.org/simple} EXTRA_PIP_INDEX_URL: ${EXTRA_PIP_INDEX_URL:-https://pip.grassrootseconomics.net} @@ -685,27 +690,28 @@ services: CELERY_RESULT_URL: ${CELERY_RESULT_URL:-redis://redis:6379} CIC_REGISTRY_ADDRESS: $CIC_REGISTRY_ADDRESS RPC_PROVIDER: ${RPC_PROVIDER:-http://evm:8545} - OUT_DIR: out - NUMBER_OF_USERS: 10 - CONFIG: config + #OUT_DIR: out + NUMBER_OF_USERS: ${NUMBER_OF_USERS:-10} + #CONFIG_DIR: config CHAIN_SPEC: ${CHAIN_SPEC:-evm:byzantium:8996:bloxberg} - TOKEN_SYMBOL: GFT + TOKEN_SYMBOL: $TOKEN_SYMBOL #KEYSTORE_PATH: keystore/UTC--2021-01-08T17-18-44.521011372Z--eb3907ecad74a0013c259d5874ae7f22dcbcc95c WALLET_KEY_FILE: ${WALLET_KEY_FILE:-/root/keystore/UTC--2021-01-08T17-18-44.521011372Z--eb3907ecad74a0013c259d5874ae7f22dcbcc95c} USSD_HOST: cic-user-ussd-server USSD_PORT: 9000 INCLUDE_BALANCES: y - USSD_SSL: n - NOTIFY_DATABASE_NAME: cic_notify + GIFT_THRESHOLD: ${GIFT_THRESHOLD:-0} + USSD_SSL: $USSD_SSL + DATABASE_NAME_NOTIFY: cic_notify REDIS_HOST: redis REDIS_PORT: 6379 REDIS_DB: 0 META_HOST: meta META_PORT: 8000 META_URL: http://meta:8000 + # TODO: this should be generated from host/port/ssl USSD_PROVIDER: http://cic-user-ussd-server:9000 CELERY_QUEUE: cic-import-ussd - EXCLUSIONS: ussd command: - /bin/bash - -c diff --git a/scripts/local-build-push.sh b/scripts/local-build-push.sh new file mode 100644 index 00000000..967733e5 --- /dev/null +++ b/scripts/local-build-push.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env sh + +# dependencies: +# - docker-compose >= v1.25.0 +# - sbot >= v1.0.0. + +set -e + +export DOCKER_BUILDKIT=1 +export COMPOSE_DOCKER_CLI_BUILD=1 + +PREDICTED_TAG=$(sbot predict version -m auto) + +docker-compose -f docker-compose.build.yml build --progress plain + +export TAG=v$PREDICTED_TAG + +docker-compose -f docker-compose.build.yml build --progress plain +docker-compose -f docker-compose.build.yml push + +export TAG=latest + +docker-compose -f docker-compose.build.yml push