From 18d4842fc6f2d99ec9331464509e84c3b91287f7 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:12:18 +0530 Subject: [PATCH 01/12] init --- CHANGELOG.md | 2 + .../src6-vault/multi_asset_vault/src/main.sw | 65 ++++++++++++++++++- .../single_asset_single_sub_vault/src/main.sw | 63 +++++++++++++++++- .../src6-vault/single_asset_vault/src/main.sw | 51 ++++++++++++++- 4 files changed, 177 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1d3c0d..75cdfdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. +- [#150](https://github.com/FuelLabs/sway-standards/pull/150) Fixes SRC-6 standard examples conform to the latest SRC-20 spec of logging values after updates. +- [#150](https://github.com/FuelLabs/sway-standards/pull/150) Formats code of SRC-6 examples, and fixes some comments. ## [Version 0.6.0] diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index ac964e3..6614caf 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -12,7 +12,18 @@ use std::{ string::String, }; -use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}}; +use standards::{ + src20::SRC20, + src6::{ + Deposit, + SetDecimalsEvent, + SetNameEvent, + SetSymbolEvent, + SRC6, + TotalSupplyEvent, + Withdraw, + }, +}; pub struct VaultInfo { /// Amount of assets currently managed by this vault @@ -170,6 +181,38 @@ impl SRC20 for Contract { } } +abi SetSRC20Data { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + name: Option, + symbol: Option, + decimals: u8, + ); +} + +impl SetSRC20Data for Contract { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + supply: u64, + name: Option, + symbol: Option, + 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)); @@ -233,6 +276,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 + .total_supply + .get(asset_id) + .unwrap(), + msg_sender() + .unwrap(), + ) + .log(); } #[storage(read, write)] @@ -247,4 +300,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 + .total_supply + .get(asset_id) + .unwrap(), + msg_sender() + .unwrap(), + ) + .log(); } diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index 32a43b9..db742cf 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -12,7 +12,18 @@ use std::{ string::String, }; -use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}}; +use standards::{ + src20::SRC20, + src6::{ + Deposit, + SetDecimalsEvent, + SetNameEvent, + SetSymbolEvent, + SRC6, + TotalSupplyEvent, + Withdraw, + }, +}; configurable { /// The only sub vault that can be deposited and withdrawn from this vault. @@ -178,6 +189,38 @@ impl SRC20 for Contract { } } +abi SetSRC20Data { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + name: Option, + symbol: Option, + decimals: u8, + ); +} + +impl SetSRC20Data for Contract { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + supply: u64, + name: Option, + symbol: Option, + 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 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); @@ -211,6 +254,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 + .total_supply + .read(), + msg_sender() + .unwrap(), + ) + .log(); } #[storage(read, write)] @@ -225,4 +277,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 + .total_supply + .read(), + msg_sender() + .unwrap(), + ) + .log(); } diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 2a282b5..65d03c2 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -12,7 +12,18 @@ use std::{ string::String, }; -use standards::{src20::SRC20, src6::{Deposit, SRC6, Withdraw}}; +use standards::{ + src20::SRC20, + src6::{ + Deposit, + SetDecimalsEvent, + SetNameEvent, + SetSymbolEvent, + SRC6, + TotalSupplyEvent, + Withdraw, + }, +}; pub struct VaultInfo { /// Amount of assets currently managed by this vault @@ -150,7 +161,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, } @@ -184,6 +195,38 @@ impl SRC20 for Contract { } } +abi SetSRC20Data { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + name: Option, + symbol: Option, + decimals: u8, + ); +} + +impl SetSRC20Data for Contract { + #[storage(read)] + fn set_src20_data( + asset: AssetId, + supply: u64, + name: Option, + symbol: Option, + 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)); @@ -248,6 +291,8 @@ pub fn _mint( .total_supply .insert(asset_id, current_supply + amount); mint_to(recipient, vault_sub_id, amount); + TotalSupplyEvent::new(asset_id, storage.total_supply.read(), msg_sender().unwrap()) + .log(); } #[storage(read, write)] @@ -262,4 +307,6 @@ 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, supply - amount, msg_sender().unwrap()) + .log(); } From 64dbef4034cd71d7c072078d549550bb539d47f8 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:17:28 +0530 Subject: [PATCH 02/12] remove unused param from SetSRC20Data impl --- examples/src6-vault/multi_asset_vault/src/main.sw | 1 - examples/src6-vault/single_asset_single_sub_vault/src/main.sw | 1 - examples/src6-vault/single_asset_vault/src/main.sw | 1 - 3 files changed, 3 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index 6614caf..269deb0 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -195,7 +195,6 @@ impl SetSRC20Data for Contract { #[storage(read)] fn set_src20_data( asset: AssetId, - supply: u64, name: Option, symbol: Option, decimals: u8, diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index db742cf..9232003 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -203,7 +203,6 @@ impl SetSRC20Data for Contract { #[storage(read)] fn set_src20_data( asset: AssetId, - supply: u64, name: Option, symbol: Option, decimals: u8, diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 65d03c2..e492158 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -209,7 +209,6 @@ impl SetSRC20Data for Contract { #[storage(read)] fn set_src20_data( asset: AssetId, - supply: u64, name: Option, symbol: Option, decimals: u8, From 6ab90c36584f0e7124c63cfc2c135caeb3351cde Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:20:03 +0530 Subject: [PATCH 03/12] fix import --- examples/src6-vault/multi_asset_vault/src/main.sw | 10 ++++++---- .../single_asset_single_sub_vault/src/main.sw | 10 ++++++---- examples/src6-vault/single_asset_vault/src/main.sw | 10 ++++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index 269deb0..39662cf 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -13,14 +13,16 @@ use std::{ }; use standards::{ - src20::SRC20, - src6::{ - Deposit, + src20::{ SetDecimalsEvent, SetNameEvent, SetSymbolEvent, - SRC6, + SRC20, TotalSupplyEvent, + }, + src6::{ + Deposit, + SRC6, Withdraw, }, }; diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index 9232003..8abde9d 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -13,14 +13,16 @@ use std::{ }; use standards::{ - src20::SRC20, - src6::{ - Deposit, + src20::{ SetDecimalsEvent, SetNameEvent, SetSymbolEvent, - SRC6, + SRC20, TotalSupplyEvent, + }, + src6::{ + Deposit, + SRC6, Withdraw, }, }; diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index e492158..97ae1c0 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -13,14 +13,16 @@ use std::{ }; use standards::{ - src20::SRC20, - src6::{ - Deposit, + src20::{ SetDecimalsEvent, SetNameEvent, SetSymbolEvent, - SRC6, + SRC20, TotalSupplyEvent, + }, + src6::{ + Deposit, + SRC6, Withdraw, }, }; From 6dc8a28fd635023ac24c55b0cbb16b5f0afa649e Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:23:24 +0530 Subject: [PATCH 04/12] fix some get calls --- examples/src6-vault/single_asset_vault/src/main.sw | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 97ae1c0..bd0d95a 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -292,7 +292,7 @@ pub fn _mint( .total_supply .insert(asset_id, current_supply + amount); mint_to(recipient, vault_sub_id, amount); - TotalSupplyEvent::new(asset_id, storage.total_supply.read(), msg_sender().unwrap()) + TotalSupplyEvent::new(asset_id, storage.total_supply.get(asset_id).unwrap(), msg_sender().unwrap()) .log(); } @@ -308,6 +308,6 @@ 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, supply - amount, msg_sender().unwrap()) + TotalSupplyEvent::new(asset_id, storage.total_supply.get(asset_id).unwrap(), msg_sender().unwrap()) .log(); } From 90d748c5e3c3581c9104ea52079f716e9e95260c Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:24:43 +0530 Subject: [PATCH 05/12] fmt 2 --- .../src6-vault/single_asset_vault/src/main.sw | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index bd0d95a..1a0dbd1 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -292,7 +292,15 @@ pub fn _mint( .total_supply .insert(asset_id, current_supply + amount); mint_to(recipient, vault_sub_id, amount); - TotalSupplyEvent::new(asset_id, storage.total_supply.get(asset_id).unwrap(), msg_sender().unwrap()) + TotalSupplyEvent::new( + asset_id, + storage + .total_supply + .get(asset_id) + .unwrap(), + msg_sender() + .unwrap(), + ) .log(); } @@ -308,6 +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.total_supply.get(asset_id).unwrap(), msg_sender().unwrap()) + TotalSupplyEvent::new( + asset_id, + storage + .total_supply + .get(asset_id) + .unwrap(), + msg_sender() + .unwrap(), + ) .log(); } From 3263c4a0bce5113bf26fe6e1e4b765c93dc1a933 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:27:20 +0530 Subject: [PATCH 06/12] unwrap -> read --- examples/src6-vault/multi_asset_vault/src/main.sw | 4 ++-- examples/src6-vault/single_asset_vault/src/main.sw | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index 39662cf..b3231b3 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -282,7 +282,7 @@ pub fn _mint( storage .total_supply .get(asset_id) - .unwrap(), + .read(), msg_sender() .unwrap(), ) @@ -306,7 +306,7 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) { storage .total_supply .get(asset_id) - .unwrap(), + .read(), msg_sender() .unwrap(), ) diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 1a0dbd1..5bdaf79 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -297,7 +297,7 @@ pub fn _mint( storage .total_supply .get(asset_id) - .unwrap(), + .read(), msg_sender() .unwrap(), ) @@ -321,7 +321,7 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) { storage .total_supply .get(asset_id) - .unwrap(), + .read(), msg_sender() .unwrap(), ) From d9ad42697c4e73f5423dea6a4829c42cdf5dd7a9 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:29:18 +0530 Subject: [PATCH 07/12] remove get from sasbv --- examples/src6-vault/single_asset_single_sub_vault/src/main.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index 8abde9d..cb0ae00 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -211,7 +211,7 @@ impl SetSRC20Data for Contract { ) { // 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() { + if storage.total_supply.try_read().is_none() { revert(0); } let sender = msg_sender().unwrap(); From 1718d5a84af3feab793f0d78c69dbb339b7fc84d Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Tue, 10 Sep 2024 11:40:38 +0530 Subject: [PATCH 08/12] ujpdate changelog pr number --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75cdfdb..7dad81c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,8 +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. -- [#150](https://github.com/FuelLabs/sway-standards/pull/150) Fixes SRC-6 standard examples conform to the latest SRC-20 spec of logging values after updates. -- [#150](https://github.com/FuelLabs/sway-standards/pull/150) Formats code of SRC-6 examples, and fixes some comments. +- [#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] From 731e5a267fd29e2c29d1b78776fe528f33a00521 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 11 Sep 2024 17:51:18 +0530 Subject: [PATCH 09/12] resolve comments --- .../src6-vault/multi_asset_vault/src/main.sw | 43 ++++++++----- .../single_asset_single_sub_vault/src/main.sw | 63 ++++++++++++++----- .../src6-vault/single_asset_vault/src/main.sw | 44 ++++++++----- 3 files changed, 104 insertions(+), 46 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index b3231b3..85eaea4 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -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(); } } @@ -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(), ) @@ -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(), ) diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index cb0ae00..0ad7e68 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -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 { @@ -166,7 +173,10 @@ impl SRC20 for Contract { #[storage(read)] fn name(asset: AssetId) -> Option { if asset == vault_assetid() { - Some(String::from_ascii_str("Vault Shares")) + match storage.name.read_slice() { + Some(name) => Some(name), + None => None, + } } else { None } @@ -175,7 +185,10 @@ impl SRC20 for Contract { #[storage(read)] fn symbol(asset: AssetId) -> Option { if asset == vault_assetid() { - Some(String::from_ascii_str("VLTSHR")) + match storage.symbol.read_slice() { + Some(symbol) => Some(symbol), + None => None, + } } else { None } @@ -184,7 +197,7 @@ impl SRC20 for Contract { #[storage(read)] fn decimals(asset: AssetId) -> Option { if asset == vault_assetid() { - Some(9_u8) + Some(storage.decimals.read()) } else { None } @@ -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(); } } @@ -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(), ) @@ -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(), ) diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 5bdaf79..11f858b 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -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(); } } @@ -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(), ) @@ -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(), ) From 6948958706c60bb8d81fe815c570425e4dc1f353 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 11 Sep 2024 17:51:55 +0530 Subject: [PATCH 10/12] fmt --- examples/src6-vault/multi_asset_vault/src/main.sw | 14 ++------------ .../single_asset_single_sub_vault/src/main.sw | 15 ++------------- .../src6-vault/single_asset_vault/src/main.sw | 14 ++------------ 3 files changed, 6 insertions(+), 37 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index 85eaea4..38c2e68 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -297,12 +297,7 @@ pub fn _mint( 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, - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap()) .log(); } @@ -319,11 +314,6 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) { let new_supply = supply - amount; storage.total_supply.insert(asset_id, new_supply); burn(vault_sub_id, amount); - TotalSupplyEvent::new( - asset_id, - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap()) .log(); } diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index 0ad7e68..4d2639c 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -38,7 +38,6 @@ 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. @@ -287,12 +286,7 @@ pub fn _mint(recipient: Identity, amount: u64) { 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(), - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(vault_assetid(), new_supply, msg_sender().unwrap()) .log(); } @@ -309,11 +303,6 @@ pub fn _burn(asset_id: AssetId, amount: u64) { let new_supply = supply - amount; storage.total_supply.write(new_supply); burn(PRE_CALCULATED_SHARE_VAULT_SUB_ID, amount); - TotalSupplyEvent::new( - vault_assetid(), - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(vault_assetid(), new_supply, msg_sender().unwrap()) .log(); } diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 11f858b..5b68e0a 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -311,12 +311,7 @@ pub fn _mint( 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, - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap()) .log(); } @@ -333,11 +328,6 @@ pub fn _burn(asset_id: AssetId, vault_sub_id: SubId, amount: u64) { let new_supply = supply - amount; storage.total_supply.insert(asset_id, new_supply); burn(vault_sub_id, amount); - TotalSupplyEvent::new( - asset_id, - new_supply, - msg_sender() - .unwrap(), - ) + TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap()) .log(); } From 2ebb457fe17c9bc828cad83deeda90f2d720892b Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 11 Sep 2024 17:54:19 +0530 Subject: [PATCH 11/12] get(asset) for maps --- examples/src6-vault/multi_asset_vault/src/main.sw | 10 +++++----- examples/src6-vault/single_asset_vault/src/main.sw | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index 38c2e68..add5dca 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -210,27 +210,27 @@ impl SetSRC20Data for Contract { match name { Some(unwrapped_name) => { - storage.name.write_slice(unwrapped_name); + storage.name.get(asset).write_slice(unwrapped_name); SetNameEvent::new(asset, name, sender).log(); }, None => { - let _ = storage.name.clear(); + let _ = storage.name.get(asset).clear(); SetNameEvent::new(asset, name, sender).log(); } } match symbol { Some(unwrapped_symbol) => { - storage.symbol.write_slice(unwrapped_symbol); + storage.symbol.get(asset).write_slice(unwrapped_symbol); SetSymbolEvent::new(asset, symbol, sender).log(); }, None => { - let _ = storage.symbol.clear(); + let _ = storage.symbol.get(asset).clear(); SetSymbolEvent::new(asset, symbol, sender).log(); } } - storage.decimals.write(decimals); + storage.decimals.get(asset).write(decimals); SetDecimalsEvent::new(asset, decimals, sender).log(); } } diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 5b68e0a..9064f81 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -224,27 +224,27 @@ impl SetSRC20Data for Contract { match name { Some(unwrapped_name) => { - storage.name.write_slice(unwrapped_name); + storage.name.get(asset).write_slice(unwrapped_name); SetNameEvent::new(asset, name, sender).log(); }, None => { - let _ = storage.name.clear(); + let _ = storage.name.get(asset).clear(); SetNameEvent::new(asset, name, sender).log(); } } match symbol { Some(unwrapped_symbol) => { - storage.symbol.write_slice(unwrapped_symbol); + storage.symbol.get(asset).write_slice(unwrapped_symbol); SetSymbolEvent::new(asset, symbol, sender).log(); }, None => { - let _ = storage.symbol.clear(); + let _ = storage.symbol.get(asset).clear(); SetSymbolEvent::new(asset, symbol, sender).log(); } } - storage.decimals.write(decimals); + storage.decimals.get(asset).write(decimals); SetDecimalsEvent::new(asset, decimals, sender).log(); } } From 2a822dab510a4ab702eb370e9ebfd3b938a5a462 Mon Sep 17 00:00:00 2001 From: SwayStar123 Date: Wed, 11 Sep 2024 17:56:13 +0530 Subject: [PATCH 12/12] add write permission --- examples/src6-vault/multi_asset_vault/src/main.sw | 4 ++-- examples/src6-vault/single_asset_single_sub_vault/src/main.sw | 4 ++-- examples/src6-vault/single_asset_vault/src/main.sw | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/src6-vault/multi_asset_vault/src/main.sw b/examples/src6-vault/multi_asset_vault/src/main.sw index add5dca..1b249ba 100644 --- a/examples/src6-vault/multi_asset_vault/src/main.sw +++ b/examples/src6-vault/multi_asset_vault/src/main.sw @@ -184,7 +184,7 @@ impl SRC20 for Contract { } abi SetSRC20Data { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option, @@ -194,7 +194,7 @@ abi SetSRC20Data { } impl SetSRC20Data for Contract { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option, diff --git a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw index 4d2639c..b07c572 100644 --- a/examples/src6-vault/single_asset_single_sub_vault/src/main.sw +++ b/examples/src6-vault/single_asset_single_sub_vault/src/main.sw @@ -204,7 +204,7 @@ impl SRC20 for Contract { } abi SetSRC20Data { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option, @@ -214,7 +214,7 @@ abi SetSRC20Data { } impl SetSRC20Data for Contract { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option, diff --git a/examples/src6-vault/single_asset_vault/src/main.sw b/examples/src6-vault/single_asset_vault/src/main.sw index 9064f81..9ad6407 100644 --- a/examples/src6-vault/single_asset_vault/src/main.sw +++ b/examples/src6-vault/single_asset_vault/src/main.sw @@ -198,7 +198,7 @@ impl SRC20 for Contract { } abi SetSRC20Data { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option, @@ -208,7 +208,7 @@ abi SetSRC20Data { } impl SetSRC20Data for Contract { - #[storage(read)] + #[storage(read, write)] fn set_src20_data( asset: AssetId, name: Option,