0%

eth | struct 和 bytes 怎么交互

这里用 lifeform | 合约 来做案例。

具体案例是 0xd7d75807d4c92bdf2f69e549d9be20b31e3c576e45137d1de201e9b696ad29dc

看合约其中 struct

1
2
3
4
5
6
7
8
9
10
11
12
struct MintRule {
address mintRule;
uint256 udIndex;
address stakeErc20;
uint256 stakeErc20Amount;
address costErc20;
uint256 costErc20Amount;
uint256 limitTimes;
uint256 mintType;
bytes32 signCode;
bytes wlSignature; //wlSignature
}

合约交互如下

使用 python 交互如下

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
from web3 import Web3

abi = """
[{"inputs":[{"internalType":"address","name":"SIGNER","type":"address"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeAmount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"signer","type":"address"},{"indexed":false,"internalType":"uint256","name":"blockNum","type":"uint256"}],"name":"eUpdateSigner","type":"event"},{"inputs":[],"name":"DOMAIN_SEPARATOR","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"EIP712DOMAIN_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"TYPE_HASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_SIGNER","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_costAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_costErc20","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"_mintTimes","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_onceSignCode","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_stakeAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_stakeErc20","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"rule","type":"address"}],"name":"addMintRule","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getMintRules","outputs":[{"internalType":"address[]","name":"","type":"address[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"address","name":"user","type":"address"}],"name":"getTheMintTimes","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"mintRule","type":"address"},{"internalType":"uint256","name":"udIndex","type":"uint256"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeErc20Amount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costErc20Amount","type":"uint256"},{"internalType":"uint256","name":"limitTimes","type":"uint256"},{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"bytes32","name":"signCode","type":"bytes32"},{"internalType":"bytes","name":"wlSignature","type":"bytes"}],"internalType":"struct ICartoonMintRule.MintRule","name":"mintData","type":"tuple"}],"name":"hashCondition","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"mintRule","type":"address"},{"internalType":"uint256","name":"udIndex","type":"uint256"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeErc20Amount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costErc20Amount","type":"uint256"},{"internalType":"uint256","name":"limitTimes","type":"uint256"},{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"bytes32","name":"signCode","type":"bytes32"},{"internalType":"bytes","name":"wlSignature","type":"bytes"}],"internalType":"struct ICartoonMintRule.MintRule","name":"mintData","type":"tuple"}],"name":"hashDigest","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"bytes32","name":"signCode","type":"bytes32"}],"name":"hashWhiteList","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"signCode","type":"bytes32"}],"name":"isExistSignCode","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"mintRule","type":"address"},{"internalType":"uint256","name":"udIndex","type":"uint256"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeErc20Amount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costErc20Amount","type":"uint256"},{"internalType":"uint256","name":"limitTimes","type":"uint256"},{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"bytes32","name":"signCode","type":"bytes32"},{"internalType":"bytes","name":"wlSignature","type":"bytes"}],"internalType":"struct ICartoonMintRule.MintRule","name":"mintData","type":"tuple"},{"internalType":"bytes","name":"dataSignature","type":"bytes"}],"name":"mintAvatar721","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"rule","type":"address"}],"name":"removeMintRule","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"enable","type":"bool"}],"name":"setOnceSignCode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costAmount","type":"uint256"}],"name":"updateCostErc20","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"SIGNER","type":"address"}],"name":"updateSigner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeAmount","type":"uint256"}],"name":"updateStakeErc20","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"mintRule","type":"address"},{"internalType":"uint256","name":"udIndex","type":"uint256"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeErc20Amount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costErc20Amount","type":"uint256"},{"internalType":"uint256","name":"limitTimes","type":"uint256"},{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"bytes32","name":"signCode","type":"bytes32"},{"internalType":"bytes","name":"wlSignature","type":"bytes"}],"internalType":"struct ICartoonMintRule.MintRule","name":"mintData","type":"tuple"},{"internalType":"address","name":"user","type":"address"},{"internalType":"bytes","name":"dataSignature","type":"bytes"}],"name":"verify","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"mintRule","type":"address"},{"internalType":"uint256","name":"udIndex","type":"uint256"},{"internalType":"address","name":"stakeErc20","type":"address"},{"internalType":"uint256","name":"stakeErc20Amount","type":"uint256"},{"internalType":"address","name":"costErc20","type":"address"},{"internalType":"uint256","name":"costErc20Amount","type":"uint256"},{"internalType":"uint256","name":"limitTimes","type":"uint256"},{"internalType":"uint256","name":"mintType","type":"uint256"},{"internalType":"bytes32","name":"signCode","type":"bytes32"},{"internalType":"bytes","name":"wlSignature","type":"bytes"}],"internalType":"struct ICartoonMintRule.MintRule","name":"mintData","type":"tuple"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"verifyCondition","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"hash","type":"bytes32"},{"internalType":"bytes","name":"signature","type":"bytes"}],"name":"verifySignature","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"}]
"""
w3 = Web3(Web3.HTTPProvider("https://bsc-mainnet.nodereal.io/v1/***"))

contract = "0x37ac6a9b55DCEc42145a2147c2fccCB4c737c7E4"

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


def encode_test():
params1 = (
"0x0C522b99695e6555c5CE853f3d8d76Cb027f6Ea0",
262145,
"0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56",
0,
"0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56",
0,
1,
1,
w3.toHex(hexstr="0x8a3e42c36d299949f5cc4e0196432723f2cbacbf2f55c9bfe4419379054dc8d0"),
w3.toHex(
hexstr="0x3a7c78a1385f2fe9f2df2c795c70d61032e60de10f2a0089c46cc7b22d3ffe1c5427184c387bba8ebc665d859fbf7dc33440905372c6cfd5523ddb7698fdf0321c")
)

params2 = w3.toHex(
hexstr="0xc92324f0ec34aa659dbdb355a303c2872141ae17bcef244d96977934cdccebb36b9261732f77236e4ed4fed04de7118415ddf4ea394951f6021506b86f6a3ac21b")

try:
tx_dic = contract_.caller.mintAvatar721(params1, params2). \
buildTransaction(
{
'gas': 500000,
}
)
except Exception as e:
print(e)


if __name__ == '__main__':
tx_dic = encode_test()
请我喝杯咖啡吧~