create-stylus 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/issue_template.md +7 -0
- package/.github/pull_request_template.md +11 -0
- package/.github/workflows/release-alpha.yml +32 -0
- package/.yarnrc.yml +1 -0
- package/CONTRIBUTING.md +42 -0
- package/README.md +66 -0
- package/bin/create-dapp-ss.js +4 -0
- package/dist/cli.js +656 -0
- package/dist/cli.js.map +1 -0
- package/package.json +46 -0
- package/rollup.config.js +22 -0
- package/src/cli.ts +14 -0
- package/src/extensions.json +14 -0
- package/src/main.ts +70 -0
- package/src/tasks/copy-extension-file.ts +227 -0
- package/src/tasks/copy-template-files.ts +252 -0
- package/src/tasks/create-first-git-commit.ts +35 -0
- package/src/tasks/create-project-directory.ts +34 -0
- package/src/tasks/index.ts +5 -0
- package/src/tasks/install-packages.ts +15 -0
- package/src/tasks/prettier-format.ts +17 -0
- package/src/types.ts +31 -0
- package/src/utils/consts.ts +1 -0
- package/src/utils/find-files-recursively.ts +19 -0
- package/src/utils/link.ts +44 -0
- package/src/utils/load-extensions.ts +10 -0
- package/src/utils/merge-package-json.ts +33 -0
- package/src/utils/parse-arguments-into-options.ts +38 -0
- package/src/utils/prompt-for-missing-options.ts +53 -0
- package/src/utils/render-intro-message.ts +11 -0
- package/src/utils/render-outro-message.ts +34 -0
- package/templates/base/.github/ISSUE_TEMPLATE/bug_report.yml +58 -0
- package/templates/base/.github/ISSUE_TEMPLATE/config.yml +8 -0
- package/templates/base/.github/pull_request_template.md +16 -0
- package/templates/base/.github/workflows/lint.yaml +300 -0
- package/templates/base/.gitignore.template.mjs +19 -0
- package/templates/base/.gitmodules +0 -0
- package/templates/base/.husky/pre-commit +4 -0
- package/templates/base/.lintstagedrc.js +21 -0
- package/templates/base/.vscode/settings.json +7 -0
- package/templates/base/.yarn/plugins/@yarnpkg/plugin-typescript.cjs +9 -0
- package/templates/base/.yarn/releases/yarn-3.2.3.cjs +783 -0
- package/templates/base/.yarnrc.yml +11 -0
- package/templates/base/CONTRIBUTING.md +86 -0
- package/templates/base/LICENCE +21 -0
- package/templates/base/nitro-devnode/LICENSE +201 -0
- package/templates/base/nitro-devnode/README.md +70 -0
- package/templates/base/nitro-devnode/run-dev-node.sh +132 -0
- package/templates/base/nitro-devnode/start-chain-with-cors.sh +150 -0
- package/templates/base/nitro-devnode/stylus-deployer-bytecode.txt +1 -0
- package/templates/base/nitro-devnode/stylus-dev/Dockerfile +10 -0
- package/templates/base/package.json +43 -0
- package/templates/base/packages/nextjs/.env.example +13 -0
- package/templates/base/packages/nextjs/.eslintignore +11 -0
- package/templates/base/packages/nextjs/.eslintrc.json +15 -0
- package/templates/base/packages/nextjs/.gitignore.template.mjs +42 -0
- package/templates/base/packages/nextjs/.prettierrc.js +9 -0
- package/templates/base/packages/nextjs/.prettierrc.json +8 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/AddressCodeTab.tsx +27 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/AddressComponent.tsx +36 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/AddressLogsTab.tsx +21 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/AddressStorageTab.tsx +61 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/BackButton.tsx +12 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/ContractTabs.tsx +102 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/PaginationButton.tsx +39 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/SearchBar.tsx +49 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/TransactionHash.tsx +28 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/TransactionsTable.tsx +71 -0
- package/templates/base/packages/nextjs/app/blockexplorer/_components/index.tsx +7 -0
- package/templates/base/packages/nextjs/app/blockexplorer/address/[address]/page.tsx +101 -0
- package/templates/base/packages/nextjs/app/blockexplorer/layout.tsx +12 -0
- package/templates/base/packages/nextjs/app/blockexplorer/page.tsx +83 -0
- package/templates/base/packages/nextjs/app/blockexplorer/transaction/[txHash]/page.tsx +23 -0
- package/templates/base/packages/nextjs/app/blockexplorer/transaction/_components/TransactionComp.tsx +152 -0
- package/templates/base/packages/nextjs/app/debug/_components/DebugContracts.tsx +73 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ContractInput.tsx +84 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ContractReadMethods.tsx +43 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ContractUI.tsx +164 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ContractVariables.tsx +50 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ContractWriteMethods.tsx +49 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/DisplayVariable.tsx +85 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/InheritanceTooltip.tsx +14 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/ReadOnlyFunctionForm.tsx +102 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/Tuple.tsx +44 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/TupleArray.tsx +142 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/TxReceipt.tsx +42 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/WriteOnlyFunctionForm.tsx +144 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/index.tsx +8 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/utilsContract.tsx +166 -0
- package/templates/base/packages/nextjs/app/debug/_components/contract/utilsDisplay.tsx +114 -0
- package/templates/base/packages/nextjs/app/debug/page.tsx +14 -0
- package/templates/base/packages/nextjs/app/layout.tsx +67 -0
- package/templates/base/packages/nextjs/app/not-found.tsx +16 -0
- package/templates/base/packages/nextjs/app/page.tsx +94 -0
- package/templates/base/packages/nextjs/components/Background.tsx +37 -0
- package/templates/base/packages/nextjs/components/Card.tsx +40 -0
- package/templates/base/packages/nextjs/components/Footer.tsx +93 -0
- package/templates/base/packages/nextjs/components/Header.tsx +114 -0
- package/templates/base/packages/nextjs/components/ScaffoldEthAppWithProviders.tsx +77 -0
- package/templates/base/packages/nextjs/components/SwitchTheme.tsx +41 -0
- package/templates/base/packages/nextjs/components/ThemeProvider.tsx +13 -0
- package/templates/base/packages/nextjs/components/assets/BuidlGuidlLogo.tsx +18 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Address/Address.tsx +187 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Address/AddressCopyIcon.tsx +23 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Address/AddressLinkWrapper.tsx +29 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Balance.tsx +75 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/BlockieAvatar.tsx +17 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Faucet.tsx +131 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/FaucetButton.tsx +75 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/AddressInput.tsx +120 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/Bytes32Input.tsx +31 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/BytesInput.tsx +28 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/EtherInput.tsx +128 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/InputBase.tsx +66 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/IntegerInput.tsx +63 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/index.ts +9 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/Input/utils.ts +109 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/AddressInfoDropdown.tsx +121 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/AddressQRCodeModal.tsx +33 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/BurnerWalletModal.tsx +63 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/NetworkOptions.tsx +48 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/WrongNetworkDropdown.tsx +32 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/RainbowKitCustomConnectButton/index.tsx +89 -0
- package/templates/base/packages/nextjs/components/scaffold-eth/index.tsx +7 -0
- package/templates/base/packages/nextjs/contracts/deployedContracts.ts +9 -0
- package/templates/base/packages/nextjs/contracts/externalContracts.ts +16 -0
- package/templates/base/packages/nextjs/eslint.config.mjs +32 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/index.ts +17 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useAnimationConfig.ts +20 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useContractLogs.ts +40 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useCopyToClipboard.ts +19 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useDeployedContractInfo.ts +86 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useDisplayUsdMode.ts +21 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useFetchBlocks.ts +133 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useInitializeNativeCurrencyPrice.ts +32 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useNativeCurrencyPrice.ts +34 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useNetworkColor.ts +22 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useOutsideClick.ts +23 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useScaffoldContract.ts +65 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts +213 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useScaffoldReadContract.ts +80 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useScaffoldWatchContractEvent.ts +40 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useScaffoldWriteContract.ts +191 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useSelectedNetwork.ts +18 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useTargetNetwork.ts +23 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useTransactor.tsx +114 -0
- package/templates/base/packages/nextjs/hooks/scaffold-eth/useWatchBalance.ts +21 -0
- package/templates/base/packages/nextjs/icons/CompassIcon.tsx +39 -0
- package/templates/base/packages/nextjs/icons/DarkBugAntIcon.tsx +30 -0
- package/templates/base/packages/nextjs/icons/LightBugAntIcon.tsx +52 -0
- package/templates/base/packages/nextjs/next-env.d.ts +5 -0
- package/templates/base/packages/nextjs/next.config.js +19 -0
- package/templates/base/packages/nextjs/package.json +58 -0
- package/templates/base/packages/nextjs/postcss.config.js +6 -0
- package/templates/base/packages/nextjs/public/debug-image.png +0 -0
- package/templates/base/packages/nextjs/public/favicon.png +0 -0
- package/templates/base/packages/nextjs/public/logo.svg +8 -0
- package/templates/base/packages/nextjs/public/manifest.json +5 -0
- package/templates/base/packages/nextjs/public/thumbnail.jpg +0 -0
- package/templates/base/packages/nextjs/react-copy-to-clipboard.d.ts +44 -0
- package/templates/base/packages/nextjs/scaffold.config.ts +56 -0
- package/templates/base/packages/nextjs/services/store/store.ts +39 -0
- package/templates/base/packages/nextjs/services/web3/wagmiConfig.tsx +44 -0
- package/templates/base/packages/nextjs/services/web3/wagmiConnectors.tsx +51 -0
- package/templates/base/packages/nextjs/styles/globals.css +80 -0
- package/templates/base/packages/nextjs/tailwind.config.js +97 -0
- package/templates/base/packages/nextjs/tsconfig.json +28 -0
- package/templates/base/packages/nextjs/types/abitype/abi.d.ts +16 -0
- package/templates/base/packages/nextjs/types/utils.ts +3 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/block.ts +17 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/common.ts +8 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/contract.ts +352 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/contractsData.ts +11 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/decodeTxData.ts +65 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts +72 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/getMetadata.ts +50 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/getParsedError.ts +35 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/index.ts +6 -0
- package/templates/base/packages/nextjs/utils/scaffold-eth/notification.tsx +90 -0
- package/templates/base/packages/nextjs/utils/scaffold-stylus/burner.ts +59 -0
- package/templates/base/packages/nextjs/utils/scaffold-stylus/chain.ts +42 -0
- package/templates/base/packages/nextjs/utils/scaffold-stylus/index.ts +3 -0
- package/templates/base/packages/nextjs/utils/scaffold-stylus/networks.ts +94 -0
- package/templates/base/packages/nextjs/vercel.json +3 -0
- package/templates/base/packages/stylus/.env.example +13 -0
- package/templates/base/packages/stylus/.eslintrc.js +23 -0
- package/templates/base/packages/stylus/.gitignore.template.mjs +7 -0
- package/templates/base/packages/stylus/README.md +263 -0
- package/templates/base/packages/stylus/header.png +0 -0
- package/templates/base/packages/stylus/jest.config.js +15 -0
- package/templates/base/packages/stylus/package.json +49 -0
- package/templates/base/packages/stylus/scripts/deploy.ts +29 -0
- package/templates/base/packages/stylus/scripts/deploy_all_contracts.ts +59 -0
- package/templates/base/packages/stylus/scripts/deploy_contract.ts +93 -0
- package/templates/base/packages/stylus/scripts/deploy_wrapper.ts +79 -0
- package/templates/base/packages/stylus/scripts/export_abi.ts +87 -0
- package/templates/base/packages/stylus/scripts/index.ts +0 -0
- package/templates/base/packages/stylus/scripts/new_module.sh +35 -0
- package/templates/base/packages/stylus/scripts/test_network.ts +31 -0
- package/templates/base/packages/stylus/scripts/utils/command.ts +165 -0
- package/templates/base/packages/stylus/scripts/utils/contract.ts +219 -0
- package/templates/base/packages/stylus/scripts/utils/deployment.ts +136 -0
- package/templates/base/packages/stylus/scripts/utils/index.ts +6 -0
- package/templates/base/packages/stylus/scripts/utils/network.ts +112 -0
- package/templates/base/packages/stylus/scripts/utils/type.ts +48 -0
- package/templates/base/packages/stylus/scripts/utils.ts +3 -0
- package/templates/base/packages/stylus/tsconfig.json +41 -0
- package/templates/base/packages/stylus/your-contract/.cargo/config.toml +18 -0
- package/templates/base/packages/stylus/your-contract/Cargo.lock +5744 -0
- package/templates/base/packages/stylus/your-contract/Cargo.toml +46 -0
- package/templates/base/packages/stylus/your-contract/examples/counter.rs +78 -0
- package/templates/base/packages/stylus/your-contract/header.png +0 -0
- package/templates/base/packages/stylus/your-contract/licenses/Apache-2.0 +201 -0
- package/templates/base/packages/stylus/your-contract/licenses/COPYRIGHT.md +5 -0
- package/templates/base/packages/stylus/your-contract/licenses/DCO.txt +34 -0
- package/templates/base/packages/stylus/your-contract/licenses/MIT +21 -0
- package/templates/base/packages/stylus/your-contract/rust-toolchain.toml +2 -0
- package/templates/base/packages/stylus/your-contract/src/lib.rs +211 -0
- package/templates/base/packages/stylus/your-contract/src/main.rs +10 -0
- package/templates/base/readme.md +187 -0
- package/templates/base/yarn.lock +17860 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "your-contract"
|
|
3
|
+
version = "0.1.11"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "MIT OR Apache-2.0"
|
|
6
|
+
homepage = "https://github.com/OffchainLabs/stylus-hello-world"
|
|
7
|
+
repository = "https://github.com/OffchainLabs/stylus-hello-world"
|
|
8
|
+
keywords = ["arbitrum", "ethereum", "stylus", "alloy"]
|
|
9
|
+
description = "Stylus hello world example"
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
alloy-primitives = "=0.8.20"
|
|
13
|
+
alloy-sol-types = "=0.8.20"
|
|
14
|
+
stylus-sdk = "0.9.0"
|
|
15
|
+
hex = { version = "0.4", default-features = false }
|
|
16
|
+
|
|
17
|
+
[dev-dependencies]
|
|
18
|
+
alloy-primitives = { version = "=0.8.20", features = ["sha3-keccak"] }
|
|
19
|
+
tokio = { version = "1.12.0", features = ["full"] }
|
|
20
|
+
ethers = "2.0"
|
|
21
|
+
eyre = "0.6.8"
|
|
22
|
+
stylus-sdk = { version = "0.9.0", features = ["stylus-test"] }
|
|
23
|
+
dotenv = "0.15.0"
|
|
24
|
+
|
|
25
|
+
[features]
|
|
26
|
+
default = ["mini-alloc"]
|
|
27
|
+
export-abi = ["stylus-sdk/export-abi"]
|
|
28
|
+
debug = ["stylus-sdk/debug"]
|
|
29
|
+
mini-alloc = ["stylus-sdk/mini-alloc"]
|
|
30
|
+
|
|
31
|
+
[[bin]]
|
|
32
|
+
name = "your-contract"
|
|
33
|
+
path = "src/main.rs"
|
|
34
|
+
|
|
35
|
+
[lib]
|
|
36
|
+
crate-type = ["lib", "cdylib"]
|
|
37
|
+
|
|
38
|
+
[profile.release]
|
|
39
|
+
codegen-units = 1
|
|
40
|
+
strip = true
|
|
41
|
+
lto = true
|
|
42
|
+
panic = "abort"
|
|
43
|
+
|
|
44
|
+
# If you need to reduce the binary size, it is advisable to try other
|
|
45
|
+
# optimization levels, such as "s" and "z"
|
|
46
|
+
opt-level = 3
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
//! Example on how to interact with a deployed `stylus-hello-world` contract using defaults.
|
|
2
|
+
//! This example uses ethers-rs to instantiate the contract using a Solidity ABI.
|
|
3
|
+
//! Then, it attempts to check the current counter value, increment it via a tx,
|
|
4
|
+
//! and check the value again. The deployed contract is fully written in Rust and compiled to WASM
|
|
5
|
+
//! but with Stylus, it is accessible just as a normal Solidity smart contract is via an ABI.
|
|
6
|
+
|
|
7
|
+
use dotenv::dotenv;
|
|
8
|
+
use ethers::{
|
|
9
|
+
middleware::SignerMiddleware,
|
|
10
|
+
prelude::abigen,
|
|
11
|
+
providers::{Http, Middleware, Provider},
|
|
12
|
+
signers::{LocalWallet, Signer},
|
|
13
|
+
types::Address,
|
|
14
|
+
};
|
|
15
|
+
use eyre::eyre;
|
|
16
|
+
use std::io::{BufRead, BufReader};
|
|
17
|
+
use std::str::FromStr;
|
|
18
|
+
use std::sync::Arc;
|
|
19
|
+
|
|
20
|
+
/// Your private key file path.
|
|
21
|
+
const PRIV_KEY_PATH: &str = "PRIV_KEY_PATH";
|
|
22
|
+
|
|
23
|
+
/// Stylus RPC endpoint url.
|
|
24
|
+
const RPC_URL: &str = "RPC_URL";
|
|
25
|
+
|
|
26
|
+
/// Deployed pragram address.
|
|
27
|
+
const STYLUS_CONTRACT_ADDRESS: &str = "STYLUS_CONTRACT_ADDRESS";
|
|
28
|
+
|
|
29
|
+
#[tokio::main]
|
|
30
|
+
async fn main() -> eyre::Result<()> {
|
|
31
|
+
dotenv().ok();
|
|
32
|
+
let priv_key_path =
|
|
33
|
+
std::env::var(PRIV_KEY_PATH).map_err(|_| eyre!("No {} env var set", PRIV_KEY_PATH))?;
|
|
34
|
+
let rpc_url = std::env::var(RPC_URL).map_err(|_| eyre!("No {} env var set", RPC_URL))?;
|
|
35
|
+
let contract_address = std::env::var(STYLUS_CONTRACT_ADDRESS)
|
|
36
|
+
.map_err(|_| eyre!("No {} env var set", STYLUS_CONTRACT_ADDRESS))?;
|
|
37
|
+
abigen!(
|
|
38
|
+
Counter,
|
|
39
|
+
r#"[
|
|
40
|
+
function number() external view returns (uint256)
|
|
41
|
+
function setNumber(uint256 number) external
|
|
42
|
+
function increment() external
|
|
43
|
+
]"#
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
let provider = Provider::<Http>::try_from(rpc_url)?;
|
|
47
|
+
let address: Address = contract_address.parse()?;
|
|
48
|
+
|
|
49
|
+
let privkey = read_secret_from_file(&priv_key_path)?;
|
|
50
|
+
let wallet = LocalWallet::from_str(&privkey)?;
|
|
51
|
+
let chain_id = provider.get_chainid().await?.as_u64();
|
|
52
|
+
let client = Arc::new(SignerMiddleware::new(
|
|
53
|
+
provider,
|
|
54
|
+
wallet.clone().with_chain_id(chain_id),
|
|
55
|
+
));
|
|
56
|
+
|
|
57
|
+
let counter = Counter::new(address, client);
|
|
58
|
+
let num = counter.number().call().await;
|
|
59
|
+
println!("Counter number value = {:?}", num);
|
|
60
|
+
|
|
61
|
+
let pending = counter.increment();
|
|
62
|
+
if let Some(receipt) = pending.send().await?.await? {
|
|
63
|
+
println!("Receipt = {:?}", receipt);
|
|
64
|
+
}
|
|
65
|
+
println!("Successfully incremented counter via a tx");
|
|
66
|
+
|
|
67
|
+
let num = counter.number().call().await;
|
|
68
|
+
println!("New counter number value = {:?}", num);
|
|
69
|
+
Ok(())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn read_secret_from_file(fpath: &str) -> eyre::Result<String> {
|
|
73
|
+
let f = std::fs::File::open(fpath)?;
|
|
74
|
+
let mut buf_reader = BufReader::new(f);
|
|
75
|
+
let mut secret = String::new();
|
|
76
|
+
buf_reader.read_line(&mut secret)?;
|
|
77
|
+
Ok(secret.trim().to_string())
|
|
78
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2023 YOUR COMPANY
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Licensing Information
|
|
2
|
+
|
|
3
|
+
Copyright 2023 YOUR COMPANY
|
|
4
|
+
|
|
5
|
+
Except as otherwise noted (below and/or in individual files), this project is licensed under the Apache License, Version 2.0 ([`LICENSE-APACHE`](Apache-2.0) or http://www.apache.org/licenses/LICENSE-2.0) or the MIT license, ([`LICENSE-MIT`](MIT) or http://opensource.org/licenses/MIT), at your option.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
Developer Certificate of Origin
|
|
2
|
+
Version 1.1
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
7
|
+
license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
Developer's Certificate of Origin 1.1
|
|
11
|
+
|
|
12
|
+
By making a contribution to this project, I certify that:
|
|
13
|
+
|
|
14
|
+
(a) The contribution was created in whole or in part by me and I
|
|
15
|
+
have the right to submit it under the open source license
|
|
16
|
+
indicated in the file; or
|
|
17
|
+
|
|
18
|
+
(b) The contribution is based upon previous work that, to the best
|
|
19
|
+
of my knowledge, is covered under an appropriate open source
|
|
20
|
+
license and I have the right under that license to submit that
|
|
21
|
+
work with modifications, whether created in whole or in part
|
|
22
|
+
by me, under the same open source license (unless I am
|
|
23
|
+
permitted to submit under a different license), as indicated
|
|
24
|
+
in the file; or
|
|
25
|
+
|
|
26
|
+
(c) The contribution was provided directly to me by some other
|
|
27
|
+
person who certified (a), (b) or (c) and I have not modified
|
|
28
|
+
it.
|
|
29
|
+
|
|
30
|
+
(d) I understand and agree that this project and the contribution
|
|
31
|
+
are public and that a record of the contribution (including all
|
|
32
|
+
personal information I submit with it, including my sign-off) is
|
|
33
|
+
maintained indefinitely and may be redistributed consistent with
|
|
34
|
+
this project or the open source license(s) involved.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2023 YOUR COMPANY
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
//!
|
|
2
|
+
//! YourContract in Stylus Rust
|
|
3
|
+
//!
|
|
4
|
+
//! A smart contract that allows changing a state variable of the contract and tracking the changes
|
|
5
|
+
//! It also allows the owner to withdraw the Ether in the contract
|
|
6
|
+
//!
|
|
7
|
+
//! This is the Stylus Rust equivalent of the Solidity YourContract.
|
|
8
|
+
//!
|
|
9
|
+
|
|
10
|
+
// Allow `cargo stylus export-abi` to generate a main function.
|
|
11
|
+
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]
|
|
12
|
+
#![cfg_attr(not(any(test, feature = "export-abi")), no_std)]
|
|
13
|
+
|
|
14
|
+
#[macro_use]
|
|
15
|
+
extern crate alloc;
|
|
16
|
+
|
|
17
|
+
use alloc::string::String;
|
|
18
|
+
use alloc::vec::Vec;
|
|
19
|
+
|
|
20
|
+
/// Import items from the SDK. The prelude contains common traits and macros.
|
|
21
|
+
use stylus_sdk::{
|
|
22
|
+
alloy_primitives::{Address, U256},
|
|
23
|
+
prelude::*,
|
|
24
|
+
alloy_sol_types::sol,
|
|
25
|
+
stylus_core::log,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/// Helper macro for require-like assertions
|
|
29
|
+
macro_rules! require {
|
|
30
|
+
($condition:expr, $message:expr) => {
|
|
31
|
+
if !$condition {
|
|
32
|
+
panic!($message);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Define the GreetingChange event
|
|
38
|
+
sol! {
|
|
39
|
+
event GreetingChange(address indexed greetingSetter, string newGreeting, bool premium, uint256 value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Define persistent storage using the Solidity ABI.
|
|
43
|
+
// `YourContract` will be the entrypoint.
|
|
44
|
+
sol_storage! {
|
|
45
|
+
#[entrypoint]
|
|
46
|
+
pub struct YourContract {
|
|
47
|
+
address owner;
|
|
48
|
+
string greeting;
|
|
49
|
+
bool premium;
|
|
50
|
+
uint256 total_counter;
|
|
51
|
+
mapping(address => uint256) user_greeting_counter;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/// Declare that `YourContract` is a contract with the following external methods.
|
|
56
|
+
#[public]
|
|
57
|
+
impl YourContract {
|
|
58
|
+
/// Constructor equivalent - initializes the contract
|
|
59
|
+
pub fn init(&mut self, owner: Address) {
|
|
60
|
+
self.owner.set(owner);
|
|
61
|
+
self.greeting.set_str("Building Unstoppable Apps!!!");
|
|
62
|
+
self.premium.set(false);
|
|
63
|
+
self.total_counter.set(U256::ZERO);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// Gets the owner address
|
|
67
|
+
pub fn owner(&self) -> Address {
|
|
68
|
+
self.owner.get()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// Gets the current greeting
|
|
72
|
+
pub fn greeting(&self) -> String {
|
|
73
|
+
self.greeting.get_string()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// Gets the premium status
|
|
77
|
+
pub fn premium(&self) -> bool {
|
|
78
|
+
self.premium.get()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/// Gets the total counter
|
|
82
|
+
pub fn total_counter(&self) -> U256 {
|
|
83
|
+
self.total_counter.get()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/// Gets the user greeting counter for a specific address
|
|
87
|
+
pub fn user_greeting_counter(&self, user: Address) -> U256 {
|
|
88
|
+
self.user_greeting_counter.get(user)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Function that allows anyone to change the state variable "greeting" of the contract and increase the counters
|
|
92
|
+
#[payable]
|
|
93
|
+
pub fn set_greeting(&mut self, new_greeting: String) {
|
|
94
|
+
// Change state variables
|
|
95
|
+
self.greeting.set_str(&new_greeting);
|
|
96
|
+
|
|
97
|
+
// Increment counters
|
|
98
|
+
let current_total = self.total_counter.get();
|
|
99
|
+
self.total_counter.set(current_total + U256::from(1));
|
|
100
|
+
|
|
101
|
+
let sender: Address = self.vm().msg_sender();
|
|
102
|
+
let current_user_count = self.user_greeting_counter.get(sender);
|
|
103
|
+
self.user_greeting_counter.insert(sender, current_user_count + U256::from(1));
|
|
104
|
+
|
|
105
|
+
// Set premium based on msg.value
|
|
106
|
+
let msg_value = self.vm().msg_value();
|
|
107
|
+
let is_premium = msg_value > U256::ZERO;
|
|
108
|
+
self.premium.set(is_premium);
|
|
109
|
+
|
|
110
|
+
// Emit the event
|
|
111
|
+
log(self.vm(),
|
|
112
|
+
GreetingChange {
|
|
113
|
+
greetingSetter: sender,
|
|
114
|
+
newGreeting: new_greeting,
|
|
115
|
+
premium: is_premium,
|
|
116
|
+
value: msg_value,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Function that allows the owner to withdraw all the Ether in the contract
|
|
121
|
+
/// The function can only be called by the owner of the contract
|
|
122
|
+
pub fn withdraw(&mut self) -> Result<(), Vec<u8>> {
|
|
123
|
+
// Check if caller is owner
|
|
124
|
+
let sender: Address = self.vm().msg_sender() ;
|
|
125
|
+
let owner: Address = self.owner.get();
|
|
126
|
+
require!(sender == owner, "Not the Owner");
|
|
127
|
+
|
|
128
|
+
// Get contract balance and transfer to owner using transfer_eth
|
|
129
|
+
let balance = self.vm().balance(sender);
|
|
130
|
+
if balance > U256::ZERO {
|
|
131
|
+
let _ = self.vm().transfer_eth(owner, balance);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
Ok(())
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/// Allow contract to receive ETH (equivalent to receive() function)
|
|
138
|
+
#[payable]
|
|
139
|
+
pub fn receive_ether(&self) {
|
|
140
|
+
// This function allows the contract to receive ETH
|
|
141
|
+
// The #[payable] attribute allows it to accept value
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// #[cfg(test)]
|
|
146
|
+
// mod test {
|
|
147
|
+
// use super::*;
|
|
148
|
+
// use stylus_sdk::testing::*;
|
|
149
|
+
|
|
150
|
+
// #[test]
|
|
151
|
+
// fn test_your_contract() {
|
|
152
|
+
// let vm = TestVM::default();
|
|
153
|
+
// let mut contract = YourContract::from(&vm);
|
|
154
|
+
|
|
155
|
+
// // Test initialization
|
|
156
|
+
// let owner_addr = Address::from([1u8; 20]);
|
|
157
|
+
// contract.init(owner_addr);
|
|
158
|
+
|
|
159
|
+
// assert_eq!(contract.owner(), owner_addr);
|
|
160
|
+
// assert_eq!(contract.greeting(), "Building Unstoppable Apps!!!");
|
|
161
|
+
// assert_eq!(contract.premium(), false);
|
|
162
|
+
// assert_eq!(contract.total_counter(), U256::ZERO);
|
|
163
|
+
|
|
164
|
+
// // Test setting greeting without payment
|
|
165
|
+
// contract.set_greeting("Hello World".to_string());
|
|
166
|
+
// assert_eq!(contract.greeting(), "Hello World");
|
|
167
|
+
// assert_eq!(contract.premium(), false);
|
|
168
|
+
// assert_eq!(contract.total_counter(), U256::from(1));
|
|
169
|
+
|
|
170
|
+
// // Test user greeting counter
|
|
171
|
+
// let sender = vm.addr();
|
|
172
|
+
// assert_eq!(contract.user_greeting_counter(sender), U256::from(1));
|
|
173
|
+
|
|
174
|
+
// // Test setting greeting with payment
|
|
175
|
+
// vm.set_value(U256::from(100));
|
|
176
|
+
// contract.set_greeting("Premium Hello".to_string());
|
|
177
|
+
// assert_eq!(contract.greeting(), "Premium Hello");
|
|
178
|
+
// assert_eq!(contract.premium(), true);
|
|
179
|
+
// assert_eq!(contract.total_counter(), U256::from(2));
|
|
180
|
+
// assert_eq!(contract.user_greeting_counter(sender), U256::from(2));
|
|
181
|
+
// }
|
|
182
|
+
|
|
183
|
+
// #[test]
|
|
184
|
+
// fn test_withdraw() {
|
|
185
|
+
// let vm = TestVM::default();
|
|
186
|
+
// let mut contract = YourContract::from(&vm);
|
|
187
|
+
|
|
188
|
+
// // Initialize with owner
|
|
189
|
+
// let owner_addr = vm.addr(); // Use VM address as owner for testing
|
|
190
|
+
// contract.init(owner_addr);
|
|
191
|
+
|
|
192
|
+
// // Test withdraw (in real scenario, contract would have received ETH)
|
|
193
|
+
// let result = contract.withdraw();
|
|
194
|
+
// assert!(result.is_ok()); // Should not panic since caller is owner
|
|
195
|
+
// }
|
|
196
|
+
|
|
197
|
+
// #[test]
|
|
198
|
+
// fn test_withdraw_not_owner() {
|
|
199
|
+
// let vm = TestVM::default();
|
|
200
|
+
// let mut contract = YourContract::from(&vm);
|
|
201
|
+
|
|
202
|
+
// // Initialize with different owner
|
|
203
|
+
// let owner_addr = Address::from([1u8; 20]);
|
|
204
|
+
// contract.init(owner_addr);
|
|
205
|
+
|
|
206
|
+
// // This test should panic with "Not the Owner" when withdraw is called by non-owner
|
|
207
|
+
// // In a real test environment, you'd want to test this more carefully
|
|
208
|
+
// // but for now we'll just verify the function exists
|
|
209
|
+
// assert_eq!(contract.owner(), owner_addr);
|
|
210
|
+
// }
|
|
211
|
+
// }
|