# Using a Wallet

Terra.wallet(key)

An instance of Key provides you with everything you need to perform standard transaction signing, but the process is quite manual, as described in the Transaction Process Flow. In particular, you will need to manually create an StdSignMsg to sign while keeping track of the chain ID, your account number, and number of transactions signed, and then create a separate StdTx applying your signature.

You can create a Wallet by passing in an instance of Key through the Terra instance.

The Wallet instance acts like an Account Query equipped with convenient functions for creating transactions from messages and signing them.

from jigu import Terra
from jigu.key.mnemonic import MnemonicKey

terra = Terra(...)
key = MnemonicKey.generate()
wallet = terra.wallet(key) # random wallet

# Account Number

Wallet.account_number
wallet.account_number

This is dynamically fetched against the node for the first time, and then cached as it does not change.

# Property Value

int

Account number.

# Sequence Number

Wallet.sequence_number
wallet.sequence_number # -> int

Due to the incrementing nature of sequence, it is dynamically fetched everytime it is requested.

# Property Value

int

Sequence number.

# Create a Transaction

Wallet.create_and_sign_tx(*msgs, fee=None, memo="")

The Wallet object enables you to create a transaction by just including the message and a fee, creating a StdSignMsg with the correct chain_id, account_number, and sequence for you, and then signing it to produce a ready-to-broadcast StdTx.

from jigu.core import StdFee
from jigu.core.msg import MsgSend, MsgSwap

send = MsgSend(
    from_address=wallet.address,
    to_address=friend.address,
    amount=[Coin("uluna", 3000)]
)

swap = MsgSend(
    trader=wallet.address,
    offer_coin=Coin("ukrw", 5000),
    ask_denom="umnt"
)

fee = StdFee.make(gas=200_000, uluna=1_000_000)

# just one message
tx1 = wallet.create_and_sign_tx(
    send,
    fee=fee,
    memo="Memo is optional."
)

# both messages
tx2 = wallet.create_and_sign_tx(
    send, swap,
    fee=fee
)

# Parameters

StdMsg
*msgs

Messages to be included in the transaction.


StdFee
fee
optional

Fee to include. If no fee is included, automatic fee estimation will be used.


DEFAULT None
str
memo
optional

An optional note.


DEFAULT ""

# Returns

StdTx

A signed transaction ready to be broadcasted.

Note that in the above example, both generated StdTx objects tx1 and tx2 will share the same sequence number as they have not yet been broadcasted, and thus cannot both be included in the blockchain. See Multiple Transactions to learn how to work around this.

# Use automatic fee estimation

If you do not supply a fee, the Wallet will try to invoke the node client to to apply automatic fee estimation and apply the result of that fee to the transaction.

See in the following example:

tx3 = wallet.create_and_sign_tx(
    [send, swap],
    memo="Memo makes it more expensive"
)
tx3.fee._pp

See more on automatic fee estimation.

# Multiple Transactions

You may want to make multiple transactions in sequence without broadcasting them immediately, such as signing and saving to broadcast at a later date. However, Wallet will poll the blockchain live for the sequence number every time a transaction is generated.

Thus, creating several transactions in a row with repeated calls to wallet.create_and_sign_tx() will not result in transactions that can be broadcasted directly.

# Method 1: Wallet in Manual Mode

You can use the Wallet object in manual mode, which pauses the automatic fetching of sequence, and automatically increments sequence number everytime a transaction is generated. Upon teardown of the context, the default sequence number polling behavior is resumed.

txs = list()
with wallet.manual() as w:
    txs.append(
        w.create_and_sign_tx(...)
    ) # TX 1
    txs.append(
        w.create_and_sign_tx(...)
    ) # TX 2
    # ...

# broadcast them sequentially (example)
for tx in txs:
    terra.broadcast(tx)

# Method 2: Signing Manually

Wallet.create_tx(*msgs, fee=None, memo="")

You can also create unsigned transactions (StdSignMsg objects) and manually increment their sequence fields in the order they appear, and then sign them.

utxs = list() # unsigned txs (StdSignMsgs)
utxs.append(wallet.create_tx(...)) # TX 1
utxs.append(wallet.create_tx(...)) # TX 2
# ...
txs = list()
for i, utx in enumerate(utxs):
    utx.sequence += i
    txs.append(wallet.key.sign_tx(utx))

# broadcast them sequentially (example)
for tx in txs:
    terra.broadcast(tx)

# Parameters

StdMsg
*msgs

Messages to be included in the transaction.


StdFee
fee
optional

Fee to include. If no fee is included, automatic fee estimation will be used.


DEFAULT None
str
memo
optional

An optional note.


DEFAULT ""

# Returns

StdSignMsg

An unsigned transaction with the correct chain ID, account number and sequence values. Must be signed before being broadcasted.

Updated on: 3/11/2020, 8:43:58 PM