kopia lustrzana https://github.com/bugout-dev/moonworm
added helper functions
rodzic
27444f6c6f
commit
ca6f87c5c3
|
@ -6,6 +6,7 @@ from typing import Any, Dict, Union
|
|||
import libcst as cst
|
||||
from eth_typing.evm import Address, ChecksumAddress
|
||||
from libcst._nodes.statement import SimpleStatementLine
|
||||
from libcst._parser.entrypoints import parse_statement
|
||||
|
||||
CONTRACT_TEMPLATE_PATH = os.path.join(os.path.dirname(__file__), "contract.py.template")
|
||||
try:
|
||||
|
@ -39,7 +40,7 @@ EVM_PYTHON_TYPE_MAPPINGS = {
|
|||
"uint256": make_annotation(["int"]),
|
||||
"uint8": make_annotation(["int"]),
|
||||
"uint": make_annotation(["int"]),
|
||||
"bytes4": make_annotation(["str"]),
|
||||
"bytes4": make_annotation(["bytes"]),
|
||||
"string": make_annotation(["str"]),
|
||||
"address": make_annotation(["Address", "ChecksumAddress"]),
|
||||
}
|
||||
|
@ -54,8 +55,9 @@ def generate_contract_class(
|
|||
body=cst.IndentedBlock(
|
||||
body=[
|
||||
cst.parse_statement("self.web3 = web3"),
|
||||
cst.parse_statement("self.address = contract_address"),
|
||||
cst.parse_statement(
|
||||
"self.contract = web3.eth.contract(address=web3.toChecksumAddress(CONTRACT_ADDRESS), abi=CONTRACT_ABI)"
|
||||
"self.contract = web3.eth.contract(address=self.address, abi=CONTRACT_ABI)"
|
||||
),
|
||||
]
|
||||
),
|
||||
|
@ -66,6 +68,10 @@ def generate_contract_class(
|
|||
name=cst.Name("web3"),
|
||||
annotation=cst.Annotation(annotation=cst.Name("Web3")),
|
||||
),
|
||||
cst.Param(
|
||||
name=cst.Name("contract_address"),
|
||||
annotation=make_annotation(["Address", "ChecksumAddress"]),
|
||||
),
|
||||
]
|
||||
),
|
||||
)
|
||||
|
@ -117,14 +123,17 @@ def generate_contract_function(
|
|||
)
|
||||
|
||||
|
||||
def generate_contract_file(address: str, abi: Dict[str, Any]):
|
||||
def generate_contract_file(abi: Dict[str, Any], output_path: str):
|
||||
contract_body = cst.Module(body=[generate_contract_class(abi)]).code
|
||||
|
||||
JSON_FILE_PATH = "abi.json"
|
||||
JSON_FILE_PATH = os.path.join(output_path, "abi.json")
|
||||
|
||||
content = REPORTER_FILE_TEMPLATE.format(
|
||||
abi_json=JSON_FILE_PATH, contract_body=contract_body, contract_address=address
|
||||
abi_json=JSON_FILE_PATH,
|
||||
contract_body=contract_body,
|
||||
)
|
||||
with open("lol.py", "w") as ofp:
|
||||
contract_file_path = os.path.join(output_path, "lol.py")
|
||||
with open(contract_file_path, "w") as ofp:
|
||||
ofp.write(content)
|
||||
|
||||
with open(JSON_FILE_PATH, "w") as ofp:
|
||||
|
|
|
@ -1,18 +1,30 @@
|
|||
import json
|
||||
from hexbytes.main import HexBytes
|
||||
from dataclasses import dataclass
|
||||
from os import name
|
||||
from typing import Any, Dict, List, Optional, Tuple
|
||||
|
||||
from eth_typing.evm import ChecksumAddress
|
||||
import web3
|
||||
from web3 import Web3
|
||||
from .generator import generate_contract_file
|
||||
|
||||
f = open("centipede/abi.json")
|
||||
abi = json.load(f)
|
||||
IPC_PATH = "http://127.0.0.1:18375"
|
||||
from web3.contract import Contract
|
||||
from web3.types import ABIEvent, ABIFunction
|
||||
|
||||
|
||||
w3 = Web3(Web3.HTTPProvider(IPC_PATH))
|
||||
contract = w3.eth.contract(
|
||||
abi=abi, address=w3.toChecksumAddress("0x06012c8cf97bead5deae237070f9587f8e7a266d")
|
||||
)
|
||||
print(type(contract.functions.getKitty(1).call()))
|
||||
def init_web3(ipc_path: str) -> Web3:
|
||||
return web3.HTTPProvider(ipc_path)
|
||||
|
||||
generate_contract_file("0x06012c8cf97bead5deae237070f9587f8e7a266d", abi)
|
||||
|
||||
def init_contract(web3: Web3, abi: Dict[str, Any], address: Optional[str]) -> Contract:
|
||||
checksum_address: Optional[ChecksumAddress] = None
|
||||
if address is not None:
|
||||
checksum_address = web3.toChecksumAddress(address)
|
||||
return web3.eth.contract(address=checksum_address, abi=abi)
|
||||
|
||||
|
||||
def abi_show(abi: Dict[str, Any]) -> Tuple[List[ABIFunction], List[ABIEvent]]:
|
||||
abi_functions = [item for item in abi if item["type"] == "function"]
|
||||
abi_events = [item for item in abi if item["type"] == "event"]
|
||||
return (abi_functions, abi_events)
|
||||
|
||||
|
||||
def call_function(contract: Contract, function_name, *args) -> Any:
|
||||
contract.functions[function_name]().call(*args)
|
||||
|
|
Ładowanie…
Reference in New Issue