Skip to content

Commit

Permalink
Update sdk, deposit refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DogLooksGood committed Nov 15, 2024
1 parent fe4617d commit fa88d4e
Show file tree
Hide file tree
Showing 44 changed files with 1,008 additions and 854 deletions.
22 changes: 0 additions & 22 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ members = [
"test",
"local-db",
"examples/minimal",
"examples/draw-card",
"examples/raffle",
# "examples/draw-card",
# "examples/raffle",
# "examples/simple-settle",
# "examples/blackjack",
# "examples/roshambo",
Expand Down
49 changes: 22 additions & 27 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
set dotenv-load

# Release build transactor and command line tool
build: build-transactor build-cli

# Install NPM / Cargo dependencies
dep:
cargo fetch
npm --prefix ./js i -ws
npm --prefix ./examples/demo-app i

# Release build facade server
build-facade:
cargo build -r -p race-facade

# Release build transactor server
build-transactor:
cargo build -r -p race-transactor

# Release build command line tool
build-cli:
cargo build -r -p race-cli

# Call command line tool, use `just cli help` to show help menu
cli *ARGS:
cargo run -p race-cli -- {{ARGS}}

test: test-core test-transactor

test-transactor:
cargo test -p race-transactor

test-core:
cargo test -p race-core
# Run cargo test
test:
cargo test
npm test --prefix ./js

examples: example-chat example-raffle

Expand Down Expand Up @@ -58,15 +61,19 @@ example-draw-card:
mkdir -p dev/dist
cp target/race_example_draw_card.wasm dev/dist/

# Start demo app which serves the games in `examples`
dev-demo-app:
npm --prefix ./examples/demo-app run dev

# Release build the demo-app
build-demo-app:
npm --prefix ./examples/demo-app run build

# Release build the demo-app and open it in browser
preview-demo-app: build-demo-app
npm --prefix ./examples/demo-app run preview

# Run facade with dev build, use `--help` to show help menu
dev-facade *ARGS:
cargo run -p race-facade -- {{ARGS}}

Expand All @@ -76,15 +83,10 @@ dev-reg-transactor conf:
dev-run-transactor conf:
cargo run -p race-transactor -- -c {{conf}} run

# Start transactor dev build, read CONF configuration, register and run
dev-transactor conf: (dev-reg-transactor conf) (dev-run-transactor conf)

solana:
(cd contracts/solana; cargo build-sbf)

solana-local: solana
solana program deploy ./target/deploy/race_solana.so

borsh:
sdk-borsh:
npm --prefix ./js/borsh run build

sdk-core:
Expand All @@ -96,29 +98,22 @@ sdk-solana:
sdk-facade:
npm --prefix ./js/sdk-facade run build

sdk: borsh sdk-core sdk-solana sdk-facade

publish name url:
cargo run -p race-cli -- -e local publish solana {{name}} {{url}}

create-reg:
cargo run -p race-cli -- -e local create-reg solana

create-game spec:
cargo run -p race-cli -- -e local create-game solana {{spec}}

validator:
solana-test-validator --bpf-program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s token_metadata_program.so
# Build all libs under js folder
sdk: sdk-borsh sdk-core sdk-solana sdk-facade

# Publish js PKG to npmjs
publish-npmjs pkg:
npm --prefix ./js/{{pkg}} run build
(cd js/{{pkg}}; npm publish --access=public)

# Publish all js pacakges
publish-npmjs-all: (publish-npmjs "borsh") (publish-npmjs "sdk-core") (publish-npmjs "sdk-facade") (publish-npmjs "sdk-solana")

# Publish rust PKG to crates.io
publish-crates pkg:
cargo check -p {{pkg}}
cargo test -p {{pkg}}
cargo publish -p {{pkg}}

# Publish all rust packages to crates.io
publish-crates-all: (publish-crates "race-api") (publish-crates "race-proc-macro") (publish-crates "race-core") (publish-crates "race-encryptor") (publish-crates "race-client") (publish-crates "race-test")
29 changes: 9 additions & 20 deletions api/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ pub struct Ask {
pub struct Assign {
pub random_id: RandomId,
pub player_id: u64,
pub indexes: Vec<usize>,
pub indices: Vec<usize>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
pub struct Reveal {
pub random_id: RandomId,
pub indexes: Vec<usize>,
pub indices: Vec<usize>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -68,15 +68,13 @@ impl SubGame {
bundle_addr: String,
max_players: u16,
init_data: S,
checkpoint_state: T,
) -> Result<Self> {
Ok(Self {
id,
bundle_addr,
init_account: InitAccount {
max_players,
data: borsh::to_vec(&init_data)?,
checkpoint: Some(borsh::to_vec(&checkpoint_state)?),
},
})
}
Expand Down Expand Up @@ -132,15 +130,15 @@ impl EmitBridgeEvent {
/// ```
/// # use race_api::effect::Effect;
/// let mut effect = Effect::default();
/// effect.assign(1 /* random_id */, 0 /* player_id */, vec![0, 1, 2] /* indexes */);
/// effect.assign(1 /* random_id */, 0 /* player_id */, vec![0, 1, 2] /* indices */);
/// ```
/// To reveal some items to the public, use [`Effect::reveal`].
/// It makes those items visible to everyone, including servers.
///
/// ```
/// # use race_api::effect::Effect;
/// let mut effect = Effect::default();
/// effect.reveal(1 /* random_id */, vec![0, 1, 2] /* indexes */);
/// effect.reveal(1 /* random_id */, vec![0, 1, 2] /* indices */);
/// ```
///
/// # Decisions
Expand Down Expand Up @@ -183,13 +181,7 @@ impl EmitBridgeEvent {
/// # use race_api::effect::Effect;
/// use race_api::types::Settle;
/// let mut effect = Effect::default();
/// // Increase assets
/// effect.settle(Settle::add(0 /* player_id */, 100 /* amount */));
/// // Decrease assets
/// effect.settle(Settle::sub(1 /* player_id */, 200 /* amount */));
/// // Remove player from this game, its assets will be paid out
/// effect.settle(Settle::eject(2 /* player_id*/));
/// // Make the checkpoint
/// effect.settle(0 /* player_id */, 100 /* amount */);
/// effect.checkpoint();
/// ```

Expand Down Expand Up @@ -259,20 +251,20 @@ impl Effect {
&mut self,
random_id: RandomId,
player_id: u64,
indexes: Vec<usize>,
indices: Vec<usize>,
) -> Result<()> {
self.assert_player_id(player_id, "assign random id")?;
self.assigns.push(Assign {
random_id,
player_id,
indexes,
indices,
});
Ok(())
}

/// Reveal some random items to the public.
pub fn reveal(&mut self, random_id: RandomId, indexes: Vec<usize>) {
self.reveals.push(Reveal { random_id, indexes })
pub fn reveal(&mut self, random_id: RandomId, indices: Vec<usize>) {
self.reveals.push(Reveal { random_id, indices })
}

/// Return the revealed random items by id.
Expand Down Expand Up @@ -388,9 +380,6 @@ impl Effect {
init_account: InitAccount {
max_players,
data: borsh::to_vec(&init_data)?,
// The checkpoint is always None
// It represents no checkpoint or we should use the existing one
checkpoint: None,
},
});
Ok(())
Expand Down
5 changes: 2 additions & 3 deletions api/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use borsh::{BorshDeserialize, BorshSerialize};
use crate::{effect::Effect, error::HandleResult, event::Event, init_account::InitAccount};

pub trait GameHandler: Sized + BorshSerialize + BorshDeserialize {
/// Initialize handler state with on-chain game account data. The
/// initial state must be determined by the `init_account`.
fn init_state(effect: &mut Effect, init_account: InitAccount) -> HandleResult<Self>;
/// Initialize handler state with on-chain game account data.
fn init_state(init_account: InitAccount) -> HandleResult<Self>;

/// Handle event.
fn handle_event(&mut self, effect: &mut Effect, event: Event) -> HandleResult<()>;
Expand Down
10 changes: 5 additions & 5 deletions api/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ pub enum Event {
/// Timeout when waiting for start
WaitingTimeout,

/// Random drawer takes random items by indexes.
/// Random drawer takes random items by indices.
DrawRandomItems {
sender: u64,
random_id: usize,
indexes: Vec<usize>,
indices: Vec<usize>,
},

/// Timeout for drawing random items
Expand Down Expand Up @@ -203,11 +203,11 @@ impl std::fmt::Display for Event {
Event::DrawRandomItems {
sender,
random_id,
indexes,
indices,
} => write!(
f,
"DrawRandomItems from {} for random {} with indexes {:?}",
sender, random_id, indexes
"DrawRandomItems from {} for random {} with indices {:?}",
sender, random_id, indices
),
Event::DrawTimeout => write!(f, "DrawTimeout"),
Event::ActionTimeout { player_id } => write!(f, "ActionTimeout for {}", player_id),
Expand Down
14 changes: 1 addition & 13 deletions api/src/init_account.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
use borsh::{BorshDeserialize, BorshSerialize};

use crate::{
error::{HandleError, HandleResult},
};
use crate::error::HandleError;

/// A set of arguments for game handler initialization.
#[derive(Debug, Clone, BorshSerialize, BorshDeserialize, PartialEq, Eq)]
pub struct InitAccount {
pub max_players: u16,
pub data: Vec<u8>,
pub checkpoint: Option<Vec<u8>>,
}

impl InitAccount {
pub fn data<S: BorshDeserialize>(&self) -> Result<S, HandleError> {
S::try_from_slice(&self.data).or(Err(HandleError::MalformedGameAccountData))
}

/// Get deserialized checkpoint, return None if not available.
pub fn checkpoint<T: BorshDeserialize>(&self) -> HandleResult<Option<T>> {
self.checkpoint
.as_ref()
.map(|c| T::try_from_slice(c).map_err(|_| HandleError::MalformedCheckpointData))
.transpose()
}
}

impl Default for InitAccount {
fn default() -> Self {
Self {
max_players: 0,
data: Vec::new(),
checkpoint: None,
}
}
}
2 changes: 1 addition & 1 deletion client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl Client {
}

/// Decrypt the ciphertexts with shared secrets.
/// Return a mapping from mapping from indexes to decrypted value.
/// Return a mapping from mapping from indices to decrypted value.
pub fn decrypt(
&self,
ctx: &GameContext,
Expand Down
Loading

0 comments on commit fa88d4e

Please sign in to comment.