Skip to content

Commit

Permalink
Merge pull request #45 from dojoengine/account-nonce
Browse files Browse the repository at this point in the history
feat: add get accoutn nonce
  • Loading branch information
Larkooo authored Aug 8, 2024
2 parents 4eec4a2 + 97989e4 commit cb180e8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions dojo.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ struct FieldElement account_chain_id(struct Account *account);

void account_set_block_id(struct Account *account, struct BlockId block_id);

struct ResultFieldElement account_nonce(struct Account *account);

struct ResultFieldElement account_execute_raw(struct Account *account,
const struct Call *calldata,
uintptr_t calldata_len);
Expand Down
2 changes: 2 additions & 0 deletions dojo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,8 @@ FieldElement account_chain_id(Account *account);

void account_set_block_id(Account *account, BlockId block_id);

Result<FieldElement> account_nonce(Account *account);

Result<FieldElement> account_execute_raw(Account *account,
const Call *calldata,
uintptr_t calldata_len);
Expand Down
10 changes: 10 additions & 0 deletions dojo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,14 @@ cdef extern from *:

FieldElement poseidon_hash(const FieldElement *felts, uintptr_t felts_len);

ResultFieldElement get_selector_from_name(const char *name);

FieldElement starknet_keccak(const uint8_t *bytes, uintptr_t bytes_len);

ResultFieldElement cairo_short_string_to_felt(const char *str);

Resultc_char parse_cairo_short_string(FieldElement felt);

ResultFieldElement typed_data_encode(const char *typed_data, FieldElement address);

FieldElement signing_key_new();
Expand Down Expand Up @@ -453,6 +461,8 @@ cdef extern from *:

void account_set_block_id(Account *account, BlockId block_id);

ResultFieldElement account_nonce(Account *account);

ResultFieldElement account_execute_raw(Account *account,
const Call *calldata,
uintptr_t calldata_len);
Expand Down
15 changes: 14 additions & 1 deletion src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::os::raw::c_char;
use std::sync::Arc;

use cainome::cairo_serde::{self, ByteArray, CairoSerde};
use starknet::accounts::{Account as StarknetAccount, ExecutionEncoding, SingleOwnerAccount};
use starknet::accounts::{
Account as StarknetAccount, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount,
};
use starknet::core::types::FunctionCall;
use starknet::core::utils::get_contract_address;
use starknet::providers::jsonrpc::HttpTransport;
Expand Down Expand Up @@ -583,6 +585,17 @@ pub unsafe extern "C" fn account_set_block_id(account: *mut Account, block_id: B
(*account).0.set_block_id(block_id);
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn account_nonce(account: *mut Account) -> Result<types::FieldElement> {
let nonce = match tokio::runtime::Runtime::new().unwrap().block_on((*account).0.get_nonce()) {
Ok(nonce) => nonce,
Err(e) => return Result::Err(e.into()),
};

Result::Ok((&nonce).into())
}

#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn account_execute_raw(
Expand Down
6 changes: 6 additions & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ impl Account {

Result::Ok(Account(account))
}

#[wasm_bindgen(js_name = nonce)]
pub async unsafe fn nonce(&self) -> Result<String, JsValue> {
let nonce = self.0.get_nonce().await.map_err(|e| JsValue::from(e.to_string()))?;
Ok(format!("{:#x}", nonce))
}
}

#[wasm_bindgen(js_name = hashGetContractAddress)]
Expand Down

0 comments on commit cb180e8

Please sign in to comment.