FuelLabs / FuelLabs/sway

Compilation fails based on position of `storage` declaration in code

Open
#5,752 0 comments 0 reactions 1 assignee Claimed by @tritao View on GitHub
bug compiler: collection compiler: frontend team:compiler
Dominant language
Rust
Stars
61.4k
Forks
5.4k
Avg merge
3h 33m
Merged PRs (30d)
4

Description

When compiling the below contract it compiles. If the storage declaration is moved down beside the second contract, compilation fails.

```sway
contract;

use std::{
asset::{
burn,
mint_to,
},
call_frames::{
contract_id,
msg_asset_id,
},
constants::DEFAULT_SUB_ID,
context::msg_amount,
string::String,
};

/// Determines the state of ownership.
pub enum State {
/// The ownership has not been set.
Uninitialized: (),
/// The user which has been given ownership.
Initialized: Identity,
/// The ownership has been given up and can never be set again.
Revoked: (),
}

impl core::ops::Eq for State {
fn eq(self, other: Self) -> bool {
match (self, other) {
(State::Initialized(owner1), State::Initialized(owner2)) => {
owner1 == owner2
},
(State::Uninitialized, State::Uninitialized) => true,
(State::Revoked, State::Revoked) => true,
_ => false,
}
}
}

/// Error log for when access is denied.
pub enum AccessError {
/// Emitted when the caller is not the owner of the contract.
NotOwner: (),
}

abi SRC5 {
#[storage(read)]
fn owner() -> State;
}

abi SRC3 {
#[storage(read, write)]
fn mint(recipient: Identity, vault_sub_id: SubId, amount: u64);

#[storage(read, write)]
fn burn(vault_sub_id: SubId, amount: u64);
}

abi SRC20 {
#[storage(read)]
fn total_assets() -> u64;

#[storage(read)]
fn total_supply(asset: AssetId) -> Option;

#[storage(read)]
fn name(asset: AssetId) -> Option;

#[storage(read)]
fn symbol(asset: AssetId) -> Option;

#[storage(read)]
fn decimals(asset: AssetId) -> Option;
}

configurable {
/// The decimals of the asset minted by this contract.
DECIMALS: u8 = 9u8,
/// The name of the asset minted by this contract.
NAME: str[7] = __to_str_array("MyAsset"),
/// The symbol of the asset minted by this contract.
SYMBOL: str[5] = __to_str_array("MYTKN"),
}

storage {
/// The total supply of the asset minted by this contract.
total_supply: u64 = 0,

/// Owner.
owner: State = State::Uninitialized,
}

impl SRC20 for Contract {
#[storage(read)]
fn total_assets() -> u64 {
1
}

#[storage(read)]
fn total_supply(asset: AssetId) -> Option {
if asset == AssetId::default() {
Some(storage.total_supply.read())
} else {
None
}
}

#[storage(read)]
fn name(asset: AssetId) -> Option {
if asset == AssetId::default() {
Some(String::from_ascii_str(from_str_array(NAME)))
} else {
None
}
}

#[storage(read)]
fn symbol(asset: AssetId) -> Option {
if asset == AssetId::default() {
Some(String::from_ascii_str(from_str_array(SYMBOL)))
} else {
None
}
}

#[storage(read)]
fn decimals(asset: AssetId) -> Option {
if asset == AssetId::default() {
Some(DECIMALS)
} else {
None
}
}
}

#[storage(read)]
fn is_owner() {
require(
storage.owner.read() == State::Initialized(msg_sender().unwrap()),
AccessError::NotOwner,
);
}

impl SRC3 for Contract {
#[storage(read, write)]
fn mint(recipient: Identity, sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "Incorrect Sub Id");
is_owner();

// Increment total supply of the asset and mint to the recipient.
storage
.total_supply
.write(amount + storage.total_supply.read());
mint_to(recipient, DEFAULT_SUB_ID, amount);
}

#[storage(read, write)]
fn burn(sub_id: SubId, amount: u64) {
require(sub_id == DEFAULT_SUB_ID, "Incorrect Sub Id");
require(msg_amount() >= amount, "Incorrect amount provided");
require(
msg_asset_id() == AssetId::default(),
"Incorrect asset provided",
);
is_owner();

// Decrement total supply of the asset and burn.
storage
.total_supply
.write(storage.total_supply.read() - amount);
burn(DEFAULT_SUB_ID, amount);
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.