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

Add required logging of SRC-20 to SRC-6 examples #151

Merged
merged 12 commits into from
Sep 17, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Description of the upcoming release here.
- [#137](https://github.com/FuelLabs/sway-standards/pull/137) Resolves warnings for SRC-6, SRC-14, and SRC-5 standard examples.
- [#136](https://github.com/FuelLabs/sway-standards/pull/136) Fixes SRC14 to recommend namespacing all non-standardized storage variables under the SRC14 namespace, fixes typos, and improves markdown in docs and inline documentation.
- [#142](https://github.com/FuelLabs/sway-standards/pull/142) Fixes errors in inline documentation for SRC-10, SRC-12, SRC-14, SRC-20, SRC-3, SRC-5, SRC-7 standards.
- [#151](https://github.com/FuelLabs/sway-standards/pull/151) Fixes SRC-6 standard examples conform to the latest SRC-20 spec of logging values after updates.
- [#151](https://github.com/FuelLabs/sway-standards/pull/151) Formats code of SRC-6 examples, and fixes some comments.

## [Version 0.6.0]

Expand Down
66 changes: 65 additions & 1 deletion examples/src6-vault/multi_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ use std::{
string::String,
};

use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}};
use standards::{
src20::{
SetDecimalsEvent,
SetNameEvent,
SetSymbolEvent,
SRC20,
TotalSupplyEvent,
},
src6::{
Deposit,
SRC6,
Withdraw,
},
};

pub struct VaultInfo {
/// Amount of assets currently managed by this vault
Expand Down Expand Up @@ -170,6 +183,37 @@ impl SRC20 for Contract {
}
}

abi SetSRC20Data {
#[storage(read)]
fn set_src20_data(
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
);
}

impl SetSRC20Data for Contract {
#[storage(read)]
fn set_src20_data(
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
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.get(asset).try_read().is_none() {
revert(0);
}
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}

/// Returns the vault shares assetid and subid for the given assets assetid and the vaults sub id
fn vault_asset_id(asset: AssetId, vault_sub_id: SubId) -> (AssetId, SubId) {
let share_asset_vault_sub_id = sha256((asset, vault_sub_id));
Expand Down Expand Up @@ -233,6 +277,16 @@ pub fn _mint(
.total_supply
.insert(asset_id, supply.unwrap_or(0) + amount);
mint_to(recipient, vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.get(asset_id)
.read(),
msg_sender()
.unwrap(),
)
.log();
}

#[storage(read, write)]
Expand All @@ -247,4 +301,14 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) {
let supply = storage.total_supply.get(asset_id).try_read().unwrap();
storage.total_supply.insert(asset_id, supply - amount);
burn(vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.get(asset_id)
.read(),
msg_sender()
.unwrap(),
)
.log();
}
64 changes: 63 additions & 1 deletion examples/src6-vault/single_asset_single_sub_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ use std::{
string::String,
};

use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}};
use standards::{
src20::{
SetDecimalsEvent,
SetNameEvent,
SetSymbolEvent,
SRC20,
TotalSupplyEvent,
},
src6::{
Deposit,
SRC6,
Withdraw,
},
};

configurable {
/// The only sub vault that can be deposited and withdrawn from this vault.
Expand Down Expand Up @@ -178,6 +191,37 @@ impl SRC20 for Contract {
}
}

abi SetSRC20Data {
#[storage(read)]
fn set_src20_data(
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
);
}

impl SetSRC20Data for Contract {
#[storage(read)]
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
fn set_src20_data(
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
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);
}
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}

/// Returns the vault shares assetid for the given assets assetid and the vaults sub id
fn vault_assetid() -> AssetId {
let share_asset_id = AssetId::new(ContractId::this(), PRE_CALCULATED_SHARE_VAULT_SUB_ID);
Expand Down Expand Up @@ -211,6 +255,15 @@ pub fn _mint(recipient: Identity, amount: u64) {
let supply = storage.total_supply.read();
storage.total_supply.write(supply + amount);
mint_to(recipient, PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
TotalSupplyEvent::new(
vault_assetid(),
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.read(),
msg_sender()
.unwrap(),
)
.log();
}

#[storage(read, write)]
Expand All @@ -225,4 +278,13 @@ pub fn _burn(asset_id: AssetId, amount: u64) {
let supply = storage.total_supply.read();
storage.total_supply.write(supply - amount);
burn(PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount);
TotalSupplyEvent::new(
vault_assetid(),
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.read(),
msg_sender()
.unwrap(),
)
.log();
}
68 changes: 66 additions & 2 deletions examples/src6-vault/single_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ use std::{
string::String,
};

use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}};
use standards::{
src20::{
SetDecimalsEvent,
SetNameEvent,
SetSymbolEvent,
SRC20,
TotalSupplyEvent,
},
src6::{
Deposit,
SRC6,
Withdraw,
},
};

pub struct VaultInfo {
/// Amount of assets currently managed by this vault
Expand Down Expand Up @@ -150,7 +163,7 @@ impl SRC6 for Contract {
underlying_asset == AssetId::base(),
storage.vault_info.get(vault_share_asset).try_read(),
) {
// In this implementation managed_assets and max_withdrawable are the same. However in case of lending out of assets, total_assets should be greater than max_withdrawable.
// In this implementation managed_assets and max_withdrawable are the same. However in case of lending out of assets, managed_assets should be greater than max_withdrawable.
(true, Some(vault_info)) => Some(vault_info.managed_assets),
_ => None,
}
Expand Down Expand Up @@ -184,6 +197,37 @@ impl SRC20 for Contract {
}
}

abi SetSRC20Data {
#[storage(read)]
fn set_src20_data(
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
);
}

impl SetSRC20Data for Contract {
#[storage(read)]
fn set_src20_data(
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
asset: AssetId,
name: Option<String>,
symbol: Option<String>,
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.get(asset).try_read().is_none() {
revert(0);
}
let sender = msg_sender().unwrap();

SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, decimals, sender).log();
}
}

/// Returns the vault shares assetid and subid for the given assets assetid and the vaults sub id
fn vault_asset_id(underlying_asset: AssetId, vault_sub_id: SubId) -> (AssetId, SubId) {
let share_asset_vault_sub_id = sha256((underlying_asset, vault_sub_id));
Expand Down Expand Up @@ -248,6 +292,16 @@ pub fn _mint(
.total_supply
.insert(asset_id, current_supply + amount);
mint_to(recipient, vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.get(asset_id)
.read(),
msg_sender()
.unwrap(),
)
.log();
}

#[storage(read, write)]
Expand All @@ -262,4 +316,14 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) {
let supply = storage.total_supply.get(asset_id).try_read().unwrap();
storage.total_supply.insert(asset_id, supply - amount);
burn(vault_sub_id, amount);
TotalSupplyEvent::new(
asset_id,
storage
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
.total_supply
.get(asset_id)
.read(),
msg_sender()
.unwrap(),
)
.log();
}
Loading