def test_sell(self, node_param): if node_param == "non_appbase": bts = self.bts else: bts = self.appbase bts.txbuffer.clear() m = Market(u'STEEM:SBD', steem_instance=bts) tx = m.sell(5, 0.1, account="test") self.assertEqual((tx["operations"][0][0]), "limit_order_create") op = tx["operations"][0][1] self.assertIn("test", op["owner"]) self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 STEEM', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"SBD:STEEM") tx = m.sell(p, 0.1, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 STEEM', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"SBD:STEEM", steem_instance=bts) a = Amount(0.1, "STEEM", steem_instance=bts) tx = m.sell(p, a, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 STEEM', steem_instance=bts)), op["amount_to_sell"])
def test_price(self, node_param): if node_param == "instance": set_shared_steem_instance(self.bts) o = Price( 10.0, "%s/%s" % (self.bts.token_symbol, self.bts.backed_token_symbol)) self.assertIn(o.blockchain.rpc.url, self.urls) with self.assertRaises(RPCConnection): Price(10.0, "%s/%s" % (self.bts.token_symbol, self.bts.backed_token_symbol), steem_instance=Steem(node="https://abc.d", autoconnect=False, num_retries=1)) else: set_shared_steem_instance( Steem(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = Price(10.0, "%s/%s" % (self.bts.token_symbol, self.bts.backed_token_symbol), steem_instance=stm) self.assertIn(o.blockchain.rpc.url, self.urls) with self.assertRaises(RPCConnection): Price( 10.0, "%s/%s" % (self.bts.token_symbol, self.bts.backed_token_symbol))
def test_sell(self): bts = self.bts bts.txbuffer.clear() m = Market(u'HIVE:HBD', steem_instance=bts) tx = m.sell(5, 0.1, account="test") self.assertEqual( (tx["operations"][0][0]), "limit_order_create" ) op = tx["operations"][0][1] self.assertIn("test", op["owner"]) self.assertEqual(str(Amount('0.500 HBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 HIVE', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"HBD:HIVE") tx = m.sell(p, 0.1, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 HBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 HIVE', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"HBD:HIVE", steem_instance=bts) a = Amount(0.1, "HIVE", steem_instance=bts) tx = m.sell(p, a, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 HBD', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 HIVE', steem_instance=bts)), op["amount_to_sell"])
def test_buy(self): bts = self.bts m = Market(u'STEEM:SBD', steem_instance=bts) bts.txbuffer.clear() tx = m.buy(5, 0.1, account="test") self.assertEqual((tx["operations"][0][0]), "limit_order_create") op = tx["operations"][0][1] self.assertIn("test", op["owner"]) self.assertEqual(str(Amount('0.100 STEEM', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"SBD:STEEM", steem_instance=bts) tx = m.buy(p, 0.1, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.100 STEEM', steem_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["amount_to_sell"]) p = Price(5, u"SBD:STEEM", steem_instance=bts) a = Amount(0.1, "STEEM", steem_instance=bts) tx = m.buy(p, a, account="test") op = tx["operations"][0][1] self.assertEqual(str(a), op["min_to_receive"]) self.assertEqual(str(Amount('0.500 SBD', steem_instance=bts)), op["amount_to_sell"])
def test_ne(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "STEEM/SBD") self.assertTrue(p1 != p2) self.assertTrue(p1 == p1) self.assertTrue(p1 != 5) self.assertTrue(p1 == 10)
def test_leeq(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "STEEM/SBD") self.assertTrue(p1 >= p2) self.assertTrue(p2 <= p1) self.assertTrue(p1 >= 5) self.assertTrue(p2 <= 10)
def test_ltge(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "STEEM/SBD") self.assertTrue(p1 > p2) self.assertTrue(p2 < p1) self.assertTrue(p1 > 5) self.assertTrue(p2 < 10)
def test_div(self): p1 = Price(10.0, "HIVE/HBD") p2 = Price(5.0, "HIVE/VESTS") # 10 HIVE/HBD / 5 HIVE/VESTS = 2 VESTS/HBD p3 = p1 / p2 p4 = p3.as_base("VESTS") self.assertEqual(p4["base"]["symbol"], "VESTS") self.assertEqual(p4["quote"]["symbol"], "HBD") # 10 HIVE/HBD * 0.2 VESTS/HIVE = 2 VESTS/HBD = 0.5 HBD/VESTS self.assertEqual(float(p4), 2)
def test_div(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "STEEM/VESTS") # 10 STEEM/SBD / 5 STEEM/VESTS = 2 VESTS/SBD p3 = p1 / p2 p4 = p3.as_base("VESTS") self.assertEqual(p4["base"]["symbol"], "VESTS") self.assertEqual(p4["quote"]["symbol"], "SBD") # 10 STEEM/SBD * 0.2 VESTS/STEEM = 2 VESTS/SBD = 0.5 SBD/VESTS self.assertEqual(float(p4), 2)
def test_price(self, node_param): if node_param == "instance": stm = Steem(node="abc", autoconnect=False, num_retries=1) set_shared_steem_instance(self.bts) o = Price(10.0, "STEEM/SBD") self.assertIn(o.steem.rpc.url, self.urls) with self.assertRaises(NumRetriesReached): Price(10.0, "STEEM/SBD", steem_instance=stm) else: set_shared_steem_instance( Steem(node="abc", autoconnect=False, num_retries=1)) stm = self.bts o = Price(10.0, "STEEM/SBD", steem_instance=stm) self.assertIn(o.steem.rpc.url, self.urls) with self.assertRaises(NumRetriesReached): Price(10.0, "STEEM/SBD")
def test_price(self, node_param): if node_param == "instance": set_shared_steem_instance(self.bts) o = Price(10.0, "STEEM/SBD") self.assertIn(o.steem.rpc.url, self.urls) with self.assertRaises( RPCConnection ): Price(10.0, "STEEM/SBD", steem_instance=Steem(node="https://abc.d", autoconnect=False, num_retries=1)) else: set_shared_steem_instance(Steem(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = Price(10.0, "STEEM/SBD", steem_instance=stm) self.assertIn(o.steem.rpc.url, self.urls) with self.assertRaises( RPCConnection ): Price(10.0, "STEEM/SBD")
def test_div2(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "STEEM/SBD") # 10 STEEM/SBD / 5 STEEM/VESTS = 2 VESTS/SBD p3 = p1 / p2 self.assertTrue(isinstance(p3, (float, int))) self.assertEqual(float(p3), 2.0) p3 = p1 / 5 self.assertEqual(float(p3), 2.0) p3 = p1 / Amount("1 SBD") self.assertEqual(float(p3), 0.1) p3 = p1 p3 /= p2 self.assertEqual(float(p3), 2.0) p3 = p1 p3 /= 5 self.assertEqual(float(p3), 2.0)
def test_init(self): # self.assertEqual(1, 1) Price("0.315 STEEM/SBD") Price(1.0, "STEEM/SBD") Price(0.315, base="STEEM", quote="SBD") Price(0.315, base=Asset("STEEM"), quote=Asset("SBD")) Price({ "base": {"amount": 1, "asset_id": "SBD"}, "quote": {"amount": 10, "asset_id": "STEEM"}}) Price("", quote="10 SBD", base="1 STEEM") Price("10 SBD", "1 STEEM") Price(Amount("10 SBD"), Amount("1 STEEM"))
def test_init(self): # self.assertEqual(1, 1) Price("0.315 HIVE/HBD") Price(1.0, "HIVE/HBD") Price(0.315, base="HIVE", quote="HBD") Price(0.315, base=Asset("HIVE"), quote=Asset("HBD")) Price({ "base": {"amount": 1, "asset_id": "HBD"}, "quote": {"amount": 10, "asset_id": "HIVE"}}) Price("", quote="10 HBD", base="1 HIVE") Price("10 HBD", "1 HIVE") Price(Amount("10 HBD"), Amount("1 HIVE"))
def test_multiplication(self): p1 = Price(10.0, "STEEM/SBD") p2 = Price(5.0, "VESTS/STEEM") p3 = p1 * p2 p4 = p3.as_base("SBD") p4_2 = p3.as_quote("VESTS") self.assertEqual(p4["quote"]["symbol"], "VESTS") self.assertEqual(p4["base"]["symbol"], "SBD") # 10 STEEM/SBD * 0.2 VESTS/STEEM = 50 VESTS/SBD = 0.02 SBD/VESTS self.assertEqual(float(p4), 0.02) self.assertEqual(p4_2["quote"]["symbol"], "VESTS") self.assertEqual(p4_2["base"]["symbol"], "SBD") self.assertEqual(float(p4_2), 0.02) p3 = p1 * 5 self.assertEqual(float(p3), 50) # Inline multiplication p5 = Price(10.0, "STEEM/SBD") p5 *= p2 p4 = p5.as_base("SBD") self.assertEqual(p4["quote"]["symbol"], "VESTS") self.assertEqual(p4["base"]["symbol"], "SBD") # 10 STEEM/SBD * 0.2 VESTS/STEEM = 2 VESTS/SBD = 0.02 SBD/VESTS self.assertEqual(float(p4), 0.02) p6 = Price(10.0, "STEEM/SBD") p6 *= 5 self.assertEqual(float(p6), 50)
def store_blocks(self, ops): filename = self.get_filename() if self.db_type == "shelve": with shelve.open(filename) as db: db['ops'] = ops elif self.db_type == "pickle": db = {"ops": ops} save(filename, db) elif self.db_type == "deepdish": db = {"ops": ops} dd.io.save(filename, db, compression=None) elif self.db_type == "sqlite": with dataset.connect('sqlite:///%s?check_same_thread=False' % (filename)) as db: table = db["ops"] for op in ops: json_params = [ "required_auths", "required_posting_auths", "extensions", "proposal_ids", "active", "posting", "props", "owner", "new_owner_authority", "recent_owner_authority" ] amount_params = [ "reward_steem", "reward_sbd", "reward_vests", "fee", "max_accepted_payout", "amount", "sbd_amount", "steem_amount", "daily_pay", "min_to_receive", "amount_to_sell" ] datetime_params = [ "start_date", "end_date", "escrow_expiration", "expiration", "ratification_deadline" ] price_params = ["exchange_rate"] for key in op: if key in json_params: op[key] = json.dumps(op[key]) elif key in amount_params: op[key] = str(Amount(op[key])) elif key in datetime_params: op[key] = formatTimeString(op[key]) elif key in price_params: op[key] = str(Price(op[key])) if "id" in op: json_id = op.pop("id") op["json_id"] = json_id table.insert(op)
def store_account_hist(self, ops): filename = self.get_filename() if self.db_type == "shelve": with shelve.open(filename) as db: db['ops'] = ops elif self.db_type == "pickle": db = {"ops": ops} save(filename, db) elif self.db_type == "deepdish": db = {"ops": ops} dd.io.save(filename, db, compression=None) elif self.db_type == "sqlite": with dataset.connect('sqlite:///%s?check_same_thread=False' % (filename)) as db: table = db["history"] for op in ops: if "index" not in op: print(op) index = None else: index = op.pop("index") _id = op.pop("_id") _type = op.pop("type") virtual_op = op.pop("virtual_op") block = op.pop("block") trx_in_block = op.pop("trx_in_block") if trx_in_block < 0: trx_in_block = 0 if "trx_id" in op: trx_id = op.pop("trx_id") if trx_id == "0000000000000000000000000000000000000000": trx_in_block = 0 else: print(op) trx_id = None timestamp = formatTimeString(op.pop("timestamp")) op_in_trx = op.pop("op_in_trx") parameter_list = [ "from", "to", "memo", "voter", "permlink", "account", "weight", "author", "json_metadata", "curator", "reward", "comment_permlink", "comment_author", "amount", "parent_author", "parent_permlink", "title", "body", "reward_steem", "reward_sbd", "reward_vests", "id", "json", "delegator", "delegatee", "vesting_shares", "witness", "approve", "sbd_payout", "steem_payout", "vesting_payout", "owner", "orderid", "amount_to_sell", "min_to_receive", "fill_or_kill", "expiration", "current_owner", "current_orderid", "current_pays", "open_owner", "open_orderid", "open_pays", "benefactor", "producer", "publisher", "exchange_rate", "required_auths", "required_posting_auths", "max_accepted_payout", "percent_steem_dollars", "allow_votes", "allow_curation_rewards", "extensions", "proposal_ids", "fee", "creator", "new_account_name", "active", "posting", "delegation", "memo_key", "props", "url", "requestid", "amount_in", "amount_out", "posting_json_metadata", "account_to_recover", "new_recovery_account", "block_signing_key", "maximum_block_size", "sbd_interest_rate", "proxy", "from_account", "to_account", "withdrawn", "deposited", "interest", "request_id", "recovery_account", "new_owner_authority", "recent_owner_authority", "percent", "auto_vest" ] amount_params = [ "amount", "reward", "reward_steem", "reward_sbd", "reward_vests", "vesting_shares", "sbd_payout", "steem_payout", "vesting_payout", "amount_to_sell", "min_to_receive", "current_pays", "open_pays", "max_accepted_payout", "fee", "delegation", "amount_in", "amount_out", "withdrawn", "deposited", "interest" ] price_params = ["exchange_rate"] json_params = [ "required_auths", "required_posting_auths", "extensions", "proposal_ids", "active", "posting", "props", "owner", "new_owner_authority", "recent_owner_authority" ] extracted_op_values = {} for param in parameter_list: extracted_op_values[param] = None if param in op: if param == "id": extracted_op_values["json_id"] = op.pop("id") elif param in amount_params: extracted_op_values[param] = str( Amount(op.pop(param))) elif param in price_params: extracted_op_values[param] = str( Price(op.pop(param))) elif param in json_params: extracted_op_values[param] = json.dumps( op.pop(param)) else: extracted_op_values[param] = op.pop(param) if len(op) > 0: print(op) db_row = { "index": index, "_id": _id, "type": _type, "virtual_op": virtual_op, "block": block, "trx_in_block": trx_in_block, "trx_id": trx_id, "timestamp": timestamp, "op_in_trx": op_in_trx, "op": op } for param in extracted_op_values: if extracted_op_values[param] is None: continue db_row[param] = extracted_op_values[param] table.insert(db_row)
def test_price(self): o = Price(10.0, "STEEM/SBD") self.assertEqual(o.steem.rpc.url, self.url)