forked from kkrt-labs/kakarot-rpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refacto chain definition (kkrt-labs#598)
Time spent on this PR: 0.5 days ## Pull request type Please check the type of change your PR introduces: - [ ] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [x] Refactoring (no functional changes, no api changes) - [ ] Build related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? In the scripts/constants.py file, there are a bunch of parameters that need to be updated whenever we need to add a new chain. ## What is the new behavior? The const definition has been updated to instead be a list of chain, each with all their parameters. ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. -->
- Loading branch information
1 parent
c439cc5
commit 50db72c
Showing
6 changed files
with
109 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,115 @@ | ||
import logging | ||
import os | ||
import re | ||
from enum import Enum | ||
from math import ceil, log | ||
from pathlib import Path | ||
|
||
from dotenv import load_dotenv | ||
from eth_keys import keys | ||
from starknet_py.net.full_node_client import FullNodeClient | ||
|
||
logging.basicConfig() | ||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
load_dotenv() | ||
|
||
ETH_TOKEN_ADDRESS = 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7 | ||
EVM_PRIVATE_KEY = os.getenv("EVM_PRIVATE_KEY") | ||
EVM_ADDRESS = ( | ||
EVM_PRIVATE_KEY | ||
and keys.PrivateKey( | ||
bytes.fromhex(EVM_PRIVATE_KEY[2:]) | ||
).public_key.to_checksum_address() | ||
) | ||
|
||
NETWORK = os.getenv("STARKNET_NETWORK", "devnet") | ||
NETWORK = ( | ||
"testnet" | ||
if re.match(r".*(testnet|goerli)$", NETWORK, flags=re.I) | ||
else "testnet2" | ||
if re.match(r".*(testnet|goerli)-?2$", NETWORK, flags=re.I) | ||
else "mainnet" | ||
if re.match(r".*(mainnet).*", NETWORK, flags=re.I) | ||
else "sharingan" | ||
if re.match(r".*(sharingan).*", NETWORK, flags=re.I) | ||
else "katana" | ||
if re.match(r".*(katana).*", NETWORK, flags=re.I) | ||
else "madara" | ||
if re.match(r".*(madara).*", NETWORK, flags=re.I) | ||
else "devnet" | ||
) | ||
STARKSCAN_URLS = { | ||
"mainnet": "https://starkscan.co", | ||
"testnet": "https://testnet.starkscan.co", | ||
"testnet2": "https://testnet-2.starkscan.co", | ||
"devnet": "https://devnet.starkscan.co", | ||
"sharingan": "https://starknet-madara.netlify.app/#/explorer/query", | ||
"katana": "", | ||
"madara": "", | ||
} | ||
STARKSCAN_URL = STARKSCAN_URLS[NETWORK] | ||
|
||
if not os.getenv("RPC_KEY") and NETWORK in ["mainnet", "testnet", "testnet2"]: | ||
raise ValueError(f"RPC_KEY env variable is required when targeting {NETWORK}") | ||
RPC_URLS = { | ||
"mainnet": f"https://starknet-mainnet.infura.io/v3/{os.getenv('RPC_KEY')}", | ||
"testnet": f"https://starknet-goerli.infura.io/v3/{os.getenv('RPC_KEY')}", | ||
"testnet2": f"https://starknet-goerli2.infura.io/v3/{os.getenv('RPC_KEY')}", | ||
"devnet": "http://127.0.0.1:5050/rpc", | ||
"sharingan": os.getenv("SHARINGAN_RPC_URL"), | ||
"katana": "http://127.0.0.1:5050", | ||
"madara": "http://127.0.0.1:9944", | ||
} | ||
RPC_CLIENT = FullNodeClient(node_url=RPC_URLS[NETWORK]) | ||
|
||
|
||
class ChainId(Enum): | ||
mainnet = int.from_bytes(b"SN_MAIN", "big") | ||
testnet = int.from_bytes(b"SN_GOERLI", "big") | ||
testnet2 = int.from_bytes(b"SN_GOERLI2", "big") | ||
devnet = int.from_bytes(b"SN_GOERLI", "big") | ||
sharingan = int.from_bytes(b"SN_GOERLI", "big") | ||
katana = int.from_bytes(b"KATANA", "big") | ||
madara = int.from_bytes(b"SN_GOERLI", "big") | ||
|
||
|
||
BUILD_DIR = Path("build") | ||
BUILD_DIR.mkdir(exist_ok=True, parents=True) | ||
SOURCE_DIR = Path("src") | ||
CONTRACTS = {p.stem: p for p in list(SOURCE_DIR.glob("**/*.cairo"))} | ||
|
||
NETWORKS = { | ||
"mainnet": { | ||
"name": "mainnet", | ||
"explorer_url": "https://starkscan.co", | ||
"rpc_url": f"https://starknet-mainnet.infura.io/v3/{os.getenv('INFURA_KEY')}", | ||
"chain_id": ChainId.mainnet, | ||
}, | ||
"testnet": { | ||
"name": "testnet", | ||
"explorer_url": "https://testnet.starkscan.co", | ||
"rpc_url": f"https://starknet-goerli.infura.io/v3/{os.getenv('INFURA_KEY')}", | ||
"chain_id": ChainId.testnet, | ||
}, | ||
"testnet2": { | ||
"name": "testnet2", | ||
"explorer_url": "https://testnet-2.starkscan.co", | ||
"rpc_url": f"https://starknet-goerli2.infura.io/v3/{os.getenv('INFURA_KEY')}", | ||
"chain_id": ChainId.testnet2, | ||
}, | ||
"devnet": { | ||
"name": "devnet", | ||
"explorer_url": "", | ||
"rpc_url": "http://127.0.0.1:5050/rpc", | ||
"chain_id": ChainId.testnet, | ||
}, | ||
"katana": { | ||
"name": "katana", | ||
"explorer_url": "", | ||
"rpc_url": "http://127.0.0.1:5050", | ||
"chain_id": ChainId.katana, | ||
}, | ||
"madara": { | ||
"name": "madara", | ||
"explorer_url": "", | ||
"rpc_url": "http://127.0.0.1:9944", | ||
"chain_id": ChainId.testnet, | ||
}, | ||
"sharingan": { | ||
"name": "sharingan", | ||
"explorer_url": "", | ||
"rpc_url": os.getenv("SHARINGAN_RPC_URL"), | ||
"chain_id": ChainId.testnet, | ||
}, | ||
} | ||
|
||
ACCOUNT_ADDRESS = os.environ.get( | ||
f"{NETWORK.upper()}_ACCOUNT_ADDRESS" | ||
) or os.environ.get("ACCOUNT_ADDRESS") | ||
PRIVATE_KEY = os.environ.get(f"{NETWORK.upper()}_PRIVATE_KEY") or os.environ.get( | ||
"PRIVATE_KEY" | ||
NETWORK = NETWORKS[os.getenv("STARKNET_NETWORK", "devnet")] | ||
NETWORK["account_address"] = os.environ.get( | ||
f"{NETWORK['name'].upper()}_ACCOUNT_ADDRESS" | ||
) | ||
if NETWORK["account_address"] is None: | ||
logger.warning( | ||
f"⚠️ {NETWORK['name'].upper()}_ACCOUNT_ADDRESS not set, defaulting to ACCOUNT_ADDRESS" | ||
) | ||
NETWORK["account_address"] = os.getenv("ACCOUNT_ADDRESS") | ||
NETWORK["private_key"] = os.environ.get(f"{NETWORK['name'].upper()}_PRIVATE_KEY") | ||
if NETWORK["private_key"] is None: | ||
logger.warning( | ||
f"⚠️ {NETWORK['name'].upper()}_PRIVATE_KEY not set, defaulting to PRIVATE_KEY" | ||
) | ||
NETWORK["private_key"] = os.getenv("PRIVATE_KEY") | ||
|
||
DEPLOYMENTS_DIR = Path("deployments") / NETWORK | ||
DEPLOYMENTS_DIR.mkdir(exist_ok=True, parents=True) | ||
RPC_CLIENT = FullNodeClient(node_url=NETWORK["rpc_url"]) | ||
|
||
# TODO: get CHAIN_ID from RPC endpoint when starknet-py doesn't expect an enum | ||
CHAIN_ID = getattr(ChainId, NETWORK) | ||
KAKAROT_CHAIN_ID = 1263227476 # KKRT (0x4b4b5254) in ASCII | ||
ETH_TOKEN_ADDRESS = 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7 | ||
SOURCE_DIR = Path("src") | ||
CONTRACTS = {p.stem: p for p in list(SOURCE_DIR.glob("**/*.cairo"))} | ||
|
||
BUILD_DIR = Path("build") | ||
BUILD_DIR.mkdir(exist_ok=True, parents=True) | ||
DEPLOYMENTS_DIR = Path("deployments") / NETWORK["name"] | ||
DEPLOYMENTS_DIR.mkdir(exist_ok=True, parents=True) | ||
COMPILED_CONTRACTS = [ | ||
{"contract_name": "kakarot", "is_account_contract": False}, | ||
{"contract_name": "blockhash_registry", "is_account_contract": False}, | ||
{"contract_name": "contract_account", "is_account_contract": False}, | ||
{"contract_name": "externally_owned_account", "is_account_contract": True}, | ||
{"contract_name": "proxy", "is_account_contract": False}, | ||
] | ||
|
||
KAKAROT_CHAIN_ID = 1263227476 # KKRT (0x4b4b5254) in ASCII | ||
EVM_PRIVATE_KEY = os.getenv("EVM_PRIVATE_KEY") | ||
EVM_ADDRESS = ( | ||
EVM_PRIVATE_KEY | ||
and keys.PrivateKey( | ||
bytes.fromhex(EVM_PRIVATE_KEY[2:]) | ||
).public_key.to_checksum_address() | ||
) | ||
|
||
logger.info( | ||
f"ℹ️ Connected to CHAIN_ID {NETWORK['chain_id'].value.to_bytes(ceil(log(NETWORK['chain_id'].value, 256)), 'big')} " | ||
f"with RPC {RPC_CLIENT.url}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters