Skip to content

Commit

Permalink
feat: add RPC URL support
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 3, 2023
1 parent 75eaa18 commit d339809
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
STARKNET_NETWORK=testnet

TESTNET_RPC_URL="https://starknet-goerli.g.alchemy.com/v2/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
TESTNET_ACCOUNT_ADDRESS=0x00a00373A00352aa367058555149b573322910D54FCDf3a926E3E56D0dCb4b0c
TESTNET_PRIVATE_KEY=0x1

MAINNET_RPC_URL="https://starknet-mainnet.g.alchemy.com/v2/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
MAINNET_ACCOUNT_ADDRESS=0x00a00373A00352aa367058555149b573322910D54FCDf3a926E3E56D0dCb4b0c
MAINNET_PRIVATE_KEY=0x1

Expand Down
27 changes: 13 additions & 14 deletions scripts/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path

from dotenv import load_dotenv
from starknet_py.net.gateway_client import GatewayClient
from starknet_py.net.full_node_client import FullNodeClient
from starknet_py.net.models.chains import StarknetChainId

logging.basicConfig()
Expand All @@ -14,13 +14,13 @@
NETWORKS = {
"mainnet": {
"name": "mainnet",
"explorer_url": "https://voyager.online",
"feeder_gateway_url": "https://alpha-mainnet.starknet.io/feeder_gateway",
"gateway_url": "https://alpha-mainnet.starknet.io/gateway",
},
"testnet": {
"name": "testnet",
"explorer_url": "https://testnet.starkscan.co",
"rpc_url": f"https://starknet-goerli.infura.io/v3/{os.getenv('INFURA_KEY')}",
"explorer_url": "https://goerli.voyager.online",
"feeder_gateway_url": "https://alpha4.starknet.io/feeder_gateway",
"gateway_url": "https://alpha4.starknet.io/gateway",
},
Expand All @@ -34,9 +34,7 @@
}

VARS = NETWORKS[os.getenv("STARKNET_NETWORK", "devnet")]
VARS["account_address"] = os.environ.get(
f"{VARS['name'].upper()}_ACCOUNT_ADDRESS"
)
VARS["account_address"] = os.environ.get(f"{VARS['name'].upper()}_ACCOUNT_ADDRESS")
if VARS["account_address"] is None:
logger.warning(
f"⚠️ {VARS['name'].upper()}_ACCOUNT_ADDRESS not set, defaulting to ACCOUNT_ADDRESS"
Expand All @@ -49,21 +47,22 @@
)
VARS["private_key"] = os.getenv("PRIVATE_KEY")
if VARS["name"] == "mainnet":
VARS["rpc_url"] = os.getenv("MAINNET_RPC_URL")
VARS["chain_id"] = StarknetChainId.MAINNET
elif VARS["name"] == "testnet2":
StarknetChainId.TESTNET2
else:
VARS["rpc_url"] = os.getenv("TESTNET2_RPC_URL")
VARS["chain_id"] = StarknetChainId.TESTNET2
elif VARS["name"] != "devnet":
VARS["rpc_url"] = os.getenv("TESTNET_RPC_URL")
VARS["chain_id"] = StarknetChainId.TESTNET

GATEWAY_CLIENT = GatewayClient(
net={
"feeder_gateway_url": VARS["feeder_gateway_url"],
"gateway_url": VARS["gateway_url"],
}
FULL_NODE_CLIENT = FullNodeClient(
node_url=VARS["rpc_url"],
net=VARS["feeder_gateway_url"],
)

ETH_TOKEN_ADDRESS = 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7
ETH_CLASS_HASH = 0x6a22bf63c7bc07effa39a25dfbd21523d211db0100a0afd054d172b81840eaf
ETH_CLASS_HASH = 0x6A22BF63C7BC07EFFA39A25DFBD21523D211DB0100A0AFD054D172B81840EAF
SOURCE_DIR = Path("src")
CONTRACTS = {p.stem: p for p in list(SOURCE_DIR.glob("**/*.cairo"))}

Expand Down
34 changes: 21 additions & 13 deletions scripts/utils/starknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
DEPLOYMENTS_DIR,
ETH_TOKEN_ADDRESS,
VARS,
GATEWAY_CLIENT,
FULL_NODE_CLIENT,
# SOURCE_DIR,
)

Expand Down Expand Up @@ -59,7 +59,7 @@ async def get_starknet_account(

return Account(
address=address,
client=GATEWAY_CLIENT,
client=FULL_NODE_CLIENT,
chain=VARS["chain_id"],
key_pair=key_pair,
)
Expand All @@ -80,6 +80,7 @@ async def get_contract(contract_name) -> Contract:
await get_starknet_account(),
)


def dump_declarations(declarations):
json.dump(
{name: hex(class_hash) for name, class_hash in declarations.items()},
Expand Down Expand Up @@ -127,21 +128,27 @@ def get_alias(contract_name):
def get_tx_url(tx_hash: int) -> str:
return f"{VARS['explorer_url']}/tx/0x{tx_hash:064x}"


def get_sierra_artifact(contract_name):
return BUILD_DIR / f"{contract_name}.sierra.json"


def get_casm_artifact(contract_name):
return BUILD_DIR / f"{contract_name}.casm.json"


def get_abi(contract_name):
sierra_artifact = get_sierra_artifact(contract_name)
contract_compiled_sierra = Path(sierra_artifact).read_text()
return create_sierra_compiled_contract(compiled_contract = contract_compiled_sierra).abi
return create_sierra_compiled_contract(
compiled_contract=contract_compiled_sierra
).abi


async def declare_v2(contract_name):
logger.info(f"ℹ️ Declaring {contract_name}")

# contract_compiled_casm is a string containing the content of the starknet-sierra-compile (.casm file)
# contract_compiled_casm is a string containing the content of the starknet-sierra-compile (.casm file)
casm_artifact = get_casm_artifact(contract_name)
contract_compiled_casm = Path(casm_artifact).read_text()
casm_class = create_casm_class(contract_compiled_casm)
Expand All @@ -151,10 +158,10 @@ async def declare_v2(contract_name):
sierra_artifact = get_sierra_artifact(contract_name)
contract_compiled_sierra = Path(sierra_artifact).read_text()
sierra_class = create_sierra_compiled_contract(contract_compiled_sierra)
sierra_class_hash= compute_sierra_class_hash(sierra_class)
sierra_class_hash = compute_sierra_class_hash(sierra_class)
# Check has not been declared before
try:
await GATEWAY_CLIENT.get_class_by_hash(class_hash=sierra_class_hash)
await FULL_NODE_CLIENT.get_class_by_hash(class_hash=sierra_class_hash)
logger.info(f"✅ Class already declared, skipping")
return sierra_class_hash
except Exception:
Expand All @@ -175,24 +182,22 @@ async def declare_v2(contract_name):
logger.info(f"✅ {contract_name} class hash: {hex(resp.class_hash)}")
return resp.class_hash


async def deploy_v2(contract_name, *args):
logger.info(f"ℹ️ Deploying {contract_name}")

account = await get_starknet_account()

sierra_class_hash = get_declarations()[contract_name]
abi = get_abi(contract_name)

deploy_result = await Contract.deploy_contract(
account=account,
class_hash=sierra_class_hash,
abi=json.loads(abi),
constructor_args=list(args),
cairo_version=1,
max_fee=int(1e17),
)
await deploy_result.wait_for_acceptance()

logger.info(
f"✅ {contract_name} deployed at: {hex(deploy_result.deployed_contract.address)}"
)
Expand All @@ -207,9 +212,11 @@ async def invoke(contract_name, function_name, inputs, address=None):
account = await get_starknet_account()
deployments = get_deployments()
call = Call(
to_addr=int(deployments[contract_name]["address"], 16) if address is None else address,
selector=get_selector_from_name(function_name),
calldata=inputs
to_addr=int(deployments[contract_name]["address"], 16)
if address is None
else address,
selector=get_selector_from_name(function_name),
calldata=inputs,
)
print("call", call)
logger.info(f"ℹ️ Invoking {contract_name}.{function_name}({json.dumps(inputs)})")
Expand All @@ -221,6 +228,7 @@ async def invoke(contract_name, function_name, inputs, address=None):
)
return response.transaction_hash


async def invoke_cairo0(contract_name, function_name, *inputs, address=None):
account = await get_starknet_account()
deployments = get_deployments()
Expand All @@ -236,4 +244,4 @@ async def invoke_cairo0(contract_name, function_name, *inputs, address=None):
f"✅ {contract_name}.{function_name} invoked at tx: %s",
hex(response.transaction_hash),
)
return response.transaction_hash
return response.transaction_hash

0 comments on commit d339809

Please sign in to comment.