Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "this" method to ContractId #5099

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sway-lib-std/src/call_frames.sw
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ const SECOND_PARAMETER_OFFSET: u64 = 74;
/// # Examples
///
/// ```sway
/// use std::{call_frames::contract_id, token::mint};
/// use std::{call_frames::contract_id, constants::ZERO_B256, token::mint};
///
/// fn foo() {
/// let this_contract = contract_id();
/// mint(50);
/// Address::from(ZERO_B256).transfer(50, this_contract);
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
/// mint(ZERO_B256, 50);
/// Address::from(ZERO_B256).transfer(AssetId::default(this_contract), 50);
/// }
/// ```
pub fn contract_id() -> ContractId {
Expand Down
27 changes: 27 additions & 0 deletions sway-lib-std/src/contract_id.sw
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ impl AssetId {
}

impl ContractId {
/// Returns the ContractId of the currently executing contract.
///
/// # Additional Information
///
/// This is equivalent to std::callframes::contract_id().
///
/// **_Note:_** If called in an external context, this will **not** return a ContractId.
/// If called externally, will actually return a pointer to the Transaction Id (Wrapped in the ContractId struct).
///
/// # Returns
///
/// * [ContractId] - The contract id of this contract.
///
/// # Examples
///
/// ```sway
/// use std::{constants::ZERO_B256, token::mint};
///
/// fn foo() {
/// let this_contract = ContractId::this();
/// mint(ZERO_B256, 50);
/// Address::from(ZERO_B256).transfer(AssetId::default(this_contract), 50);
/// }
/// ```
pub fn this() -> ContractId {
SwayStar123 marked this conversation as resolved.
Show resolved Hide resolved
ContractId::from(asm() { fp: b256 })
}
/// UNCONDITIONAL transfer of `amount` coins of type `asset_id` to
/// the ContractId.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct TestStruct2 {

abi CallFramesTest {
fn get_id() -> ContractId;
fn get_id_contract_id_this() -> ContractId;
fn get_asset_id() -> AssetId;
fn get_code_size() -> u64;
fn get_first_param() -> u64;
Expand Down
7 changes: 7 additions & 0 deletions test/src/sdk-harness/test_projects/call_frames/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ async fn can_get_contract_id() {
assert_eq!(result.value, id);
}

#[tokio::test]
async fn can_get_id_contract_id_this() {
let (instance, id) = get_call_frames_instance().await;
let result = instance.methods().get_id_contract_id_this().call().await.unwrap();
assert_eq!(result.value, id);
}

#[tokio::test]
async fn can_get_code_size() {
let (instance, _id) = get_call_frames_instance().await;
Expand Down
4 changes: 4 additions & 0 deletions test/src/sdk-harness/test_projects/call_frames/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ impl CallFramesTest for Contract {
contract_id()
}

fn get_id_contract_id_this() -> ContractId {
ContractId::this()
}

fn get_asset_id() -> AssetId {
msg_asset_id()
}
Expand Down
Loading