Skip to content

Commit

Permalink
Merge branch 'master' into josh/allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel authored Nov 8, 2023
2 parents 7728e8b + 37b0a70 commit 6ef9da2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
6 changes: 3 additions & 3 deletions examples/liquidity_pool/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
contract_id,
msg_asset_id,
},
constants::ZERO_B256,
constants::DEFAULT_SUB_ID,
context::msg_amount,
hash::*,
token::{
Expand All @@ -32,11 +32,11 @@ impl LiquidityPool for Contract {
let amount_to_mint = msg_amount() * 2;

// Mint some LP token based upon the amount of the base token.
mint_to_address(recipient, ZERO_B256, amount_to_mint);
mint_to_address(recipient, DEFAULT_SUB_ID, amount_to_mint);
}

fn withdraw(recipient: Address) {
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();
assert(msg_asset_id() == asset_id);
assert(msg_amount() > 0);

Expand Down
10 changes: 5 additions & 5 deletions examples/native_token/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
contract;

use std::{constants::ZERO_B256, context::*, token::*};
use std::{constants::DEFAULT_SUB_ID, context::*, token::*};

abi NativeAssetToken {
fn mint_coins(mint_amount: u64);
Expand All @@ -16,12 +16,12 @@ abi NativeAssetToken {
impl NativeAssetToken for Contract {
/// Mint an amount of this contracts native asset to the contracts balance.
fn mint_coins(mint_amount: u64) {
mint(ZERO_B256, mint_amount);
mint(DEFAULT_SUB_ID, mint_amount);
}

/// Burn an amount of this contracts native asset.
fn burn_coins(burn_amount: u64) {
burn(ZERO_B256, burn_amount);
burn(DEFAULT_SUB_ID, burn_amount);
}

/// Transfer coins to a target contract.
Expand All @@ -46,11 +46,11 @@ impl NativeAssetToken for Contract {

/// Mint and send this contracts native token to a destination contract.
fn mint_and_send_to_contract(amount: u64, destination: ContractId) {
mint_to_contract(destination, ZERO_B256, amount);
mint_to_contract(destination, DEFAULT_SUB_ID, amount);
}

/// Mind and send this contracts native token to a destination address.
fn mint_and_send_to_address(amount: u64, recipient: Address) {
mint_to_address(recipient, ZERO_B256, amount);
mint_to_address(recipient, DEFAULT_SUB_ID, amount);
}
}
14 changes: 14 additions & 0 deletions sway-lib-std/src/constants.sw
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ pub const BASE_ASSET_ID: AssetId = AssetId::from(ZERO_B256);
/// }
/// ```
pub const ZERO_B256 = 0x0000000000000000000000000000000000000000000000000000000000000000;

/// The default Sub Id for assets.
///
/// # Examples
///
/// ```sway
/// use std::{call_frames::contract_id, constants::DEFAULT_SUB_ID};
///
/// fn foo() {
/// let asset = AssetId::default();
/// assert(AssetId::new(contract_id(), DEFAULT_SUB_ID) == msg_asset_id());
/// }
/// ```
pub const DEFAULT_SUB_ID = ZERO_B256;
21 changes: 7 additions & 14 deletions sway-lib-std/src/contract_id.sw
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,24 @@ impl AssetId {
Self { value: result_buffer }
}

/// Creates a new AssetId from a ContractId and the zero SubId.
///
/// # Arguments
///
/// * `contract_id`: [ContractId] - The ContractId of the contract that created the asset.
/// 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 zero SubId.
/// * [AssetId] - The AssetId of the asset. Computed by hashing the ContractId and the default SubId.
///
/// # Examples
///
/// ```sway
/// use std::{callframes::contract_id, constants::ZERO_B256};
/// use std::{callframes::contract_id, constants::DEFAULT_SUB_ID};
///
/// fn foo() {
/// let contract_id = contract_id();
/// let sub_id = ZERO_B256;
///
/// let asset_id = AssetId::default(contract_id);
///
/// assert(asset_id == AssetId::new(contract_id, sub_id));
/// let asset_id = AssetId::default();
/// assert(asset_id == AssetId::new(contract_id(), DEFAULT_SUB_ID));
/// }
/// ```
pub fn default(contract_id: ContractId) -> Self {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ script;
use std::context::balance_of;
use std::token::*;
use std::contract_id::*;
use std::constants::DEFAULT_SUB_ID;
use test_fuel_coin_abi::*;

struct Opts {
Expand All @@ -16,7 +17,7 @@ fn main() -> bool {

// the deployed fuel_coin Contract_Id:
let fuelcoin_id = ContractId::from(0x5d10689c7eecb405937a3f35fab7baf05a3f6189f9a2993ee70e21ccc1212460);
let fuelcoin_asset_id = AssetId::default(fuelcoin_id);
let fuelcoin_asset_id = AssetId::new(fuelcoin_id, DEFAULT_SUB_ID);

// contract ID for sway/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/balance_test_contract/
let balance_test_id = ContractId::from(0x4a00baa517980432b9274a0e2f03c88735bdb483730816679c6eb37b4046d060);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl ContextCaller for Contract {
fn call_get_this_balance_with_coins(send_amount: u64, target: ContractId) -> u64 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_this_balance {
gas: 500_000,
Expand All @@ -30,7 +30,7 @@ impl ContextCaller for Contract {
fn call_get_balance_of_contract_with_coins(send_amount: u64, target: ContractId) -> u64 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_balance_of_contract {
gas: 500_000,
Expand All @@ -42,7 +42,7 @@ impl ContextCaller for Contract {
fn call_get_amount_with_coins(send_amount: u64, target: ContractId) -> u64 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_amount {
gas: 500_000,
Expand All @@ -54,7 +54,7 @@ impl ContextCaller for Contract {
fn call_get_asset_id_with_coins(send_amount: u64, target: ContractId) -> b256 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_asset_id {
gas: 500_000,
Expand All @@ -66,7 +66,7 @@ impl ContextCaller for Contract {
fn call_get_gas_with_coins(send_amount: u64, target: ContractId) -> u64 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_gas {
gas: 500_000,
Expand All @@ -78,7 +78,7 @@ impl ContextCaller for Contract {
fn call_get_global_gas_with_coins(send_amount: u64, target: ContractId) -> u64 {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.get_global_gas {
gas: 500_000,
Expand All @@ -90,7 +90,7 @@ impl ContextCaller for Contract {
fn call_receive_coins(send_amount: u64, target: ContractId) {
let id = target.value;
let context_contract = abi(ContextTesting, id);
let asset_id = AssetId::default(contract_id());
let asset_id = AssetId::default();

context_contract.receive_coins {
gas: 500_000,
Expand Down

0 comments on commit 6ef9da2

Please sign in to comment.