Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Fixed according to PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
Diogo Ribeiro committed Feb 25, 2022
1 parent 0ff9843 commit fdc7831
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 94 deletions.
34 changes: 1 addition & 33 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ python = "^3.7"
Flask = {extras = ["async"], version = "^2.0.2"}
flask-cors = "^3.0.10"
cairo-lang = "0.7.1"
py-solc-x = "^1.1.1"
dill = "^0.3.4"

[tool.poetry.dev-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions scripts/setup_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ npm link @shardlabs/starknet-hardhat-plugin

# generate artifacts
npx hardhat starknet-compile

npx hardhat compile
82 changes: 32 additions & 50 deletions test/test_postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,43 @@

from test.settings import L1_URL, GATEWAY_URL
from test.util import call, deploy, invoke, run_devnet_in_background, load_file_content
from test.web3_util import web3_call, web3_compile, web3_deploy, web3_transact
from test.web3_util import web3_call, web3_deploy, web3_transact

import os
import atexit
import time
import json
import subprocess
import requests
import pytest

from solcx import install_solc, install_solc_pragma, set_solc_version
from web3 import Web3
from web3.contract import Contract
from semantic_version import Version




SOLIDITY_CONTRACTS_PATH = "../starknet-hardhat-example/contracts"
ARTIFACTS_PATH = "../starknet-hardhat-example/starknet-artifacts/contracts"
ARTIFACTS_PATH = "starknet-hardhat-example/starknet-artifacts/contracts"
CONTRACT_PATH = f"{ARTIFACTS_PATH}/l1l2.cairo/l1l2.json"
ABI_PATH = f"{ARTIFACTS_PATH}/l1l2.cairo/l1l2_abi.json"
STARKNET_MESSAGING_PATH = f"{SOLIDITY_CONTRACTS_PATH}/MockStarknetMessaging.sol"
L1L2_EXAMPLE_ETH_PATH = f"{SOLIDITY_CONTRACTS_PATH}/L1L2.sol"

L1L2_EXAMPLE_CONTRACT_DEFINITION: Contract
MESSAGING_CONTRACT_DEFINITION: Contract
L2_CONTRACT_ADDRESS: str
ETH_CONTRACTS_PATH = "starknet-hardhat-example/artifacts/contracts"
STARKNET_MESSAGING_PATH = f"{ETH_CONTRACTS_PATH}/MockStarknetMessaging.sol/MockStarknetMessaging.json"
L1L2_EXAMPLE_PATH = f"{ETH_CONTRACTS_PATH}/L1L2.sol/L1L2Example.json"

WEB3: Web3
def pytest_configure():
pytest.L1L2_EXAMPLE_CONTRACT = None
pytest.STARKNET_MESSAGING_CONTRACT = None
pytest.L2_CONTRACT_ADDRESS = None
pytest.WEB3 = None

@pytest.mark.web3_deploy
def test_init_local_testnet():
"""Initializes a new local Hardhat testnet instance and a new Mock Messaging contract"""

global WEB3 # pylint: disable=global-statement

# Setup L1 testnet
command = ["npx", "hardhat", "node"]
# pylint: disable=consider-using-with
proc = subprocess.Popen(command,cwd="starknet-hardhat-example", close_fds=True, stdout=subprocess.PIPE)
atexit.register(proc.terminate)
atexit.register(proc.kill)
time.sleep(25)
WEB3 = Web3(Web3.HTTPProvider(L1_URL))
WEB3.eth.default_account = WEB3.eth.accounts[0]
pytest.WEB3 = Web3(Web3.HTTPProvider(L1_URL))
pytest.WEB3.eth.default_account = pytest.WEB3.eth.accounts[0]
run_devnet_in_background(sleep_seconds=5)
deploy_messaging_contract_request = {
"networkUrl": L1_URL
Expand All @@ -67,28 +59,19 @@ def test_init_local_testnet():
def test_deploy_l1_contracts():
"""Deploys Ethereum contracts in the Hardhat testnet instance, including the L1L2Example and MockStarknetMessaging contracts"""

global MESSAGING_CONTRACT_DEFINITION # pylint: disable=global-statement
global L1L2_EXAMPLE_CONTRACT_DEFINITION # pylint: disable=global-statement
global WEB3 # pylint: disable=global-statement disable=global-variable-not-assigned

install_solc(Version('0.6.12'))
set_solc_version(Version('0.6.12'))
install_solc_pragma("pragma solidity ^0.6.12;")
messaging_contract = load_file_content(STARKNET_MESSAGING_PATH)
l1l2_example_contract = load_file_content(L1L2_EXAMPLE_ETH_PATH)
messaging_contract = json.loads(load_file_content(STARKNET_MESSAGING_PATH))
l1l2_example_contract = json.loads(load_file_content(L1L2_EXAMPLE_PATH))

messaging_contract_interface = web3_compile(messaging_contract, os.path.abspath(SOLIDITY_CONTRACTS_PATH))
l1l2_example_contract_interface = web3_compile(l1l2_example_contract, os.path.abspath(SOLIDITY_CONTRACTS_PATH))
MESSAGING_CONTRACT_DEFINITION = web3_deploy(WEB3,messaging_contract_interface)
L1L2_EXAMPLE_CONTRACT_DEFINITION = web3_deploy(WEB3,l1l2_example_contract_interface,MESSAGING_CONTRACT_DEFINITION.address)
pytest.STARKNET_MESSAGING_CONTRACT = web3_deploy(pytest.WEB3,messaging_contract)
pytest.L1L2_EXAMPLE_CONTRACT = web3_deploy(pytest.WEB3,l1l2_example_contract,pytest.STARKNET_MESSAGING_CONTRACT.address)

@pytest.mark.web3_deploy
def test_load_messaging_contract():
"""Loads a Mock Messaging contract already deployed in the local testnet instance"""

load_messaging_contract_request = {
"networkUrl": L1_URL,
"address": MESSAGING_CONTRACT_DEFINITION.address
"address": pytest.STARKNET_MESSAGING_CONTRACT.address
}

resp = requests.post(
Expand All @@ -97,14 +80,13 @@ def test_load_messaging_contract():
)

resp_dict = json.loads(resp.text)
assert resp_dict["address"] == MESSAGING_CONTRACT_DEFINITION.address
assert resp_dict["address"] == pytest.STARKNET_MESSAGING_CONTRACT.address
assert resp_dict["l1_provider"] == L1_URL

@pytest.mark.deploy
def test_init_l2_contract():
"""Deploys the L1L2Example cairo contract"""

global L2_CONTRACT_ADDRESS # pylint: disable=global-statement
deploy_info = deploy(CONTRACT_PATH)

# increase and withdraw balance
Expand All @@ -118,7 +100,7 @@ def test_init_l2_contract():
function="withdraw",
address=deploy_info["address"],
abi_path=ABI_PATH,
inputs=["1","1000",L1L2_EXAMPLE_CONTRACT_DEFINITION.address]
inputs=["1","1000",pytest.L1L2_EXAMPLE_CONTRACT.address]
)

# flush postman messages
Expand All @@ -134,7 +116,7 @@ def test_init_l2_contract():
inputs=["1"]
)

L2_CONTRACT_ADDRESS=deploy_info["address"]
pytest.L2_CONTRACT_ADDRESS=deploy_info["address"]
assert value == "2333"

@pytest.mark.web3_messaging
Expand All @@ -144,27 +126,27 @@ def test_l1_l2_message_exchange():
# assert contract balance when starting
balance = web3_call(
"userBalances",
L1L2_EXAMPLE_CONTRACT_DEFINITION,
pytest.L1L2_EXAMPLE_CONTRACT,
1)
assert balance == 0

# withdraw in l1 and assert contract balance
web3_transact(
WEB3,
pytest.WEB3,
"withdraw",
L1L2_EXAMPLE_CONTRACT_DEFINITION,
int(L2_CONTRACT_ADDRESS,base=16), 1, 1000)
pytest.L1L2_EXAMPLE_CONTRACT,
int(pytest.L2_CONTRACT_ADDRESS,base=16), 1, 1000)

balance = web3_call(
"userBalances",
L1L2_EXAMPLE_CONTRACT_DEFINITION,
pytest.L1L2_EXAMPLE_CONTRACT,
1)
assert balance == 1000

# assert l2 contract balance
l2_balance = call(
function="get_balance",
address=L2_CONTRACT_ADDRESS,
address=pytest.L2_CONTRACT_ADDRESS,
abi_path=ABI_PATH,
inputs=["1"]
)
Expand All @@ -173,14 +155,14 @@ def test_l1_l2_message_exchange():

# deposit in l1 and assert contract balance
web3_transact(
WEB3,
pytest.WEB3,
"deposit",
L1L2_EXAMPLE_CONTRACT_DEFINITION,
int(L2_CONTRACT_ADDRESS,base=16), 1, 600)
pytest.L1L2_EXAMPLE_CONTRACT,
int(pytest.L2_CONTRACT_ADDRESS,base=16), 1, 600)

balance = web3_call(
"userBalances",
L1L2_EXAMPLE_CONTRACT_DEFINITION,
pytest.L1L2_EXAMPLE_CONTRACT,
1)

assert balance == 400
Expand All @@ -193,7 +175,7 @@ def test_l1_l2_message_exchange():
# assert l2 contract balance
l2_balance = call(
function="get_balance",
address=L2_CONTRACT_ADDRESS,
address=pytest.L2_CONTRACT_ADDRESS,
abi_path=ABI_PATH,
inputs=["1"]
)
Expand Down
14 changes: 4 additions & 10 deletions test/web3_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
from web3 import Web3


def web3_compile(source, path):
"""Compiles a Solidity contract"""
compiled_sol = compile_source(source,output_values=['abi', 'bin'],base_path=path)
contract_interface = list(compiled_sol.values())[0]
return contract_interface

def web3_deploy(web3: Web3, contract_interface, *inputs):
def web3_deploy(web3: Web3, contract, *inputs):
"""Deploys a Solidity contract"""
abi=contract_interface['abi']
bytecode=contract_interface['bin']
abi=contract['abi']
bytecode=contract['bytecode']
contract = web3.eth.contract(abi=abi, bytecode=bytecode)
tx_hash = contract.constructor(*inputs).transact()
tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash)
Expand All @@ -29,7 +23,7 @@ def web3_transact(web3: Web3, function, contract, *inputs):
return tx_hash

def web3_call(function, contract, *inputs):
"""Invokes a function in a Web3 contract"""
"""Calls a function in a Web3 contract"""

value = contract.get_function_by_name(function)(*inputs).call()

Expand Down

0 comments on commit fdc7831

Please sign in to comment.