A quick Solidity
→ Sway
cross reference for the most commonly used items
If something is missing here you can most likely find it in the Sway STD Library
In Sway, you can use various methods to get blockchain state information and perform complex mathematical operations.
height()
and timestamp()
, which allow smart contracts to retrieve the current block's height and timestamp.get_blocknumber
and get_blocktime
functions, we demonstrate how to use these functions to get the number and timestamp of the current block.uint128
type, but it can be represented using the U128
structure, which consists of two u64
type values.get_u128_number
function, we demonstrate how to use the U128
structure to perform addition operations and treat it as a uint128
type value.In the provided code, we define a smart contract named SolidityCheatsheet
with four functions: get_blocknumber
, get_blocktime
, get_msg_sender
, and get_u128_number
.
get_blocknumber
function returns the number of the current block.get_blocktime
function returns the timestamp of the current block.get_msg_sender
function returns the sender of the current message.get_u128_number
function returns the sum of two U128
type values, which is equivalent to a uint128
type value.This simple example is intended to show you how to get blockchain state information and perform U128 integer type operations in Sway. We hope this tutorial helps you better understand these features in Sway.
All Examples// The underlying smart contracts written in Sway are no different than those in Solidity // Some bytecode is deployed with an API and state to interact with contract; // The ABI (Application Binary Interface) clearly defines the signature of the functions present in the contract abi HelloModular { // The "annotation" storage indicates the impure actions of the function // In this case the greet() function only has reading capabilities. // Note: Storage can only be found in contract type programs #[storage(read)] fn my_lucky_number() -> u64; } // Storage contains all of the state available in the contract storage { lucky_number: u64 = 777, } // The actual implementation of ABI for the contract impl HelloModular for Contract { #[storage(read)] fn my_lucky_number() -> u64 { storage.lucky_number.read() } }