Scrypto Types

Vaults

In Scrypto, vaults are used to hold resources. Vaults are initiated with a resource definition and can only hold tokens of that particular resource definition. Here is how you instantiate a vault:
Vault::new(Address)

Buckets

Buckets are a way to move resources throughout a transaction. At the end of a transactions, all buckets must be moved into a vault or burned. This is to make sure no tokens ever get lost between an account and a component.
You can take resources from a vault and put them in a bucket like this:
let bucket = vault.take(amount);
let bigger_bucket = vault.take_all();

BucketRef

BucketRefs are used when you write a method that could accept a bucket but you don't need to take ownership over that bucket. For example if you only need to get the quantity or resource definition of tokens inside the bucket you can define the method argument as BucketRef instead of Bucket. This makes sure that you will not be able to store the tokens in your vault or send them to another account.
Example of a method accepting a bucket_ref:
1
pub fn steal_tokens(&mut self, bucket: BucketRef) {
2
// No errors because we are only reading a value
3
info!("Amount shown: {}", bucket.amount());
4
5
// Error ! We are trying to take ownership over the BucketRef
6
self.secret_vault.put(bucket);
7
8
bucket.drop();
9
}
Copied!
You can see that we call bucket.drop() at the end of the method. It is required to drop the bucket refs that you receive or else you will get a ResourceCheckFailure when calling the method.

Accounts

If you have the address of an account, you can instantiate an Account struct like this:
Account::from(address: Address)
Here is an example where we deposit a token inside an account:
1
pub fn send_gift(&self, address: Address) {
2
let account = Account::from(address);
3
account.deposit(self.oranges.take(1));
4
}
Copied!

Components

Just like with an Account, you instantiate a component struct from the address of the component. You can then call methods on the component:
1
pub fn call_animal(&self, address: Address) {
2
let component = Component::from(address);
3
component.call::<()>("make_sound", vec![]);
4
}
Copied!
The type annotation we add on the method "::<()>" specifies the return type of the component's method. In this case, we expect it to return an empty tuple.

Import component with an ABI

Calling components like in the previous section is useful when you don't know the package's address at compile time. When you do know the package address, it is easier to use it by importing its Application Binary Interface (ABI). The ABI is a json representation of the package. It describes its functions, methods, arguments, etc... The resim CLI comes with a useful command to export the ABI of any packages easily. No need to manually write the ABI, try it with the HelloWorld package:
1
> resim export-abi [package_address] Hello
Copied!
When you have the ABI and the package's address, you can import it inside your component:
1
use scrypto::prelude::*;
2
3
// Import a package at the provided address
4
// and with the provided ABI.
5
import! {
6
r#"
7
{
8
"package": "[package_address]",
9
"name": "TokenGiver",
10
"functions": [
11
{
12
"name": "new",
13
"inputs": [],
14
"output": {
15
"type": "Custom",
16
"name": "scrypto::core::Component",
17
"generics": []
18
}
19
}
20
],
21
"methods": [
22
{
23
"name": "free_token",
24
"mutability": "Immutable",
25
"inputs": [],
26
"output": {
27
"type": "Custom",
28
"name": "scrypto::resource::Bucket",
29
"generics": []
30
}
31
}
32
]
33
}
34
"#
35
}
36
37
blueprint! {
38
struct ComponentCaller {
39
vault: Vault,
40
// You can now define variables with the name of the package as its type
41
free_token_component: TokenGiver
42
}
43
44
impl ComponentCaller {
45
pub fn new() -> Component {
46
Self {
47
vault: Vault::new(RADIX_TOKEN),
48
free_token_component: TokenGiver::new().into()
49
}
50
.instantiate()
51
}
52
53
pub fn call_get_tokens(&mut self) {
54
// You can now call methods defined in the ABI
55
let bucket = self.free_token_component.free_token();
56
self.vault.put(bucket);
57
}
58
}
59
}
Copied!
Copy link
Edit on GitHub