Jelajahi Sumber

importing open zeppelin

Ayush Suresh 3 bulan lalu
induk
melakukan
4438f86c8e

+ 35 - 18
target_chains/stylus/Cargo.lock

@@ -469,9 +469,9 @@ dependencies = [
 
 [[package]]
 name = "alloy-sol-macro"
-version = "0.8.25"
+version = "0.8.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e10ae8e9a91d328ae954c22542415303919aabe976fe7a92eb06db1b68fd59f2"
+checksum = "13f28f2131dc3a7b8e2cda882758ad4d5231ca26281b9861d4b18c700713e2da"
 dependencies = [
  "alloy-sol-macro-expander",
  "alloy-sol-macro-input",
@@ -483,9 +483,9 @@ dependencies = [
 
 [[package]]
 name = "alloy-sol-macro-expander"
-version = "0.8.25"
+version = "0.8.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "83ad5da86c127751bc607c174d6c9fe9b85ef0889a9ca0c641735d77d4f98f26"
+checksum = "1ee2da033256a3b27131c030933eab0460a709fbcc4d4bd57bf9a5650b2441c5"
 dependencies = [
  "alloy-sol-macro-input",
  "const-hex",
@@ -501,14 +501,13 @@ dependencies = [
 
 [[package]]
 name = "alloy-sol-macro-input"
-version = "0.8.25"
+version = "0.8.20"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ba3d30f0d3f9ba3b7686f3ff1de9ee312647aac705604417a2f40c604f409a9e"
+checksum = "4e9d9918b0abb632818bf27e2dfb86b209be8433baacf22100b190bbc0904bd4"
 dependencies = [
  "const-hex",
  "dunce",
  "heck",
- "macro-string",
  "proc-macro2",
  "quote",
  "syn 2.0.101",
@@ -3150,17 +3149,6 @@ dependencies = [
  "hashbrown 0.15.3",
 ]
 
-[[package]]
-name = "macro-string"
-version = "0.1.4"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1b27834086c65ec3f9387b096d66e99f221cf081c2b738042aa252bcd41204e3"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.101",
-]
-
 [[package]]
 name = "md-5"
 version = "0.10.6"
@@ -3499,6 +3487,34 @@ dependencies = [
  "vcpkg",
 ]
 
+[[package]]
+name = "openzeppelin-stylus"
+version = "0.3.0-alpha.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bcca485edc671a34ef24d98a8c5c776bfcb15671ad9a70b62b8b215b29c3f62f"
+dependencies = [
+ "alloy-primitives 0.8.20",
+ "alloy-sol-macro",
+ "alloy-sol-macro-expander",
+ "alloy-sol-macro-input",
+ "alloy-sol-types",
+ "keccak-const",
+ "openzeppelin-stylus-proc",
+ "stylus-sdk",
+]
+
+[[package]]
+name = "openzeppelin-stylus-proc"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f592a114a2f3bbfb29303435064b4444ea3d9c4283b667f551a8543e8da8896f"
+dependencies = [
+ "convert_case 0.6.0",
+ "proc-macro2",
+ "quote",
+ "syn 2.0.101",
+]
+
 [[package]]
 name = "option-ext"
 version = "0.2.0"
@@ -3904,6 +3920,7 @@ dependencies = [
  "hex",
  "mini-alloc 0.4.2",
  "motsu",
+ "openzeppelin-stylus",
  "pyth-receiver-stylus",
  "pythnet-sdk",
  "stylus-sdk",

+ 1 - 0
target_chains/stylus/contracts/pyth-proxy/Cargo.toml

@@ -13,6 +13,7 @@ alloy-sol-types = "=0.8.20"
 stylus-sdk = "0.9.0"
 mini-alloc = "0.4.2"
 hex = { version = "0.4", default-features = false }
+openzeppelin-stylus = { version = "0.3.0-alpha.1" }
 
 [dev-dependencies]
 motsu = "0.9.0"

+ 101 - 65
target_chains/stylus/contracts/pyth-proxy/src/lib.rs

@@ -1,86 +1,122 @@
 #![cfg_attr(not(any(test, feature = "export-abi")), no_main)]
 #![cfg_attr(not(any(test, feature = "export-abi")), no_std)]
-
-#[macro_use]
+#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]
 extern crate alloc;
 
-#[cfg(test)]
-mod end_to_end_proxy_tests;
-#[cfg(test)]
-mod proxy_integration_tests;
-
 use alloc::vec::Vec;
 
-use stylus_sdk::{alloy_primitives::Address, call::delegate_call, prelude::*};
+use openzeppelin_stylus::proxy::IProxy;
+use stylus_sdk::{alloy_primitives::Address, prelude::*, storage::StorageAddress, ArbResult};
 
-sol_storage! {
-    #[entrypoint]
-    pub struct Proxy {
-        bool is_initialized;
-        MetaInformation meta_information;
-    }
-
-    pub struct MetaInformation {
-        address owner;
-        address implementation_address;
-    }
+#[entrypoint]
+#[storage]
+struct ProxyExample {
+    implementation: StorageAddress,
 }
 
 #[public]
-impl Proxy {
-    pub fn init(&mut self, owner: Address) -> Result<(), Vec<u8>> {
-        if self.is_initialized.get() {
-            return Err(b"Already initialized".to_vec());
-        }
-        self.meta_information.owner.set(owner);
-        self.is_initialized.set(true);
-        Ok(())
+impl ProxyExample {
+    #[constructor]
+    pub fn constructor(&mut self, implementation: Address) {
+        self.implementation.set(implementation);
     }
 
-    pub fn get_implementation(&self) -> Result<Address, Vec<u8>> {
-        let addr = self.meta_information.implementation_address.get();
-        if addr == Address::ZERO {
-            return Err(b"Implementation not set".to_vec());
-        }
-        Ok(addr)
+    fn implementation(&self) -> Result<Address, Vec<u8>> {
+        IProxy::implementation(self)
     }
 
-    pub fn set_implementation(&mut self, implementation: Address) -> Result<(), Vec<u8>> {
-        self.only_owner()?;
-        if implementation == Address::ZERO {
-            return Err(b"Invalid implementation address".to_vec());
-        }
-        self.meta_information
-            .implementation_address
-            .set(implementation);
-        Ok(())
+    #[fallback]
+    fn fallback(&mut self, calldata: &[u8]) -> ArbResult {
+        unsafe { self.do_fallback(calldata) }
     }
+}
 
-    pub fn get_owner(&self) -> Address {
-        self.meta_information.owner.get()
+unsafe impl IProxy for ProxyExample {
+    fn implementation(&self) -> Result<Address, Vec<u8>> {
+        Ok(self.implementation.get())
     }
+}
 
-    pub fn is_initialized(&self) -> bool {
-        self.is_initialized.get()
-    }
+// #[macro_use]
+// extern crate alloc;
 
-    #[payable]
-    pub fn relay_to_implementation(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
-        let implementation_address = self.get_implementation()?;
-        let res;
-        unsafe { res = delegate_call(self, implementation_address, &data[..]) };
+// #[cfg(test)]
+// mod end_to_end_proxy_tests;
+// #[cfg(test)]
+// mod proxy_integration_tests;
 
-        match res {
-            Ok(res) => Ok(res.into()),
-            Err(e) => Err(format!("Delegate call failed: {:?}", e).into()),
-        }
-    }
+// use alloc::vec::Vec;
 
-    fn only_owner(&self) -> Result<(), Vec<u8>> {
-        let owner = self.meta_information.owner.get();
-        if owner != self.vm().msg_sender() {
-            return Err(b"Unauthorized: not owner".to_vec());
-        }
-        Ok(())
-    }
-}
+// use stylus_sdk::{alloy_primitives::Address, call::delegate_call, prelude::*};
+
+// sol_storage! {
+//     #[entrypoint]
+//     pub struct Proxy {
+//         bool is_initialized;
+//         MetaInformation meta_information;
+//     }
+
+//     pub struct MetaInformation {
+//         address owner;
+//         address implementation_address;
+//     }
+// }
+
+// #[public]
+// impl Proxy {
+//     pub fn init(&mut self, owner: Address) -> Result<(), Vec<u8>> {
+//         if self.is_initialized.get() {
+//             return Err(b"Already initialized".to_vec());
+//         }
+//         self.meta_information.owner.set(owner);
+//         self.is_initialized.set(true);
+//         Ok(())
+//     }
+
+//     pub fn get_implementation(&self) -> Result<Address, Vec<u8>> {
+//         let addr = self.meta_information.implementation_address.get();
+//         if addr == Address::ZERO {
+//             return Err(b"Implementation not set".to_vec());
+//         }
+//         Ok(addr)
+//     }
+
+//     pub fn set_implementation(&mut self, implementation: Address) -> Result<(), Vec<u8>> {
+//         self.only_owner()?;
+//         if implementation == Address::ZERO {
+//             return Err(b"Invalid implementation address".to_vec());
+//         }
+//         self.meta_information
+//             .implementation_address
+//             .set(implementation);
+//         Ok(())
+//     }
+
+//     pub fn get_owner(&self) -> Address {
+//         self.meta_information.owner.get()
+//     }
+
+//     pub fn is_initialized(&self) -> bool {
+//         self.is_initialized.get()
+//     }
+
+//     #[payable]
+//     pub fn relay_to_implementation(&mut self, data: Vec<u8>) -> Result<Vec<u8>, Vec<u8>> {
+//         let implementation_address = self.get_implementation()?;
+//         let res;
+//         unsafe { res = delegate_call(self, implementation_address, &data[..]) };
+
+//         match res {
+//             Ok(res) => Ok(res.into()),
+//             Err(e) => Err(format!("Delegate call failed: {:?}", e).into()),
+//         }
+//     }
+
+//     fn only_owner(&self) -> Result<(), Vec<u8>> {
+//         let owner = self.meta_information.owner.get();
+//         if owner != self.vm().msg_sender() {
+//             return Err(b"Unauthorized: not owner".to_vec());
+//         }
+//         Ok(())
+//     }
+// }