【问题标题】:web3.py return from a get function in strange formatweb3.py 从 get 函数以奇怪的格式返回
【发布时间】:2020-11-26 23:59:06
【问题描述】:

我正在开发一个 python 应用程序。这个应用程序只是为了从区块链中获取数据。在 web3.js 中一切正常,但我需要在 python 中完成(客户端想要一个 python 应用程序)。这一切几乎都很好;该脚本完成了它需要做的事情,但是当调用 get 函数时,我得到一个奇怪的输出(使用 remix 或 web3.js 上的 get 函数,而我编写的 nodeJs api 非常完美):

D:\pytoh\b_get\Scripts\python.exe C:/Users/Alessandro/PycharmProjects/pytoh/b_get/bget.py
True
[(b'\xe9:=\xd87/\x98\x00\xe4\x89\xe3\x8eb[c\x9cj\xc4\xa3\xa2b\x1d\x9a\xf0%\x1d\xdaB\xb7\xd9\xc4\xe3', 
b'\xe9:=\xd87/\x98\x00\xe4\x89\xe3\x8eb[c\x9cj\xc4\xa3\xa2b\x1d\x9a\xf0%\x1d\xdaB\xb7\xd9\xc4\xe3')]

Process finished with exit code 0

我需要的输出是这样的(我不理解为什么PY输出就是这样):[0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3,0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3] P>

python脚本很简单(我还在写):

import json
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))
print(w3.isConnected())
with open("ABI.json") as f:
  info_json = json.load(f)
abi = info_json["output"]["abi"]

 address = "0x1305ef6377fe8cB7C6dD7Eb2B6cAD83A34fC7503"
 get = w3.eth.contract(address=address, abi=abi) 
 result=get.functions.getUser("0xe93a3dd8372f9800e489e38e
                       625b639c6ac4a3a2621d9af0251dda42b7d9c4e3").call()
 print(result)

这是客户写的智能合约:

pragma solidity ^0.4.26;
pragma experimental ABIEncoderV2;

contract crud{
  address owner;
  constructor() public{
  owner = msg.sender;
}
modifier onlyOwner () {
   require(msg.sender == owner);
    _;
}
struct user{
    bytes32 hash_json;
    bytes32 hash_corso;
}
 mapping (bytes32 => user[]) public users;

 
 function setUser(bytes32 _hash, bytes32 _hash_json, bytes32 _hash_corso) onlyOwner  public {
     user memory usr;
     usr.hash_json=_hash_json;
     usr.hash_corso= _hash_corso;
     users[_hash].push(usr);
 }
 
 function getUser(bytes32 _hash) view public returns (user[] memory) {
     return users[_hash];
 }

 }

感谢您的帮助!

【问题讨论】:

    标签: python blockchain ethereum solidity web3py


    【解决方案1】:

    结果是一个字节数组。当您打印它时,它会将控制字符转换为十六进制但打印可读字符。

    要获得完整的十六进制字符串,试试这个:

    result = bytearray([22,32,7,67,14,87,90])  # for testing
    hxstr = "".join(["{:02x}".format(v) for v in result])
    print("0x"+hxstr)
    

    输出

    0x162007430e575a
    

    【讨论】:

    • 嗨,你的回答,代码现在可以工作,但我还有另一个大问题。控制台显示,我不知道该怎么做:` Traceback(最近一次调用最后):文件“C:/Users/Alessandro/PycharmProjects/pytoh/b_get/bget.py”,第 12 行,在 结果中=bytearray(get.functions.getUser("0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3").call()) TypeError: 'tuple' 对象不能被解释为整数'
    • 您的代码中不应需要bytearray()。只需使用result=get.functions....
    • 对不起,我改变了它,但现在它说:Traceback(最近一次调用最后):文件“C:/Users/Alessandro/PycharmProjects/pytoh/b_get/bget.py”,第13行,在 hxstr = "".join(["{:02x}".format(v) for v in result]) 文件“C:/Users/Alessandro/PycharmProjects/pytoh/b_get/bget.py”,行13、在 hxstr = "".join(["{:02x}".format(v) for v in result]) TypeError: unsupported format string passing to tuple.__format__
    • 我认为call() 正在返回一个元组。尝试在 hxstr 行中使用....v in result[0][0]])
    • 嗨,我重写了智能合约,并在结果中添加了 ` ....v [0]) `,现在它可以完美运行了
    猜你喜欢
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 2021-07-18
    • 1970-01-01
    • 2018-06-21
    • 2019-05-29
    相关资源
    最近更新 更多