闲着无聊,合计做一个bitshares的Memo监视功能,应该挺好玩的。比如说,谁给我转了50W BTS,并附加了备注,我一下子就会收到消息。如果再加上声光报警,语音提示,滴,你收到了一笔50W BTS的转账,这该多美啊!😍
(图源 :pixabay)
说干就干,初步设想是这样滴:
想来想去,我的MQTT服务器早就被我扔垃圾堆里了,MQTT的客户端的代码我也早忘干净了,那么还是简化一下任务吧,在屏幕一下凑合吧。
有了上述思路,以及我们之前的学习,那么代码实现起来是很简单的
一个粗糙无比的代码示例如下:
from pprint import pprint
from bitshares import BitShares
from bitshares.blockchain import Blockchain
from bitshares.memo import Memo
account = 'test2018'
bts = BitShares()
chain = Blockchain()
id = bts.rpc.get_account_by_name(account)['id']
print(id)
def get_memo_text(op):
m = Memo(op['from'], op['to'])
enc = op['memo']
plaintext = m.decrypt(enc)
return plaintext
for operation in chain.stream(opNames=['transfer']):
if operation['to'] == id:
print("You got Message")
msg = get_memo_text(operation)
from_user = bts.rpc.get_account(operation['from'])['name']
print(f"Message from {from_user}: {msg}")
#pprint(operations)
下面我们来测试一下上边的代码是否工作。
接收到的信息如下(后边两条文本):
看起来我的程序是正常工作地。
如果再加上转账(发消息)功能,这就是一个聊天软件嘛。
(图源 :pixabay)
尽管这个听起来很好玩,但是我不打算再去玩了,也不打算去完善了。为啥?上边截图中显示的转账费用竟然高达0.01759 BTS,按现在BTS 3.51人民币的价格计算,一条消息竟然需要至少6分钱!这太奢侈了吧,像我这种话痨,每天聊个几十万条不费劲,姑且算10W条吧,那就是6000多块钱啊。
如果这软件普及了,估计能让大家更深刻地理解沉默是金的道理吧。