diff --git a/.gitignore b/.gitignore index 73aa31e60..0ed97fa9e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ target .snfoundry_cache/ +build +.protostar_cache diff --git a/deployment/Pipfile b/deployment/Pipfile new file mode 100644 index 000000000..71e4f7cbf --- /dev/null +++ b/deployment/Pipfile @@ -0,0 +1,11 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] + +[dev-packages] + +[requires] +python_version = "3.9" diff --git a/deployment/Pipfile.lock b/deployment/Pipfile.lock new file mode 100644 index 000000000..8aee7e8e0 --- /dev/null +++ b/deployment/Pipfile.lock @@ -0,0 +1,20 @@ +{ + "_meta": { + "hash": { + "sha256": "a36a5392bb1e8bbc06bfaa0761e52593cf2d83b486696bf54667ba8da616c839" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.9" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": {}, + "develop": {} +} diff --git a/deployment/README.md b/deployment/README.md new file mode 100644 index 000000000..066262e13 --- /dev/null +++ b/deployment/README.md @@ -0,0 +1,5 @@ +docker run -p 5050:5050 shardlabs/starknet-devnet-rs --seed 0 + +``` +export PROTOSTAR_ACCOUNT_PRIVATE_KEY=0x71d7bb07b9a64f6f78ac4c816aff4da9 && protostar declare-cairo0 --account-address 0x64b48806902a367c8598f4f95c305e8c1a1acba5f082d294a43793113115691 --max-fee auto --gateway-url http://localhost:5050/ --chain-id 1536727068981429685321 build/main.json +``` diff --git a/deployment/protostar.toml b/deployment/protostar.toml new file mode 100644 index 000000000..09111913c --- /dev/null +++ b/deployment/protostar.toml @@ -0,0 +1,7 @@ +[project] +protostar-version = "0.14.0" +lib-path = "lib" + +[contracts] +main = ["src/main.cairo"] + diff --git a/deployment/src/main.cairo b/deployment/src/main.cairo new file mode 100644 index 000000000..6321e351e --- /dev/null +++ b/deployment/src/main.cairo @@ -0,0 +1,32 @@ +%lang starknet +from starkware.cairo.common.math import assert_nn +from starkware.cairo.common.cairo_builtins import HashBuiltin + +@storage_var +func balance() -> (res: felt) { +} + +@external +func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}( + amount: felt +) { + with_attr error_message("Amount must be positive. Got: {amount}.") { + assert_nn(amount); + } + + let (res) = balance.read(); + balance.write(res + amount); + return (); +} + +@view +func get_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (res: felt) { + let (res) = balance.read(); + return (res,); +} + +@constructor +func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() { + balance.write(0); + return (); +} diff --git a/deployment/tests/test_main.cairo b/deployment/tests/test_main.cairo new file mode 100644 index 000000000..049bb5939 --- /dev/null +++ b/deployment/tests/test_main.cairo @@ -0,0 +1,28 @@ +%lang starknet +from src.main import balance, increase_balance +from starkware.cairo.common.cairo_builtins import HashBuiltin + +@external +func test_increase_balance{syscall_ptr: felt*, range_check_ptr, pedersen_ptr: HashBuiltin*}() { + let (result_before) = balance.read(); + assert result_before = 0; + + increase_balance(42); + + let (result_after) = balance.read(); + assert result_after = 42; + return (); +} + +@external +func test_cannot_increase_balance_with_negative_value{ + syscall_ptr: felt*, range_check_ptr, pedersen_ptr: HashBuiltin* +}() { + let (result_before) = balance.read(); + assert result_before = 0; + + %{ expect_revert("TRANSACTION_FAILED", "Amount must be positive") %} + increase_balance(-42); + + return (); +}