|
| 1 | +use alloy::{ |
| 2 | + network::EthereumWallet, primitives::Address, providers::ProviderBuilder, |
| 3 | + signers::local::PrivateKeySigner, sol, sol_types::SolConstructor, |
| 4 | +}; |
| 5 | +use koba::config::Deploy; |
| 6 | + |
| 7 | +sol!( |
| 8 | + #[sol(rpc)] |
| 9 | + contract BasicToken { |
| 10 | + constructor(string memory name_, string memory symbol_); |
| 11 | + |
| 12 | + function name() external view returns (string name); |
| 13 | + function symbol() external view returns (string symbol); |
| 14 | + } |
| 15 | +); |
| 16 | + |
| 17 | +const RPC_URL: &str = "https://sepolia-rollup.arbitrum.io/rpc"; |
| 18 | +const PRIVATE_KEY: &str = "your private key"; |
| 19 | +const TOKEN_NAME: &str = "Test Token"; |
| 20 | +const TOKEN_SYMBOL: &str = "TTK"; |
| 21 | + |
| 22 | +#[tokio::main] |
| 23 | +async fn main() { |
| 24 | + let contract_address = deploy().await; |
| 25 | + |
| 26 | + // WARNING: Please use a more secure method for storing your privaket key |
| 27 | + // than a string at the top of this file. The following code is for testing |
| 28 | + // purposes only. |
| 29 | + let signer = PRIVATE_KEY |
| 30 | + .parse::<PrivateKeySigner>() |
| 31 | + .expect("should parse the private key"); |
| 32 | + let wallet = EthereumWallet::from(signer); |
| 33 | + |
| 34 | + let rpc_url = RPC_URL.parse().expect("should parse rpc url"); |
| 35 | + let provider = ProviderBuilder::new() |
| 36 | + .with_recommended_fillers() |
| 37 | + .wallet(wallet) |
| 38 | + .on_http(rpc_url); |
| 39 | + |
| 40 | + let contract = BasicToken::new(contract_address, &provider); |
| 41 | + |
| 42 | + let call_result = contract.name().call().await.unwrap(); |
| 43 | + assert_eq!(call_result.name, TOKEN_NAME.to_owned()); |
| 44 | + |
| 45 | + let call_result = contract.symbol().call().await.unwrap(); |
| 46 | + assert_eq!(call_result.symbol, TOKEN_SYMBOL.to_owned()); |
| 47 | +} |
| 48 | + |
| 49 | +/// Deploy a `BasicToken` contract to `RPC_URL` using `koba`. |
| 50 | +async fn deploy() -> Address { |
| 51 | + let args = BasicToken::constructorCall { |
| 52 | + name_: TOKEN_NAME.to_owned(), |
| 53 | + symbol_: TOKEN_SYMBOL.to_owned(), |
| 54 | + }; |
| 55 | + let args = alloy::hex::encode(args.abi_encode()); |
| 56 | + |
| 57 | + let manifest_dir = |
| 58 | + std::env::current_dir().expect("should get current dir from env"); |
| 59 | + |
| 60 | + // NOTE: It's expected that you compiled your contract beforehand. |
| 61 | + // |
| 62 | + // You should run `cargo build --release --target wasm32-unknown-unknown` to |
| 63 | + // get a wasm binary at `target/wasm32-unknown-unknown/release/{name}.wasm`. |
| 64 | + let wasm_path = manifest_dir |
| 65 | + .join("target") |
| 66 | + .join("wasm32-unknown-unknown") |
| 67 | + .join("release") |
| 68 | + .join("basic_example.wasm"); |
| 69 | + let sol_path = manifest_dir |
| 70 | + .join("examples") |
| 71 | + .join("basic") |
| 72 | + .join("token") |
| 73 | + .join("src") |
| 74 | + .join("constructor.sol"); |
| 75 | + |
| 76 | + let config = Deploy { |
| 77 | + generate_config: koba::config::Generate { |
| 78 | + wasm: wasm_path.clone(), |
| 79 | + sol: sol_path, |
| 80 | + args: Some(args), |
| 81 | + legacy: false, |
| 82 | + }, |
| 83 | + auth: koba::config::PrivateKey { |
| 84 | + private_key_path: None, |
| 85 | + private_key: Some(PRIVATE_KEY.to_owned()), |
| 86 | + keystore_path: None, |
| 87 | + keystore_password_path: None, |
| 88 | + }, |
| 89 | + endpoint: RPC_URL.to_owned(), |
| 90 | + deploy_only: false, |
| 91 | + }; |
| 92 | + |
| 93 | + koba::deploy(&config).await.expect("should deploy contract") |
| 94 | +} |
0 commit comments