Python 位元幣教程之二 機械人收發位元幣

2021-09-12 17:08:29 字數 2964 閱讀 7802

按本篇教程後學習後完成後,你的機械人將會接受使用者傳送過來的加密貨幣,然後立即轉回使用者。

完整**如下:

from mixin_ws_api import mixin_ws_api

from mixin_api import mixin_api

import mixin_config

import json

import time

from io import bytesio

import base64

import gzip

try:

import thread

except importerror:

import _thread as thread

def on_message(ws, message):

inbuffer = bytesio(message)

f = gzip.gzipfile(mode="rb", fileobj=inbuffer)

rdata_injson = f.read()

rdata_obj = json.loads(rdata_injson)

print("-------json object begin---------")

print(rdata_obj)

print("-------json object end---------")

action = rdata_obj["action"]

if rdata_obj["data"] is not none:

print("data in message:",rdata_obj["data"])

if rdata_obj["data"] is not none and rdata_obj["data"]["category"] is not none:

print(rdata_obj["data"]["category"])

if action == "create_message":

data = rdata_obj["data"]

msgid = data["message_id"]

typeindata = data["type"]

categoryindata = data["category"]

userid = data["user_id"]

conversationid = data["conversation_id"]

dataindata = data["data"]

created_at = data["created_at"]

updated_at = data["updated_at"]

realdata = base64.b64decode(dataindata)

mixin_ws_api.replaymessage(ws, msgid)

print('userid', userid)

print("created_at",created_at)

if categoryindata == "plain_text":

realdata = realdata.decode('utf-8')

print("dataindata",realdata)

mixin_ws_api.sendusertext(ws, conversationid, userid, realdata)

elif categoryindata == "system_account_snapshot":

rdjs = json.loads(realdata)

if ( float(rdjs["amount"]) > 0 ):

mixin_api.transferto(userid, rdjs["asset_id"], rdjs["amount"], "")

if __name__ == "__main__":

mixin_api = mixin_api(mixin_config)

mixin_ws = mixin_ws_api(on_message=on_message)

mixin_ws.run()

cd mixin_labs-python-bot

source ./bin/activate

ws open

-------json object begin---------

-------json object end---------

開發者可以通過訊息面板,給機械人轉位元幣,當機械人收到位元幣後,馬上返還給使用者!

事實上,使用者可以傳送任意的幣種給機械人,它都能馬上返還!

如果機械人收到幣,rdjs["amount"] 大於零;如果機械人支付幣給使用者,接收到的訊息是一樣的,唯一不同的是,rdjs["amount"]是乙個負數.

最後一步,呼叫sdk的 mixin_api.transferto 將幣返還使用者!

coming soon!

mixin network的開發資源彙編

Python 位元幣教程之二 機械人收發位元幣

按本篇教程後學習後完成後,你的機械人將會接受使用者傳送過來的加密貨幣,然後立即轉回使用者。完整 如下 from mixin ws api import mixin ws api from mixin api import mixin api import mixin config import js...

Python 位元幣教程之二 機械人收發位元幣

按本篇教程後學習後完成後,你的機械人將會接受使用者傳送過來的加密貨幣,然後立即轉回使用者。完整 如下 from mixin ws api import mixin ws api from mixin api import mixin api import mixin config import js...

區塊鏈系列教程之 位元幣中的共識

目錄總結 在位元幣的p2p網路中是怎麼達成共識的呢?達成共識需要做哪些交易的校驗呢?交易和區塊是怎麼傳播到整個區塊鏈網路的呢?看完這篇文章你就明白了。之前我在講分布式系統的時候有講過分布式系統的幾個共識演算法,包括raft,paxos和拜占庭容錯演算法。位元幣的共識和之前講的都不同,它使用的是工作量...