Ejemplo n.º 1
0
    def _test_transfer_qa(self):
        chain.set_active_chain(chain.TestNet)

        account = Account(address="b50c2404e699fd985f71b2c3f032059f13d6543b")
        print(account)
        print("Account1 balance", repr(account.get_balance()))

        account2 = Account.from_keystore(
            "zxcvbnm,", path_join("crypto", "zilliqa_keystore.json"))
        print(account2)
        balance = account2.get_balance()
        print("Account2 balance", repr(balance))
        assert balance > 0

        txn_info = account2.transfer(account.bech32_address, Qa(123456789))
        pprint(txn_info)

        txn_details = account2.wait_txn_confirm(txn_info["TranID"],
                                                timeout=240)
        pprint(txn_details)
        assert txn_details

        print("Account1 balance", repr(account.get_balance()))
        print("Account1 balance", repr(account.get_balance_qa()))

        account = Account(
            private_key=
            "d0b47febbef2bd0c4a4ee04aa20b60d61eb02635e8df5e7fd62409a2b1f5ddf8")
        txn_info = account.transfer(account2.checksum_address,
                                    Qa(123456789),
                                    confirm=True)
        pprint(txn_info)
Ejemplo n.º 2
0
    def transfer(self, to_addr: str,
                 zils: Union[str, float, Zil, Qa],
                 nonce: Optional[int]=None,
                 gas_price: Optional[int]=None, gas_limit=1,
                 code="", data="", priority=False,
                 confirm=False, timeout=300, sleep=20, hit_chain=False):
        """Transfer zils to another address."""
        if not self.zil_key or not self.zil_key.encoded_private_key:
            raise RuntimeError("can not create transaction without private key")

        to_addr = zilkey.normalise_address(to_addr)
        if not to_addr:
            raise ValueError("invalid to address")

        if isinstance(zils, Qa):
            amount = zils
        else:
            if not isinstance(zils, Zil):
                zils = Zil(zils)
            amount = zils.toQa()

        if gas_price is None:
            if hit_chain:
                gas_price = self.get_min_gas_price(refresh=False)
            else:
                gas_price = 100

        if nonce is None:
            resp = self.get_balance_nonce()
            if amount > Qa(resp["balance"]):
                raise ValueError("insufficient balance to send")
            nonce = resp["nonce"] + 1

        params = active_chain.build_transaction_params(
            self.zil_key, to_addr,
            amount, nonce,
            gas_price, gas_limit,
            code, data, priority
        )
        self.last_params = params

        if not hit_chain:
            return None
        else:
            txn_info = active_chain.api.CreateTransaction(params)
            self.last_txn_info = txn_info
            if not confirm:
                return txn_info

            if not txn_info:
                return None

            txn_details = Account.wait_txn_confirm(
                txn_info["TranID"],
                timeout=timeout, sleep=sleep
            )
            self.last_txn_details = txn_details
            return txn_details
Ejemplo n.º 3
0
def test_txn(args):
    global ERROR_MESSAGE
    global SEND_ZILS_FORWARD_OR_BACK

    # Load source account
    account = Account(address=args['srchex'], private_key=args['srckey'])
    balance = account.get_balance()

    # Load destination account
    account2 = Account(address=args['dsthex'], private_key=args['dstkey'])
    balance2 = account2.get_balance()

    if SEND_ZILS_FORWARD_OR_BACK == True:
        print("SRC: {}: {}".format(account, balance))
        print("DST: {}: {}".format(account2, balance2))
        # Send 100 Qa from srchex to dstzil
        txn_info = account.transfer(to_addr=args['dstzil'], zils=Qa(100))
        SEND_ZILS_FORWARD_OR_BACK = False
    else:
        print("SRC: {}: {}".format(account2, balance2))
        print("DST: {}: {}".format(account, balance))
        # Send 100 Qa from dsthex to srczil
        txn_info = account2.transfer(to_addr=args['srczil'], zils=Qa(100))
        SEND_ZILS_FORWARD_OR_BACK = True

    pprint(txn_info)
    txn_id = txn_info["TranID"]

    sys.stdout.flush()

    # Wait for confirmation (timeout = 20mins, to take into account a view change)
    txn_details = account.wait_txn_confirm(txn_id, timeout=1200)
    pprint(txn_details)
    if txn_details and txn_details["receipt"]["success"]:
        print("Txn success: {}".format(txn_id))
    else:
        print("Txn failed: {}".format(txn_id))
        raise Exception("Txn failed: {}".format(txn_id))
Ejemplo n.º 4
0
    def transfer(self, to_addr: str,
                 zils: Union[str, float, Zil, Qa],
                 nonce: Optional[int]=None,
                 gas_price: Optional[int]=None, gas_limit=1,
                 code="", data="", priority=False):
        """Transfer zils to another address."""
        if not self.zil_key or not self.zil_key.encoded_private_key:
            raise RuntimeError("can not create transaction without private key")

        to_addr = zilkey.to_checksum_address(to_addr)
        if not to_addr:
            raise ValueError("invalid to address")

        if isinstance(zils, Qa):
            amount = zils
        else:
            if not isinstance(zils, Zil):
                zils = Zil(zils)
            amount = zils.toQa()

        if gas_price is None:
            gas_price = self.get_min_gas_price(refresh=False)

        if nonce is None:
            resp = self.get_balance_nonce()
            if amount > Qa(resp["balance"]):
                raise ValueError("insufficient balance to send")
            nonce = resp["nonce"] + 1

        params = active_chain.build_transaction_params(
            self.zil_key, to_addr,
            amount, nonce,
            gas_price, gas_limit,
            code, data, priority
        )

        txn_info = active_chain.api.CreateTransaction(params)
        return txn_info
Ejemplo n.º 5
0
    def test_transfer_qa(self):
        chain.set_active_chain(chain.TestNet)

        account = Account(address="b50c2404e699fd985f71b2c3f032059f13d6543b")
        print(account)
        print("Account1 balance", repr(account.get_balance()))

        account2 = Account.from_keystore("zxcvbnm,", path_join("crypto", "zilliqa_keystore.json"))
        print(account2)
        balance = account2.get_balance()
        print("Account2 balance", repr(balance))
        assert balance > 0

        to_addr = account.address
        txn_info = account2.transfer(to_addr, Qa(123456789))
        pprint(txn_info)

        txn_details = account2.wait_txn_confirm(txn_info["TranID"], timeout=120)
        pprint(txn_details)
        assert txn_details

        print("Account1 balance", repr(account.get_balance()))
        print("Account1 balance", repr(account.get_balance_qa()))
Ejemplo n.º 6
0
 def get_balance_qa(self) -> Qa:
     resp = self.get_balance_nonce()
     return Qa(resp["balance"])
Ejemplo n.º 7
0
 def get_balance(self) -> Zil:
     """Return account balance in Zil."""
     resp = self.get_balance_nonce()
     return Qa(resp["balance"]).toZil()