Starknet 백서

Starknet

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 13쪽

re-starknet-syscalls-library_call_syscall Calls the requested function in any previously declared class. ## Signature ```rust theme={null} pub extern fn library_call_syscall(class_hash: ClassHash, function_selector: felt252, calldata: Span) -> Result, Array> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `class_hash` - The hash of the class to be used. * `function_selector` - A selector for a function within that class. * `calldata` - Call arguments. # core::starknet::syscalls::meta_tx_v0_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-meta_tx_v0_syscall Invokes the given entry point as a v0 meta transaction. * The signature is replaced with the given signature. * The caller is the OS (address 0). * The transaction version is replaced by 0. * The transaction hash is replaced by the corresponding version-0 transaction hash. The changes apply to the called contract and the inner contracts it calls. NOTE: This syscall should only be used to allow support for old version-0 bound accounts, and should not be used for other purposes. ## Signature ```rust theme={null} pub extern fn meta_tx_v0_syscall(address: ContractAddress, entry_point_selector: felt252, calldata: Span, signature: Span) -> Result, Array> implicits(GasBuiltin, System) nopanic; ``` # core::starknet::syscalls::replace_class_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-replace_class_syscall Replaces the class hash of the current contract, instantly modifying its entrypoints. The new class becomes effective only after the current function call completes. The remaining code in the current function will continue executing from the old class. The new class will be used: * In subsequent transactions * If the contract is called via `call_contract` syscall later in the same transaction ## Signature ```rust theme={null} pub extern fn replace_class_syscall(class_hash: ClassHash) -> Result> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `class_hash` - The class hash that should replace the current one. # core::starknet::syscalls::send_message_to_l1_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-send_message_to_l1_syscall Sends a message to L1. ## Signature ```rust theme={null} pub extern fn send_message_to_l1_syscall(to_address: felt252, payload: Span) -> Result> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `to_address` - The recipient's L1 address. * `payload` - The content of the message. # core::starknet::syscalls::sha256_process_block_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-sha256_process_block_syscall Computes the next SHA-256 state of the input with the given state. ## Signature ```rust theme={null} pub extern fn sha256_process_block_syscall(state: Sha256StateHandle, input: Box) -> Result> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `state` - The current SHA-256 state. * `input` - The input provided to compute the next SHA-256 state. #### Returns * The next SHA-256 state of the input with the givens state. The system call does not add any padding and the input needs to be a multiple of 512 bits (== 16 u32 word). # core::starknet::syscalls::storage_read_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-storage_read_syscall Gets the value of a key in the storage of the calling contract. ## Signature ```rust theme={null} pub extern fn storage_read_syscall(address_domain: u32, address: StorageAddress) -> Result> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `address_domain` - The domain of the address. Only `address_domain` 0 is currently supported, in the future it will enable access to address spaces with different data availability guarantees. * `address` - The address of the storage key to read. # core::starknet::syscalls::storage_write_syscall Source: https://docs.starknet.io/build/corelib/core-starknet-syscalls-storage_write_syscall Sets the value of a key in the storage of the calling contract. ## Signature ```rust theme={null} pub extern fn storage_write_syscall(address_domain: u32, address: StorageAddress, value: felt252) -> Result> implicits(GasBuiltin, System) nopanic; ``` #### Arguments * `address_domain` - The domain of the address. Only `address_domain` 0 is currently supported, in the future it will enable access to address spaces with different data availability guarantees. * `address` - The address of the storage key to write. * `value` - The value to write to the key. # core::starknet::testing::cheatcode Source: https://docs.starknet.io/build/corelib/core-starknet-testing-cheatcode A general cheatcode function used to simplify implementation of Starknet testing functions. This is the base function used by testing utilities to interact with the test environment. External users can implement custom cheatcodes by injecting a custom `CairoHintProcessor` in the runner. ## Signature ```rust theme={null} pub extern fn cheatcode(input: Span) -> Span nopanic; ``` #### Arguments `selector` - The cheatcode identifier. `input` - Input parameters for the cheatcode. #### Returns * A span containing the cheatcode's output # core::starknet::testing::pop_l2_to_l1_message Source: https://docs.starknet.io/build/corelib/core-starknet-testing-pop_l2_to_l1_message Pop the earliest unpopped l2 to l1 message for the contract. ## Signature ```rust theme={null} pub fn pop_l2_to_l1_message(address: ContractAddress) -> Option)> ``` #### Arguments `address` - The contract address from which to pop a l2-L1 message. The returned value is a tuple of the l1 address the message was sent to as a `felt252`, and the message data as a span. May be called multiple times to pop multiple messages. Useful for testing the contract's l2 to l1 message emission. # core::starknet::testing::pop_log Source: https://docs.starknet.io/build/corelib/core-starknet-testing-pop_log Pop the earliest unpopped logged event for the contract as the requested type. ## Signature ```rust theme={null} pub fn pop_log>(address: ContractAddress) -> Option ``` #### Arguments `address` - The contract address from which to pop an event. Should be used when the type of the event is known. Type of the event should be the event defined within the contract. Useful for testing the contract's event emission. May be called multiple times to pop multiple events. If called until `None` is returned, all events have been popped. ## Examples ```rust theme={null} #[starknet::contract] mod contract { #[event] #[derive(Copy, Drop, Debug, PartialEq, starknet::Event)] pub enum Event { Event1: felt252, Event2: u128, } ... } #[test] fn test_event() { let contract_address = somehow_get_contract_address(); call_code_causing_events(contract_address); assert_eq!( starknet::testing::pop_log(contract_address), Some(contract::Event::Event1(42)) ); assert_eq!( starknet::testing::pop_log(contract_address), Some(contract::Event::Event2(41)) ); assert_eq!( starknet::testing::pop_log(contract_address), Some(contract::Event::Event1(40)) ); assert_eq!(starknet::testing::pop_log_raw(contract_address), None); } ``` # core::starknet::testing::pop_log_raw Source: https://docs.starknet.io/build/corelib/core-starknet-testing-pop_log_raw Pop the earliest unpopped logged event for the contract. ## Signature ```rust theme={null} pub fn pop_log_raw(address: ContractAddress) -> Option, Span)> ``` #### Arguments `address` - The contract address from which to pop an event. The value is returned as a tuple of two spans, the first for the keys and the second for the data. May be called multiple times to pop multiple events. If called until `None` is returned, all events have been popped. # core::starknet::testing::set_account_contract_address Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_account_contract_address Sets the account contract address. ## Signature ```rust theme={null} pub fn set_account_contract_address(address: ContractAddress) ``` #### Arguments `address` - The account contract to set. After a call to `set_account_contract_address`, `starknet::get_execution_info().tx_info.account_contract_address` will return the set value. # core::starknet::testing::set_block_hash Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_block_hash Set the hash for a block. ## Signature ```rust theme={null} pub fn set_block_hash(block_number: u64, value: felt252) ``` #### Arguments `block_number` - The targeted block number. `value` - The block hash to set. After a call to `set_block_hash`, `starknet::syscalls::get_block_hash_syscall` for the block\_number will return the set value. Unset blocks values call would fail. # core::starknet::testing::set_block_number Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_block_number Sets the block number to the provided value. ## Signature ```rust theme={null} pub fn set_block_number(block_number: u64) ``` #### Arguments `block_number` - The block number to set. After a call to `set_block_number`, `starknet::get_execution_info().block_info.block_number` will return the set value. # core::starknet::testing::set_block_timestamp Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_block_timestamp Sets the block timestamp to the provided value. ## Signature ```rust theme={null} pub fn set_block_timestamp(block_timestamp: u64) ``` #### Arguments `block_timestamp` - The block timestamp to set. After a call to `set_block_timestamp`, `starknet::get_execution_info().block_info.block_timestamp` will return the set value. # core::starknet::testing::set_caller_address Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_caller_address Sets the caller address to the provided value. ## Signature ```rust theme={null} pub fn set_caller_address(address: ContractAddress) ``` #### Arguments `address` - The caller address to set. After a call to `set_caller_address`, `starknet::get_execution_info().caller_address` will return the set value. # core::starknet::testing::set_chain_id Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_chain_id Set the transaction chain id. ## Signature ```rust theme={null} pub fn set_chain_id(chain_id: felt252) ``` #### Arguments `chain_id` - The chain id to set. After a call to `set_chain_id`, `starknet::get_execution_info().tx_info.chain_id` will return the set value. # core::starknet::testing::set_contract_address Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_contract_address Sets the contract address to the provided value. ## Signature ```rust theme={null} pub fn set_contract_address(address: ContractAddress) ``` #### Arguments `address` - The contract address to set. After a call to `set_contract_address`, `starknet::get_execution_info().contract_address` will return the set value. # core::starknet::testing::set_max_fee Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_max_fee Sets the transaction max fee. ## Signature ```rust theme={null} pub fn set_max_fee(fee: u128) ``` #### Arguments `fee` - The max fee to set. After a call to `set_max_fee`, `starknet::get_execution_info().tx_info.max_fee` will return the set value. # core::starknet::testing::set_nonce Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_nonce Set the transaction nonce. ## Signature ```rust theme={null} pub fn set_nonce(nonce: felt252) ``` #### Arguments `non` - The nonce to set. After a call to `set_nonce`, `starknet::get_execution_info().tx_info.nonce` will return the set value. # core::starknet::testing::set_sequencer_address Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_sequencer_address Sets the sequencer address to the provided value. ## Signature ```rust theme={null} pub fn set_sequencer_address(address: ContractAddress) ``` #### Arguments `address` - The sequencer address to set. After a call to `set_sequencer_address`, `starknet::get_execution_info().block_info.sequencer_address` will return the set value. # core::starknet::testing::set_signature Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_signature Set the transaction signature. ## Signature ```rust theme={null} pub fn set_signature(signature: Span) ``` #### Arguments `signature` - The signature to set. After a call to `set_signature`, `starknet::get_execution_info().tx_info.signature` will return the set value. # core::starknet::testing::set_transaction_hash Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_transaction_hash Sets the transaction hash. ## Signature ```rust theme={null} pub fn set_transaction_hash(hash: felt252) ``` #### Arguments `hash` - The transaction hash to set. After a call to `set_transaction_hash`, `starknet::get_execution_info().tx_info.transaction_hash` will return the set value. # core::starknet::testing::set_version Source: https://docs.starknet.io/build/corelib/core-starknet-testing-set_version Sets the version to the provided value. ## Signature ```rust theme={null} pub fn set_version(version: felt252) ``` #### Arguments `version` - The version to set. After a call to `set_version`, `starknet::get_execution_info().tx_info.version` will return the set value. # Core string StringLiteral Source: https://docs.starknet.io/build/corelib/core-string-StringLiteral # StringLiteral Fully qualified path: [core](./intro.md)::[string](./core-string.md)::[StringLiteral](./core-string-StringLiteral.md) <pre><code>pub trait StringLiteral\<T></code></pre> # core::testing::get_available_gas Source: https://docs.starknet.io/build/corelib/core-testing-get_available_gas Returns the amount of gas available in the `GasBuiltin`. Useful for asserting that a certain amount of gas was consumed. Note: The actual gas consumption observed by calls to `get_available_gas` is only exact immediately before calls to `withdraw_gas`. ## Examples ```rust theme={null} use core::testing::get_available_gas; fn gas_heavy_function() { // ... some gas-intensive code } fn test_gas_consumption() { let gas_before = get_available_gas(); // Making sure `gas_before` is exact. core::gas::withdraw_gas().unwrap(); gas_heavy_function(); let gas_after = get_available_gas(); // Making sure `gas_after` is exact core::gas::withdraw_gas().unwrap(); assert!(gas_after - gas_before u128 implicits(GasBuiltin) nopanic; ``` # core::testing::get_unspent_gas Source: https://docs.starknet.io/build/corelib/core-testing-get_unspent_gas Returns the amount of gas available in the `GasBuiltin`, as well as the amount of gas unused in the local wallet. Useful for asserting that a certain amount of gas was used. Note: This function call costs exactly `2300` gas, so this may be ignored in calculations. ## Examples ```rust theme={null} use core::testing::get_unspent_gas; fn gas_heavy_function() { // ... some gas-intensive code } fn test_gas_consumption() { let gas_before = get_unspent_gas(); gas_heavy_function(); let gas_after = get_unspent_gas(); assert!(gas_after - gas_before u128 implicits(GasBuiltin) nopanic; ``` # core::to_byte_array::AppendFormattedToByteArray Source: https://docs.starknet.io/build/corelib/core-to_byte_array-AppendFormattedToByteArray A trait for appending the ASCII representation of a number to an existing `ByteArray`. ## Signature ```rust theme={null} pub trait AppendFormattedToByteArray ``` ## Trait functions ### append\_formatted\_to\_byte\_array Appends the ASCII representation of the value to the provided `ByteArray`. #### Signature ```rust theme={null} fn append_formatted_to_byte_array(self: @T, ref byte_array: ByteArray, base: NonZero) ``` #### Examples ```rust theme={null} use core::to_byte_array::AppendFormattedToByteArray; let mut buffer = "Count: "; let num: u32 = 42; num.append_formatted_to_byte_array(ref buffer, 10); assert!(buffer == "Count: 42"); ``` # core::to_byte_array::FormatAsByteArray Source: https://docs.starknet.io/build/corelib/core-to_byte_array-FormatAsByteArray A trait for formatting values into their ASCII string representation in a `ByteArray`. ## Signature ```rust theme={null} pub trait FormatAsByteArray ``` ## Trait functions ### format\_as\_byte\_array Returns a new `ByteArray` containing the ASCII representation of the value. #### Signature ```rust theme={null} fn format_as_byte_array(self: @T, base: NonZero) -> ByteArray ``` #### Examples ```rust theme={null} use core::to_byte_array::FormatAsByteArray; let num: u32 = 42; let formatted = num.format_as_byte_array(16); assert!(formatted, "2a"); ``` # core::traits::Add Source: https://docs.starknet.io/build/corelib/core-traits-Add The addition operator `+`. ## Signature ```rust theme={null} pub trait Add ``` ## Examples `Add`able types: ```rust theme={null} assert!(1_u8 + 2_u8 == 3_u8); ``` Implementing `Add` for a type: ```rust theme={null} #[derive(Copy, Drop, PartialEq)] struct Point { x: u32, y: u32, } impl PointAdd of Add { fn add(lhs: Point, rhs: Point) -> Point { Point { x: lhs.x + rhs.x, y: lhs.y + rhs.y, } } } let p1 = Point { x: 1, y: 0 }; let p2 = Point { x: 2, y: 3 }; let p3 = p1 + p2; assert!(p3 == Point { x: 3, y: 3 }); ``` ## Trait functions ### add Performs the `+` operation. #### Signature ```rust theme={null} fn add(lhs: T, rhs: T) -> T ``` #### Examples ```rust theme={null} assert!(12 + 1 == 13); ``` # core::traits::AddEq Source: https://docs.starknet.io/build/corelib/core-traits-AddEq ## Signature ```rust theme={null} pub trait AddEq ``` ## Trait functions ### add\_eq #### Signature ```rust theme={null} fn add_eq(ref self: T, other: T) ``` # core::traits::BitAnd Source: https://docs.starknet.io/build/corelib/core-traits-BitAnd The bitwise AND operator `&`. ## Signature ```rust theme={null} pub trait BitAnd ``` ## Examples An implementation of `BitAnd` for a wrapper around `bool`. ```rust theme={null} use core::traits::BitAnd; #[derive(Drop, PartialEq)] struct Scalar { inner: bool, } impl BitAndScalar of BitAnd { fn bitand(lhs: Scalar, rhs: Scalar) -> Scalar { Scalar { inner: lhs.inner & rhs.inner } } } assert!(Scalar { inner: true } & Scalar { inner: true } == Scalar { inner: true }); assert!(Scalar { inner: true } & Scalar { inner: false } == Scalar { inner: false }); assert!(Scalar { inner: false } & Scalar { inner: true } == Scalar { inner: false }); assert!(Scalar { inner: false } & Scalar { inner: false } == Scalar { inner: false }); ``` ## Trait functions ### bitand Performs the `&` operation. #### Signature ```rust theme={null} fn bitand(lhs: T, rhs: T) -> T ``` #### Examples ```rust theme={null} assert_eq!(true & false, false); assert_eq!(5_u8 & 1_u8, 1); assert_eq!(true & true, true); assert_eq!(5_u8 & 2_u8, 0); ``` # core::traits::BitNot Source: https://docs.starknet.io/build/corelib/core-traits-BitNot The bitwise NOT operator `~`. ## Signature ```rust theme={null} pub trait BitNot ``` ## Examples An implementation of `BitNot` for a wrapper around `u8`. ```rust theme={null} use core::traits::BitNot; #[derive(Drop, PartialEq)] struct Wrapper { u8: u8, } impl BitNotWrapper of BitNot { fn bitnot(a: Wrapper) -> Wrapper { Wrapper { u8: ~a.u8 } } } assert!(~Wrapper { u8: 0 } == Wrapper { u8 : 255 }); assert!(~Wrapper { u8: 1 } == Wrapper { u8 : 254 }); ``` ## Trait functions ### bitnot Performs the `~` operation. #### Signature ```rust theme={null} fn bitnot(a: T) -> T ``` #### Examples ```rust theme={null} assert!(~1_u8 == 254); ``` # core::traits::BitOr Source: https://docs.starknet.io/build/corelib/core-traits-BitOr The bitwise OR operator `|`. ## Signature ```rust theme={null} pub trait BitOr ``` ## Examples An implementation of `BitOr` for a wrapper around `bool`. ```rust theme={null} use c
프로젝트로 돌아가기