Skip to content

Commit

Permalink
Addressed review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx committed Aug 7, 2023
1 parent bfddc93 commit a0f2274
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 20 deletions.
8 changes: 3 additions & 5 deletions examples/contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use fuels::tx::{Bytes32, ContractIdExt};
use fuels::types::errors::{error, Error, Result};
use fuels::types::Bits256;

#[tokio::test]
async fn instantiate_client() -> Result<()> {
Expand Down Expand Up @@ -345,9 +345,7 @@ mod tests {
let response = contract_methods.mint_coins(1_000_000).call().await?;
// ANCHOR: variable_outputs
let address = wallet.address();
let asset_id = ContractId::from(contract_id)
.asset_id(&Bytes32::zeroed())
.into();
let asset_id = contract_id.asset_id(&Bits256::zeroed()).into();

// withdraw some tokens to wallet
let response = contract_methods
Expand Down Expand Up @@ -410,7 +408,7 @@ mod tests {
.await?;
// ANCHOR_END: dependency_estimation_manual

let asset_id = ContractId::from(*caller_contract_id.hash()).asset_id(&Bytes32::zeroed());
let asset_id = caller_contract_id.asset_id(&Bits256::zeroed());
let balance = wallet.get_asset_balance(&asset_id).await?;
assert_eq!(balance, amount);

Expand Down
4 changes: 2 additions & 2 deletions examples/cookbook/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
mod tests {
use fuels::tx::{Bytes32, ContractIdExt};
use fuels::types::Bits256;
use fuels::{
prelude::Result,
types::transaction_builders::{ScriptTransactionBuilder, TransactionBuilder},
Expand Down Expand Up @@ -66,7 +66,7 @@ mod tests {
// ANCHOR_END: liquidity_deposit

// ANCHOR: liquidity_withdraw
let lp_asset_id = ContractId::from(*contract_id.hash()).asset_id(&Bytes32::zeroed());
let lp_asset_id = contract_id.asset_id(&Bits256::zeroed());
let lp_token_balance = wallet.get_asset_balance(&lp_asset_id).await?;

let call_params = CallParameters::default()
Expand Down
12 changes: 11 additions & 1 deletion packages/fuels-core/src/types/bech32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::{
str::FromStr,
};

use crate::types::Bits256;
use bech32::{FromBase32, ToBase32, Variant::Bech32m};
use fuel_tx::{Address, Bytes32, ContractId};
use fuel_tx::{Address, Bytes32, ContractId, ContractIdExt};
use fuel_types::AssetId;

use crate::types::errors::{Error, Result};

Expand Down Expand Up @@ -128,6 +130,14 @@ impl From<ContractId> for Bech32ContractId {
}
}

impl Bech32ContractId {
/// Creates an `AssetId` from the `Bech32ContractId` and `sub_id`.
pub fn asset_id(&self, sub_id: &Bits256) -> AssetId {
let sub_id = Bytes32::from(sub_id.0);
ContractId::from(self).asset_id(&sub_id)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
5 changes: 5 additions & 0 deletions packages/fuels-core/src/types/core/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ use crate::types::errors::{error, Error, Result};
pub struct Bits256(pub [u8; 32]);

impl Bits256 {
/// Returns `Self` with zeroes inside.
pub fn zeroed() -> Self {
Self([0; 32])
}

/// Create a new `Bits256` from a string representation of a hex.
/// Accepts both `0x` prefixed and non-prefixed hex strings.
pub fn from_hex_str(hex: &str) -> Result<Self> {
Expand Down
3 changes: 1 addition & 2 deletions packages/fuels/tests/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::future::Future;
use std::vec;

use fuel_core::chain_config::ChainConfig;
use fuel_tx::{Bytes32, ContractIdExt};
use fuels::{
accounts::{predicate::Predicate, Account},
core::codec::{calldata, fn_selector},
Expand Down Expand Up @@ -662,7 +661,7 @@ async fn setup_output_variable_estimation_test(
.deploy(&wallets[0], TxParameters::default())
.await?;

let mint_asset_id = ContractId::from(&contract_id).asset_id(&Bytes32::zeroed());
let mint_asset_id = contract_id.asset_id(&Bits256::zeroed());
let addresses: [Address; 3] = wallets
.iter()
.map(|wallet| wallet.address().into())
Expand Down
6 changes: 1 addition & 5 deletions packages/fuels/tests/logs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use fuel_tx::ContractIdExt;
use std::fmt::Debug;

use fuels::tx::Bytes32;
use fuels::{
prelude::*,
tx::Receipt,
Expand Down Expand Up @@ -1246,9 +1244,7 @@ async fn contract_token_ops_error_messages() -> Result<()> {

{
let contract_id = contract_instance.contract_id();
let asset_id = ContractId::from(contract_id)
.asset_id(&Bytes32::zeroed())
.into();
let asset_id = contract_id.asset_id(&Bits256::zeroed()).into();
let address = wallet.address();

let error = contract_methods
Expand Down
10 changes: 5 additions & 5 deletions packages/fuels/tests/providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::{iter, ops::Add, str::FromStr, vec};
use chrono::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};
use fuel_core::service::{Config as CoreConfig, FuelService, ServiceTrait};
use fuel_core_types::tai64::Tai64;
use fuel_tx::{Bytes32, ContractIdExt};
use fuels::{
accounts::{fuel_crypto::SecretKey, Account},
client::{PageDirection, PaginationRequest},
Expand All @@ -12,6 +11,7 @@ use fuels::{
tx::Receipt,
types::{block::Block, coin_type::CoinType, errors::error, message::Message},
};
use fuels_core::types::Bits256;

#[tokio::test]
async fn test_provider_launch_and_connect() -> Result<()> {
Expand Down Expand Up @@ -338,6 +338,8 @@ async fn test_gas_forwarded_defaults_to_tx_limit() -> Result<()> {
),
);

// The gas used by the script to call a contract and forward remaining gas limit.
let gas_used_by_script = 161;
let gas_limit = 225_883;
let response = contract_instance
.methods()
Expand All @@ -354,7 +356,7 @@ async fn test_gas_forwarded_defaults_to_tx_limit() -> Result<()> {
.gas()
.unwrap();

assert_eq!(gas_limit, gas_forwarded + 161);
assert_eq!(gas_limit, gas_forwarded + gas_used_by_script);

Ok(())
}
Expand All @@ -375,9 +377,7 @@ async fn test_amount_and_asset_forwarding() -> Result<()> {
);
let contract_id = contract_instance.contract_id();
let contract_methods = contract_instance.methods();
let asset_id = ContractId::from(contract_id)
.asset_id(&Bytes32::zeroed())
.into();
let asset_id = contract_id.asset_id(&Bits256::zeroed()).into();

let mut balance_response = contract_methods
.get_balance(contract_id, asset_id)
Expand Down

0 comments on commit a0f2274

Please sign in to comment.