def test_mint(): currency_spec = token_lib.CurrencySpec({ 'name': "BBc Point", 'symbol': "BBP", }) mint = token_lib.BBcMint(domain_id, mint_id, mint_id, idPubkeyMap) mint.set_condition(0, keypair=keypairs[0]) mint.set_currency_spec(currency_spec, keypair=keypairs[0]) assert mint.get_condition() == 0 assert mint.get_currency_spec() == currency_spec (user_a_id, keypairs_a) = idPubkeyMap.create_user_id(num_pubkeys=1) (user_b_id, keypairs_b) = idPubkeyMap.create_user_id(num_pubkeys=1) mint.issue(user_a_id, 1000, keypair=keypairs[0]) assert mint.get_balance_of(user_a_id) == 1000 assert mint.get_balance_of(user_b_id) == 0 mint.transfer(user_a_id, user_b_id, 100, keypair_from=keypairs_a[0], keypair_mint=keypairs[0]) assert mint.get_balance_of(user_a_id) == 900 assert mint.get_balance_of(user_b_id) == 100 mint.issue(user_a_id, 10, keypair=keypairs[0]) assert mint.get_balance_of(user_a_id) == 910 assert mint.get_balance_of(user_b_id) == 100 mint.transfer(user_a_id, user_b_id, 100, keypair_from=keypairs_a[0], keypair_mint=keypairs[0]) assert mint.get_balance_of(user_a_id) == 810 assert mint.get_balance_of(user_b_id) == 200
def define_currency(name, symbol, file, dic_currencies): idPubkeyMap = id_lib.BBcIdPublickeyMap(domain_id) (mint_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=1) f = open(file, 'r') j_currency_spec = json.load(f) f.close() j_currency_spec['name'] = name j_currency_spec['symbol'] = symbol currency_spec = token_lib.CurrencySpec(j_currency_spec) mint = token_lib.BBcMint(domain_id, mint_id, mint_id, idPubkeyMap) mint.set_condition(0, keypair=keypairs[0]) mint.set_currency_spec(currency_spec, keypair=keypairs[0]) clear_selected(dic_currencies) dic_currencies[currency_spec.name] = User(mint_id, keypairs[0], True) write_dic(F_JSON_CURRENCIES, dic_currencies) print("currency %s/%s is defined." % (name, symbol))
def test_store(): (mint_id, keypairs) = idPubkeyMap.create_user_id(num_pubkeys=1) app = bbc_app.BBcAppClient(port=DEFAULT_CORE_PORT, loglevel="all") app.set_user_id(mint_id) app.set_domain_id(domain_id) app.set_callback(bbc_app.Callback()) ret = app.register_to_core() store = token_lib.Store(domain_id, mint_id, app) currency_spec = token_lib.CurrencySpec({ 'name': "Japanese Yen", 'symbol': "JPY", }) store.set_condition(0, keypair=keypairs[0], idPublickeyMap=idPubkeyMap) store.set_currency_spec(currency_spec, keypair=keypairs[0], idPublickeyMap=idPubkeyMap) assert store.get_condition() == 0 assert store.get_currency_spec() == currency_spec
def test_mint_depreciation(): currency_spec = token_lib.CurrencySpec({ 'name': 'BBc X', 'symbol': 'BBX', 'decimal': 2, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': 0, 'time_unit': 4, 'expire_after': 0, }], }) mint = token_lib.BBcMint(domain_id, mint_id, mint_id, idPubkeyMap) mint.set_condition(0, keypair=keypairs[0]) mint.set_currency_spec(currency_spec, keypair=keypairs[0]) assert mint.get_condition() == 0 assert mint.get_currency_spec() == currency_spec (user_a_id, keypairs_a) = idPubkeyMap.create_user_id(num_pubkeys=1) (user_b_id, keypairs_b) = idPubkeyMap.create_user_id(num_pubkeys=1) mint.issue(user_a_id, 100000, keypair=keypairs[0]) assert mint.get_balance_of(user_a_id) == 100000 assert mint.get_balance_of(user_b_id) == 0 print("\n5-second interval.") time.sleep(5) assert mint.get_balance_of(user_a_id) == 99000 assert mint.get_balance_of(user_b_id) == 0 mint.transfer(user_a_id, user_b_id, 10000, keypair_from=keypairs_a[0], keypair_mint=keypairs[0]) assert mint.get_balance_of(user_a_id) == 88999 assert mint.get_balance_of(user_b_id) == 10000 mint.issue(user_a_id, 1000, keypair=keypairs[0]) assert mint.get_balance_of(user_a_id) == 89999 assert mint.get_balance_of(user_b_id) == 10000 mint.transfer(user_a_id, user_b_id, 10000, keypair_from=keypairs_a[0], keypair_mint=keypairs[0]) assert mint.get_balance_of(user_a_id) == 79998 assert mint.get_balance_of(user_b_id) == 20000
def test_currency_spec(): rate = token_lib.Fraction(-1, 10) rate_to_stop = token_lib.Fraction(1, 2) time_unit = 100 expire_after = 0 name = "Japanese Yen" symbol = "JPY" decimal = 2 currency_spec_dict = { 'name': name, 'symbol': symbol, 'decimal': decimal, 'variation_specs': [ { 'condition': 0, 'variation_type': "simple", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }, { 'condition': 1, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "0", 'time_unit': 60 * 60 * 24 * 30, 'expire_after': 60 * 60 * 24 * 180, }, ], 'option_witnesses_required': True, 'option_expiration_rebased': True, 'option_conditions_irreversible': False, } spec = token_lib.CurrencySpec(currency_spec_dict) assert spec.name == name assert spec.symbol == symbol assert spec.decimal == decimal assert len(spec.variation_specs) == 2 assert spec.variation_specs[0].condition == 0 assert spec.variation_specs[0].type == token_lib.Variation.T_SIMPLE assert spec.variation_specs[0].rate == 0 assert spec.variation_specs[0].rate_to_stop == 1 assert spec.variation_specs[0].time_unit == 0x7fffffffffffffff assert spec.variation_specs[0].expire_after == 0 assert spec.variation_specs[1].condition == 1 assert spec.variation_specs[1].type == token_lib.Variation.T_SIMPLE assert spec.variation_specs[1].rate == token_lib.Fraction("-1/100") assert spec.variation_specs[1].rate_to_stop == 0 assert spec.variation_specs[1].time_unit == 60 * 60 * 24 * 30 assert spec.variation_specs[1].expire_after == 60 * 60 * 24 * 180 assert spec.option_witnesses_required == True assert spec.option_expiration_rebased == True assert spec.option_conditions_irreversible == False spec1 = token_lib.CurrencySpec(currency_spec_dict) assert spec1 == spec dat = spec1.serialize() _, spec2 = token_lib.CurrencySpec.from_serialized_data(0, dat) assert spec2 == spec1 == spec currency_spec_dict = { 'name': "Whatever", 'symbol': "WTV", } spec = token_lib.CurrencySpec(currency_spec_dict) assert not spec2 == spec assert spec.decimal == 0 assert len(spec.variation_specs) == 0 assert spec.option_witnesses_required == False assert spec.option_expiration_rebased == False assert spec.option_conditions_irreversible == True currency_spec_dict = { 'name': 123, 'symbol': symbol, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 10 assert spec == 10 currency_spec_dict = { 'name': "US Dollar", 'symbol': 31, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 1 assert spec == 1 currency_spec_dict = { 'name': "US Dollar", 'symbol': "USD", 'decimal': "2", } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 13 assert spec == 13 currency_spec_dict = { 'name': "US Dollar", 'symbol': "USD", 'decimal': -1, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 14 assert spec == 14 currency_spec_dict = { 'name': "US Dollar", 'symbol': "USD", 'decimal': 13, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 15 assert spec == 15 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 16 assert not spec == 16 assert len(spec.variation_specs) == 1 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': "0", 'variation_type': "simple", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 17 assert spec == 17 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': -1, 'variation_type': "simple", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 18 assert spec == 18 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 128, 'variation_type': "simple", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 19 assert spec == 19 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': 4, 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 20 assert spec == 20 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "exact", 'rate': "0", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 21 assert spec == 21 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "1/32768", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 22 assert spec == 22 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "32768/32761", 'rate_to_stop': "1/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 23 assert spec == 23 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "1/100", 'rate_to_stop': "32768/32761", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 24 assert spec == 24 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "2/1", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 25 assert spec == 25 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "32761/32768", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 26 assert spec == 26 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "1/100", 'rate_to_stop': "1/2", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 27 assert spec == 27 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "1/2", 'time_unit': 0x8fffffffffffffff, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 28 assert spec == 28 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "1/2", 'time_unit': -1, 'expire_after': 0, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 29 assert spec == 29 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "1/2", 'time_unit': 0x7fffffffffffffff, 'expire_after': -1, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 30 assert spec == 30 currency_spec_dict = { 'name': name, 'symbol': symbol, 'variation_specs': [{ 'condition': 0, 'variation_type': "simple", 'rate': "-1/100", 'rate_to_stop': "1/2", 'time_unit': 0x7fffffffffffffff, 'expire_after': 0x8000000000000000, }], } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 31 assert spec == 31 currency_spec_dict = { 'name': name, 'symbol': symbol, 'option_witnesses_required': "yes", 'option_expiration_rebased': True, 'option_conditions_irreversible': False, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 31.5 assert spec == 31.5 currency_spec_dict = { 'name': name, 'symbol': symbol, 'option_witnesses_required': "yes", 'option_expiration_rebased': "yes", 'option_conditions_irreversible': False, } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 32 assert spec == 32 currency_spec_dict = { 'name': name, 'symbol': symbol, 'option_witnesses_required': True, 'option_expiration_rebased': True, 'option_conditions_irreversible': "no", } try: spec = token_lib.CurrencySpec(currency_spec_dict) except TypeError: spec = 33 assert spec == 33