Skip to content

Commit

Permalink
Merge pull request #8 from BeakerTools/feature/better_environment_types
Browse files Browse the repository at this point in the history
Better environment types and non fungible ids macro
  • Loading branch information
arthurvinci committed Jun 7, 2024
2 parents 2a6873c + 044bae4 commit 644a294
Show file tree
Hide file tree
Showing 40 changed files with 751 additions and 414 deletions.
3 changes: 1 addition & 2 deletions data-structures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.2.0" }

[dev-dependencies]
test-engine = { path = "../test-engine" }
1 change: 1 addition & 0 deletions data-structures/src/big_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl<T: ScryptoEncode + ScryptoDecode + ScryptoDescribe + Categorize<ScryptoCust
}

#[derive(ScryptoSbor)]
#[sbor(categorize_types = "V")]
pub struct BigVec<V: BigVecElement> {
pub start_index: usize,
pub capacity_per_vec: usize,
Expand Down
4 changes: 2 additions & 2 deletions data-structures/tests/big_vec/package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "0.1.0"
edition = "2021"

[dependencies]
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.2.0" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.2.0" }
data-structures = { path = "../../../../data-structures" }

[profile.release]
Expand Down
22 changes: 11 additions & 11 deletions data-structures/tests/big_vec/package/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use data_structures::big_vec;
use data_structures::big_vec::BigVec;
use scrypto::prelude::*;
use std::ops::Deref;

#[blueprint]
mod big_vec {
use data_structures::big_vec;
use data_structures::big_vec::BigVec;
use std::ops::Deref;
mod big_vec_blueprint {

struct BigVecContract {
struct BigVecBlueprint {
vec: BigVec<u32>,
}

impl BigVecContract {
impl BigVecBlueprint {
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///
/// ///
/// Interface to the `BigVec` data structures for methods that can actually be called ///
/// ///
/// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// /// ///

pub fn new() -> Global<BigVecContract> {
pub fn new() -> Global<BigVecBlueprint> {
Self { vec: BigVec::new() }
.instantiate()
.prepare_to_globalize(OwnerRole::None)
.globalize()
}

pub fn with_capacity_per_vec(capacity_per_vec: usize) -> Global<BigVecContract> {
pub fn with_capacity_per_vec(capacity_per_vec: usize) -> Global<BigVecBlueprint> {
Self {
vec: BigVec::with_capacity_per_vec(capacity_per_vec),
}
Expand All @@ -33,7 +33,7 @@ mod big_vec {
.globalize()
}

pub fn default() -> Global<BigVecContract> {
pub fn default() -> Global<BigVecBlueprint> {
Self {
vec: BigVec::default(),
}
Expand All @@ -42,7 +42,7 @@ mod big_vec {
.globalize()
}

pub fn from(vec: Vec<u32>) -> Global<BigVecContract> {
pub fn from(vec: Vec<u32>) -> Global<BigVecBlueprint> {
Self {
vec: BigVec::from(vec),
}
Expand Down Expand Up @@ -114,7 +114,7 @@ mod big_vec {
/// ///
/// /// /// /// /// /// /// /// /// /// //// ///

pub fn with_macros() -> Global<BigVecContract> {
pub fn with_macros() -> Global<BigVecBlueprint> {
Self {
vec: big_vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}
Expand Down
16 changes: 7 additions & 9 deletions data-structures/tests/big_vec/unit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use test_engine::receipt_traits::{GetReturn, Outcome};
use test_engine::test_engine::TestEngine;
use test_engine::{env_args, global_package};
use test_engine::prelude::*;

global_package!(BIG_VEC_PACKAGE, "tests/big_vec/package");

fn instantiate() -> TestEngine {
let mut test_engine = TestEngine::with_package("big vec package", &BIG_VEC_PACKAGE);
test_engine.new_component(
"big vec comp",
"BigVecContract",
"BigVecBlueprint",
"with_capacity_per_vec",
env_args!(3 as usize),
env_args!(3usize),
);
test_engine
}
Expand All @@ -32,7 +30,7 @@ fn get_vec(test_engine: &mut TestEngine) -> Vec<u32> {
#[test]
fn test_new_big_vec() {
let mut test_engine = TestEngine::with_package("big vec package", &BIG_VEC_PACKAGE);
test_engine.new_component("big vec comp", "BigVecContract", "new", env_args!());
test_engine.new_component("big vec comp", "BigVecBlueprint", "new", env_args!());

let is_empty: bool = test_engine
.call_method("is_empty", env_args!())
Expand Down Expand Up @@ -65,7 +63,7 @@ fn test_new_with_capacity_vec() {
#[test]
fn test_new_default() {
let mut test_engine = TestEngine::with_package("big vec package", &BIG_VEC_PACKAGE);
test_engine.new_component("big vec comp", "BigVecContract", "default", env_args!());
test_engine.new_component("big vec comp", "BigVecBlueprint", "default", env_args!());

let is_empty: bool = test_engine
.call_method("is_empty", env_args!())
Expand All @@ -85,7 +83,7 @@ fn test_from() {
let expected_items: Vec<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
test_engine.new_component(
"big vec comp",
"BigVecContract",
"BigVecBlueprint",
"from",
env_args!(expected_items.clone()),
);
Expand Down Expand Up @@ -232,7 +230,7 @@ fn test_insert() {
assert_eq!(items, expected_items);

test_engine
.new_component("ok", "BigVecContract", "new", env_args!())
.new_component("ok", "BigVecBlueprint", "new", env_args!())
.assert_is_success();
test_engine.set_current_component("ok");
test_engine.call_method("insert", env_args!(0 as usize, 1 as u32));
Expand Down
4 changes: 2 additions & 2 deletions maths/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
edition = "2021"

[dependencies]
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-common = "1.2.0"
radix-common-derive = "1.2.0"

[lib]
5 changes: 3 additions & 2 deletions maths/src/exponential.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use radix_engine::types::{Decimal, I192, I256};
use crate::internal_prelude::*;

pub const SMALLEST_NON_ZERO: Decimal = Decimal(I192::from_digits([
13893700547235832536,
Expand Down Expand Up @@ -41,8 +41,9 @@ impl Exponential for Decimal {
#[cfg(test)]
mod test_exp {
use crate::exponential::{Exponential, SMALLEST_NON_ZERO};
use crate::internal_prelude::*;
use crate::RELATIVE_PRECISION;
use radix_engine::types::{dec, Decimal, I192};
use radix_common_derive::dec;

#[test]
fn test_zero() {
Expand Down
1 change: 1 addition & 0 deletions maths/src/internal_prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use radix_common::prelude::{Decimal, I192, I256, U192};
4 changes: 2 additions & 2 deletions maths/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use radix_engine::types::{Decimal, I192};

use internal_prelude::*;
pub mod exponential;
pub(crate) mod internal_prelude;
pub mod logarithm;
pub mod power;

Expand Down
5 changes: 3 additions & 2 deletions maths/src/logarithm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::exponential::Exponential;
use radix_engine::types::{Decimal, I192, U192};
use crate::internal_prelude::*;

pub const LN_2: Decimal = Decimal(I192::from_digits([693147180559945309, 0, 0]));
pub const LN_10: Decimal = Decimal(I192::from_digits([2302585092994045684, 0, 0]));
Expand Down Expand Up @@ -77,9 +77,10 @@ impl Logarithm for Decimal {
#[cfg(test)]
mod test_ln {
use crate::exponential::Exponential;
use crate::internal_prelude::*;
use crate::logarithm::{Logarithm, LN_2};
use crate::RELATIVE_PRECISION;
use radix_engine::types::{dec, Decimal, I192};
use radix_common_derive::dec;

#[test]
#[should_panic]
Expand Down
2 changes: 1 addition & 1 deletion maths/src/power.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::exponential::Exponential;
use crate::internal_prelude::*;
use crate::logarithm::Logarithm;
use radix_engine::types::Decimal;

pub trait Power {
fn pow(self, exp: Self) -> Self;
Expand Down
13 changes: 7 additions & 6 deletions test-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ license = "MIT"
edition = "2021"

[dependencies]
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-engine-stores = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v1.1.1" }
radix-common = "1.2.0"
radix-engine = "1.2.0"
radix-engine-interface = "1.2.0"
radix-transactions = "1.2.0"
scrypto-test = "1.2.0"
radix-substate-store-impls = "1.2.0"
lazy_static = "1.4.0"
indexmap = "2.2.6"

[lib]
6 changes: 3 additions & 3 deletions test-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ test-engine = { git = "https://github.com/BeakerTools/scrypto-toolkit", branch =

# Main Features

- [Basics](tutorials/1. Basics)
- [Packages and blueprints](tutorials/2. Packages and Blueprints)
- [Calling methods](tutorials/3. Method Calls)
- [Basics](tutorials/1.Basics.md)
- [Packages and blueprints](tutorials/2.Packages_and_Blueprints.md)
- [Calling methods](tutorials/3.MethodsCalls.md)

# Examples

Expand Down
7 changes: 1 addition & 6 deletions test-engine/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use radix_engine::types::Secp256k1PublicKey;
use radix_engine_common::crypto::PublicKey;
use radix_engine_interface::blueprints::resource::FromPublicKey;
use radix_engine_interface::prelude::NonFungibleGlobalId;
use radix_engine_interface::types::ComponentAddress;

use crate::engine_interface::EngineInterface;
use crate::internal_prelude::*;

#[derive(Debug, Clone)]
pub struct Account {
Expand Down
56 changes: 23 additions & 33 deletions test-engine/src/call_builder.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
use std::collections::BTreeSet;
use std::vec::Vec;

use radix_engine::transaction::{TransactionReceipt, TransactionResult};
use radix_engine::types::{
manifest_decode, ComponentAddress, Decimal, Encoder, ManifestArgs, ManifestEncoder,
ManifestExpression, ManifestValueKind, NonFungibleLocalId, PackageAddress, ResourceAddress,
FAUCET, MANIFEST_SBOR_V1_MAX_DEPTH, MANIFEST_SBOR_V1_PAYLOAD_PREFIX,
};

use transaction::builder::{ManifestBuilder, ResolvableGlobalAddress};
use transaction::manifest::decompiler::ManifestObjectNames;
use transaction::manifest::dumper::dump_manifest_to_file_system;
use transaction::prelude::{dec, DynamicGlobalAddress, ResolvableArguments, TransactionManifestV1};

use crate::account::Account;
use crate::environment::{Environment, EnvironmentEncode};
use crate::manifest_args;
use crate::environment::{EnvironmentEncode, Fungible, NonFungible};
use crate::internal_prelude::*;
use crate::method_call::SimpleMethodCaller;
use crate::references::{ComponentReference, GlobalReference, ReferenceName, ResourceReference};
use crate::test_engine::TestEngine;
use crate::to_id::ToId;

struct TransactionManifestData {
transaction_manifest: TransactionManifestV1,
Expand Down Expand Up @@ -150,7 +139,11 @@ impl<'a> CallBuilder<'a> {
/// * `recipient`: resources to transfer to.
/// * `resource`: reference name of the resource to transfer.
/// * `amount`: amount to transfer.
pub fn transfer<E: ReferenceName, R: ReferenceName + Clone + 'static, D: TryInto<Decimal>>(
pub fn transfer<
E: ReferenceName,
R: ReferenceName + Clone + 'static,
D: TryInto<Decimal> + Clone + 'static,
>(
self,
recipient: E,
resource: R,
Expand All @@ -163,10 +156,7 @@ impl<'a> CallBuilder<'a> {
recipient,
"try_deposit_or_abort",
vec![
Box::new(Environment::FungibleBucket(
resource.clone(),
amount.try_into().unwrap(),
)),
Box::new(Fungible::Bucket(resource.clone(), amount)),
Box::new(None::<u64>),
],
)
Expand All @@ -178,17 +168,20 @@ impl<'a> CallBuilder<'a> {
/// * `recipient`: resources to transfer to.
/// * `resource`: reference name of the resource to transfer.
/// * `ids`: ids to transfer.
pub fn transfer_non_fungibles<E: ReferenceName, R: ReferenceName + Clone + 'static>(
pub fn transfer_non_fungibles<E: ReferenceName, R: ReferenceName + Clone + 'static, T: ToId>(
self,
recipient: E,
resource: R,
ids: Vec<NonFungibleLocalId>,
ids: Vec<T>,
) -> Self {
self.call_from_component(
recipient,
"try_deposit_or_abort",
vec![
Box::new(Environment::NonFungibleBucket(resource, ids)),
Box::new(NonFungible::Bucket(
resource,
ids.into_iter().map(|id| id.to_id()).collect(),
)),
Box::new(None::<u64>),
],
)
Expand Down Expand Up @@ -351,7 +344,7 @@ impl<'a> CallBuilder<'a> {

manifest.instructions.insert(
0,
transaction::model::InstructionV1::CallMethod {
InstructionV1::CallMethod {
address: DynamicGlobalAddress::from(self.fee_payer),
method_name: "lock_fee".to_string(),
args: manifest_args!(self.fee_locked).resolve(),
Expand All @@ -362,22 +355,19 @@ impl<'a> CallBuilder<'a> {
fn write_deposit(&mut self) {
let manifest = &mut self.manifest_data.as_mut().unwrap().transaction_manifest;

manifest
.instructions
.push(transaction::model::InstructionV1::CallMethod {
address: DynamicGlobalAddress::from(*self.caller.address()),
method_name: "deposit_batch".to_string(),
args: manifest_args!(ManifestExpression::EntireWorktop).resolve(),
});
manifest.instructions.push(InstructionV1::CallMethod {
address: DynamicGlobalAddress::from(*self.caller.address()),
method_name: "deposit_batch".to_string(),
args: manifest_args!(ManifestExpression::EntireWorktop).resolve(),
});
}
fn write_badge(&mut self) {
let manifest = &mut self.manifest_data.as_mut().unwrap().transaction_manifest;

for (badge, opt_ids) in &self.admin_badge {
if badge.is_fungible() {
manifest.instructions.insert(
1,
transaction::model::InstructionV1::CallMethod {
InstructionV1::CallMethod {
address: DynamicGlobalAddress::from(*self.caller.address()),
method_name: "create_proof_of_amount".to_string(),
args: manifest_args!(badge, Decimal::one()).resolve(),
Expand All @@ -386,7 +376,7 @@ impl<'a> CallBuilder<'a> {
} else {
manifest.instructions.insert(
1,
transaction::model::InstructionV1::CallMethod {
InstructionV1::CallMethod {
address: DynamicGlobalAddress::from(*self.caller.address()),
method_name: "create_proof_of_non_fungibles".to_string(),
args: manifest_args!(badge, opt_ids.clone().unwrap()).resolve(),
Expand Down
Loading

0 comments on commit 644a294

Please sign in to comment.