Skip to content

Commit

Permalink
fix: modify for deploy to starknet
Browse files Browse the repository at this point in the history
  • Loading branch information
posaune0423 committed Sep 12, 2024
1 parent 35138b3 commit af1f99a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 43 deletions.
22 changes: 22 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ The App is also tracking score for each Player.
- UI then calls the functions with only the hash value
- Reveal
- there will be 2 params: "rv_NAME" (the actual param) and "rs_NAME" (the used salt)

## How to Deploy to Starknet

### Sepolia

1. Build

```zsh
sozo build --manifest-path Scarb_deploy.toml --profile sepolia
```

2. Migrate

```zsh
sozo migrate plan --account-address $YOUR_ACCOUNT_ADDRESS --private-key $YOUR_PRIVATE_KEY --profile sepolia --manifest-path Scarb_deploy.toml
```

3. Deploy

```zsh
sozo migrate apply --account-address $YOUR_ACCOUNT_ADDRESS --private-key $YOUR_PRIVATE_KEY --profile sepolia --manifest-path Scarb_deploy.toml
```
3 changes: 2 additions & 1 deletion contracts/Scarb_deploy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ dojo = { git = "https://github.com/dojoengine/dojo", tag = "v1.0.0-alpha.11" }

[[target.dojo]]
build-external-contracts = []
path = "../contracts/src/lib.cairo"

[profile.dev-pop]

[profile.slot]

[profile.sepolia]
2 changes: 0 additions & 2 deletions contracts/dojo_sepolia.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ mappings = {}

[env]
rpc_url = "https://starknet-sepolia.public.blastapi.io/rpc/v0_7"
account_address = ""
private_key = ""
15 changes: 0 additions & 15 deletions contracts/src/apps/paint/app.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ mod paint_actions {
/// * `position` - Position of the pixel.
/// * `new_color` - Color to set the pixel to.
fn interact(ref world: IWorldDispatcher, default_params: DefaultParameters) {
println!("interact");

// let core_actions = get_core_actions(world);
let position = default_params.position;
// let player = core_actions.get_player_address(default_params.for_player);
Expand Down Expand Up @@ -136,8 +134,6 @@ mod paint_actions {
/// * `position` - Position of the pixel.
/// * `new_color` - Color to set the pixel to.
fn put_color(ref world: IWorldDispatcher, default_params: DefaultParameters) {
println!("put_color");

// Load important variables

let core_actions = get_core_actions(world);
Expand Down Expand Up @@ -178,8 +174,6 @@ mod paint_actions {
action: Option::None // Not using this feature for paint
}
);

println!("put_color DONE");
}


Expand All @@ -193,7 +187,6 @@ mod paint_actions {
// continue (x - offset) to the left

if (image_data.is_empty()) {
println!("image_data empty");
return;
}
let core_actions = get_core_actions(world);
Expand All @@ -205,7 +198,6 @@ mod paint_actions {
let mut pixel_index = 0;
let mut felt: u256 = (*image_data.at(felt_index)).into();
let mut stop = false;
println!("first felt: {}", felt);
while !stop {
// Each felt contains 7 pixels of 4 bytes each, so 224 bits. The leftmost 28 bits
// are 0 padded.
Expand Down Expand Up @@ -256,29 +248,24 @@ mod paint_actions {
/// * `position` - Position of the pixel.
/// * `new_color` - Color to set the pixel to.
fn fade(ref world: IWorldDispatcher, default_params: DefaultParameters) {
println!("fade");

let core_actions = get_core_actions(world);
let position = default_params.position;
let player = core_actions.get_player_address(default_params.for_player);
let system = core_actions.get_system_address(default_params.for_system);
let pixel = get!(world, (position.x, position.y), Pixel);

println!("decode_color");

let (r, g, b, a) = decode_color(pixel.color);

// If the color is 0,0,0 , let's stop the process, fading is done.
if r == 0 && g == 0 && b == 0 {
println!("fading is done");
delete!(world, (pixel));
return;
}

// Fade the color
let FADE_STEP = 5;

println!("encode_color");
let new_color = encode_color(
subu8(r, FADE_STEP), subu8(g, FADE_STEP), subu8(b, FADE_STEP), a
);
Expand Down Expand Up @@ -328,7 +315,6 @@ mod paint_actions {
0x89ce6748d77414b79f2312bb20f6e67d3aa4a9430933a0f461fedc92983084, // This selector
calldata.span() // The calldata prepared
);
println!("put_fading_color DONE");
}
}

Expand Down Expand Up @@ -359,7 +345,6 @@ mod paint_actions {
} else if index == 6 {
result = (felt & MASK_32).try_into().unwrap();
}
println!("{}", result);
result
}
}
11 changes: 0 additions & 11 deletions contracts/src/apps/snake/app.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ mod snake_actions {
fn interact(
ref world: IWorldDispatcher, default_params: DefaultParameters, direction: Direction
) -> u32 {
println!("snake: interact");

let core_actions = get_core_actions(world);
let position = default_params.position;

Expand Down Expand Up @@ -274,8 +272,6 @@ mod snake_actions {
}

fn move(ref world: IWorldDispatcher, owner: ContractAddress) {
println!("snake: move");

let core_actions = get_core_actions(world);

// Load the Snake
Expand All @@ -286,12 +282,10 @@ mod snake_actions {

// If the snake is dying, handle that
if snake.is_dying {
println!("snake shrinks due to dying");
snake.last_segment_id = remove_last_segment(world, core_actions, snake);
snake.length -= 1;

if snake.length == 0 {
println!("snake is dead: deleting");
let position = Position { x: first_segment.x, y: first_segment.y };
core_actions.alert_player(position, snake.owner, 'Snake died here');
emit!(
Expand Down Expand Up @@ -356,7 +350,6 @@ mod snake_actions {
// Determine what happens to the snake
// MOVE, GROW, SHRINK, DIE
if next_pixel.owner == contract_address_const::<0>() { // Snake just moves
println!("snake moves");
// Add a new segment on the next pixel and update the snake
snake
.first_segment_id =
Expand All @@ -365,11 +358,9 @@ mod snake_actions {
);
snake.last_segment_id = remove_last_segment(world, core_actions, snake);
} else if !has_write_access {
println!("snake will die");
// Snake hit a pixel that is not allowing anyting: DIE
snake.is_dying = true;
} else if next_pixel.owner == snake.owner {
println!("snake grows");
// Next pixel is owned by snake owner: GROW

// Add a new segment
Expand All @@ -389,7 +380,6 @@ mod snake_actions {
// We leave the tail as is

} else {
println!("snake shrinks");
// Next pixel is not owned but can be used temporarily
// SHRINK, though
if snake.length == 1 {
Expand All @@ -406,7 +396,6 @@ mod snake_actions {
}
}
} else {
println!("snake will die");
// Snake hit a pixel that is not allowing anyting: DIE
snake.is_dying = true;
}
Expand Down
15 changes: 1 addition & 14 deletions contracts/src/core/actions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ pub mod actions {
selector: felt252,
calldata: Span<felt252>
) {
println!("schedule_queue");

// TODO Review security

// Retrieve the caller system from the address.
Expand All @@ -185,7 +183,6 @@ pub mod actions {
QueueScheduled { id, timestamp, called_system, selector, calldata: calldata }
))
);
println!("schedule_queue DONE");
}

fn process_queue(
Expand All @@ -196,8 +193,6 @@ pub mod actions {
selector: felt252,
calldata: Span<felt252>
) {
println!("process_queue");

// A quick check on the timestamp so we know its not too early for this one
assert(timestamp <= starknet::get_block_timestamp(), 'timestamp still in the future');

Expand Down Expand Up @@ -232,7 +227,6 @@ pub mod actions {

// Tell the offchain schedulers that this one is done
emit!(world, (Event::QueueProcessed(QueueProcessed { id })));
println!("process_queue DONE");
}

fn has_write_access(
Expand Down Expand Up @@ -303,16 +297,13 @@ pub mod actions {
for_system: ContractAddress,
pixel_update: PixelUpdate
) {
println!("update_pixel");

let mut pixel = get!(world, (pixel_update.x, pixel_update.y), (Pixel));

assert(
self.has_write_access(for_player, for_system, pixel, pixel_update), 'No access!'
);

let old_pixel_app = pixel.app;
println!("{:?}", old_pixel_app);

if old_pixel_app != contract_address_const::<0>() {
let interoperable_app = IInteroperabilityDispatcher {
Expand Down Expand Up @@ -364,19 +355,15 @@ pub mod actions {
let app_caller = get!(world, for_system, (App));
interoperable_app.on_post_update(pixel_update, app_caller, for_player)
}

println!("update_pixel DONE");
}

fn get_player_address(for_player: ContractAddress) -> ContractAddress {
if for_player == contract_address_const::<0>() {
println!("get_player_address.zero");
let result = get_tx_info().unbox().account_contract_address;
println!("{:?}", result);

// Return the caller account from the transaction (the end user)
return result;
} else {
println!("get_player_address.nonzero");
// TODO: check if getter is a system or the core actions contract

// Return the for_player
Expand Down

0 comments on commit af1f99a

Please sign in to comment.