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

feat: add manual initial storage slots #441

Merged
merged 14 commits into from
Jul 5, 2022
16 changes: 16 additions & 0 deletions docs/src/getting-started/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ Alternatively, if you want multiple instances of the same contract then use `dep
{{#include ../../../examples/contracts/src/lib.rs:deploy_with_salt}}
```

### Initializing storage slots

The storage slots of a contract can be initialized manually from `Vec<StorageSlot>` where `StorageSlot` is a struct that holds the key-value pair for a given slot. The helper function `create_storage_slot` makes it easy to create a `StorageSlot` from generic types for the key/value.

```rust,ignore
{{#include ../../../examples/contracts/src/lib.rs:storage_slot_create}}
```

Note that `create_storage_slot` will panic if you try to use a key/value with a size greater than 32 bytes.

Once created, the slots can be passed to `deploy` or `deploy_with_salt`:

```rust,ignore
{{#include ../../../examples/contracts/src/lib.rs:manual_storage}}
```

## Setting up multiple test wallets

If you need multiple test wallets, they can be setup as follows:
Expand Down
59 changes: 54 additions & 5 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ mod tests {
Some(gas_limit),
Some(byte_price),
Some(maturity)
)
),
vec![],
)
.await?;

Expand All @@ -77,6 +78,44 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn manual_storage_init() -> Result<(), Error> {
use fuels::prelude::*;

abigen!(
MyContract,
"packages/fuels-abigen-macro/tests/test_projects/storage/out/debug/storage-abi.json"
);

let wallet = launch_provider_and_get_wallet().await;

// ANCHOR: storage_slot_create
let storage_slot = create_storage_slot("slot", 42);
// ANCHOR_END: storage_slot_create

let key = **storage_slot.key();
let expected_value = storage_slot.value().to_owned();
MujkicA marked this conversation as resolved.
Show resolved Hide resolved

// ANCHOR: manual_storage
let contract_id = Contract::deploy(
"tests/test_projects/storage/out/debug/storage.bin",
&wallet,
TxParameters::default(),
vec![storage_slot],
MujkicA marked this conversation as resolved.
Show resolved Hide resolved
)
.await?;
// ANCHOR_END: manual_storage

println!("Foo contract deployed @ {:x}", contract_id);
MujkicA marked this conversation as resolved.
Show resolved Hide resolved

let contract_instance = MyContract::new(contract_id.to_string(), wallet.clone());

let value = contract_instance.get_value(key).call().await?.value;
assert_eq!(expected_value.as_slice(), value.as_slice());

Ok(())
}

#[tokio::test]
// ANCHOR: deploy_with_salt
async fn deploy_with_salt() -> Result<(), Error> {
Expand All @@ -94,6 +133,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default(),
vec![],
)
.await?;

Expand All @@ -106,6 +146,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default(),
vec![],
Salt::from(salt),
)
.await?;
Expand Down Expand Up @@ -133,6 +174,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallets[0],
TxParameters::default(),
vec![],
)
.await?;

Expand All @@ -151,6 +193,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallets[1],
TxParameters::default(),
vec![],
)
.await?;

Expand Down Expand Up @@ -179,10 +222,12 @@ mod tests {

let wallet = launch_provider_and_get_wallet().await;
let contract_id = Contract::deploy(
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default()
).await?;
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default(),
vec![]
)
.await?;
println!("Contract deployed @ {:x}", contract_id);
// ANCHOR: instantiate_contract
let contract_instance = MyContract::new(contract_id.to_string(), wallet.clone());
Expand Down Expand Up @@ -261,6 +306,7 @@ mod tests {
.bin",
&wallet,
TxParameters::default(),
vec![],
)
.await?;
println!("Contract deployed @ {:x}", contract_id);
Expand Down Expand Up @@ -299,6 +345,7 @@ mod tests {
.bin",
&wallet,
TxParameters::default(),
vec![],
)
.await?;
let contract_instance = TestContract::new(contract_id.to_string(), wallet);
Expand Down Expand Up @@ -358,6 +405,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default(),
vec![],
)
.await?;

Expand Down Expand Up @@ -396,6 +444,7 @@ mod tests {
"../../packages/fuels-abigen-macro/tests/test_projects/contract_test/out/debug/contract_test.bin",
&wallet,
TxParameters::default(),
vec![],
)
.await?;

Expand Down
Loading