diff --git a/sway-lib-std/src/address.sw b/sway-lib-std/src/address.sw index 29e6e0f5137..fe454a0c2cf 100644 --- a/sway-lib-std/src/address.sw +++ b/sway-lib-std/src/address.sw @@ -1,15 +1,8 @@ //! A wrapper around the `b256` type to help enhance type-safety. library; -use ::alias::SubId; -use ::call_frames::contract_id; -use ::contract_id::AssetId; use ::convert::From; -use ::hash::*; -use ::error_signals::FAILED_TRANSFER_TO_ADDRESS_SIGNAL; -use ::hash::sha256; -use ::revert::revert; -use ::outputs::{Output, output_amount, output_count, output_type}; +use ::hash::{Hash, Hasher}; /// The `Address` type, a struct wrapper around the inner `b256` value. pub struct Address { @@ -70,82 +63,6 @@ impl From for Address { } } -impl Address { - /// Transfer `amount` coins of type `asset_id` and send them to - /// the Address. - /// - /// # Arguments - /// - /// * `asset_id`: [AssetId] - The `AssetId` of the token to transfer. - /// * `amount`: [u64] - The amount of tokens to transfer. - /// - /// # Reverts - /// - /// * When `amount` is greater than the contract balance for `asset_id`. - /// * When `amount` is equal to zero. - /// * When there are no free variable outputs. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::{BASE_ASSET_ID, ZERO_B256}; - /// - /// fn foo() { - /// let address = Address::from(ZERO_B256); - /// address.transfer(BASE_ASSET_ID, 500); - /// } - /// ``` - pub fn transfer(self, asset_id: AssetId, amount: u64) { - // maintain a manual index as we only have `while` loops in sway atm: - let mut index = 0; - - // If an output of type `OutputVariable` is found, check if its `amount` is - // zero. As one cannot transfer zero coins to an output without a panic, a - // variable output with a value of zero is by definition unused. - let number_of_outputs = output_count(); - while index < number_of_outputs { - if let Output::Variable = output_type(index) { - if output_amount(index) == 0 { - asm(r1: self.value, r2: index, r3: amount, r4: asset_id.value) { - tro r1 r2 r3 r4; - }; - return; - } - } - index += 1; - } - - revert(FAILED_TRANSFER_TO_ADDRESS_SIGNAL); - } -} - -impl Address { - /// Mint `amount` coins of the current contract's `asset_id` and send them to - /// the Address. - /// - /// # Arguments - /// - /// * `sub_id`: [SubId] - The sub id of the token to mint. - /// * `amount`: [u64] - The amount of tokens to mint. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::ZERO_B256; - /// - /// fn foo() { - /// let address = Address::from(ZERO_B256); - /// address.mint_to(ZERO_B256, 500); - /// } - /// ``` - pub fn mint_to(self, sub_id: SubId, amount: u64) { - asm(r1: amount, r2: sub_id) { - mint r1 r2; - }; - self.transfer(AssetId::new(contract_id(), sub_id), amount); - } -} - impl Hash for Address { fn hash(self, ref mut state: Hasher) { let Address { value } = self; diff --git a/sway-lib-std/src/asset_id.sw b/sway-lib-std/src/asset_id.sw new file mode 100644 index 00000000000..ce6bd231338 --- /dev/null +++ b/sway-lib-std/src/asset_id.sw @@ -0,0 +1,216 @@ +library; + +use ::alias::SubId; +use ::contract_id::ContractId; +use ::convert::From; +use ::hash::{Hash, Hasher}; + +/// An AssetId is used for interacting with an asset on the network. +/// +/// # Additional Information +/// +/// It is calculated by taking the sha256 hash of the originating ContractId and a SubId. +/// i.e. sha256((contract_id, sub_id)). +/// +/// An exception is the Base Asset, which is just the ZERO_B256 AssetId. +/// +/// The SubId is used to differentiate between different assets that are created by the same contract. +pub struct AssetId { + value: b256, +} + +impl Hash for AssetId { + fn hash(self, ref mut state: Hasher) { + let Self { value } = self; + value.hash(state); + } +} + +impl core::ops::Eq for AssetId { + fn eq(self, other: Self) -> bool { + self.value == other.value + } +} + +impl From for AssetId { + /// Casts raw `b256` data to an `AssetId`. + /// + /// # Arguments + /// + /// * `bits`: [b256] - The raw `b256` data to be casted. + /// + /// # Returns + /// + /// * [AssetId] - The newly created `AssetId` from the raw `b256`. + /// + /// # Examples + /// + /// ```sway + /// use std::constants::ZERO_B256; + /// + /// fn foo() { + /// let asset_id = AssetId::from(ZERO_B256); + /// } + /// ``` + fn from(bits: b256) -> Self { + Self { value: bits } + } + + /// Casts an `AssetId` to raw `b256` data. + /// + /// # Returns + /// + /// * [b256] - The underlying raw `b256` data of the `AssetId`. + /// + /// # Examples + /// + /// ```sway + /// use std::constants::ZERO_B256; + /// + /// fn foo() { + /// let asset_id = AssetId::from(ZERO_B256); + /// let b256_data = asset_id.into(); + /// assert(b256_data == ZERO_B256); + /// } + /// ``` + fn into(self) -> b256 { + self.value + } +} + +impl AssetId { + /// Creates a new AssetId from a ContractId and SubId. + /// + /// # Arguments + /// + /// * `contract_id`: [ContractId] - The ContractId of the contract that created the asset. + /// * `sub_id`: [SubId] - The SubId of the asset. + /// + /// # Returns + /// + /// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and SubId. + /// + /// # Examples + /// + /// ```sway + /// use std::{callframes::contract_id, constants::ZERO_B256}; + /// + /// fn foo() { + /// let contract_id = contract_id(); + /// let sub_id = ZERO_B256; + /// + /// let asset_id = AssetId::new(contract_id, sub_id); + /// } + /// ``` + pub fn new(contract_id: ContractId, sub_id: SubId) -> Self { + let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm( + asset_id: result_buffer, + ptr: (contract_id, sub_id), + bytes: 64, + ) { + s256 asset_id ptr bytes; + }; + + Self { + value: result_buffer, + } + } + + /// Creates a new AssetId with the default SubId for the current contract. + /// + /// # Returns + /// + /// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and the default SubId. + /// + /// # Examples + /// + /// ```sway + /// use std::{callframes::contract_id, constants::DEFAULT_SUB_ID}; + /// + /// fn foo() { + /// let asset_id = AssetId::default(); + /// assert(asset_id == AssetId::new(contract_id(), DEFAULT_SUB_ID)); + /// } + /// ``` + pub fn default() -> Self { + let contract_id = asm() { + fp: b256 + }; + let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; + asm( + asset_id: result_buffer, + ptr: ( + contract_id, + 0x0000000000000000000000000000000000000000000000000000000000000000, + ), + bytes: 64, + ) { + s256 asset_id ptr bytes; + }; + + Self { + value: result_buffer, + } + } + + /// The base_asset_id represents the base asset of a chain. + /// + /// # Additional Information + /// + /// On the Fuel network, the base asset is Ether. It is hardcoded as the 0x00..00 AssetId. + /// + /// # Returns + /// + /// * [AssetId] - The AssetId of the base asset. + /// + /// # Examples + /// + /// ```sway + /// use std::{constants::ZERO_B256, token::transfer}; + /// + /// fn foo() { + /// let asset_id = AssetId::base_asset_id(); + /// let amount = 100; + /// let recipient = Identity::ContractId(ContractId::from(ZERO_B256)); + /// + /// transfer(recipient, asset_id, amount); + /// ``` + pub fn base_asset_id() -> Self { + Self { + value: 0x0000000000000000000000000000000000000000000000000000000000000000, + } + } +} + +#[test()] +fn test_hasher_sha256_asset_id() { + use ::assert::assert; + let mut hasher = Hasher::new(); + AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000) + .hash(hasher); + let s256 = hasher.sha256(); + assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001) + .hash(hasher); + let s256 = hasher.sha256(); + assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); +} + +#[test()] +fn test_hasher_sha256_contract_id() { + use ::assert::assert; + let mut hasher = Hasher::new(); + ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000) + .hash(hasher); + let s256 = hasher.sha256(); + assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); + + let mut hasher = Hasher::new(); + ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001) + .hash(hasher); + let s256 = hasher.sha256(); + assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); +} diff --git a/sway-lib-std/src/call_frames.sw b/sway-lib-std/src/call_frames.sw index 1ffdf989709..38d861ca6ed 100644 --- a/sway-lib-std/src/call_frames.sw +++ b/sway-lib-std/src/call_frames.sw @@ -2,7 +2,8 @@ //! [Call frames](https://fuellabs.github.io/fuel-specs/master/vm#call-frames) store metadata across untrusted inter-contract calls. library; -use ::contract_id::{AssetId, ContractId}; +use ::asset_id::AssetId; +use ::contract_id::ContractId; use ::intrinsics::is_reference_type; use ::registers::frame_ptr; diff --git a/sway-lib-std/src/constants.sw b/sway-lib-std/src/constants.sw index 938b83b6a8e..628444afc92 100644 --- a/sway-lib-std/src/constants.sw +++ b/sway-lib-std/src/constants.sw @@ -1,7 +1,7 @@ //! Base asset and zero address constants. library; -use ::contract_id::AssetId; +use ::asset_id::AssetId; /// The `BASE_ASSET_ID` represents the base asset of a chain. /// diff --git a/sway-lib-std/src/context.sw b/sway-lib-std/src/context.sw index 777574e5c6c..192c3d6ef71 100644 --- a/sway-lib-std/src/context.sw +++ b/sway-lib-std/src/context.sw @@ -1,8 +1,9 @@ //! Functionality for accessing context-specific information about the current contract or message. library; +use ::asset_id::AssetId; use ::call_frames::contract_id; -use ::contract_id::{AssetId, ContractId}; +use ::contract_id::ContractId; use ::registers::balance; /// Get the balance of coin `asset_id` for the current contract. diff --git a/sway-lib-std/src/contract_id.sw b/sway-lib-std/src/contract_id.sw index efd9a4c15bb..1e51f3fee4f 100644 --- a/sway-lib-std/src/contract_id.sw +++ b/sway-lib-std/src/contract_id.sw @@ -1,9 +1,8 @@ //! A wrapper around the `b256` type to help enhance type-safety. library; -use ::alias::SubId; use ::convert::From; -use ::hash::*; +use ::hash::{Hash, Hasher}; /// The `ContractId` type, a struct wrapper around the inner `b256` value. pub struct ContractId { @@ -71,184 +70,6 @@ impl Hash for ContractId { } } -/// An AssetId is used for interacting with an asset on the network. -/// -/// # Additional Information -/// -/// It is calculated by taking the sha256 hash of the originating ContractId and a SubId. -/// i.e. sha256((contract_id, sub_id)). -/// -/// An exception is the Base Asset, which is just the ZERO_B256 AssetId. -/// -/// The SubId is used to differentiate between different assets that are created by the same contract. -pub struct AssetId { - value: b256, -} - -impl Hash for AssetId { - fn hash(self, ref mut state: Hasher) { - let Self { value } = self; - value.hash(state); - } -} - -impl core::ops::Eq for AssetId { - fn eq(self, other: Self) -> bool { - self.value == other.value - } -} - -impl From for AssetId { - /// Casts raw `b256` data to an `AssetId`. - /// - /// # Arguments - /// - /// * `bits`: [b256] - The raw `b256` data to be casted. - /// - /// # Returns - /// - /// * [AssetId] - The newly created `AssetId` from the raw `b256`. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::ZERO_B256; - /// - /// fn foo() { - /// let asset_id = AssetId::from(ZERO_B256); - /// } - /// ``` - fn from(bits: b256) -> Self { - Self { value: bits } - } - - /// Casts an `AssetId` to raw `b256` data. - /// - /// # Returns - /// - /// * [b256] - The underlying raw `b256` data of the `AssetId`. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::ZERO_B256; - /// - /// fn foo() { - /// let asset_id = AssetId::from(ZERO_B256); - /// let b256_data = asset_id.into(); - /// assert(b256_data == ZERO_B256); - /// } - /// ``` - fn into(self) -> b256 { - self.value - } -} - -impl AssetId { - /// Creates a new AssetId from a ContractId and SubId. - /// - /// # Arguments - /// - /// * `contract_id`: [ContractId] - The ContractId of the contract that created the asset. - /// * `sub_id`: [SubId] - The SubId of the asset. - /// - /// # Returns - /// - /// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and SubId. - /// - /// # Examples - /// - /// ```sway - /// use std::{callframes::contract_id, constants::ZERO_B256}; - /// - /// fn foo() { - /// let contract_id = contract_id(); - /// let sub_id = ZERO_B256; - /// - /// let asset_id = AssetId::new(contract_id, sub_id); - /// } - /// ``` - pub fn new(contract_id: ContractId, sub_id: SubId) -> Self { - let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; - asm( - asset_id: result_buffer, - ptr: (contract_id, sub_id), - bytes: 64, - ) { - s256 asset_id ptr bytes; - }; - - Self { - value: result_buffer, - } - } - - /// Creates a new AssetId with the default SubId for the current contract. - /// - /// # Returns - /// - /// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and the default SubId. - /// - /// # Examples - /// - /// ```sway - /// use std::{callframes::contract_id, constants::DEFAULT_SUB_ID}; - /// - /// fn foo() { - /// let asset_id = AssetId::default(); - /// assert(asset_id == AssetId::new(contract_id(), DEFAULT_SUB_ID)); - /// } - /// ``` - pub fn default() -> Self { - let contract_id = asm() { - fp: b256 - }; - let result_buffer = 0x0000000000000000000000000000000000000000000000000000000000000000; - asm( - asset_id: result_buffer, - ptr: ( - contract_id, - 0x0000000000000000000000000000000000000000000000000000000000000000, - ), - bytes: 64, - ) { - s256 asset_id ptr bytes; - }; - - Self { - value: result_buffer, - } - } - - /// The base_asset_id represents the base asset of a chain. - /// - /// # Additional Information - /// - /// On the Fuel network, the base asset is Ether. It is hardcoded as the 0x00..00 AssetId. - /// - /// # Returns - /// - /// * [AssetId] - The AssetId of the base asset. - /// - /// # Examples - /// - /// ```sway - /// use std::{constants::ZERO_B256, token::transfer}; - /// - /// fn foo() { - /// let asset_id = AssetId::base_asset_id(); - /// let amount = 100; - /// let recipient = Identity::ContractId(ContractId::from(ZERO_B256)); - /// - /// transfer(recipient, asset_id, amount); - /// ``` - pub fn base_asset_id() -> Self { - Self { - value: 0x0000000000000000000000000000000000000000000000000000000000000000, - } - } -} - impl ContractId { /// Returns the ContractId of the currently executing contract. /// @@ -279,112 +100,4 @@ impl ContractId { fp: b256 }) } - /// UNCONDITIONAL transfer of `amount` coins of type `asset_id` to - /// the ContractId. - /// - /// # Additional Information - /// - /// **_WARNING:_** - /// This will transfer coins to a contract even with no way to retrieve them - /// (i.e. no withdrawal functionality on receiving contract), possibly leading - /// to the **_PERMANENT LOSS OF COINS_** if not used with care. - /// - /// # Arguments - /// - /// * `asset_id`: [AssetId] - The `AssetId` of the token to transfer. - /// * `amount`: [u64] - The amount of tokens to transfer. - /// - /// # Reverts - /// - /// * When `amount` is greater than the contract balance for `asset_id`. - /// * When `amount` is equal to zero. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::{BASE_ASSET_ID, ZERO_B256}; - /// - /// fn foo() { - /// let contract_id = ContractId::from(ZERO_B256); - /// contract_id.transfer(BASE_ASSET_ID, 500); - /// } - /// ``` - pub fn transfer(self, asset_id: AssetId, amount: u64) { - asm(r1: amount, r2: asset_id.value, r3: self.value) { - tr r3 r1 r2; - } - } -} - -impl ContractId { - /// Mint `amount` coins of `sub_id` and send them UNCONDITIONALLY to the contract at `to`. - /// - /// # Additional Information - /// - /// **_WARNING:_** - /// This will transfer coins to a contract even with no way to retrieve them - /// (i.e: no withdrawal functionality on the receiving contract), possibly leading to - /// the **_PERMANENT LOSS OF COINS_** if not used with care. - /// - /// # Arguments - /// - /// * `sub_id`: [SubId] - The sub identifier of the asset which to mint. - /// * `amount`: [u64] - The amount of tokens to mint. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::ZERO_B256; - /// - /// fn foo() { - /// let contract_id = ContractId::from(ZERO_B256); - /// contract_id.mint_to(ZERO_B256, 500); - /// } - /// ``` - pub fn mint_to(self, sub_id: SubId, amount: u64) { - asm(r1: amount, r2: sub_id) { - mint r1 r2; - }; - self.transfer( - AssetId::new( - ContractId::from(asm() { - fp: b256 - }), - sub_id, - ), - amount, - ); - } -} - -#[test()] -fn test_hasher_sha256_asset_id() { - use ::assert::assert; - let mut hasher = Hasher::new(); - AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000000) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - AssetId::from(0x0000000000000000000000000000000000000000000000000000000000000001) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); -} - -#[test()] -fn test_hasher_sha256_contract_id() { - use ::assert::assert; - let mut hasher = Hasher::new(); - ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000000) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0x66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925); - - let mut hasher = Hasher::new(); - ContractId::from(0x0000000000000000000000000000000000000000000000000000000000000001) - .hash(hasher); - let s256 = hasher.sha256(); - assert(s256 == 0xec4916dd28fc4c10d78e287ca5d9cc51ee1ae73cbfde08c6b37324cbfaac8bc5); } diff --git a/sway-lib-std/src/identity.sw b/sway-lib-std/src/identity.sw index 69614c1c97d..ee8bc13fa96 100644 --- a/sway-lib-std/src/identity.sw +++ b/sway-lib-std/src/identity.sw @@ -5,10 +5,11 @@ library; use ::assert::assert; use ::address::Address; use ::alias::SubId; +use ::asset_id::AssetId; use ::call_frames::contract_id; use ::constants::{BASE_ASSET_ID, ZERO_B256}; -use ::contract_id::{AssetId, ContractId}; -use ::hash::*; +use ::contract_id::ContractId; +use ::hash::{Hash, Hasher}; use ::option::Option::{self, *}; /// The `Identity` type: either an `Address` or a `ContractId`. @@ -123,80 +124,20 @@ impl Identity { Self::ContractId(_) => true, } } - - /// Transfer `amount` coins of the type `asset_id` and send them - /// to the Identity. - /// - /// # Additional Information - /// - /// **_WARNING:_** - /// If the Identity is a contract this may transfer coins to the contract even with no way to retrieve them - /// (i.e. no withdrawal functionality on receiving contract), possibly leading - /// to the **_PERMANENT LOSS OF COINS_** if not used with care. - /// - /// # Arguments - /// - /// * `asset_id`: [AssetId] - The `AssetId` of the token to transfer. - /// * `amount`: [u64] - The amount of tokens to transfer. - /// - /// # Reverts - /// - /// * When `amount` is greater than the contract balance for `asset_id`. - /// * When `amount` is equal to zero. - /// * When there are no free variable outputs when transferring to an `Address`. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::{BASE_ASSET_ID, ZERO_B256}; - /// - /// fn foo() { - /// let to_address = Identity::Address(Address::from(ZERO_B256)); - /// let to_contract_id = Identity::ContractId(ContractId::from(ZERO_B256)); - /// to_address.transfer(BASE_ASSET_ID, 500); - /// to_contract_id.transfer(BASE_ASSET_ID, 500); - /// } - /// ``` - pub fn transfer(self, asset_id: AssetId, amount: u64) { - match self { - Identity::Address(addr) => addr.transfer(asset_id, amount), - Identity::ContractId(id) => id.transfer(asset_id, amount), - }; - } } -impl Identity { - /// Mint `amount` coins of `sub_id` and transfer them to the Identity. - /// - /// # Additional Information - /// - /// **_WARNING:_** - /// If the Identity is a contract, this will transfer coins to the contract even with no way to retrieve them - /// (i.e: no withdrawal functionality on the receiving contract), possibly leading to - /// the **_PERMANENT LOSS OF COINS_** if not used with care. - /// - /// # Arguments - /// - /// * `sub_id`: [SubId] - The sub identifier of the asset which to mint. - /// * `amount`: [u64] - The amount of tokens to mint. - /// - /// # Examples - /// - /// ```sway - /// use std::constants::ZERO_B256; - /// - /// fn foo() { - /// let address_identity = Identity::Address(Address::from(ZERO_B256)); - /// let contract_identity = Identity::ContractId(ContractId::from(ZERO_B256)); - /// address_identity.mint_to(ZERO_B256, 500); - /// contract_identity.mint_to(ZERO_B256, 500); - /// } - /// ``` - pub fn mint_to(self, sub_id: SubId, amount: u64) { - asm(r1: amount, r2: sub_id) { - mint r1 r2; - }; - self.transfer(AssetId::new(contract_id(), sub_id), amount); +impl Hash for Identity { + fn hash(self, ref mut state: Hasher) { + match self { + Identity::Address(address) => { + 0_u8.hash(state); + address.hash(state); + }, + Identity::ContractId(id) => { + 1_u8.hash(state); + id.hash(state); + }, + } } } @@ -219,18 +160,3 @@ fn test_contract_id() { assert(identity.as_contract_id().unwrap().value == id); assert(identity.as_address().is_none()); } - -impl Hash for Identity { - fn hash(self, ref mut state: Hasher) { - match self { - Identity::Address(address) => { - 0_u8.hash(state); - address.hash(state); - }, - Identity::ContractId(id) => { - 1_u8.hash(state); - id.hash(state); - }, - } - } -} diff --git a/sway-lib-std/src/inputs.sw b/sway-lib-std/src/inputs.sw index 7f17b1dfa7c..5358e301c23 100644 --- a/sway-lib-std/src/inputs.sw +++ b/sway-lib-std/src/inputs.sw @@ -4,9 +4,10 @@ library; use ::address::Address; use ::assert::assert; +use ::asset_id::AssetId; use ::bytes::Bytes; use ::constants::BASE_ASSET_ID; -use ::contract_id::{AssetId, ContractId}; +use ::contract_id::ContractId; use ::option::Option::{self, *}; use ::revert::revert; use ::tx::{ diff --git a/sway-lib-std/src/lib.sw b/sway-lib-std/src/lib.sw index 6b8529f2187..2fbd0168c37 100644 --- a/sway-lib-std/src/lib.sw +++ b/sway-lib-std/src/lib.sw @@ -19,6 +19,7 @@ pub mod u128; pub mod u256; pub mod alias; pub mod hash; +pub mod asset_id; pub mod contract_id; pub mod constants; pub mod call_frames; diff --git a/sway-lib-std/src/low_level_call.sw b/sway-lib-std/src/low_level_call.sw index 22b62384bac..ca4fe2d2a5a 100644 --- a/sway-lib-std/src/low_level_call.sw +++ b/sway-lib-std/src/low_level_call.sw @@ -2,8 +2,9 @@ library; use ::assert::assert; +use ::asset_id::AssetId; use ::bytes::Bytes; -use ::contract_id::{AssetId, ContractId}; +use ::contract_id::ContractId; use ::option::Option; use ::revert::require; use ::vec::Vec; diff --git a/sway-lib-std/src/outputs.sw b/sway-lib-std/src/outputs.sw index 77cf023b669..262400a6312 100644 --- a/sway-lib-std/src/outputs.sw +++ b/sway-lib-std/src/outputs.sw @@ -2,7 +2,8 @@ //! This includes `Output::Coins`, `Input::Messages` and `Input::Contracts`. library; -use ::contract_id::{AssetId, ContractId}; +use ::asset_id::AssetId; +use ::contract_id::ContractId; use ::revert::revert; use ::tx::{ GTF_CREATE_OUTPUT_AT_INDEX, diff --git a/sway-lib-std/src/prelude.sw b/sway-lib-std/src/prelude.sw index 3cd36ad0fe1..ee79680264d 100644 --- a/sway-lib-std/src/prelude.sw +++ b/sway-lib-std/src/prelude.sw @@ -6,7 +6,8 @@ library; // Blockchain types use ::address::Address; use ::alias::SubId; -use ::contract_id::{AssetId, ContractId}; +use ::asset_id::AssetId; +use ::contract_id::ContractId; use ::identity::Identity; // `StorageKey` API diff --git a/sway-lib-std/src/token.sw b/sway-lib-std/src/token.sw index ddbe04dd008..f61c310ee67 100644 --- a/sway-lib-std/src/token.sw +++ b/sway-lib-std/src/token.sw @@ -3,8 +3,9 @@ library; use ::address::Address; use ::alias::SubId; +use ::asset_id::AssetId; use ::call_frames::contract_id; -use ::contract_id::{AssetId, ContractId}; +use ::contract_id::ContractId; use ::error_signals::FAILED_TRANSFER_TO_ADDRESS_SIGNAL; use ::identity::Identity; use ::revert::revert; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/context_testing_contract/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/context_testing_contract/json_abi_oracle.json index a9ac1fcfefd..b7ddefcc8ca 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/context_testing_contract/json_abi_oracle.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/context_testing_contract/json_abi_oracle.json @@ -106,7 +106,7 @@ "typeArguments": null } ], - "type": "struct std::contract_id::AssetId", + "type": "struct std::asset_id::AssetId", "typeId": 1, "typeParameters": null }, diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/test_fuel_coin_contract/json_abi_oracle.json b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/test_fuel_coin_contract/json_abi_oracle.json index f3183c61135..0248c09a08d 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/test_fuel_coin_contract/json_abi_oracle.json +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/test_fuel_coin_contract/json_abi_oracle.json @@ -83,7 +83,7 @@ "typeArguments": null } ], - "type": "struct std::contract_id::AssetId", + "type": "struct std::asset_id::AssetId", "typeId": 2, "typeParameters": null }, diff --git a/test/src/sdk-harness/test_projects/token_ops/mod.rs b/test/src/sdk-harness/test_projects/token_ops/mod.rs index ca1df572357..478513d4332 100644 --- a/test/src/sdk-harness/test_projects/token_ops/mod.rs +++ b/test/src/sdk-harness/test_projects/token_ops/mod.rs @@ -535,354 +535,6 @@ async fn get_balance_contract_id(wallet: WalletUnlocked) -> ContractId { balance_id.into() } -#[tokio::test] -async fn test_address_mint_to() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = wallet.address(); - - fuelcontract_instance - .methods() - .address_mint_to(to, amount, Bits256(*sub_id)) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance = wallet - .get_asset_balance(&AssetId::from(*asset_id)) - .await - .unwrap(); - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_address_transfer_new_mint() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = wallet.address(); - - fuelcontract_instance - .methods() - .mint_coins(amount, Bits256(*sub_id)) - .call() - .await - .unwrap(); - - fuelcontract_instance - .methods() - .address_transfer(to, Bits256(*asset_id), amount) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance = wallet - .get_asset_balance(&AssetId::from(*asset_id)) - .await - .unwrap(); - - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_address_transfer_base_asset() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, _) = get_fuelcoin_instance(wallet.clone()).await; - - let amount = 44u64; - let to = wallet.address(); - - let balance_prev = wallet.get_asset_balance(&AssetId::BASE).await.unwrap(); - - fuelcontract_instance - .methods() - .address_transfer(to, Bits256([0u8; 32]), amount) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance_new = wallet.get_asset_balance(&AssetId::BASE).await.unwrap(); - - assert_eq!(balance_new, balance_prev + amount); -} - -#[tokio::test] -async fn test_contract_mint_to() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = get_balance_contract_id(wallet.clone()).await; - - fuelcontract_instance - .methods() - .contract_mint_to(to, amount, Bits256(*sub_id)) - .append_contract(to.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256(*asset_id), to) - .append_contract(to.into()) - .call() - .await - .unwrap() - .value; - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_contract_transfer_new_mint() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = get_balance_contract_id(wallet.clone()).await; - - fuelcontract_instance - .methods() - .mint_coins(amount, Bits256(*sub_id)) - .call() - .await - .unwrap(); - - fuelcontract_instance - .methods() - .contract_transfer(to, Bits256(*asset_id), amount) - .append_contract(to.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256(*asset_id), to) - .append_contract(to.into()) - .call() - .await - .unwrap() - .value; - - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_contract_transfer_base_asset() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, _) = get_fuelcoin_instance(wallet.clone()).await; - - let amount = 44u64; - let to = get_balance_contract_id(wallet.clone()).await; - - fuelcontract_instance - .methods() - .contract_transfer(to, Bits256([0u8; 32]), amount) - .append_contract(to.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256([0u8; 32]), to) - .append_contract(to.into()) - .call() - .await - .unwrap() - .value; - - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_identity_address_mint_to() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = wallet.address().into(); - let to = Identity::Address(to); - - fuelcontract_instance - .methods() - .identity_mint_to(to, amount, Bits256(*sub_id)) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance = wallet - .get_asset_balance(&AssetId::from(*asset_id)) - .await - .unwrap(); - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_identity_address_transfer_new_mint() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = wallet.address().into(); - let to = Identity::Address(to); - - fuelcontract_instance - .methods() - .mint_coins(amount, Bits256(*sub_id)) - .call() - .await - .unwrap(); - - fuelcontract_instance - .methods() - .identity_transfer(to, Bits256(*asset_id), amount) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance = wallet - .get_asset_balance(&AssetId::from(*asset_id)) - .await - .unwrap(); - - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_identity_address_transfer_base_asset() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, _) = get_fuelcoin_instance(wallet.clone()).await; - - let amount = 44u64; - let to = wallet.address().into(); - let to = Identity::Address(to); - - let balance_prev = wallet.get_asset_balance(&AssetId::BASE).await.unwrap(); - - fuelcontract_instance - .methods() - .identity_transfer(to, Bits256([0u8; 32]), amount) - .append_variable_outputs(1) - .call() - .await - .unwrap(); - - let balance_new = wallet.get_asset_balance(&AssetId::BASE).await.unwrap(); - - assert_eq!(balance_new, balance_prev + amount); -} - -#[tokio::test] -async fn test_identity_contract_mint_to() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let balance_contract_id = get_balance_contract_id(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = Identity::ContractId(balance_contract_id); - - fuelcontract_instance - .methods() - .identity_mint_to(to, amount, Bits256(*sub_id)) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256(*asset_id), balance_contract_id) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap() - .value; - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_identity_contract_transfer_new_mint() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, fuelcontract_id) = get_fuelcoin_instance(wallet.clone()).await; - let balance_contract_id = get_balance_contract_id(wallet.clone()).await; - let sub_id = Bytes32::zeroed(); - let asset_id = get_asset_id(sub_id, fuelcontract_id).await; - let amount = 44u64; - let to = Identity::ContractId(balance_contract_id); - - fuelcontract_instance - .methods() - .mint_coins(amount, Bits256(*sub_id)) - .call() - .await - .unwrap(); - - fuelcontract_instance - .methods() - .identity_transfer(to, Bits256(*asset_id), amount) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256(*asset_id), balance_contract_id) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap() - .value; - - assert_eq!(balance, amount); -} - -#[tokio::test] -async fn test_identity_contract_transfer_base_asset() { - let wallet = launch_provider_and_get_wallet().await.unwrap(); - let (fuelcontract_instance, _) = get_fuelcoin_instance(wallet.clone()).await; - let balance_contract_id = get_balance_contract_id(wallet.clone()).await; - - let amount = 44u64; - let to = Identity::ContractId(balance_contract_id); - - fuelcontract_instance - .methods() - .identity_transfer(to, Bits256([0u8; 32]), amount) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap(); - - let balance = fuelcontract_instance - .methods() - .get_balance(Bits256([0u8; 32]), balance_contract_id) - .append_contract(balance_contract_id.into()) - .call() - .await - .unwrap() - .value; - - assert_eq!(balance, amount); -} - async fn get_asset_id(sub_id: Bytes32, contract: ContractId) -> Bytes32 { let mut hasher = Sha256::new(); hasher.update(*contract); diff --git a/test/src/sdk-harness/test_projects/token_ops/src/main.sw b/test/src/sdk-harness/test_projects/token_ops/src/main.sw index 0008cc55a79..0cf535aa044 100644 --- a/test/src/sdk-harness/test_projects/token_ops/src/main.sw +++ b/test/src/sdk-harness/test_projects/token_ops/src/main.sw @@ -1,6 +1,6 @@ contract; -use std::{contract_id::AssetId, bytes::Bytes, constants::ZERO_B256, context::balance_of, message::send_message, primitive_conversions::u64::*, token::*}; +use std::{bytes::Bytes, constants::ZERO_B256, context::balance_of, message::send_message, primitive_conversions::u64::*, token::*}; abi TestFuelCoin { fn mint_coins(mint_amount: u64, sub_id: b256); @@ -13,13 +13,6 @@ abi TestFuelCoin { fn generic_mint_to(amount: u64, to: Identity, sub_id: b256); fn generic_transfer(amount: u64, asset_id: b256, to: Identity); fn send_message(recipient: b256, msg_data: Vec, coins: u64); - - fn address_mint_to(recipient: Address, amount: u64, sub_id: b256); - fn address_transfer(recipient: Address, asset_id: b256, amount: u64); - fn contract_mint_to(recipient: ContractId, amount: u64, sub_id: b256); - fn contract_transfer(recipient: ContractId, asset_id: b256, amount: u64); - fn identity_mint_to(recipient: Identity, amount: u64, sub_id: b256); - fn identity_transfer(recipient: Identity, asset_id: b256, amount: u64); } impl TestFuelCoin for Contract { @@ -73,31 +66,4 @@ impl TestFuelCoin for Contract { send_message(recipient, data, coins); } - - fn address_mint_to(recipient: Address, amount: u64, sub_id: b256) { - recipient.mint_to(sub_id, amount); - } - - fn address_transfer(recipient: Address, asset_id: b256, amount: u64) { - let asset_id = AssetId { value: asset_id }; - recipient.transfer(asset_id, amount); - } - - fn contract_mint_to(recipient: ContractId, amount: u64, sub_id: b256) { - recipient.mint_to(sub_id, amount); - } - - fn contract_transfer(recipient: ContractId, asset_id: b256, amount: u64) { - let asset_id = AssetId { value: asset_id }; - recipient.transfer(asset_id, amount); - } - - fn identity_mint_to(recipient: Identity, amount: u64, sub_id: b256) { - recipient.mint_to(sub_id, amount); - } - - fn identity_transfer(recipient: Identity, asset_id: b256, amount: u64) { - let asset_id = AssetId { value: asset_id }; - recipient.transfer(asset_id, amount); - } }