def reset(self): """ Resets the arrays not the stored account history """ self.own_vests = [ Amount(0, self.dpay.vests_symbol, dpay_instance=self.dpay) ] self.own_dpay = [ Amount(0, self.dpay.dpay_symbol, dpay_instance=self.dpay) ] self.own_bbd = [ Amount(0, self.dpay.bbd_symbol, dpay_instance=self.dpay) ] self.delegated_vests_in = [{}] self.delegated_vests_out = [{}] self.timestamps = [addTzInfo(datetime(1970, 1, 1, 0, 0, 0, 0))] import dpayclibase.operationids self.ops_statistics = dpayclibase.operationids.operations.copy() for key in self.ops_statistics: self.ops_statistics[key] = 0 self.reward_timestamps = [] self.author_rewards = [] self.curation_rewards = [] self.curation_per_1000_SP_timestamp = [] self.curation_per_1000_SP = [] self.out_vote_timestamp = [] self.out_vote_weight = [] self.in_vote_timestamp = [] self.in_vote_weight = [] self.in_vote_rep = [] self.in_vote_rshares = [] self.vp = [] self.vp_timestamp = [] self.rep = [] self.rep_timestamp = []
def test_leeq(self): a1 = Amount(1, self.symbol) a2 = Amount(1, self.symbol) self.assertTrue(a1 <= a2) self.assertTrue(a1 >= a2) self.assertTrue(a1 <= 1) self.assertTrue(a1 >= 1)
def test_ltge(self): a1 = Amount(1, self.symbol) a2 = Amount(2, self.symbol) self.assertTrue(a1 < a2) self.assertTrue(a2 > a1) self.assertTrue(a2 > 1) self.assertTrue(a1 < 5)
def test_ne(self): a1 = Amount(1, self.symbol) a2 = Amount(2, self.symbol) self.assertTrue(a1 != a2) self.assertTrue(a1 != 5) a1 = Amount(1, self.symbol) a2 = Amount(1, self.symbol) self.assertTrue(a1 == a2) self.assertTrue(a1 == 1)
def test_json_appbase(self): asset = Asset("BBD", dpay_instance=self.bts) amount = Amount("1", asset, new_appbase_format=False, dpay_instance=self.bts) if self.bts.rpc.get_use_appbase(): self.assertEqual( amount.json(), [str(1 * 10**asset.precision), asset.precision, asset.asset]) else: self.assertEqual(amount.json(), "1.000 BBD")
def test_init(self): # self.assertEqual(1, 1) Price("0.315 BEX/BBD") Price(1.0, "BEX/BBD") Price(0.315, base="BEX", quote="BBD") Price(0.315, base=Asset("BEX"), quote=Asset("BBD")) Price({ "base": {"amount": 1, "asset_id": "BBD"}, "quote": {"amount": 10, "asset_id": "BEX"}}) Price("", quote="10 BBD", base="1 BEX") Price("10 BBD", "1 BEX") Price(Amount("10 BBD"), Amount("1 BEX"))
def test_plus(self): a1 = Amount(1, self.symbol) a2 = Amount(2, self.symbol) self.dotest(a1 + a2, 3, self.symbol) self.dotest(a1 + 2, 3, self.symbol) with self.assertRaises(Exception): a1 + Amount(1, asset=self.asset2) # inline a2 = Amount(2, self.symbol) a2 += a1 self.dotest(a2, 3, self.symbol) a2 += 5 self.dotest(a2, 8, self.symbol) with self.assertRaises(Exception): a1 += Amount(1, asset=self.asset2)
def test_json_appbase2(self): asset = Asset("BBD", dpay_instance=self.bts) amount = Amount("1", asset, new_appbase_format=True, dpay_instance=self.bts) if self.bts.rpc.get_use_appbase(): self.assertEqual( amount.json(), { 'amount': str(1 * 10**asset.precision), 'nai': asset.asset, 'precision': asset.precision }) else: self.assertEqual(amount.json(), "1.000 BBD")
def test_transfer_2of2_offline(self): # Send a 2 of 2 transaction from dpaycli5 which needs dpaycli4's cosign to send # funds but sign the transaction with dpaycli5's key and then serialize the transaction # and deserialize the transaction. After that, sign with dpaycli4's key. dpay = self.bts dpay.nobroadcast = False dpay.wallet.unlock("123") # dpay.wallet.removeAccount("dpaycli4") dpay.wallet.removePrivateKeyFromPublicKey(str(PublicKey(self.active_private_key_of_dpaycli4, prefix=core_unit))) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 serialized/deserialized transaction'})) tx.appendSigner("dpaycli5", "active") tx.addSigningInformation("dpaycli5", "active") tx.sign() tx.clearWifs() self.assertEqual(len(tx['signatures']), 1) # dpay.wallet.removeAccount("dpaycli5") dpay.wallet.removePrivateKeyFromPublicKey(str(PublicKey(self.active_private_key_of_dpaycli5, prefix=core_unit))) dpay.wallet.addPrivateKey(self.active_private_key_of_dpaycli4) tx.appendMissingSignatures() tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True dpay.wallet.addPrivateKey(self.active_private_key_of_dpaycli5)
def test_minus(self): a1 = Amount(1, self.symbol) a2 = Amount(2, self.symbol) self.dotest(a1 - a2, -1, self.symbol) self.dotest(a1 - 5, -4, self.symbol) with self.assertRaises(Exception): a1 - Amount(1, asset=self.asset2) # inline a2 = Amount(2, self.symbol) a2 -= a1 self.dotest(a2, 1, self.symbol) a2 -= 1 self.dotest(a2, 0, self.symbol) self.dotest(a2 - 2, -2, self.symbol) with self.assertRaises(Exception): a1 -= Amount(1, asset=self.asset2)
def test_amount(self, node_param): if node_param == "instance": stm = DPay(node="https://abc.d", autoconnect=False, num_retries=1) set_shared_dpay_instance(self.bts) o = Amount("1 BBD") self.assertIn(o.dpay.rpc.url, self.urls) with self.assertRaises(RPCConnection): Amount("1 BBD", dpay_instance=stm) else: set_shared_dpay_instance( DPay(node="https://abc.d", autoconnect=False, num_retries=1)) stm = self.bts o = Amount("1 BBD", dpay_instance=stm) self.assertIn(o.dpay.rpc.url, self.urls) with self.assertRaises(RPCConnection): Amount("1 BBD")
def test_init(self): stm = self.bts # String init asset = Asset("BBD", dpay_instance=stm) symbol = asset["symbol"] precision = asset["precision"] amount = Amount("1 {}".format(symbol), dpay_instance=stm) self.dotest(amount, 1, symbol) # Amount init amount = Amount(amount, dpay_instance=stm) self.dotest(amount, 1, symbol) # blockchain dict init amount = Amount({ "amount": 1 * 10**precision, "asset_id": asset["id"] }, dpay_instance=stm) self.dotest(amount, 1, symbol) # API dict init amount = Amount({ "amount": 1.3 * 10**precision, "asset": asset["id"] }, dpay_instance=stm) self.dotest(amount, 1.3, symbol) # Asset as symbol amount = Amount(1.3, Asset("BBD"), dpay_instance=stm) self.dotest(amount, 1.3, symbol) # Asset as symbol amount = Amount(1.3, symbol, dpay_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=Asset("BBD", dpay_instance=stm), dpay_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=dict(Asset("BBD", dpay_instance=stm)), dpay_instance=stm) self.dotest(amount, 1.3, symbol) # keyword inits amount = Amount(amount=1.3, asset=symbol, dpay_instance=stm) self.dotest(amount, 1.3, symbol)
def test_sell(self): bts = self.bts bts.txbuffer.clear() m = Market(u'BEX:BBD', dpay_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 BBD', dpay_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 BEX', dpay_instance=bts)), op["amount_to_sell"]) p = Price(5, u"BBD:BEX") tx = m.sell(p, 0.1, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 BBD', dpay_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 BEX', dpay_instance=bts)), op["amount_to_sell"]) p = Price(5, u"BBD:BEX", dpay_instance=bts) a = Amount(0.1, "BEX", dpay_instance=bts) tx = m.sell(p, a, account="test") op = tx["operations"][0][1] self.assertEqual(str(Amount('0.500 BBD', dpay_instance=bts)), op["min_to_receive"]) self.assertEqual(str(Amount('0.100 BEX', dpay_instance=bts)), op["amount_to_sell"])
def time_transfer(self): self.op = operations.Transfer(**{ "from": "foo", "to": "baar", "amount": Amount("111.110 BEX", dpay_instance=self.stm), "memo": "Fooo", "prefix": self.default_prefix }) self.doit()
def test_verifyAuthority(self): stm = self.bts stm.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1.300 BBD", dpay_instance=stm), "memo": "Foobar"})) account = Account("dpaycli", dpay_instance=stm) tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() tx.verify_authority() self.assertTrue(len(tx["signatures"]) > 0)
def test_Transfer_broadcast(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, expiration=10, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) tx.appendSigner("dpaycli", "active") tx.sign() tx.broadcast()
def test_transfer_2of2_simple(self): # Send a 2 of 2 transaction from elf which needs dpaycli4's cosign to send funds dpay = self.bts dpay.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 simple transaction'})) tx.appendWif(self.active_private_key_of_dpaycli5) tx.sign() tx.clearWifs() tx.appendWif(self.active_private_key_of_dpaycli4) tx.sign(reconstruct_tx=False) self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True
def test_transfer_1of1(self): dpay = self.bts dpay.nobroadcast = False tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '1 of 1 transaction'})) self.assertEqual( tx["operations"][0]["type"], "transfer_operation" ) tx.appendWif(self.active_key) tx.sign() tx.sign() self.assertEqual(len(tx['signatures']), 1) tx.broadcast() dpay.nobroadcast = True
def test_TransactionConstructor(self): stm = self.bts opTransfer = Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""}) tx1 = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx1.appendOps(opTransfer) tx = TransactionBuilder(tx1, dpay_instance=stm) self.assertFalse(tx.is_empty()) self.assertTrue(len(tx.list_operations()) == 1) self.assertTrue(repr(tx) is not None) self.assertTrue(str(tx) is not None) account = Account("dpaycli", dpay_instance=stm) tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_div2(self): p1 = Price(10.0, "BEX/BBD") p2 = Price(5.0, "BEX/BBD") # 10 BEX/BBD / 5 BEX/VESTS = 2 VESTS/BBD 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 BBD") 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_transfer(self, node_param): if node_param == "normal": bts = self.bts acc = self.account elif node_param == "testnet": bts = self.testnet acc = self.account_testnet acc.dpay.txbuffer.clear() tx = acc.transfer("test", 1.33, "BBD", memo="Foobar", account="test1") self.assertEqual(tx["operations"][0][0], "transfer") self.assertEqual(len(tx["operations"]), 1) op = tx["operations"][0][1] self.assertIn("memo", op) self.assertEqual(op["memo"], "Foobar") self.assertEqual(op["from"], "test1") self.assertEqual(op["to"], "test") amount = Amount(op["amount"], dpay_instance=bts) self.assertEqual(float(amount), 1.33)
def test_transfer_2of2_wallet(self): # Send a 2 of 2 transaction from dpaycli5 which needs dpaycli4's cosign to send # priv key of dpaycli5 and dpaycli4 are stored in the wallet # appendSigner fetches both keys and signs automatically with both keys. dpay = self.bts dpay.nobroadcast = False dpay.wallet.unlock("123") tx = TransactionBuilder(use_condenser_api=True, dpay_instance=dpay) tx.appendOps(Transfer(**{"from": 'dpaycli5', "to": 'dpaycli1', "amount": Amount("0.01 BEX", dpay_instance=dpay), "memo": '2 of 2 serialized/deserialized transaction'})) tx.appendSigner("dpaycli5", "active") tx.sign() self.assertEqual(len(tx['signatures']), 2) tx.broadcast() dpay.nobroadcast = True
def test_pow(self): a1 = Amount(15, self.symbol) a2 = Amount(3, self.symbol) self.dotest(a1**3, 15**3, self.symbol) self.dotest(a1**a2, 15**3, self.symbol) self.dotest(a1**2, 15**2, self.symbol) with self.assertRaises(Exception): a1**Amount(1, asset=self.asset2) # inline a2 = a1.copy() a2 **= 3 self.dotest(a2, 15**3, self.symbol) with self.assertRaises(Exception): a1 **= Amount(2, asset=self.asset2)
def test_mod(self): a1 = Amount(15, self.symbol) a2 = Amount(3, self.symbol) self.dotest(a1 % 3, 0, self.symbol) self.dotest(a1 % a2, 0, self.symbol) self.dotest(a1 % 2, 1, self.symbol) with self.assertRaises(Exception): a1 % Amount(1, asset=self.asset2) # inline a2 = a1.copy() a2 %= 3 self.dotest(a2, 0, self.symbol) with self.assertRaises(Exception): a1 %= Amount(2, asset=self.asset2)
def test_div(self): a1 = Amount(15, self.symbol) self.dotest(a1 / 3, 5, self.symbol) self.dotest(a1 // 2, 7, self.symbol) with self.assertRaises(Exception): a1 / Amount(1, asset=self.asset2) # inline a2 = a1.copy() a2 /= 3 self.dotest(a2, 5, self.symbol) a2 = a1.copy() a2 //= 2 self.dotest(a2, 7, self.symbol) with self.assertRaises(Exception): a1 *= Amount(2, asset=self.asset2)
def test_transfer(self): bts = self.bts bts.nobroadcast = False bts.wallet.unlock("123") # bts.wallet.addPrivateKey(self.active_key) # bts.prefix ="STX" acc = Account("dpaycli", dpay_instance=bts) tx = acc.transfer( "dpaycli1", 1.33, "BBD", memo="Foobar") self.assertEqual( tx["operations"][0][0], "transfer" ) self.assertEqual(len(tx['signatures']), 1) op = tx["operations"][0][1] self.assertIn("memo", op) self.assertEqual(op["from"], "dpaycli") self.assertEqual(op["to"], "dpaycli1") amount = Amount(op["amount"], dpay_instance=bts) self.assertEqual(float(amount), 1.33) bts.nobroadcast = True
def test_appendSigner(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), keys=[self.active_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) account = Account("dpaycli", dpay_instance=stm) with self.assertRaises( AssertionError ): tx.appendSigner(account, "abcdefg") tx.appendSigner(account, "active") self.assertTrue(len(tx.wifs) > 0) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_mul(self): a1 = Amount(5, self.symbol) a2 = Amount(2, self.symbol) self.dotest(a1 * a2, 10, self.symbol) self.dotest(a1 * 3, 15, self.symbol) with self.assertRaises(Exception): a1 * Amount(1, asset=self.asset2) # inline a2 = Amount(2, self.symbol) a2 *= 5 self.dotest(a2, 10, self.symbol) a2 = Amount(2, self.symbol) a2 *= a1 self.dotest(a2, 10, self.symbol) with self.assertRaises(Exception): a1 *= Amount(2, asset=self.asset2)
def test_appendWif(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) with self.assertRaises( MissingKeyError ): tx.sign() with self.assertRaises( InvalidWifError ): tx.appendWif("abcdefg") tx.appendWif(self.active_key) tx.sign() self.assertTrue(len(tx["signatures"]) > 0)
def test_verifyAuthorityException(self): nodelist = NodeList() stm = DPay(node=nodelist.get_testnet(), keys=[self.posting_key], nobroadcast=True, expiration=120, num_retries=10) tx = TransactionBuilder(use_condenser_api=True, dpay_instance=stm) tx.appendOps(Transfer(**{"from": "dpaycli", "to": "dpaycli1", "amount": Amount("1 BEX", dpay_instance=stm), "memo": ""})) account = Account("dpaycli2", dpay_instance=stm) tx.appendSigner(account, "active") tx.appendWif(self.posting_key) self.assertTrue(len(tx.wifs) > 0) tx.sign() with self.assertRaises( exceptions.MissingRequiredActiveAuthority ): tx.verify_authority() self.assertTrue(len(tx["signatures"]) > 0)