This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Providers * add starkli content * fix broken links * add ownable contract * Add bash examples
- Loading branch information
1 parent
1e37e7d
commit b1e5f30
Showing
10 changed files
with
443 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target | ||
.env |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Code generated by scarb DO NOT EDIT. | ||
version = 1 | ||
|
||
[[package]] | ||
name = "ownable_starknet" | ||
version = "0.1.0" | ||
dependencies = [ | ||
"snforge_std", | ||
] | ||
|
||
[[package]] | ||
name = "snforge_std" | ||
version = "0.1.0" | ||
source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.7.1#d1bd8b9a361d437e8eaeb4ebffac291a48b4c920" |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "ownable_starknet" | ||
version = "0.1.0" | ||
|
||
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest | ||
|
||
[dependencies] | ||
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.7.1" } #v0.7.0 | ||
starknet = ">=2.2.0" | ||
|
||
[[target.starknet-contract]] | ||
sierra = true | ||
casm = true |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
'admin' |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use core::traits::TryInto; | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IData<T> { | ||
fn get_data(self: @T) -> felt252; | ||
fn set_data(ref self: T, new_value: felt252); | ||
fn other_func(self: @T, other_contract: ContractAddress) -> felt252; | ||
} | ||
|
||
#[starknet::interface] | ||
trait OwnableTrait<T> { | ||
fn transfer_ownership(ref self: T, new_owner: ContractAddress); | ||
fn owner(self: @T) -> ContractAddress; | ||
} | ||
|
||
#[starknet::contract] | ||
mod ownable { | ||
use starknet::get_caller_address; | ||
use super::{ContractAddress, IData, IDataDispatcherTrait, IDataDispatcher, OwnableTrait}; | ||
|
||
#[storage] | ||
struct Storage { | ||
owner: ContractAddress, | ||
data: felt252, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
OwnershipTransferred: OwnershipTransferred, | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct OwnershipTransferred { | ||
#[key] | ||
prev_owner: ContractAddress, | ||
#[key] | ||
new_owner: ContractAddress, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, initial_owner: ContractAddress,) { | ||
self.owner.write(initial_owner); | ||
self.data.write(1); | ||
// Any variable of the storage that is not initialized | ||
// will have default value -> data = 0. | ||
} | ||
|
||
#[external(v0)] | ||
impl OwnableDataImpl of IData<ContractState> { | ||
fn other_func(self: @ContractState, other_contract: ContractAddress) -> felt252 { | ||
IDataDispatcher { contract_address: other_contract }.get_data() | ||
} | ||
|
||
fn get_data(self: @ContractState) -> felt252 { | ||
self.data.read() | ||
} | ||
|
||
fn set_data(ref self: ContractState, new_value: felt252) { | ||
self.only_owner(); | ||
self.data.write(new_value); | ||
} | ||
} | ||
|
||
#[external(v0)] | ||
impl OwnableTraitImpl of OwnableTrait<ContractState> { | ||
fn transfer_ownership(ref self: ContractState, new_owner: ContractAddress) { | ||
self.only_owner(); | ||
let prev_owner = self.owner.read(); | ||
self.owner.write(new_owner); | ||
|
||
self.emit(OwnershipTransferred { prev_owner, new_owner, }); | ||
} | ||
|
||
fn owner(self: @ContractState) -> ContractAddress { | ||
self.owner.read() | ||
} | ||
} | ||
|
||
#[generate_trait] | ||
impl PrivateMethods of PrivateMethodsTrait { | ||
fn only_owner(self: @ContractState) { | ||
let caller = get_caller_address(); | ||
assert(caller == self.owner.read(), 'Caller is not the owner'); | ||
} | ||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use starknet::{ContractAddress, Into, TryInto, OptionTrait}; | ||
use starknet::syscalls::deploy_syscall; | ||
use result::ResultTrait; | ||
use array::{ArrayTrait, SpanTrait}; | ||
use snforge_std::{declare, ContractClassTrait}; | ||
use snforge_std::io::{FileTrait, read_txt}; | ||
use snforge_std::{start_prank, stop_prank}; | ||
use snforge_std::{start_mock_call, stop_mock_call}; | ||
|
||
use ownable_starknet::{OwnableTraitDispatcher, OwnableTraitDispatcherTrait}; | ||
use ownable_starknet::{IDataSafeDispatcher, IDataSafeDispatcherTrait}; | ||
|
||
mod Errors { | ||
const INVALID_OWNER: felt252 = 'Caller is not the owner'; | ||
const INVALID_DATA: felt252 = 'Invalid data'; | ||
} | ||
|
||
mod Accounts { | ||
use traits::TryInto; | ||
use starknet::ContractAddress; | ||
|
||
fn admin() -> ContractAddress { | ||
'admin'.try_into().unwrap() | ||
} | ||
fn new_admin() -> ContractAddress { | ||
'new_admin'.try_into().unwrap() | ||
} | ||
fn bad_guy() -> ContractAddress { | ||
'bad_guy'.try_into().unwrap() | ||
} | ||
} | ||
|
||
fn deploy_contract(name: felt252) -> ContractAddress { | ||
// let account = Accounts::admin(); | ||
let contract = declare(name); | ||
|
||
let file = FileTrait::new('data/calldata.txt'); | ||
let calldata = read_txt(@file); | ||
//deploy contract | ||
contract.deploy(@calldata).unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_construct_with_admin() { | ||
let contract_address = deploy_contract('ownable'); | ||
let dispatcher = OwnableTraitDispatcher { contract_address }; | ||
let owner = dispatcher.owner(); | ||
assert(owner == Accounts::admin(), Errors::INVALID_OWNER); | ||
} | ||
|
||
#[test] | ||
fn test_transfer_ownership() { | ||
let contract_address = deploy_contract('ownable'); | ||
let dispatcher = OwnableTraitDispatcher { contract_address }; | ||
start_prank(contract_address, Accounts::admin()); | ||
dispatcher.transfer_ownership(Accounts::new_admin()); | ||
|
||
assert(dispatcher.owner() == Accounts::new_admin(), Errors::INVALID_OWNER); | ||
} | ||
|
||
#[test] | ||
#[should_panic(expected: ('Caller is not the owner',))] | ||
fn test_transfer_ownership_bad_guy() { | ||
let contract_address = deploy_contract('ownable'); | ||
let dispatcher = OwnableTraitDispatcher { contract_address }; | ||
start_prank(contract_address, Accounts::bad_guy()); | ||
dispatcher.transfer_ownership(Accounts::bad_guy()); | ||
|
||
assert(dispatcher.owner() == Accounts::bad_guy(), Errors::INVALID_OWNER); | ||
} | ||
|
||
#[test] | ||
fn test_data_mock_call_get_data() { | ||
let contract_address = deploy_contract('ownable'); | ||
let safe_dispatcher = IDataSafeDispatcher { contract_address }; | ||
let mock_ret_data = 100; | ||
start_mock_call(contract_address, 'get_data', mock_ret_data); | ||
start_prank(contract_address, Accounts::admin()); | ||
safe_dispatcher.set_data(20); | ||
let data = safe_dispatcher.get_data().unwrap(); | ||
assert(data == mock_ret_data, Errors::INVALID_DATA); | ||
stop_mock_call(contract_address, 'get_data'); | ||
|
||
let data2 = safe_dispatcher.get_data().unwrap(); | ||
assert(data2 == 20, Errors::INVALID_DATA); | ||
stop_prank(contract_address); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
chain="$(starkli chain-id --rpc http://0.0.0.0:5050)" | ||
echo "Where are connected to the starknet local devnet with chain id: $chain" | ||
|
||
block="$(starkli block-number --rpc http://0.0.0.0:5050)" | ||
echo "The latest block number on Katana is: $block" | ||
|
||
account1="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" | ||
balance="$(starkli balance $account1 --rpc http://0.0.0.0:5050)" | ||
echo "The balance of account $account1 is: $balance eth" |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/bash | ||
|
||
echo "input your testnet API URL: " | ||
read url | ||
chain="$(starkli chain-id --rpc $url)" | ||
echo "Where are connected to the starknet testnet with chain id: $chain" | ||
|
||
block="$(starkli block-number --rpc $url)" | ||
echo "The latest block number on Goerli is: $block" | ||
|
||
echo "input your transaction hash: " | ||
read hash | ||
receipt="$(starkli receipt $hash --rpc $url)" | ||
echo "The receipt of transaction $hash is: $receipt" | ||
|
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
Oops, something went wrong.