Skip to content

Commit 87c4373

Browse files
authored
Merge pull request #6450 from obycode/feat/current-contract
[Clarity-4] implement `current-contract`
2 parents eb0f852 + 6ea7ee9 commit 87c4373

File tree

7 files changed

+218
-72
lines changed

7 files changed

+218
-72
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
1616
- Creates epoch 3.3 and costs-4 in preparation for a hardfork to activate Clarity 4
1717
- Adds support for new Clarity 4 builtins (not activated until epoch 3.3):
1818
- `contract-hash?`
19+
- `current-contract`
1920
- `block-time`
2021
- `to-ascii?`
2122
- Added `contract_cost_limit_percentage` to the miner config file — sets the percentage of a block’s execution cost at which, if a large non-boot contract call would cause a BlockTooBigError, the miner will stop adding further non-boot contract calls and only include STX transfers and boot contract calls for the remainder of the block.

clarity/src/vm/analysis/arithmetic_checker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl ArithmeticOnlyChecker<'_> {
143143
match native_var {
144144
ContractCaller | TxSender | TotalLiquidMicroSTX | BlockHeight | BurnBlockHeight
145145
| Regtest | TxSponsor | Mainnet | ChainId | StacksBlockHeight | TenureHeight
146-
| BlockTime => Err(Error::VariableForbidden(native_var)),
146+
| BlockTime | CurrentContract => Err(Error::VariableForbidden(native_var)),
147147
NativeNone | NativeTrue | NativeFalse => Ok(()),
148148
}
149149
} else {

clarity/src/vm/analysis/type_checker/v2_05/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ fn type_reserved_variable(variable_name: &str) -> Result<Option<TypeSignature>,
330330
NativeFalse => TypeSignature::BoolType,
331331
TotalLiquidMicroSTX => TypeSignature::UIntType,
332332
Regtest => TypeSignature::BoolType,
333-
TxSponsor | Mainnet | ChainId | StacksBlockHeight | TenureHeight | BlockTime => {
333+
TxSponsor | Mainnet | ChainId | StacksBlockHeight | TenureHeight | BlockTime | CurrentContract => {
334334
return Err(CheckErrors::Expects(
335-
"tx-sponsor, mainnet, chain-id, stacks-block-height, tenure-height, and block-time should not reach here in 2.05".into(),
335+
"tx-sponsor, mainnet, chain-id, stacks-block-height, tenure-height, block-time, and current-contract should not reach here in 2.05".into(),
336336
)
337337
.into())
338338
}

clarity/src/vm/analysis/type_checker/v2_1/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ fn type_reserved_variable(
10221022
Regtest => TypeSignature::BoolType,
10231023
Mainnet => TypeSignature::BoolType,
10241024
ChainId => TypeSignature::UIntType,
1025+
CurrentContract => TypeSignature::PrincipalType,
10251026
BlockTime => TypeSignature::UIntType,
10261027
};
10271028
Ok(Some(var_type))

clarity/src/vm/docs/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ to the same contract principal.",
125125
example: "(print contract-caller) ;; Will print out a Stacks address of the transaction sender",
126126
};
127127

128+
const CURRENT_CONTRACT_KEYWORD: SimpleKeywordAPI = SimpleKeywordAPI {
129+
name: "current-contract",
130+
snippet: "current-contract",
131+
output_type: "principal",
132+
description: "Returns the principal of the current contract.",
133+
example:
134+
"(print current-contract) ;; Will print out the Stacks address of the current contract",
135+
};
136+
128137
const STACKS_BLOCK_HEIGHT_KEYWORD: SimpleKeywordAPI = SimpleKeywordAPI {
129138
name: "stacks-block-height",
130139
snippet: "stacks-block-height",
@@ -2690,6 +2699,7 @@ pub fn make_keyword_reference(variable: &NativeVariables) -> Option<KeywordAPI>
26902699
NativeVariables::Mainnet => MAINNET_KEYWORD.clone(),
26912700
NativeVariables::ChainId => CHAINID_KEYWORD.clone(),
26922701
NativeVariables::TxSponsor => TX_SPONSOR_KEYWORD.clone(),
2702+
NativeVariables::CurrentContract => CURRENT_CONTRACT_KEYWORD.clone(),
26932703
NativeVariables::BlockTime => BLOCK_TIME_KEYWORD.clone(),
26942704
};
26952705
Some(KeywordAPI {

0 commit comments

Comments
 (0)