Пример #1
0
def test_sign_offline(cluster):
    """
    check simple transfer tx success
    - send 1cro from community to reserve
    """
    # 1. first create two hd new wallet
    seed = "dune car envelope chuckle elbow slight proud fury remove candy uphold \
    puzzle call select sibling sport gadget please want vault glance verb damage gown"

    wallet_1 = Wallet(seed)
    address_1 = wallet_1.address
    wallet_2 = Wallet.new()
    address_2 = wallet_2.address

    sender_addr = cluster.address("signer1")

    sender_balance = cluster.balance(sender_addr)
    assert sender_balance > 100 * 10**8
    balance_1 = cluster.balance(wallet_1.address)
    assert balance_1 == 0
    balance_2 = cluster.balance(wallet_2.address)
    assert balance_2 == 0

    # 2. transfer some coin to wallet_1
    cluster.transfer(sender_addr, address_1, "100cro")
    wait_for_new_blocks(cluster, 2)

    assert cluster.balance(sender_addr) == sender_balance - 100 * 10**8
    assert cluster.balance(address_1) == 100 * 10**8

    # 3. get the send's account info
    port = ports.api_port(cluster.base_port(0))
    api = ApiUtil(port)

    amount = 1 * 10**8
    # make transaction without/with fee
    for fee in [0, 600000]:
        sender_account_info = api.account_info(address_1)
        balance_1_before = api.balance(address_1)
        balance_2_before = api.balance(address_2)
        tx = Transaction(
            wallet=wallet_1,
            account_num=sender_account_info["account_num"],
            sequence=sender_account_info["sequence"],
            chain_id=cluster.chain_id,
            fee=fee,
        )
        tx.add_transfer(to_address=address_2,
                        amount=amount,
                        base_denom="basecro")
        signed_tx = tx.get_pushable()
        assert isinstance(signed_tx, dict)
        api.broadcast_tx(signed_tx)
        wait_for_new_blocks(cluster, 3)
        balance_1_after = api.balance(address_1)
        balance_2_after = api.balance(address_2)
        assert balance_2_after == balance_2_before + amount
        assert balance_1_after == balance_1_before - amount - fee
Пример #2
0
def main():
    seed = "dune car envelope chuckle elbow slight proud fury remove candy uphold puzzle call select sibling sport gadget please want vault glance verb damage gown"
    wallet_1 = Wallet(seed)
    address_1 = wallet_1.address
    print(address_1)
    wallet_2 = Wallet.new()
    address_2 = wallet_2.address
    print(address_2)

    # the api port setted in ${home_dir of chain-maind}/config/app.toml, the default is ~/.chain-maind/config/app.toml
    base_url = "http://127.0.0.1:1317"
    url_tx = f"{base_url}/txs"
    url_account = f"{base_url}/cosmos/auth/v1beta1/accounts/{address_1}"
    url_balance = f"{base_url}/cosmos/bank/v1beta1/balances/{address_1}"

    # get the balance of address_1
    response = requests.get(url_balance)
    balance_1 = int(response.json()["balances"][0]["amount"])
    print(f"balance of address 1: {balance_1}")

    # get the account info
    response = requests.get(url_account)
    account_info = response.json()["account"]
    account_num = int(account_info["account_number"])
    sequence = int(account_info["sequence"])

    # make transaction
    tx = Transaction(
        wallet=wallet_1,
        account_num=account_num,
        sequence=sequence,
        chain_id="test",
        fee=100000,
        gas=300000,
    )
    amount = 1 * 10 ** 8
    tx.add_transfer(to_address=address_2, amount=amount)
    signed_tx = tx.get_pushable()
    print("signed tx:", signed_tx)
    response = requests.post(url_tx, json=signed_tx)
    if not response.ok:
        raise Exception(response.reason)
    result = response.json()
    print(result)
    if result.get("code"):
        raise Exception(result["raw_log"])

    # get the balance after sync
    time.sleep(5)
    response = requests.get(url_balance)
    balance_1_after = int(response.json()["balances"][0]["amount"])
    print(f"balance of address 1 after transfer: {balance_1_after}")
Пример #3
0
 def get_pushTx_sync(self, amount, sequence, timeout_block):
     tx = Transaction(
         wallet=self.wallet_1,
         account_num=self.account_num,
         sequence=sequence,
         chain_id="testnet-croeseid-2",
         fee=20000,
         fee_denom="basetcro",
         gas=200000,
         sync_mode=self.mode,
         memo=str(datetime.now()),
         timeout=timeout_block,
     )
     tx.add_transfer(to_address=self.address_1, amount=amount, base_denom="basetcro")
     signed_tx = tx.get_pushable()
     return signed_tx
Пример #4
0
def test_get_pushable_tx():
    expected_pushable_tx = '{"tx":{"msg":[{"type":"cosmos-sdk/MsgSend","value":{"from_address":"cro1u9q8mfpzhyv2s43js7l5qseapx5kt3g2rf7ppf","to_address":"cro103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf","amount":[{"denom":"basecro","amount":"288000"}]}}],"fee":{"gas":"30000","amount":[{"amount":"100000","denom":"basecro"}]},"memo":"","signatures":[{"signature":"WjB3aB3k/nUK33iyGvbMPu55iiyCJBr7ooKQXwxE1BFAdBjJXIblp1aVPUjlr/blFAlHW7fLJct9zc/7ty8ZQA==","pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AntL+UxMyJ9NZ9DGLp2v7a3dlSxiNXMaItyOXSRw8iYi"},"account_number":"11335","sequence":"0"}]},"mode":"sync"}'
    seed = "dune car envelope chuckle elbow slight proud fury remove candy uphold puzzle call select sibling sport gadget please want vault glance verb damage gown"
    wallet = Wallet(seed)
    fee = 100000
    _tx_total_cost = 388000
    amount = _tx_total_cost - fee

    tx = Transaction(
        wallet=wallet,
        account_num=11335,
        sequence=0,
        fee=fee,
        gas=30000,
        chain_id="test",
    )
    tx.add_transfer(to_address="cro103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf",
                    amount=amount)
    pushable_tx = tx.get_pushable()
    assert pushable_tx == expected_pushable_tx