# A Tour of Jigu

This is a short tutorial to help get you get up and running with Terra in Python using Jigu.

# Getting Started

# Install Jigu

Jigu requires Python 3.7+. Install the latest version of Jigu with pip on PyPI:

$ pip install jigu

# Connect to Soju testnet

Once you've installed Jigu, fire up an interactive Python shell and connect to the Soju testnet using the official Soju node provided by Terraform Labs.

from jigu import Terra

soju = Terra("soju-0013", "https://soju-lcd.terra.dev")
assert soju.is_connected()

# Create an account

Before we can make any transactions, we have to have an account. Enter in the above to create an account and print its account address.

from jigu.key.mnemonic import MnemonicKey

wallet = soju.wallet(MnemonicKey.generate())
wallet.address
# terra17w4ppj92dwdf93jjtply08nav2ldzw3z2l3wzl

# Top off with testnet funds

Great, now that we have an address, let's get some testnet funds. Head over to the Soju Faucet and top off some Luna.

faucet

After that's done, you should have 10,000 LUNA in your account. To confirm this, you can enter the following:

wallet.balance("uluna")
# Coin('uluna', 10000000000)

# Create a transaction

Let's send 23 Testnet Luna to your friend at the following address:

terra1wg2mlrxdmnnkkykgqg4znky86nyrtc45q336yv

We'll need to create a transaction containing a MsgSend alongside a short memo (note) "Hello Jigu!" -- our version of Hello World.

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

send = MsgSend(
    from_address=wallet.address,
    to_address="terra1wg2mlrxdmnnkkykgqg4znky86nyrtc45q336yv",
    amount=Coins(uluna=23_000_000)
)

fee = StdFee.make(50000, uluna=1000) # include a small fee..

tx = wallet.create_and_sign_tx(send, memo="Hello Jigu!", fee=fee)
res = wallet.broadcast(tx)

# See it on the blockchain

It should take around 6 seconds to finalize. If everything went well, you should get a result object with the height and TX hash after about 6 seconds.

print(f"TX Hash: {res.txhash}")
# TX Hash: 82D5440A4C4CAB5B74EE3C98CE7F755372CD92E945425A572654179A4A0EE678

Copy the TX hash and enter it on Finder, selecting the chain soju-0013.

txhash

# A Taste of DeFi

Now that we've gotten our bearings a little, let's take things a bit further and get a glimpse of DeFi (Decentralized Finance) through Terra using Jigu. In this example, we'll be using the Market module to swap stablecoins tracking fiat different fiat currencies.

# Swap our LUNA for stablecoins

Let's create 1 transaction including 2 MsgSwap messages. Terra transactions can include more than one as long as sufficient fee is provided to be accepted. If we do not provide a fee, Jigu will automatically try to estimate a fee before broadcasting. However, the fee estimation mechanism is not always accurate, so here we'll apply a manual one to be safe.

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

swap1 = MsgSwap(
    trader=wallet.address,
    offer_coin=Coin("uluna", 150_000_000),
    ask_denom="uusd"
)

swap2 = MsgSwap(
    trader=wallet.address,
    offer_coin=Coin("uluna", 200_000_000),
    ask_denom="ukrw"
)

# Set our gas limit to 250,000 and pay 1 LUNA
fee = StdFee.make(gas=250_000, uluna=1_000_000)

# multiple msgs in 1 transaction
tx = wallet.create_and_sign_tx(swap1, swap2, fee=fee)
res = wallet.broadcast(tx)

# See it on the blockchain

print(f"TX Hash: {res.txhash}")
# TX Hash: 04FD23C9A03A6A70118CC6FA6E729F0C442BF44838C7EBCFD7A1B6C4A70168B5

swaptxhash

# Check new account balance

We've successfully swapped our LUNA for stablecoins!

wallet.balance()
# Coins(ukrw=513520000000, uluna=6472992326, uusd=326113463)

account_balances

# Explore the Blockchain

The blockchain is a publicly accessible ledger of transactional events, with ever-changing state. Jigu allows you to access live (current) or historical blockchain data from the comfort of an interactive shell.

# Interact with Core Modules

You can interact with the directly with Core Modules through the Terra instance.

from jigu import Terra
terra = Terra("columbus-3", "https://lcd.terra.dev") # mainnet
redelegations = terra.staking.redelegations() # get all current redelegations
len(redelegations) # how many redelegations ?

Check out live parameters...

terra.market.params()
# <ApiResponse JiguBox{'pool_recovery_period': 14400, 'base_pool': Dec('250000000000'),
# 'min_spread': Dec('0.02'), 'tobin_tax': Dec('0.0025'), 'illiquid_tobin_tax_list': {'umnt': Dec('0.02')}}>

# Pretty-printing

You can pretty-print any Jigu Data Objects by using the object's ._pp property.

>>> terra.market.params()._pp
╒═════════════════════════╤═════════════════╕
│ pool_recovery_period    │ 14400           │
├─────────────────────────┼─────────────────┤
│ base_pool               │ 250000000000    │
├─────────────────────────┼─────────────────┤
│ min_spread              │ 0.02            │
├─────────────────────────┼─────────────────┤
│ tobin_tax               │ 0.0025          │
├─────────────────────────┼─────────────────┤
│ illiquid_tobin_tax_list │ ╒══════╤══════╕ │
│                         │ │ umnt │ 0.02 │ │
│                         │ ╘══════╧══════╛ │
╘═════════════════════════╧═════════════════╛

# Usage inside Jupyter

All objects will automatically display in a nice HTML table inside a Jupyter notebook.

jupyter

# Conclusion

Congratulations! You've successfully gotten set up and are ready to build applications that leverage the robust DeFi infrastructure provided by the Terra network. Explore the rest of Jigu SDK and discover what other awesome things you can build.

Updated on: 3/10/2020, 12:51:48 PM