Skip to content
Open
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
17 changes: 17 additions & 0 deletions simf/logical_operations.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// Returns the result of the NOT operation on the provided value
fn not(bit: bool) -> bool {
<u1>::into(jet::complement_1(<bool>::into(bit)))
}

/// Returns the result of the OR operation on the provided values
fn or(a: bool, b: bool) -> bool {
<u1>::into(jet::or_1(<bool>::into(a), <bool>::into(b)))
}

/// Returns the result of the AND operation on the provided values
fn and(a: bool, b: bool) -> bool {
<u1>::into(jet::and_1(<bool>::into(a), <bool>::into(b)))
}

// todo: remove after module functionality is implemented
fn main() {}
27 changes: 27 additions & 0 deletions simf/mock/logical_operations_mock.simf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// todo: switch to function import when available
fn not(bit: bool) -> bool {
<u1>::into(jet::complement_1(<bool>::into(bit)))
}

fn or(a: bool, b: bool) -> bool {
<u1>::into(jet::or_1(<bool>::into(a), <bool>::into(b)))
}

fn and(a: bool, b: bool) -> bool {
<u1>::into(jet::and_1(<bool>::into(a), <bool>::into(b)))
}

fn main() {
assert!(not(false));
assert!(not((not(true))));

assert!(or(true, false));
assert!(or(false, true));
assert!(or(true, true));
assert!(not(or(false, false)));

assert!(and(true, true));
assert!(not(and(true, false)));
assert!(not(and(false, true)));
assert!(not(and(false, false)));
}
65 changes: 65 additions & 0 deletions tests/logical_operations_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use simplex::simplicityhl::elements::Script;

use simplex::transaction::{FinalTransaction, PartialInput, ProgramInput, RequiredSignature};

use simplicityhl_std::artifacts::mock::logical_operations_mock::LogicalOperationsMockProgram;
use simplicityhl_std::artifacts::mock::logical_operations_mock::derived_logical_operations_mock::{
LogicalOperationsMockArguments, LogicalOperationsMockWitness,
};

fn get_script(context: &simplex::TestContext) -> (LogicalOperationsMockProgram, Script) {
let arguments = LogicalOperationsMockArguments {};

let logical_operations_program = LogicalOperationsMockProgram::new(arguments);

let logical_operations_script =
logical_operations_program.get_script_pubkey(context.get_network());

(logical_operations_program, logical_operations_script)
}

fn fund_script(context: &simplex::TestContext) -> anyhow::Result<()> {
let signer = context.get_default_signer();

let (_, logical_operations_script) = get_script(context);

let tx_receipt = signer.send(logical_operations_script.clone(), 50)?;
println!("Broadcast: {}", tx_receipt);

Ok(())
}

fn spend_script(context: &simplex::TestContext) -> anyhow::Result<()> {
let signer = context.get_default_signer();
let provider = context.get_default_provider();

let (logical_operations_program, logical_operations_script) = get_script(context);

let asserts_utxos = provider.fetch_scripthash_utxos(&logical_operations_script)?;

let mut ft = FinalTransaction::new();

let witness = LogicalOperationsMockWitness {};

ft.add_program_input(
PartialInput::new(asserts_utxos[0].clone()),
ProgramInput::new(
Box::new(logical_operations_program.as_ref().clone()),
Box::new(witness.clone()),
),
RequiredSignature::None,
);

let tx_receipt = signer.broadcast(&ft)?;
println!("Broadcast: {}", tx_receipt);

Ok(())
}

#[simplex::test]
fn logical_operations_test(context: simplex::TestContext) -> anyhow::Result<()> {
fund_script(&context)?;
spend_script(&context)?;

Ok(())
}
Loading