diff --git a/docs/code/deploy-and-run/img/select-chain.png b/docs/code/deploy-and-run/img/select-chain.png index b5e8cac06..869365382 100644 Binary files a/docs/code/deploy-and-run/img/select-chain.png and b/docs/code/deploy-and-run/img/select-chain.png differ diff --git a/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-deploy.png b/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-deploy.png new file mode 100644 index 000000000..88cc9032e Binary files /dev/null and b/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-deploy.png differ diff --git a/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-method-call.png b/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-method-call.png new file mode 100644 index 000000000..4fde65480 Binary files /dev/null and b/docs/tutorials/arbitrum-stylus-benchmark/img/remix-arbitrum-method-call.png differ diff --git a/docs/tutorials/arbitrum-stylus-benchmark/index.md b/docs/tutorials/arbitrum-stylus-benchmark/index.md new file mode 100644 index 000000000..1f16a2261 --- /dev/null +++ b/docs/tutorials/arbitrum-stylus-benchmark/index.md @@ -0,0 +1,238 @@ +--- +title: Arbitrum Stylus Gas Efficiency Test +description: Arbitrum Stylus Gas Efficiency Test +sidebar_position: 10 +--- + +# Gas Cost Comparison: Stylus Rust vs Solidity + +
+ +## Introduction +When developing smart contracts, gas costs are a critical consideration. This article compares the gas costs of smart contracts written in Solidity and Stylus to determine which is more efficient. +You can download the sample code from the [GitHub repository](https://github.com/dsrvlabs/arbiturm-stylus-solidity-compare). + +## Overview + +- **Solidity**: The most widely used programming language for writing smart contracts on the Ethereum blockchain. +- **Stylus**: Stylus allows you to write smart contracts in languages that compile to WASM, like Rust and C++, offering high efficiency, lower gas fees, and interoperability with Solidity on the Arbitrum network. + +## Testing with Computationally Intensive Functions + +One common example of high computational cost in smart contracts is cryptographic operations. For this comparison, we used the `keccak256` hashing function, which is known for its significant computation requirements. +This test was conducted on the Arbitrum Sepolia testnet. ( RPC: `https://sepolia-rollup.arbitrum.io/rpc` Chain ID: `421614` ) + + ++ +#### Solidity Smart Contract + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.23; + +contract HashStorage { +bytes32 public storedHash; + + function storeHash(string memory _input) public { + storedHash = keccak256(abi.encodePacked(_input)); + } + +} +``` + + | ++ +#### Stylus Rust + +```rust +#![cfg_attr(not(feature = "export-abi"), no_main)] +extern crate alloc; + +use stylus_sdk::{crypto::keccak, prelude::*, storage::StorageFixedBytes}; + +#[global_allocator] +static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT; + +sol_storage! { + #[entrypoint] + pub struct HashStorage { + StorageFixedBytes<32> h; + } +} + +#[external] +impl HashStorage { + pub fn store_hash(&mut self, input: String) { + let hash = keccak(input.as_bytes()); + self.h.set(hash.into()); + } + + pub fn get_hash(&self) -> String { + let h = self.h.get(); + hex::encode(h) + } +} +``` + | +
+ +#### Solidity Smart Contract + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.23; + +contract HashStorage1000 { + bytes32 public storedHash; + + function storeHash(string memory _input) public { + bytes32 hash = keccak256(abi.encodePacked(_input)); + + for (uint i = 0; i < 999; i++) { + hash = keccak256(abi.encodePacked(hash)); + } + + storedHash = hash; + } +} +``` + + | ++ +#### Stylus Rust + +```rust +#![cfg_attr(not(feature = "export-abi"), no_main)] +extern crate alloc; + +use stylus_sdk::{crypto::keccak, prelude::*, storage::StorageFixedBytes}; + +#[global_allocator] +static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT; + +sol_storage! { + #[entrypoint] + pub struct HashStorage1000 { + StorageFixedBytes<32> h; + } +} + +#[external] +impl HashStorage1000 { + pub fn store_hash(&mut self, input: String) { + let mut hash = keccak(input.as_bytes()); + + for _ in 1..1000 { + hash = keccak(&hash); + } + + self.h.set(hash.into()); + } + + pub fn get_hash(&self) -> String { + let h = self.h.get(); + hex::encode(h) + } +} + +``` + | +