Skip to content

Commit 17a4c2d

Browse files
Initial commit
1 parent 1c7e0eb commit 17a4c2d

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

7-defi-devpost/coupon/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

7-defi-devpost/coupon/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "coupon"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
8+
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
9+
10+
[dev-dependencies]
11+
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
12+
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
13+
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", tag = "v0.8.0" }
14+
15+
[profile.release]
16+
opt-level = 's' # Optimize for size.
17+
lto = true # Enable Link Time Optimization.
18+
codegen-units = 1 # Reduce number of codegen units to increase optimizations.
19+
panic = 'abort' # Abort on panic.
20+
strip = "debuginfo" # Strip debug info.
21+
overflow-checks = true # Panic in the case of an overflow.
22+
23+
[lib]
24+
crate-type = ["cdylib", "lib"]
25+
26+
[workspace]
27+
# Set the package crate as its own empty workspace, to hide it from any potential ancestor workspace
28+
# Remove this [workspace] section if you intend the package to be part of a Cargo workspace

7-defi-devpost/coupon/src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use scrypto::prelude::*;
2+
3+
#[blueprint]
4+
mod hello {
5+
struct Hello {
6+
// Define what resources and data will be managed by Hello components
7+
sample_vault: Vault,
8+
}
9+
10+
impl Hello {
11+
// Implement the functions and methods which will manage those resources and data
12+
13+
// This is a function, and can be called directly on the blueprint once deployed
14+
pub fn instantiate_hello() -> ComponentAddress {
15+
// Create a new token called "HelloToken," with a fixed supply of 1000, and put that supply into a bucket
16+
let my_bucket: Bucket = ResourceBuilder::new_fungible()
17+
.metadata("name", "HelloToken")
18+
.metadata("symbol", "HT")
19+
.mint_initial_supply(1000);
20+
21+
// Instantiate a Hello component, populating its vault with our supply of 1000 HelloToken
22+
Self {
23+
sample_vault: Vault::with_bucket(my_bucket)
24+
}
25+
.instantiate()
26+
.globalize()
27+
}
28+
29+
// This is a method, because it needs a reference to self. Methods can only be called on components
30+
pub fn free_token(&mut self) -> Bucket {
31+
info!("My balance is: {} HelloToken. Now giving away a token!", self.sample_vault.amount());
32+
// If the semi-colon is omitted on the last line, the last value seen is automatically returned
33+
// In this case, a bucket containing 1 HelloToken is returned
34+
self.sample_vault.take(1)
35+
}
36+
}
37+
}

7-defi-devpost/coupon/tests/lib.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use radix_engine_interface::model::FromPublicKey;
2+
use scrypto::prelude::*;
3+
use scrypto_unit::*;
4+
use transaction::builder::ManifestBuilder;
5+
6+
#[test]
7+
fn test_hello() {
8+
// Setup the environment
9+
let mut test_runner = TestRunner::builder().build();
10+
11+
// Create an account
12+
let (public_key, _private_key, account_component) = test_runner.new_allocated_account();
13+
14+
// Publish package
15+
let package_address = test_runner.compile_and_publish(this_package!());
16+
17+
// Test the `instantiate_hello` function.
18+
let manifest = ManifestBuilder::new()
19+
.call_function(package_address, "Hello", "instantiate_hello", args!())
20+
.build();
21+
let receipt = test_runner.execute_manifest_ignoring_fee(
22+
manifest,
23+
vec![NonFungibleGlobalId::from_public_key(&public_key)],
24+
);
25+
println!("{:?}\n", receipt);
26+
receipt.expect_commit_success();
27+
let component = receipt
28+
.expect_commit()
29+
.entity_changes
30+
.new_component_addresses[0];
31+
32+
// Test the `free_token` method.
33+
let manifest = ManifestBuilder::new()
34+
.call_method(component, "free_token", args!())
35+
.call_method(
36+
account_component,
37+
"deposit_batch",
38+
args!(ManifestExpression::EntireWorktop),
39+
)
40+
.build();
41+
let receipt = test_runner.execute_manifest_ignoring_fee(
42+
manifest,
43+
vec![NonFungibleGlobalId::from_public_key(&public_key)],
44+
);
45+
println!("{:?}\n", receipt);
46+
receipt.expect_commit_success();
47+
}

0 commit comments

Comments
 (0)