0%

solidity | 结果返回与获取

返回结果主要有两类

  • 需要 gas 返回
  • 不需要 gas 返回
  • 异常返回
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pragma solidity ^0.8.0;

contract ERC20{

string public name;
error testError(string msg);

function test1() public view returns(uint256){
return 1;
}

function test2() public view returns(uint256,bool){
return (1,true);
}

function test3() public view returns(uint256){
require(1>2,"1>2 error");
return 1;
}

function test4() public view returns(uint256){
assert(1>2);
return 1;
}

function test5() public view returns(uint256){
if(1 < 2){
revert testError("test5 error");
}
return 1;
}

function test6() public returns(uint256){
name = "test6";
return 1;
}

function test7() public returns(uint256){
if(1 < 2){
name = "test7";
revert testError("test5 error");
}
return 1;
}

function test8() public returns(uint256){
require(1>2,"test7 error");
return 1;
}

}

将这个合约部署到 bsc 测试网。

相关代码

无需 gas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from web3 import Web3
import traceback

abi = """[{"inputs":[],"name":"test6","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"test7","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"msg","type":"string"}],"name":"testError","type":"error"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test1","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test2","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test3","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test4","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test5","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]"""
w3 = Web3(Web3.HTTPProvider("https://bsc.getblock.io/testnet/?api_key=5de***d"))

contract = "0xf9E70F3d6dcC64D25f557e06becC6760bF0245D2"

contract_ = w3.eth.contract(
address=Web3.toChecksumAddress(contract), abi=abi)

print(contract_.functions.test1().call())
print(contract_.functions.test2().call())


def test():
try:
print(contract_.functions.test3().call())
except Exception as e:
print(traceback.format_exc())

try:
print(contract_.functions.test4().call())
except Exception as e:
print(traceback.format_exc())

try:
print(contract_.functions.test5().call())
except Exception as e:
print(traceback.format_exc())


if __name__ == '__main__':
test()

分别输出

1
2
3
4
5
6
7
8
9
10
11
12
13
1
[1, True]
Traceback (most recent call last):
...
web3.exceptions.ContractLogicError: execution reverted: 1>2 error

Traceback (most recent call last):
...
web3.exceptions.ContractLogicError: execution reverted

Traceback (most recent call last):
...
web3.exceptions.ContractLogicError: execution reverted

可以看出只有 require 才会返回错误信息。当然,也有可能是因为 bsc 阉割问题,导致不返回信息。

需要 gas

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from web3 import Web3

abi = """[{"inputs":[],"name":"test6","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"test7","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"test8","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"msg","type":"string"}],"name":"testError","type":"error"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test1","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test2","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test3","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test4","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"test5","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]"""

w3 = Web3(Web3.HTTPProvider("https://bsc.getblock.io/testnet/?api_key=5deb2510-62f9-40a9-b0e0-0039f9025ded"))

address = "0xD1B260aba3bA6f3D3099725b1b1C7cdD12581974"
contract = "0xf9E70F3d6dcC64D25f557e06becC6760bF0245D2"
private = ""

contract_ = w3.eth.contract(
address=Web3.toChecksumAddress(contract), abi=abi)


def encode_test6():
tx_dic = contract_.functions.test6(). \
buildTransaction(
{
'gas': 500000,
}
)
return tx_dic

def encode_test7():
tx_dic = contract_.functions.test7(). \
buildTransaction(
{
'gas': 500000,
}
)
return tx_dic


def encode_test8():
tx_dic = contract_.functions.test8(). \
buildTransaction(
{
'gas': 500000,
}
)
return tx_dic

def send_transaction(tx_dic):
nonce = w3.eth.getTransactionCount(address)
tx_dic["nonce"] = nonce
tx_dic['gasPrice'] = w3.eth.gasPrice
sign_tx = w3.eth.account.signTransaction(tx_dic, private_key=private)
txn_hash = w3.eth.sendRawTransaction(sign_tx.rawTransaction)
return Web3.toHex(txn_hash)


def get_wait_receipt(tx):
data = w3.eth.waitForTransactionReceipt(tx)
return data


if __name__ == '__main__':
tx_dic = encode_test6()
tx = send_transaction(tx_dic)
data = get_wait_receipt(tx)
print(data)
tx_dic = encode_test7()
tx = send_transaction(tx_dic)
data = get_wait_receipt(tx)
print(data)
tx_dic = encode_test8()
tx = send_transaction(tx_dic)
data = get_wait_receipt(tx)
print(data)

虽然 test6test7test8 有返回,但是,实际上是获取不到的,上面的 data 获取的是 logs 日志。

另外,关于一样

只有 require 才会在链上输出,但是,也有可能是 bsc 阉割问题。

请我喝杯咖啡吧~