Skip to content

Commit

Permalink
Bump repo to forc v0.35.3, fuels-rs v0.36.1, fuel-core v0.17.3, and r…
Browse files Browse the repository at this point in the history
…ust v1.67.0 (#100)

## Type of change

<!--Delete points that do not apply-->

- Improvement (refactoring, restructuring repository, cleaning tech
debt, ...)

## Changes

The following changes have been made:

- Updated libraries to forc v0.35.3
- Resolved warnings in the `UFP32` library
- Updated merkle proof to use new `From` and `.sha256()` additions to
`Bytes` type
- Updated README on String that it uses `Bytes` and not `Vec`
- Updated test projects to account for stdlib changes
- Updated tests to fuel-rs v0.36.1
- Updated CI to new versions
- Updated README to new versions

## Notes

- `String` now does not support `append()`. It will be readded when
FuelLabs/sway#4158 is resolved. Added comments
and documentation for this.
- Required for #95 

## Related Issues

<!--Delete everything after the "#" symbol and replace it with a number.
No spaces between hash and number-->

Closes #96

---------

Co-authored-by: bitzoic <[email protected]>
  • Loading branch information
bitzoic and bitzoic authored Feb 22, 2023
1 parent 55a1a3d commit d22dd19
Show file tree
Hide file tree
Showing 87 changed files with 780 additions and 730 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ concurrency:
env:
CARGO_TERM_COLOR: always
REGISTRY: ghcr.io
RUST_VERSION: 1.66.0
FORC_VERSION: 0.32.2
CORE_VERSION: 0.15.1
RUST_VERSION: 1.67.0
FORC_VERSION: 0.35.3
CORE_VERSION: 0.17.3
PATH_TO_SCRIPTS: .github/scripts

jobs:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<a href="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml" alt="CI">
<img src="https://github.com/FuelLabs/sway-libs/actions/workflows/ci.yml/badge.svg" />
</a>
<a href="https://crates.io/crates/forc/0.32.2" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.32.2-orange" />
<a href="https://crates.io/crates/forc/0.35.3" alt="forc">
<img src="https://img.shields.io/badge/forc-v0.35.3-orange" />
</a>
<a href="./LICENSE" alt="forc">
<img src="https://img.shields.io/github/license/FuelLabs/sway-libs" />
Expand Down Expand Up @@ -38,7 +38,7 @@ These libraries contain helper functions, generalized standards, and other tools
- [String](./libs/string/) is an interface to implement dynamic length strings that are UTF-8 encoded.
- [Signed Integers](./libs/signed_integers/) is an interface to implement signed integers.
- [Unsigned Fixed Point Number](./libs/fixed_point/) is an interface to implement fixed-point numbers.
- [StorageMapVec](./libs/storagemapvec/) is a temporary workaround for a StorageMap<K, StorageVec<V>> type
- [StorageMapVec](./libs/storagemapvec/) is a temporary workaround for a StorageMap<K, StorageVec<V>> type.

## Using a library

Expand Down Expand Up @@ -82,7 +82,7 @@ cargo test
Any instructions related to using a specific library should be found within the README.md of that library.

> **Note**
> All projects currently use `forc v0.32.2`, `fuels-rs v0.33.0` and `fuel-core 0.15.1`.
> All projects currently use `forc v0.35.3`, `fuels-rs v0.36.1` and `fuel-core 0.17.3`.
## Contributing

Expand Down
8 changes: 2 additions & 6 deletions libs/fixed_point/src/ufp128.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library ufp128;
// A wrapper around U128 type for a library for Sway for mathematical functions operating with signed 64.64-bit fixed point numbers.
use std::{math::{Exponent, Exponentiate, Root}, u128::U128, u256::U256};
use std::{math::{Exponent, Power, Root}, u128::U128, u256::U256};

pub struct UFP128 {
value: U128,
Expand Down Expand Up @@ -185,7 +185,7 @@ impl Root for UFP128 {
}
}

impl Exponentiate for UFP128 {
impl Power for UFP128 {
fn pow(self, exponent: Self) -> Self {
let nominator_pow = self.value.pow(exponent.value);
let u128_1 = U128::from((0, 1));
Expand All @@ -207,10 +207,6 @@ impl Exponentiate for UFP128 {
// }
// }
// }
trait Exponent {
fn exp(exponent: Self) -> Self;
}

impl Exponent for UFP128 {
fn exp(exponent: Self) -> Self {
let one = UFP128::from((1, 0));
Expand Down
8 changes: 4 additions & 4 deletions libs/fixed_point/src/ufp32.sw
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl core::ops::Multiply for UFP32 {
}

Self {
value: res_u64,
value: asm(ptr: res_u64) { ptr: u32 },
}
}
}
Expand All @@ -125,7 +125,7 @@ impl core::ops::Divide for UFP32 {
revert(0);
}
Self {
value: res_u64,
value: asm(ptr: res_u64) { ptr: u32 },
}
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ impl Exponent for UFP32 {
}
}

impl Exponentiate for UFP32 {
impl Power for UFP32 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
let demoninator_power = UFP32::denominator();
Expand All @@ -250,7 +250,7 @@ impl Exponentiate for UFP32 {
// which means that the denominator is always 2 ^ 16
// we need to delete the nominator by 2 ^ (16 * exponent - 1)
// - 1 is the formula is due to denominator need to stay 2 ^ 16
let nominator = nominator_pow >> demoninator_power * (exponent_int - 1);
let nominator = nominator_pow >> demoninator_power * (exponent_int - 1_u32);

if nominator > u32::max() {
// panic on overflow
Expand Down
4 changes: 2 additions & 2 deletions libs/fixed_point/src/ufp64.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library ufp64;
// A wrapper library around the u64 type for mathematical functions operating with signed 64-bit fixed point numbers.
use std::{math::{Exponent, Exponentiate, Root}, u128::U128};
use std::{math::{Exponent, Power, Root}, u128::U128};

pub struct UFP64 {
value: u64,
Expand Down Expand Up @@ -240,7 +240,7 @@ impl Exponent for UFP64 {
}
}

impl Exponentiate for UFP64 {
impl Power for UFP64 {
/// Power function. x ^ exponent
fn pow(self, exponent: Self) -> Self {
let demoninator_power = UFP64::denominator();
Expand Down
50 changes: 16 additions & 34 deletions libs/merkle_proof/src/binary_merkle_proof.sw
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ pub const NODE = 1u8;
///
/// * 'data' - The hash of the leaf data.
pub fn leaf_digest(data: b256) -> b256 {
// TODO: Update to use From<T> once https://github.com/FuelLabs/sway/issues/3810 is implemented
let mut bytes_u8 = Bytes::with_capacity(1);
let mut b256_as_bytes = Bytes::with_capacity(32);
bytes_u8.push(LEAF);
b256_as_bytes.len = 32;
__addr_of(data).copy_bytes_to(b256_as_bytes.buf.ptr, 32);

let bytes = bytes_u8.join(b256_as_bytes);

// TODO: Update to use bytes.sha256() when https://github.com/FuelLabs/sway/issues/3809 is implemented
let mut result_buffer: b256 = b256::min();
asm(hash: result_buffer, ptr: bytes.buf.ptr, bytes: bytes.len) {
s256 hash ptr bytes;
hash: b256
}
let mut bytes = Bytes::new();
let mut b256_as_bytes = Bytes::from(data);

bytes.push(LEAF);
bytes.append(b256_as_bytes);

bytes.sha256()
}

/// Returns the computed node hash of "MTH(D[n]) = SHA-256(0x01 || MTH(D[0:k]) || MTH(D[k:n]))".
Expand All @@ -44,25 +36,15 @@ pub fn leaf_digest(data: b256) -> b256 {
/// * 'left' - The hash of the left node.
/// * 'right' - The hash of the right node.
pub fn node_digest(left: b256, right: b256) -> b256 {
// TODO: Update to use From<T> once https://github.com/FuelLabs/sway/issues/3810 is implemented
let mut bytes_u8 = Bytes::with_capacity(1);
let mut left_as_bytes = Bytes::with_capacity(32);
let mut right_as_bytes = Bytes::with_capacity(32);
bytes_u8.push(NODE);
left_as_bytes.len = 32;
right_as_bytes.len = 32;
__addr_of(left).copy_bytes_to(left_as_bytes.buf.ptr, 32);
__addr_of(right).copy_bytes_to(right_as_bytes.buf.ptr, 32);

let left_right_as_bytes = left_as_bytes.join(right_as_bytes);
let bytes = bytes_u8.join(left_right_as_bytes);

// TODO: Update to use bytes.sha256() when https://github.com/FuelLabs/sway/issues/3809 is implemented
let mut result_buffer: b256 = b256::min();
asm(hash: result_buffer, ptr: bytes.buf.ptr, bytes: bytes.len) {
s256 hash ptr bytes;
hash: b256
}
let mut bytes = Bytes::new();
let mut left_as_bytes = Bytes::from(left);
let mut right_as_bytes = Bytes::from(right);

bytes.push(NODE);
bytes.append(left_as_bytes);
bytes.append(right_as_bytes);

bytes.sha256()
}

/// Calculates the length of the path to a leaf
Expand Down
4 changes: 2 additions & 2 deletions libs/nft/src/extensions/administrator/administrator.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abi Administrator {
/// Returns the administrator for the library.
#[storage(read)]
pub fn admin() -> Option<Identity> {
get::<Option<Identity>>(ADMIN)
get::<Option<Identity>>(ADMIN).unwrap_or(Option::None)
}

/// Changes the library's administrator.
Expand All @@ -33,7 +33,7 @@ pub fn admin() -> Option<Identity> {
/// * When the sender is not the `admin` in storage.
#[storage(read, write)]
pub fn set_admin(new_admin: Option<Identity>) {
let admin = get::<Option<Identity>>(ADMIN);
let admin = get::<Option<Identity>>(ADMIN).unwrap_or(Option::None);
require(admin.is_none() || (admin.is_some() && admin.unwrap() == msg_sender().unwrap()), AdminError::SenderNotAdmin);

store(ADMIN, new_admin);
Expand Down
6 changes: 3 additions & 3 deletions libs/nft/src/extensions/burnable/burnable.sw
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ impl Burnable for NFTCore {
fn burn(self) {
require(self.owner == msg_sender().unwrap(), AccessError::SenderNotOwner);

store(sha256((BALANCES, self.owner)), get::<u64>(sha256((BALANCES, self.owner))) - 1);
store(sha256((TOKENS, self.token_id)), Option::None::<NFTCore>());
store(sha256((BALANCES, self.owner)), get::<u64>(sha256((BALANCES, self.owner))).unwrap() - 1);
store(sha256((TOKENS, self.token_id)), Option::None::<NFTCore>);

log(BurnEvent {
owner: self.owner,
Expand All @@ -47,7 +47,7 @@ impl Burnable for NFTCore {
/// * When the `token_id` specified does not map to an existing token.
#[storage(read, write)]
pub fn burn(token_id: u64) {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);
require(nft.is_some(), InputError::TokenDoesNotExist);
nft.unwrap().burn();
}
4 changes: 2 additions & 2 deletions libs/nft/src/extensions/supply/supply.sw
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abi Supply {
/// Returns the maximum supply that has been set for the NFT library.
#[storage(read)]
pub fn max_supply() -> Option<u64> {
get::<Option<u64>>(MAX_SUPPLY)
get::<Option<u64>>(MAX_SUPPLY).unwrap_or(Option::None)
}

/// Sets the maximum supply for the NFT library.
Expand All @@ -32,7 +32,7 @@ pub fn max_supply() -> Option<u64> {
/// * When the supply has already been set
#[storage(read, write)]
pub fn set_max_supply(supply: Option<u64>) {
let current_supply = get::<Option<u64>>(MAX_SUPPLY);
let current_supply = get::<Option<u64>>(MAX_SUPPLY).unwrap_or(Option::None);
require(current_supply.is_none(), SupplyError::CannotReinitializeSupply);

store(MAX_SUPPLY, supply);
Expand Down
6 changes: 3 additions & 3 deletions libs/nft/src/extensions/token_metadata/token_metadata.sw
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait TokenMetadata<T> {
impl<T> TokenMetadata<T> for NFTCore {
#[storage(read)]
fn token_metadata(self) -> Option<T> {
get::<Option<T>>(sha256((TOKEN_METADATA, self.token_id)))
get::<Option<T>>(sha256((TOKEN_METADATA, self.token_id))).unwrap_or(Option::None)
}

#[storage(write)]
Expand All @@ -39,7 +39,7 @@ impl<T> TokenMetadata<T> for NFTCore {
/// * `token_id` - The id of the token which the metadata should be returned
#[storage(read)]
pub fn token_metadata<T>(token_id: u64) -> Option<T> {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);
match nft {
Option::Some(nft) => {
nft.token_metadata()
Expand All @@ -62,7 +62,7 @@ pub fn token_metadata<T>(token_id: u64) -> Option<T> {
/// * When the `token_id` does not map to an existing token
#[storage(read, write)]
pub fn set_token_metadata<T>(token_metadata: Option<T>, token_id: u64) {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);
require(nft.is_some(), InputError::TokenDoesNotExist);

nft.unwrap().set_token_metadata(token_metadata);
Expand Down
16 changes: 8 additions & 8 deletions libs/nft/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abi NFT {
/// * When `token_id` does not map to an existing token.
#[storage(read, write)]
pub fn approve(approved: Option<Identity>, token_id: u64) {
let mut nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let mut nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);
require(nft.is_some(), InputError::TokenDoesNotExist);
nft.unwrap().approve(approved);
}
Expand All @@ -63,7 +63,7 @@ pub fn approve(approved: Option<Identity>, token_id: u64) {
/// * `token_id` - The unique identifier of the token which the approved user should be returned.
#[storage(read)]
pub fn approved(token_id: u64) -> Option<Identity> {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);

match nft {
Option::Some(nft) => {
Expand All @@ -82,7 +82,7 @@ pub fn approved(token_id: u64) -> Option<Identity> {
/// * `owner` - The user of which the balance should be returned.
#[storage(read)]
pub fn balance_of(owner: Identity) -> u64 {
get::<u64>(sha256((BALANCES, owner)))
get::<u64>(sha256((BALANCES, owner))).unwrap_or(0)
}

/// Returns whether the `operator` user is approved to transfer all tokens on behalf of the `owner`.
Expand All @@ -93,7 +93,7 @@ pub fn balance_of(owner: Identity) -> u64 {
/// * `owner` - The user which may or may not have given approval to transfer all tokens.
#[storage(read)]
pub fn is_approved_for_all(operator: Identity, owner: Identity) -> bool {
get::<bool>(sha256((OPERATOR_APPROVAL, owner, operator)))
get::<bool>(sha256((OPERATOR_APPROVAL, owner, operator))).unwrap_or(false)
}

/// Mints `amount` number of tokens to the specified user.
Expand All @@ -104,7 +104,7 @@ pub fn is_approved_for_all(operator: Identity, owner: Identity) -> bool {
/// * `to` - The user which will own the minted tokens.
#[storage(read, write)]
pub fn mint(amount: u64, to: Identity) {
let tokens_minted = get::<u64>(TOKENS_MINTED);
let tokens_minted = get::<u64>(TOKENS_MINTED).unwrap_or(0);
let total_mint = tokens_minted + amount;

// Mint as many tokens as the sender has asked for
Expand All @@ -122,7 +122,7 @@ pub fn mint(amount: u64, to: Identity) {
/// * `token_id` - The unique identifier of the token.
#[storage(read)]
pub fn owner_of(token_id: u64) -> Option<Identity> {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);

match nft {
Option::Some(nft) => {
Expand Down Expand Up @@ -158,7 +158,7 @@ pub fn set_approval_for_all(approve: bool, operator: Identity) {
/// Returns the total number of tokens that have been minted.
#[storage(read)]
pub fn tokens_minted() -> u64 {
get::<u64>(TOKENS_MINTED)
get::<u64>(TOKENS_MINTED).unwrap_or(0)
}

/// Transfers ownership of the specified token to another user.
Expand All @@ -173,7 +173,7 @@ pub fn tokens_minted() -> u64 {
/// * When the `token_id` does not map to an existing token.
#[storage(read, write)]
pub fn transfer(to: Identity, token_id: u64) {
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id)));
let nft = get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None);
require(nft.is_some(), InputError::TokenDoesNotExist);
let _ = nft.unwrap().transfer(to);
}
14 changes: 7 additions & 7 deletions libs/nft/src/nft_core.sw
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl NFTCore {
/// * `operator` - The user which may or may not transfer all tokens on the owner`s behalf.
#[storage(read)]
pub fn is_approved_for_all(self, operator: Identity) -> bool {
get::<bool>(sha256((OPERATOR_APPROVAL, self.owner, operator)))
get::<bool>(sha256((OPERATOR_APPROVAL, self.owner, operator))).unwrap_or(false)
}

/// Mints a token to the `to` Identity with a id.
Expand All @@ -71,7 +71,7 @@ impl NFTCore {
/// * When the `token_id` is used by another token
#[storage(read, write)]
pub fn mint(to: Identity, token_id: u64) -> Self {
require(get::<Option<NFTCore>>(sha256((TOKENS, token_id))).is_none(), InputError::TokenAlreadyExists);
require(get::<Option<NFTCore>>(sha256((TOKENS, token_id))).unwrap_or(Option::None).is_none(), InputError::TokenAlreadyExists);

let nft = NFTCore {
approved: Option::None,
Expand All @@ -80,8 +80,8 @@ impl NFTCore {
};

store(sha256((TOKENS, token_id)), Option::Some(nft));
store(TOKENS_MINTED, get::<u64>(TOKENS_MINTED) + 1);
store(sha256((BALANCES, to)), get::<u64>(sha256((BALANCES, to))) + 1);
store(TOKENS_MINTED, get::<u64>(TOKENS_MINTED).unwrap_or(0) + 1);
store(sha256((BALANCES, to)), get::<u64>(sha256((BALANCES, to))).unwrap_or(0) + 1);

log(MintEvent {
owner: to,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl NFTCore {
let mut nft = self;
let from = nft.owner;
let sender = msg_sender().unwrap();
let operator_approved = get::<bool>(sha256((OPERATOR_APPROVAL, from, sender)));
let operator_approved = get::<bool>(sha256((OPERATOR_APPROVAL, from, sender))).unwrap_or(false);

// Ensure that the sender is either:
// 1. The owner of this token
Expand All @@ -160,8 +160,8 @@ impl NFTCore {

store(sha256((TOKENS, self.token_id)), Option::Some(nft));

let from_balance = get::<u64>(sha256((BALANCES, from)));
let to_balance = get::<u64>(sha256((BALANCES, to)));
let from_balance = get::<u64>(sha256((BALANCES, from))).unwrap_or(0);
let to_balance = get::<u64>(sha256((BALANCES, to))).unwrap_or(0);
store(sha256((BALANCES, from)), from_balance - 1);
store(sha256((BALANCES, to)), to_balance + 1);

Expand Down
Binary file removed libs/ownership/out/debug/ownership.bin
Binary file not shown.
Loading

0 comments on commit d22dd19

Please sign in to comment.