学习笔记3:计算txid

in STEEM CN/中文5 years ago (edited)

最近一直在玩steemmonsters.

@Bauloewe写了一个通用的机器人对战程序。我的对战程序是在他的基础上修改而来的。

里面有一个关键步骤就是计算txid。

Bauloewe的办法是获取最近的区块信息,然后遍历,这稍微有点麻烦并且容易出错。

经过大神@oflyhigh的指点(https://steem.buzz/steemdev/@oflyhigh/steem-transaction-txid)

从这里面得到了计算txid的办法。

办法就是:将Signed_Transaction的签名部分清空,序列号后生成摘要,取摘要的前20个字节,并转换成16进制字符串形式(40个字节)。

假设我们要转账给某人,然后马上计算出TXid并且返回,可以这样写:

from steem import Steem
import hashlib
from binascii import hexlify
from steembase.transactions import SignedTransaction


nodes = ['https://steemd.minnowsupportproject.org',]
s = Steem(nodes,
    keys=["XXXXX]
)
num=1

# 转账
trans=s.commit.transfer(
    "huobi-pro",
    num,
    "STEEM",
    memo="101634",
    account="maiyude"
)


tx = SignedTransaction(trans)
tx.data.pop("signatures", None)
print(tx)
h = hashlib.sha256(bytes(tx)).digest()
transaction_id=hexlify(h[:20]).decode("ascii")
print(transaction_id)
url="https://steemd.com/tx/%s" % transaction_id
print(url)

print("转账完成")

返回信息:
4f94385f24c0cae3afdfb213ffb37bd94b53b187
https://steemd.com/tx/4f94385f24c0cae3afdfb213ffb37bd94b53b187
转账完成

转账成功运行后,马上就会返回txid。把这个记录下来,就可以很方便的查询了。

同理可以运用到steemmonsters的机器人里面计算比赛的txid。
比如这样:

batte = r.find_match(player, "Ranked")

# 计算trx
tx = SignedTransaction(batte)
tx.data.pop("signatures", None)
h = hashlib.sha256(bytes(tx)).digest()
trx = hexlify(h[:20]).decode("ascii")
print(trx)

这样不仅速度快,而且出错率少,非常好用。

Sort:  

Hi @maiyude!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 3.763 which ranks you at #5396 across all Steem accounts.
Your rank has dropped 131 places in the last three days (old rank 5265).

In our last Algorithmic Curation Round, consisting of 78 contributions, your post is ranked at #33.

Evaluation of your UA score:
  • You're on the right track, try to gather more followers.
  • The readers like your work!
  • Try to work on user engagement: the more people that interact with you via the comments, the higher your UA score!

Feel free to join our @steem-ua Discord server

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62966.47
ETH 2631.87
USDT 1.00
SBD 2.79