Я пытаюсь вызвать довольно простой контракт на приватном блокчейне:
function getOwner() public returns (address) {
return owner;
}
А вот мой запуск из интерактивной оболочки Python:
>>> e.unlock_account()
>>> instance.functions.getOwner().transact()
HexBytes('0x4f366ba7bb7ae0ef70e48c3ce7eb4f920d7079e90a10bfea36fc9fbbb48d52fe')
Это прекрасно работает, но мне нужно возвращаемое значение функции, а не хэш транзакции. Итак, я считаю, что мне нужно тогда назвать это правильно?
>>> e.unlock_account()
>>> instance.functions.getOwner().call({'from': e.account})
... error trace ...
eth_abi.exceptions.InsufficientDataBytes: Tried to read 32 bytes. Only got 0 bytes
... more error traces ...
raise BadFunctionCallOutput(msg) from e
web3.exceptions.BadFunctionCallOutput: Could not transact with/call contract function, is contract deployed correctly and chain synced?
Кто-нибудь знает, что я делаю неправильно?
Изменить: я протестировал его с другим, еще более простым контрактом, и, похоже, он работает нормально. Контракты отличаются только тем, что работающий возвращает int..
function reveal() public view returns (int) {
return number;
}
Когда я вызываю () функцию выше, она возвращает правильный номер.
Редактировать 2: я удалил все функции из контракта, связанные с установкой адреса владельца, и теперь все работает. Я предоставил версии контракта до и после на случай, если кто-то еще столкнется с той же проблемой. Я до сих пор понятия не имею, почему контракт не работает с адресами. Это как-то связано с конструктором? Любые объяснения приветствуются!
BEFORE EDIT (NOT WORKING)
pragma solidity ^0.4.0;
contract CryptoCert {
struct Award {
bool isValid;
string hash;
}
struct Authority {
bool isValid;
string hash;
address[] representatives;
Award[] awards;
}
address private owner;
mapping(address => Authority) authorities;
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
modifier onlyBy(address _address) {
require(msg.sender == _address);
_;
}
function CryptoCert() public {
owner = msg.sender;
}
function kill() external {
if (msg.sender == owner) {
selfdestruct(owner);
}
}
function createAuthority(address _address, string _hash) public onlyOwner {
authorities[_address].hash = _hash;
authorities[_address].isValid = true;
}
function addAuthorizedAddress(address _authAddress, address _repAddress) public onlyBy(_authAddress) {
authorities[_authAddress].representatives.push(_repAddress);
}
function getAuthorityHash(address _address) public view returns (uint) {
if (authorities[_address].isValid) {
return 1;
} else {
return 0;
}
}
function getOwner() public view returns (address) {
return owner;
}
function test() public pure returns (string) {
return "nigchicken69";
}
}
И после...
AFTER EDIT (WORKING)
pragma solidity ^0.4.0;
contract CryptoCert2 {
struct Award {
bool isValid;
string hash;
}
struct Authority {
bool isValid;
string hash;
address[] representatives;
Award[] awards;
}
mapping(address => Authority) authorities;
function createAuthority(address _address, string _hash) public {
authorities[_address].hash = _hash;
authorities[_address].isValid = true;
}
function addAuthorizedAddress(address _authAddress, address _repAddress) public {
authorities[_authAddress].representatives.push(_repAddress);
}
function getAuthorityHash(address _address) public view returns (uint) {
if (authorities[_address].isValid) {
return 1;
} else {
return 0;
}
}
}
Вызов функций чтения прост:
print('возвращенное значение: {}'.format(contractInstance.functions.getXyz().call()))
Убедитесь, что вы установили последнюю версию Python и web3.py версии 4.
резчик
Катал
резчик