Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayStar123 committed Sep 11, 2024
1 parent 1718d5a commit 731e5a2
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 46 deletions.
43 changes: 29 additions & 14 deletions examples/src6-vault/multi_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,29 @@ impl SetSRC20Data for Contract {
}
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
match name {
Some(unwrapped_name) => {
storage.name.write_slice(unwrapped_name);
SetNameEvent::new(asset, name, sender).log();
},
None => {
let _ = storage.name.clear();
SetNameEvent::new(asset, name, sender).log();
}
}

match symbol {
Some(unwrapped_symbol) => {
storage.symbol.write_slice(unwrapped_symbol);
SetSymbolEvent::new(asset, symbol, sender).log();
},
None => {
let _ = storage.symbol.clear();
SetSymbolEvent::new(asset, symbol, sender).log();
}
}

storage.decimals.write(decimals);
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}
Expand Down Expand Up @@ -273,16 +294,12 @@ pub fn _mint(
if supply.is_none() {
storage.total_assets.write(storage.total_assets.read() + 1);
}
storage
.total_supply
.insert(asset_id, supply.unwrap_or(0) + amount);
let new_supply = supply.unwrap_or(0) + amount;
storage.total_supply.insert(asset_id, new_supply);
mint_to(recipient, vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
.total_supply
.get(asset_id)
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand All @@ -299,14 +316,12 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) {
);
// If we pass the check above, we can assume it is safe to unwrap.
let supply = storage.total_supply.get(asset_id).try_read().unwrap();
storage.total_supply.insert(asset_id, supply - amount);
let new_supply = supply - amount;
storage.total_supply.insert(asset_id, new_supply);
burn(vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
.total_supply
.get(asset_id)
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand Down
63 changes: 46 additions & 17 deletions examples/src6-vault/single_asset_single_sub_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ storage {
managed_assets: u64 = 0,
/// The total amount of shares minted by this vault.
total_supply: u64 = 0,

/// The name of a specific asset minted by this contract.
name: StorageString = StorageString {},
/// The symbol of a specific asset minted by this contract.
symbol: StorageString = StorageString {},
/// The decimals of a specific asset minted by this contract.
decimals: u8 = 9,
}

impl SRC6 for Contract {
Expand Down Expand Up @@ -166,7 +173,10 @@ impl SRC20 for Contract {
#[storage(read)]
fn name(asset: AssetId) -> Option<String> {
if asset == vault_assetid() {
Some(String::from_ascii_str("Vault Shares"))
match storage.name.read_slice() {
Some(name) => Some(name),
None => None,
}
} else {
None
}
Expand All @@ -175,7 +185,10 @@ impl SRC20 for Contract {
#[storage(read)]
fn symbol(asset: AssetId) -> Option<String> {
if asset == vault_assetid() {
Some(String::from_ascii_str("VLTSHR"))
match storage.symbol.read_slice() {
Some(symbol) => Some(symbol),
None => None,
}
} else {
None
}
Expand All @@ -184,7 +197,7 @@ impl SRC20 for Contract {
#[storage(read)]
fn decimals(asset: AssetId) -> Option<u8> {
if asset == vault_assetid() {
Some(9_u8)
Some(storage.decimals.read())
} else {
None
}
Expand All @@ -210,14 +223,32 @@ impl SetSRC20Data for Contract {
decimals: u8,
) {
// NOTE: There are no checks for if the caller has permissions to update the metadata
// If this asset does not exist, revert
if storage.total_supply.try_read().is_none() {
revert(0);
}
require(asset == vault_assetid(), "INVALID_ASSET_ID");
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
match name {
Some(unwrapped_name) => {
storage.name.write_slice(unwrapped_name);
SetNameEvent::new(asset, name, sender).log();
},
None => {
let _ = storage.name.clear();
SetNameEvent::new(asset, name, sender).log();
}
}

match symbol {
Some(unwrapped_symbol) => {
storage.symbol.write_slice(unwrapped_symbol);
SetSymbolEvent::new(asset, symbol, sender).log();
},
None => {
let _ = storage.symbol.clear();
SetSymbolEvent::new(asset, symbol, sender).log();
}
}

storage.decimals.write(decimals);
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}
Expand Down Expand Up @@ -253,13 +284,12 @@ pub fn _mint(recipient: Identity, amount: u64) {
use std::asset::mint_to;

let supply = storage.total_supply.read();
storage.total_supply.write(supply + amount);
let new_supply = supply + amount;
storage.total_supply.write(new_supply);
mint_to(recipient, PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
TotalSupplyEvent::new(
vault_assetid(),
storage
.total_supply
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand All @@ -276,13 +306,12 @@ pub fn _burn(asset_id: AssetId, amount: u64) {
);
// If we pass the check above, we can assume it is safe to unwrap.
let supply = storage.total_supply.read();
storage.total_supply.write(supply - amount);
let new_supply = supply - amount;
storage.total_supply.write(new_supply);
burn(PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
TotalSupplyEvent::new(
vault_assetid(),
storage
.total_supply
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand Down
44 changes: 29 additions & 15 deletions examples/src6-vault/single_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,29 @@ impl SetSRC20Data for Contract {
}
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
match name {
Some(unwrapped_name) => {
storage.name.write_slice(unwrapped_name);
SetNameEvent::new(asset, name, sender).log();
},
None => {
let _ = storage.name.clear();
SetNameEvent::new(asset, name, sender).log();
}
}

match symbol {
Some(unwrapped_symbol) => {
storage.symbol.write_slice(unwrapped_symbol);
SetSymbolEvent::new(asset, symbol, sender).log();
},
None => {
let _ = storage.symbol.clear();
SetSymbolEvent::new(asset, symbol, sender).log();
}
}

storage.decimals.write(decimals);
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}
Expand Down Expand Up @@ -287,17 +308,12 @@ pub fn _mint(
if supply.is_none() {
storage.total_assets.write(storage.total_assets.read() + 1);
}
let current_supply = supply.unwrap_or(0);
storage
.total_supply
.insert(asset_id, current_supply + amount);
let new_supply = supply.unwrap_or(0) + amount;
storage.total_supply.insert(asset_id, new_supply);
mint_to(recipient, vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
.total_supply
.get(asset_id)
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand All @@ -314,14 +330,12 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) {
);
// If we pass the check above, we can assume it is safe to unwrap.
let supply = storage.total_supply.get(asset_id).try_read().unwrap();
storage.total_supply.insert(asset_id, supply - amount);
let new_supply = supply - amount;
storage.total_supply.insert(asset_id, new_supply);
burn(vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
.total_supply
.get(asset_id)
.read(),
new_supply,
msg_sender()
.unwrap(),
)
Expand Down

0 comments on commit 731e5a2

Please sign in to comment.