0

我正在开发一个 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

The output that i need is like this (i don't understand why the py output is like that):[0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3,0xe93a3dd8372f9800e489e38e625b639c6ac4a3a2621d9af0251dda42b7d9c4e3]

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];
 }

 }

感谢您的帮助!

4

1 回答 1

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
于 2020-08-06T17:19:02.223 回答