Skip to content

Commit

Permalink
Skip settles when there is none
Browse files Browse the repository at this point in the history
  • Loading branch information
Linerre committed Jan 31, 2025
1 parent cd4b4f4 commit fac779b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 47 deletions.
2 changes: 1 addition & 1 deletion js/sdk-sui/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PACKAGE_ID = "0xe8df1a1cc2a14786ccfc554e47526ef26c7512f827b1760cd70bd490036e277d"
export const PACKAGE_ID = "0xd867e50728e319a23d2972895f50448b1386c8bd4e21c669236a5b37fc45c2c3"
export const PROFILE_STRUCT_TYPE = `${PACKAGE_ID}::profile::PlayerProfile`
export const SERVER_STRUCT_TYPE = `${PACKAGE_ID}::server::Server`
export const GAME_STRUCT_TYPE = `${PACKAGE_ID}::game::Game`
Expand Down
105 changes: 62 additions & 43 deletions transport/src/sui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![allow(unused_imports)]
#![allow(dead_code)]
/// Transport for Sui blockchain
use async_stream::stream;
use async_trait::async_trait;
use futures::{Stream, StreamExt};
use bcs;
Expand Down Expand Up @@ -38,7 +39,8 @@ use sui_sdk::{
SuiClient, SuiClientBuilder, SUI_COIN_TYPE, SUI_DEVNET_URL
};
use tracing::{error, info, warn};

use tokio::time;
use std::time::Duration;
use std::{path::PathBuf, pin::Pin};
use std::str::FromStr;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -674,49 +676,51 @@ impl TransportT for SuiTransport {
); // this returns the needed `checks_passed` input

// prepare settles
let mut result_settles: Vec<Argument> = Vec::new();
for Settle { player_id, amount, eject } in settles {
println!("Prepare settle for {}, amoutn = {}, eject: {}",
player_id, amount, eject);
let args = vec![
add_input(&mut ptb, new_pure_arg(&player_id)?)?,
add_input(&mut ptb, new_pure_arg(&amount)?)?,
add_input(&mut ptb, new_pure_arg(&eject)?)?,
if !settles.is_empty() {
let mut result_settles: Vec<Argument> = Vec::new();
for Settle { player_id, amount, eject } in settles {
println!("Prepare settle for {}, amoutn = {}, eject: {}",
player_id, amount, eject);
let args = vec![
add_input(&mut ptb, new_pure_arg(&player_id)?)?,
add_input(&mut ptb, new_pure_arg(&amount)?)?,
add_input(&mut ptb, new_pure_arg(&eject)?)?,
];
let settle_ret = ptb.programmable_move_call(
self.package_id,
module.clone(),
new_identifier("create_settle")?,
vec![], // no type argument
args
);
result_settles.push(settle_ret);
}

// process settles in batch
let path = format!(
"{}::settle::Settle",
self.package_id.to_hex_uncompressed()
);
let settles_arg = ptb.command(Command::make_move_vec(
Some(new_typetag(&path, None)?),
result_settles,
));
let coins_arg = ptb.make_obj_vec(settle_coins)?;
let handle_settle_args = vec![
add_input(&mut ptb, CallArg::Object(game_obj_arg))?,
settles_arg,
coins_arg,
checks_passed
];
let settle_ret = ptb.programmable_move_call(
ptb.programmable_move_call(
self.package_id,
module.clone(),
new_identifier("create_settle")?,
vec![], // no type argument
args
new_identifier("handle_settle")?,
vec![new_typetag(&game_obj.token_addr, None)?],
handle_settle_args
);
result_settles.push(settle_ret);
}

// process settles in batch
let path = format!(
"{}::settle::Settle",
self.package_id.to_hex_uncompressed()
);
let settles_arg = ptb.command(Command::make_move_vec(
Some(new_typetag(&path, None)?),
result_settles,
));
let coins_arg = ptb.make_obj_vec(settle_coins)?;
let handle_settle_args = vec![
add_input(&mut ptb, CallArg::Object(game_obj_arg))?,
settles_arg,
coins_arg,
checks_passed
];
ptb.programmable_move_call(
self.package_id,
module.clone(),
new_identifier("handle_settle")?,
vec![new_typetag(&game_obj.token_addr, None)?],
handle_settle_args
);

// prepare transfers
let handle_transfer_fn = new_identifier("handle_transfer")?;
let recipient_id = ObjectID::from_address(
Expand Down Expand Up @@ -852,9 +856,9 @@ impl TransportT for SuiTransport {
gas_coin.balance,
gas_price
);
let gas_fees = self.estimate_gas(tx_data.clone()).await?;
println!("Needed gase fees {gas_fees} and transport has balance: {}",
gas_coin.balance);
// let gas_fees = self.estimate_gas(tx_data.clone()).await?;
// println!("Needed gase fees {gas_fees} and transport has balance: {}",
// gas_coin.balance);

let response = self.send_transaction(tx_data).await?;
println!("Game settlement tx digest: {}", response.digest.to_string());
Expand Down Expand Up @@ -1066,7 +1070,23 @@ impl TransportT for SuiTransport {
}

async fn subscribe_game_account<'a>(&'a self, addr: &'a str) -> Result<Pin<Box<dyn Stream<Item = Result<GameAccount>> + Send + 'a>>> {
todo!()
Ok(Box::pin(stream! {
let mut access_version = 0;
loop {
match self.get_game_account(addr).await {
Ok(game_account_opt) => {
if let Some(game_account) = game_account_opt {
if game_account.access_version > access_version {
access_version = game_account.access_version;
yield Ok(game_account);
}
}
}
Err(e) => yield Err(Error::TransportError(e.to_string())),
}
time::sleep(Duration::from_secs(5)).await;
}
}))
}

async fn get_game_bundle(&self, addr: &str) -> Result<Option<GameBundle>> {
Expand Down Expand Up @@ -1224,7 +1244,6 @@ impl SuiTransport {
// add a small buffer to the estimated gas fees
Ok(net_gas_fees as u64 + 50)
}

}

// The `initial_shared_version` is needed for mutating an on-chain object
Expand Down
2 changes: 1 addition & 1 deletion transport/src/sui/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub const MAX_GAME_NAME_LEN: usize = 50;
pub const BUNDLE_COVER: &str = "https://ar-io.net/RxxOQizlpeUfLJzDmNYSCrBRtIWibkAUC-VhO2coFbE";

// Common Coin/Objects addresses
pub const PACKAGE_ID: &str = "0x871240baf13f7deb27ddbf0b0c3a4937a20dfca472f19cd7bf53e8d7da553d71";
pub const PACKAGE_ID: &str = "0xd867e50728e319a23d2972895f50448b1386c8bd4e21c669236a5b37fc45c2c3";
pub const SUI_ACCOUNT: &str = "0xd1204296954a3db409ecd2fd35c2ee750f12dafb1088cb1656566078fc46ad6e";
pub const PUBLISHER: &str = "0x7a1f6dc139d351b41066ea726d9b53670b6d827a0745d504dc93e61a581f7192";
pub const COIN_SUI_PATH: &str = "0x2::sui::SUI";
Expand Down
4 changes: 2 additions & 2 deletions transport/src/sui/types/object/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ pub struct ServerObject {

impl ServerObject {
pub fn into_account(self) -> ServerAccount {
let ServerObject { addr, endpoint, .. } = self;
let ServerObject { owner, endpoint, .. } = self;
ServerAccount {
addr: addr.to_string(),
addr: owner.to_string(),
endpoint
}
}
Expand Down

0 comments on commit fac779b

Please sign in to comment.