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

Commit

Permalink
Merge pull request #26 from OpenZeppelin/cairo-0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
martriay authored Nov 5, 2021
2 parents a2ccca2 + b847532 commit 7cbc373
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 86 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include LICENSE
recursive-include src/nile/base_project/ *
3 changes: 3 additions & 0 deletions src/nile/base_project/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Build and test
build :; nile compile
test :; pytest tests/
30 changes: 30 additions & 0 deletions src/nile/base_project/contracts/contract.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Declare this file as a StarkNet contract and set the required
# builtins.
%lang starknet
%builtins pedersen range_check

from starkware.cairo.common.cairo_builtins import HashBuiltin

# Define a storage variable.
@storage_var
func balance() -> (res : felt):
end
# Increases the balance by the given amount.
@external
func increase_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}(amount : felt):
let (res) = balance.read()
balance.write(res + amount)
return ()
end

# Returns the current balance.
@view
func get_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}() -> (res : felt):
let (res) = balance.read()
return (res)
end
30 changes: 30 additions & 0 deletions src/nile/base_project/tests/test_contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""contract.cairo test file."""
import os
import pytest
from starkware.starknet.testing.starknet import Starknet

# The path to the contract source code.
CONTRACT_FILE = os.path.join("contracts", "contract.cairo")


# The testing library uses python's asyncio. So the following
# decorator and the ``async`` keyword are needed.
@pytest.mark.asyncio
async def test_increase_balance():
"""Test increase_balance method."""
# Create a new Starknet class that simulates the StarkNet
# system.
starknet = await Starknet.empty()

# Deploy the contract.
contract = await starknet.deploy(
source=CONTRACT_FILE,
)

# Invoke increase_balance() twice.
await contract.increase_balance(amount=10).invoke()
await contract.increase_balance(amount=20).invoke()

# Check the result of get_balance().
execution_info = await contract.get_balance().call()
assert execution_info.result == (30,)
88 changes: 2 additions & 86 deletions src/nile/commands/init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Command to kickstart a Nile project."""
import subprocess
import sys
from distutils.dir_util import copy_tree
from pathlib import Path

from nile.commands.install import install_command
Expand All @@ -25,95 +26,10 @@ def init_command():

# create project directories
print("🗄 Creating project directory tree")
create_contracts()
create_tests()

with open("Makefile", "w") as fp:
fp.write(makefile)
copy_tree(Path(__file__).parent.parent / "base_project", ".")

print("⛵️ Nile project ready! Try running:")
print("")
print("nile compile")
print("")


def create_contracts():
"""Create contracts/ directory."""
Path("contracts/").mkdir(parents=True, exist_ok=True)
with open("contracts/contract.cairo", "w") as fp:
fp.write(contract)


def create_tests():
"""Create tests/ directory."""
Path("tests/").mkdir(parents=True, exist_ok=True)
with open("tests/test_contract.py", "w") as fp:
fp.write(test)


contract = """# Declare this file as a StarkNet contract and set the required
# builtins.
%lang starknet
%builtins pedersen range_check
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.storage import Storage
# Define a storage variable.
@storage_var
func balance() -> (res : felt):
end
# Increases the balance by the given amount.
@external
func increase_balance{
storage_ptr : Storage*, pedersen_ptr : HashBuiltin*,
range_check_ptr}(amount : felt):
let (res) = balance.read()
balance.write(res + amount)
return ()
end
# Returns the current balance.
@view
func get_balance{
storage_ptr : Storage*, pedersen_ptr : HashBuiltin*,
range_check_ptr}() -> (res : felt):
let (res) = balance.read()
return (res)
end
"""


test = """import os
import pytest
from starkware.starknet.testing.starknet import Starknet
# The path to the contract source code.
CONTRACT_FILE = os.path.join("contracts", "contract.cairo")
# The testing library uses python's asyncio. So the following
# decorator and the ``async`` keyword are needed.
@pytest.mark.asyncio
async def test_increase_balance():
# Create a new Starknet class that simulates the StarkNet
# system.
starknet = await Starknet.empty()
# Deploy the contract.
contract = await starknet.deploy(CONTRACT_FILE)
# Invoke increase_balance() twice.
await contract.increase_balance(amount=10).invoke()
await contract.increase_balance(amount=20).invoke()
# Check the result of get_balance().
assert await contract.get_balance().call() == (30,)
"""

makefile = """# Build and test
build :; nile compile
test :; pytest tests/
"""

0 comments on commit 7cbc373

Please sign in to comment.