Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add getBlockTime() to simnet #1273

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions components/clarinet-sdk-wasm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,10 @@ impl SDK {
.get_data_var(&contract_id, var_name)
.ok_or("value not found".into())
}
#[wasm_bindgen(js_name=getBlockTime)]
pub fn get_block_time(&mut self) -> u64 {
self.get_session_mut().interpreter.get_block_time()
}

#[wasm_bindgen(js_name=getMapEntry)]
pub fn get_map_entry(
Expand Down
2 changes: 1 addition & 1 deletion components/clarinet-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ rebuilt with wasm-pack and npm will use it.
Built the TS project:
```console
cd ../../clarinet-sdk
npm link @hirosystems/clarinet-sdk
npm link @hirosystems/clarinet-sdk-wasm
```

You can now run `npm test`, it wil be using the local version of `clarinet-sdk-wasm`
4 changes: 4 additions & 0 deletions components/clarinet-sdk/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ describe("simnet can read contracts data vars and maps", async () => {
const counter = simnet.getDataVar("counter", "count");
expect(counter).toStrictEqual(Cl.uint(0));
});
it("can get block time", async () => {
const bt = simnet.getBlockTime();
expect(bt).toBeDefined();
});

it("can get map entry", async () => {
// add a participant in the map
Expand Down
19 changes: 19 additions & 0 deletions components/clarity-repl/src/repl/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ impl ClarityInterpreter {
analysis_db.commit();
}

pub fn get_block_time(&mut self) -> u64 {
let block_height = self.get_block_height();
let mut conn = ClarityDatabase::new(
&mut self.datastore,
&self.burn_datastore,
&self.burn_datastore,
);
conn.get_block_time(block_height)
}

pub fn get_data_var(
&mut self,
contract_id: &QualifiedContractIdentifier,
Expand Down Expand Up @@ -796,6 +806,7 @@ impl ClarityInterpreter {

#[cfg(test)]
mod tests {

use super::*;
use crate::test_fixtures::clarity_contract::ClarityContractBuilder;
use clarity::{
Expand All @@ -812,6 +823,14 @@ mod tests {
assert_eq!(interpreter.get_tx_sender(), tx_sender);
}

#[test]
fn test_get_block_time() {
let mut interpreter =
ClarityInterpreter::new(StandardPrincipalData::transient(), Settings::default());
let bt = interpreter.get_block_time();
assert_ne!(bt, 0); // TODO placeholder
}

#[test]
fn test_set_tx_sender() {
let mut interpreter =
Expand Down
Loading