Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Steem Coding - Python, API, Piston

Fabian Schuh
November 11, 2016

Steem Coding - Python, API, Piston

This presentation gives a quick introduction to the Steem API and how to use piston to interact with the Steem network, post, vote and comment

Fabian Schuh

November 11, 2016
Tweet

More Decks by Fabian Schuh

Other Decks in Technology

Transcript

  1. Steem Coding Steem Coding Python, API, Piston Python, API, Piston

    Dr.-Ing. Fabian (@xeroc) Schuh ChainSquad GmbH (i.Gr.) October 13, 2016
  2. Requirements for this talk Requirements for this talk Coding Python:

    def main(): print("hello, world") Accessing APIs JSON {'expiration': '2016-11-09T11:09:01', 'operations': [['transfer', {.........}}]], 'ref_block_num': 17230, 'ref_block_prefix': 1220322455, 'signatures': ['.........']} Blockchain Technologies signed “Letters of intent” 1/26
  3. Outline Outline 1 Steem’s Blockchain APIs 2 First Actions Registering

    to Other APIs Using advanced APIs 3 Abstraction Layer: Piston Signed Transactions Writing to Steem Monitoring the Blockchain 4 Live Coding 5 Further Reading 2/26
  4. Steem’s Blockchain API Steem’s Blockchain API blockchain p2p-network consensus protocol

    Blockchain Database Database API Account History API Market History API Tag API Follow API Broadcast API … steemd 4/26
  5. Steem’s Blockchain API Steem’s Blockchain API blockchain p2p-network consensus protocol

    Blockchain Database Database API Account History API Market History API Tag API Follow API Broadcast API … steemd ✓ JSON formatted Remote Procedure Calls (RPC) via HTTP × No need to understand blockchain technology × No need to understand consensus schemes × No need to understand P2P networks × No need to know the protocol 4/26
  6. What are the APIs good for? What are the APIs

    good for? Database API: Database API: get_trending_tags() get_state() get_block() get_account() … Market History API (Plugin): Market History API (Plugin): get_ticker() get_trade_history() get_recent_trades() get_volume() … Broadcast API (Plugin): Broadcast API (Plugin): broadcast_transaction() broadcast_transaction_synchronous() … Follow API (Plugin): Follow API (Plugin): get_followers() get_following() get_feed() … 5/26
  7. First Actions First Actions Simple Database Query curl --data '{"jsonrpc":"2.0",

    \ "method":"call", \ "params": \ [0, \ "get_dynamic_global_properties", \ [] \ ], \ "id":0}' \ this.piston.rocks Reply {"id":0, "result":{ "id":"2.0.0", "head_block_number":6574290, "head_block_id":"006450d2ed6f58bab8e7fca49dc1488de6dea768", "time":"2016-11-09T14:01:54", "current_witness":"jesta", "total_pow":313551, "num_pow_witnesses":108,% ..., } } 7/26
  8. Using Other APIs Using Other APIs The database API (id:

    0) is enabled and accessible by default! For every other API a identifier needs to be requested! The python-steem library simplifies this > {"method": "call", "jsonrpc": "2.0", "id": 1, "params": [1, "login", ["<user>","<password>"]]} < {"id":1,"result":true} > {"method": "call", "jsonrpc": "2.0", "id": 2, "params": [1, "get_api_by_name",["network_broadcast_api"]]} < {"id":2,"result":5} 9/26
  9. Using other APIs Using other APIs Address specific APIs using

    the API number obtained in the previous step: > {"method": "call", "jsonrpc": "2.0", "id": 3, "params": [ 5, "broadcast_transaction_synchronous", ["<signed_transaction>"] ]} < {"id":1,"result":Null} 11/26
  10. Piston: Introduction Piston: Introduction The Swiss army knife for the

    Steem blockchain! Developed to make live easier for developers and user Multiple Abstraction Layers involved: python-graphene python-steem piston Uses public API server: this.piston.rocks and node.steem.ws 13/26
  11. Simple Example Simple Example Reading from Steem: from piston.steem import

    Steem # piston.Steem abstraction steem = Steem() # RPC call get_account print(steem.rpc.get_account("chainsquad") # returns a piston.Post object print(steem.get_content("@xeroc/piston") 14/26
  12. Writing to Steem! Writing to Steem! How about Writing to

    Steem? Writing to the blockchain requires: an account the (posting, or active) private key “letters of intent”: vote, or comment, or transfer, etc. Providing Keys to Piston: > piston addkeys # Shell command to use the internal wallet >>> steem = Steem(keys=["5xxxxx", "5xxxxx"]) # Provide a list of keys # Advanced users can force keys for signing >>> steem = Steem(keys={ "owner": "5xxxxx", "active": "5xxxxx", "posting": "5xxxxx", "memo": "5xxxxx", }) 16/26
  13. Signed Transactions Signed Transactions All actions that “change” the blockchain

    (e.g. votes, comments, transfers, etc) require a signed transaction of this form: {'expiration': '2016-11-09T15:03:43', 'extensions': [], 'operations': [[<list of operations>]], 'ref_block_num': 21908, 'ref_block_prefix': 2930661658, 'signatures': ['1f3dba2ba.....bf3ea1a8a0b']} 17/26
  14. Example signed Transaction Example signed Transaction The actual singing process

    is provided by python-graphene and is not part of this talk. All piston methods that “write to” the blockchain return a (partially) signed transaction of the above form. >>> from piston.steem import Steem >>> steem = Steem(nobroadcast=True) >>> print(steem.transfer(to="fabian", account="xeroc", amount=1, asset="SBD")) Passphrase: *********** Not broadcasting anything... {'ref_block_prefix': 2732406441, 'ref_block_num': 21994, 'expiration': '2016-11-09T15:08:00', 'operations': [['transfer', { 'from': 'xeroc', 'memo': '', 'to': 'fabian', 'amount': '1.000 SBD'}]], 'extensions': [], 'signatures': ['202ceb735d3c1e3b22be63b...96d5d136e41958156716de'] } 18/26
  15. Writing to Steem Writing to Steem With the installed (posting,

    active) private key(s), we enable additional piston calls: steem.post() steem.edit() steem.reply() steem.vote() steem.transfer() # required active key steem.buy() # required active key steem.convert() # required active key steem.allow() # required active key ... 20/26
  16. The Post class The Post class Posts themselves are consequently

    represented using the Post abstraction class which adds post-specific methods that correspond to this particular post: post = steem.get_content("@xeroc/piston") post.upvote() post.reply("You rock!") print(post.url) print(post["url"]) print(post.author) print(post.title) print(post.body) print(post.author_rewards) print(post.net_votes) print(post.get_comments()) # again returns Post instances ... 21/26
  17. Monitoring the Blockchain Monitoring the Blockchain Monitoring Posts Monitoring Posts

    for post in steem.stream_comments(): if not post.depth: print(post.body) Monitoring Raw Blocks Monitoring Raw Blocks for block in steem.rpc.block_stream(): print(block["block_num"]) Monitoring Specific Operations Monitoring Specific Operations for op in steem.rpc.stream(["transfer", "vote"]): print(op) 23/26