Skip to content

Commit

Permalink
Add helper functions to struct types and Eq implementations for all…
Browse files Browse the repository at this point in the history
… types (#149)

* Add helper functions to struct types and Eq implementations

* Add checks to enum types

* Update CHANGELOG
  • Loading branch information
bitzoic authored Sep 9, 2024
1 parent ab52d73 commit 29986f1
Show file tree
Hide file tree
Showing 16 changed files with 2,615 additions and 254 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ Description of the upcoming release here.

### Added v0.6.1

- None
- [#149](https://github.com/FuelLabs/sway-standards/pull/149) Adds struct field getters, `new()`, and `Eq` implementations to SRC-10's `DepositMessage` and `MetadataMessage` types and SRC-11's `SecurityInformation` type.
- [#149](https://github.com/FuelLabs/sway-standards/pull/149) Adds `Eq` implementation to SRC-5's `AccessError` error.
- [#149](https://github.com/FuelLabs/sway-standards/pull/149) Adds check functions and `Eq` implementation to SRC-5's `State` type and SRC-10's `DepositType` type.
- [#149](https://github.com/FuelLabs/sway-standards/pull/149) Adds struct field getters, `new()`, `log()`, and `Eq` implementations to SRC-6's `Deposit`, and `Withdraw` types, SRC-20's `SetNameEvent`, `SetSymbolEvent`, `SetDecimalsEvent`, and `TotalSupplyEvent` events, and SRC-7's `SetMetadataEvent` event.

### Changed v0.6.1

Expand Down
35 changes: 8 additions & 27 deletions examples/src20-native-asset/multi_asset/src/multi_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ abi SetSRC20Data {
fn set_src20_data(
asset: AssetId,
total_supply: u64,
name: String,
symbol: String,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
);
}
Expand All @@ -188,8 +188,8 @@ impl SetSRC20Data for Contract {
fn set_src20_data(
asset: AssetId,
supply: u64,
name: String,
symbol: String,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
) {
// NOTE: There are no checks for if the caller has permissions to update the metadata
Expand All @@ -199,28 +199,9 @@ impl SetSRC20Data for Contract {
}
let sender = msg_sender().unwrap();

log(SetNameEvent {
asset,
name: Some(name),
sender,
});

log(SetSymbolEvent {
asset,
symbol: Some(symbol),
sender,
});

log(SetDecimalsEvent {
asset,
decimals,
sender,
});

log(TotalSupplyEvent {
asset,
supply,
sender,
});
SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, decimals, sender).log();
TotalSupplyEvent::new(asset, supply, sender).log();
}
}
29 changes: 6 additions & 23 deletions examples/src20-native-asset/single_asset/src/single_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,12 @@ impl EmitSRC20Events for Contract {
// Metadata that is stored as a configurable should only be emitted once.
let asset = AssetId::default();
let sender = msg_sender().unwrap();
let name = Some(String::from_ascii_str(from_str_array(NAME)));
let symbol = Some(String::from_ascii_str(from_str_array(SYMBOL)));

log(SetNameEvent {
asset,
name: Some(String::from_ascii_str(from_str_array(NAME))),
sender,
});

log(SetSymbolEvent {
asset,
symbol: Some(String::from_ascii_str(from_str_array(SYMBOL))),
sender,
});

log(SetDecimalsEvent {
asset,
decimals: DECIMALS,
sender,
});

log(TotalSupplyEvent {
asset,
supply: TOTAL_SUPPLY,
sender,
});
SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, DECIMALS, sender).log();
TotalSupplyEvent::new(asset, TOTAL_SUPPLY, sender).log();
}
}
40 changes: 14 additions & 26 deletions examples/src3-mint-burn/multi_asset/src/multi_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,10 @@ impl SRC3 for Contract {
let new_supply = amount + asset_supply.unwrap_or(0);
storage.total_supply.insert(asset_id, new_supply);

log(TotalSupplyEvent {
asset: asset_id,
supply: new_supply,
sender: msg_sender().unwrap(),
});

mint_to(recipient, sub_id, amount);

TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap())
.log();
}

/// Unconditionally burns assets sent with the `sub_id` sub-identifier.
Expand Down Expand Up @@ -136,10 +133,13 @@ impl SRC3 for Contract {
require(msg_asset_id() == asset_id, "Incorrect asset provided");

// Decrement total supply of the asset and burn.
storage
.total_supply
.insert(asset_id, storage.total_supply.get(asset_id).read() - amount);
let new_supply = storage.total_supply.get(asset_id).read() - amount;
storage.total_supply.insert(asset_id, new_supply);

burn(sub_id, amount);

TotalSupplyEvent::new(asset_id, new_supply, msg_sender().unwrap())
.log();
}
}

Expand Down Expand Up @@ -194,23 +194,11 @@ impl SetSRC20Data for Contract {
revert(0);
}
let sender = msg_sender().unwrap();
let name = Some(String::from_ascii_str(from_str_array(NAME)));
let symbol = Some(String::from_ascii_str(from_str_array(SYMBOL)));

log(SetNameEvent {
asset,
name: Some(String::from_ascii_str(from_str_array(NAME))),
sender,
});

log(SetSymbolEvent {
asset,
symbol: Some(String::from_ascii_str(from_str_array(SYMBOL))),
sender,
});

log(SetDecimalsEvent {
asset,
decimals: DECIMALS,
sender,
});
SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, DECIMALS, sender).log();
}
}
40 changes: 14 additions & 26 deletions examples/src3-mint-burn/single_asset/src/single_asset.sw
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,10 @@ impl SRC3 for Contract {
let new_supply = amount + storage.total_supply.read();
storage.total_supply.write(new_supply);

log(TotalSupplyEvent {
asset: AssetId::new(ContractId::this(), DEFAULT_SUB_ID),
supply: new_supply,
sender: msg_sender().unwrap(),
});

mint_to(recipient, DEFAULT_SUB_ID, amount);

TotalSupplyEvent::new(AssetId::default(), new_supply, msg_sender().unwrap())
.log();
}

/// Unconditionally burns assets sent with the default SubId.
Expand Down Expand Up @@ -131,10 +128,13 @@ impl SRC3 for Contract {
);

// Decrement total supply of the asset and burn.
storage
.total_supply
.write(storage.total_supply.read() - amount);
let new_supply = storage.total_supply.read() - amount;
storage.total_supply.write(new_supply);

burn(DEFAULT_SUB_ID, amount);

TotalSupplyEvent::new(AssetId::default(), new_supply, msg_sender().unwrap())
.log();
}
}

Expand Down Expand Up @@ -191,23 +191,11 @@ impl EmitSRC20Events for Contract {
// Metadata that is stored as a configurable should only be emitted once.
let asset = AssetId::default();
let sender = msg_sender().unwrap();
let name = Some(String::from_ascii_str(from_str_array(NAME)));
let symbol = Some(String::from_ascii_str(from_str_array(SYMBOL)));

log(SetNameEvent {
asset,
name: Some(String::from_ascii_str(from_str_array(NAME))),
sender,
});

log(SetSymbolEvent {
asset,
symbol: Some(String::from_ascii_str(from_str_array(SYMBOL))),
sender,
});

log(SetDecimalsEvent {
asset,
decimals: DECIMALS,
sender,
});
SetNameEvent::new(asset, name, sender).log();
SetSymbolEvent::new(asset, symbol, sender).log();
SetDecimalsEvent::new(asset, DECIMALS, sender).log();
}
}
24 changes: 14 additions & 10 deletions examples/src6-vault/multi_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ impl SRC6 for Contract {
vault_info.managed_assets = vault_info.managed_assets + asset_amount;
storage.vault_info.insert(share_asset, vault_info);

log(Deposit {
caller: msg_sender().unwrap(),
Deposit::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id,
deposited_amount: asset_amount,
minted_shares: shares,
});
asset_amount,
shares,
)
.log();

shares
}
Expand Down Expand Up @@ -96,14 +98,16 @@ impl SRC6 for Contract {

transfer(receiver, underlying_asset, assets);

log(Withdraw {
caller: msg_sender().unwrap(),
Withdraw::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id,
withdrawn_amount: assets,
burned_shares: shares,
});
assets,
shares,
)
.log();

assets
}
Expand Down
24 changes: 14 additions & 10 deletions examples/src6-vault/single_asset_single_sub_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ impl SRC6 for Contract {
.managed_assets
.write(storage.managed_assets.read() + asset_amount);

log(Deposit {
caller: msg_sender().unwrap(),
Deposit::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id,
deposited_amount: asset_amount,
minted_shares: shares,
});
asset_amount,
shares,
)
.log();

shares
}
Expand Down Expand Up @@ -84,14 +86,16 @@ impl SRC6 for Contract {

transfer(receiver, underlying_asset, assets);

log(Withdraw {
caller: msg_sender().unwrap(),
Withdraw::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id,
withdrawn_amount: assets,
burned_shares: shares,
});
assets,
shares,
)
.log();

assets
}
Expand Down
32 changes: 18 additions & 14 deletions examples/src6-vault/single_asset_vault/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ impl SRC6 for Contract {
vault_info.managed_assets = vault_info.managed_assets + asset_amount;
storage.vault_info.insert(share_asset, vault_info);

log(Deposit {
caller: msg_sender().unwrap(),
receiver: receiver,
Deposit::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id: vault_sub_id,
deposited_amount: asset_amount,
minted_shares: shares,
});
vault_sub_id,
asset_amount,
shares,
)
.log();

shares
}
Expand Down Expand Up @@ -97,14 +99,16 @@ impl SRC6 for Contract {

transfer(receiver, underlying_asset, assets);

log(Withdraw {
caller: msg_sender().unwrap(),
receiver: receiver,
Withdraw::new(
msg_sender()
.unwrap(),
receiver,
underlying_asset,
vault_sub_id: vault_sub_id,
withdrawn_amount: assets,
burned_shares: shares,
});
vault_sub_id,
assets,
shares,
)
.log();

assets
}
Expand Down
Loading

0 comments on commit 29986f1

Please sign in to comment.