steem-python for dummies #9 - Creating orders in the market


Hello,

This is the 9. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look.

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos
  5. steem-python for dummies #5 - Bundling Blockchain Operations
  6. steem-python for dummies #6 - Delegating
  7. steem-python for dummies #7 - Creating Accounts
  8. steem-python for dummies #8 - Calculating Post Rewards

In this post, we will learn creating and removing orders on internal market.

Orders on internal market


Steem blockchain offers a decentralized exchange between SBD and STEEM. In Steemit, this is already possible with a nice user interface.

However, this market doesn't depend to Steemit. All operations on these market including creating orders, removing orders are stored in the chain.

That means it is possible for us to create these operations ourselves.

Creating an order


steem-python has a operation implemented as limit_order_create. However, it doesn't encapsulate like other operations, so we will build the transaction ourselves.

Before doing that, let's see what are the parameters for the operation:


LimitOrderCreate Implementation

parametermeaning
ownerrelated account
orderidA unique order id bounded to your account. Will be used to cancel later on.
amount_to_sellHow much do you want to sell? With the asset. "0.1 SBD"
min_to_receiveHow much do you want to receive? With the asset. "1 STEEM"
fill_or_killOrder should be killed if if not filled immediately? (True or False)
expirationExpiration time for the order: Ex: 2017-12-31 23:59:59

Let's use these information to build, sign, broadcast a LimitOrderCreate transaction.

from steem import Steem
from steem.transactionbuilder import TransactionBuilder
from steembase import operations
s = Steem(nodes=["https://rpc.buildteam.io"],
          keys=["active_wif"])
account = 'emrebeyler'
limit_order_create = operations.LimitOrderCreate(
        owner=account,
        orderid=0,
        amount_to_sell="0.1 SBD",
        min_to_receive="1 STEEM",
        fill_or_kill=False,
        expiration="2017-12-31T23:59:59",
)
tb = TransactionBuilder()
tb.appendOps([limit_order_create,])
tb.appendSigner(account, "active")
tb.sign()
resp = tb.broadcast()

Done! I have created an order, asking 1 STEEM for 0.1 SBD. Hoping it will be filled :)


Steemd representation of this transaction

Canceling an order


Remember, we had to post an order_id while creating the order. We will use that id to cancel it.

from steem import Steem
from steem.transactionbuilder import TransactionBuilder
from steembase import operations
s = Steem(nodes=["https://rpc.buildteam.io"],
          keys=["active_wif"])
account = 'emrebeyler'
limit_order_cancel = operations.LimitOrderCancel(
        owner=account,
        orderid=0,
)
tb = TransactionBuilder()
tb.appendOps([limit_order_cancel,])
tb.appendSigner(account, "active")
tb.sign()
resp = tb.broadcast()


Steemd representation of this transaction

Order Book

You can get order book easily.

s = Steem()
print(s.get_order_book(limit=1))

Which is resulting like this, at the moment.

{
    'asks': [{
        'order_price': {
            'base': '750.000 STEEM',
            'quote': '146.822 SBD'
        },
        'real_price': '0.19576266666666667',
        'steem': 512512,
        'sbd': 100330,
        'created': '2017-12-17T10:48:09'
    }],
    'bids': [{
        'order_price': {
            'base': '2.207 SBD',
            'quote': '11.274 STEEM'
        },
        'real_price': '0.19576015611140679',
        'steem': 11274,
        'sbd': 2207,
        'created': '2017-12-17T10:57:00'
    }]
}

That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts.



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
22 Comments