From 64b63c272826f38516ff8c080e45bbbc99f2ff33 Mon Sep 17 00:00:00 2001 From: Dorin Marian Iancu Date: Tue, 22 Oct 2024 12:15:06 +0300 Subject: [PATCH] timestamp sc --- Cargo.lock | 18 ++ Cargo.toml | 2 + common/common_structs/src/alias_types.rs | 1 + .../timestamp-oracle/Cargo.toml | 21 ++ .../timestamp-oracle/meta/Cargo.toml | 12 + .../timestamp-oracle/meta/src/main.rs | 3 + .../timestamp-oracle/multiversx.json | 3 + .../src/epoch_to_timestamp.rs | 29 +++ .../timestamp-oracle/src/lib.rs | 21 ++ .../timestamp-oracle/wasm/Cargo.lock | 228 ++++++++++++++++++ .../timestamp-oracle/wasm/Cargo.toml | 34 +++ .../timestamp-oracle/wasm/src/lib.rs | 27 +++ 12 files changed, 399 insertions(+) create mode 100644 energy-integration/timestamp-oracle/Cargo.toml create mode 100644 energy-integration/timestamp-oracle/meta/Cargo.toml create mode 100644 energy-integration/timestamp-oracle/meta/src/main.rs create mode 100644 energy-integration/timestamp-oracle/multiversx.json create mode 100644 energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs create mode 100644 energy-integration/timestamp-oracle/src/lib.rs create mode 100644 energy-integration/timestamp-oracle/wasm/Cargo.lock create mode 100644 energy-integration/timestamp-oracle/wasm/Cargo.toml create mode 100644 energy-integration/timestamp-oracle/wasm/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 724f6ca9d..0abeab928 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1907,6 +1907,24 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "timestamp-oracle" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", + "multiversx-sc-scenario", + "num-bigint", +] + +[[package]] +name = "timestamp-oracle-meta" +version = "0.0.0" +dependencies = [ + "multiversx-sc-meta-lib", + "timestamp-oracle", +] + [[package]] name = "token-unstake" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 81e48c99e..501361734 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,8 @@ members = [ "energy-integration/fees-collector/meta", "energy-integration/governance-v2", "energy-integration/governance-v2/meta", + "energy-integration/timestamp-oracle", + "energy-integration/timestamp-oracle/meta", "farm-staking/farm-staking", "farm-staking/farm-staking/meta", diff --git a/common/common_structs/src/alias_types.rs b/common/common_structs/src/alias_types.rs index 0bec7be83..77dd8e63d 100644 --- a/common/common_structs/src/alias_types.rs +++ b/common/common_structs/src/alias_types.rs @@ -6,6 +6,7 @@ pub type Nonce = u64; pub type Epoch = u64; pub type Week = usize; pub type Percent = u64; +pub type Timestamp = u64; pub type PaymentsVec = ManagedVec>; pub type UnlockPeriod = UnlockSchedule; pub type OldLockedTokenAttributes = LockedAssetTokenAttributesEx; diff --git a/energy-integration/timestamp-oracle/Cargo.toml b/energy-integration/timestamp-oracle/Cargo.toml new file mode 100644 index 000000000..60343c636 --- /dev/null +++ b/energy-integration/timestamp-oracle/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "timestamp-oracle" +version = "0.0.0" +publish = false +edition = "2021" +authors = ["you"] + +[lib] +path = "src/lib.rs" + +[dependencies.multiversx-sc] +version = "=0.53.2" + +[dependencies.common_structs] +path = "../../common/common_structs" + +[dev-dependencies] +num-bigint = "0.4" + +[dev-dependencies.multiversx-sc-scenario] +version = "=0.53.2" diff --git a/energy-integration/timestamp-oracle/meta/Cargo.toml b/energy-integration/timestamp-oracle/meta/Cargo.toml new file mode 100644 index 000000000..6cd336331 --- /dev/null +++ b/energy-integration/timestamp-oracle/meta/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "timestamp-oracle-meta" +version = "0.0.0" +edition = "2021" +publish = false + +[dependencies.timestamp-oracle] +path = ".." + +[dependencies.multiversx-sc-meta-lib] +version = "=0.53.2" +default-features = false diff --git a/energy-integration/timestamp-oracle/meta/src/main.rs b/energy-integration/timestamp-oracle/meta/src/main.rs new file mode 100644 index 000000000..d04e592b8 --- /dev/null +++ b/energy-integration/timestamp-oracle/meta/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + multiversx_sc_meta_lib::cli_main::(); +} diff --git a/energy-integration/timestamp-oracle/multiversx.json b/energy-integration/timestamp-oracle/multiversx.json new file mode 100644 index 000000000..736553962 --- /dev/null +++ b/energy-integration/timestamp-oracle/multiversx.json @@ -0,0 +1,3 @@ +{ + "language": "rust" +} \ No newline at end of file diff --git a/energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs b/energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs new file mode 100644 index 000000000..03efdd69d --- /dev/null +++ b/energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs @@ -0,0 +1,29 @@ +use common_structs::{Epoch, Timestamp}; + +multiversx_sc::imports!(); + +#[multiversx_sc::module] +pub trait EpochToTimestampModule { + #[endpoint(updateAndGetTimestampStartEpoch)] + fn update_and_get_timestamp_start_epoch(&self) -> Timestamp { + let current_epoch = self.blockchain().get_block_epoch(); + let last_update_epoch = self.epoch_last_interaction().get(); + if current_epoch == last_update_epoch { + return self.timestamp_start_epoch_last_interaction().get(); + } + + self.epoch_last_interaction().set(current_epoch); + + let current_timestamp = self.blockchain().get_block_timestamp(); + self.timestamp_start_epoch_last_interaction() + .set(current_timestamp); + + current_timestamp + } + + #[storage_mapper("epochLastInteraction")] + fn epoch_last_interaction(&self) -> SingleValueMapper; + + #[storage_mapper("timestampStartEpochLastInter")] + fn timestamp_start_epoch_last_interaction(&self) -> SingleValueMapper; +} diff --git a/energy-integration/timestamp-oracle/src/lib.rs b/energy-integration/timestamp-oracle/src/lib.rs new file mode 100644 index 000000000..7616e9dde --- /dev/null +++ b/energy-integration/timestamp-oracle/src/lib.rs @@ -0,0 +1,21 @@ +#![no_std] + +use common_structs::Timestamp; + +multiversx_sc::imports!(); + +pub mod epoch_to_timestamp; + +#[multiversx_sc::contract] +pub trait TimestampOracle: epoch_to_timestamp::EpochToTimestampModule { + #[init] + fn init(&self, current_epoch_start_timestamp: Timestamp) { + let current_epoch = self.blockchain().get_block_epoch(); + self.epoch_last_interaction().set(current_epoch); + self.timestamp_start_epoch_last_interaction() + .set(current_epoch_start_timestamp); + } + + #[upgrade] + fn upgrade(&self) {} +} diff --git a/energy-integration/timestamp-oracle/wasm/Cargo.lock b/energy-integration/timestamp-oracle/wasm/Cargo.lock new file mode 100644 index 000000000..420c97dca --- /dev/null +++ b/energy-integration/timestamp-oracle/wasm/Cargo.lock @@ -0,0 +1,228 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "common_structs" +version = "0.0.0" +dependencies = [ + "fixed-supply-token", + "math", + "mergeable", + "multiversx-sc", + "unwrappable", +] + +[[package]] +name = "endian-type" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c34f04666d835ff5d62e058c3995147c06f42fe86ff053337632bca83e42702d" + +[[package]] +name = "fixed-supply-token" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + +[[package]] +name = "math" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "mergeable" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "multiversx-sc" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75ea89a26f0aacda21437a8ae5ccfbefab99d8191942b3d2eddbcbf84f9866d7" +dependencies = [ + "bitflags", + "hex-literal", + "multiversx-sc-codec", + "multiversx-sc-derive", + "num-traits", + "unwrap-infallible", +] + +[[package]] +name = "multiversx-sc-codec" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "007d7a5a8534e5dc9128cb8f15a65a21dd378e135c6016c7cd1491cd012bc8cb" +dependencies = [ + "arrayvec", + "multiversx-sc-codec-derive", + "unwrap-infallible", +] + +[[package]] +name = "multiversx-sc-codec-derive" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dffba1dce273ed5b61ee1b90aeea5c8c744617d0f12624f620768c144d83e753" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "multiversx-sc-derive" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c17fdf90fafca2f19085ae67b0502d9f71bf8ab1be3c83808eb88e02a8c18b9" +dependencies = [ + "hex", + "proc-macro2", + "quote", + "radix_trie", + "syn", +] + +[[package]] +name = "multiversx-sc-wasm-adapter" +version = "0.53.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20659915a4377d375c46d7f237e810053a03f7e084fad6362dd5748a7233defb" +dependencies = [ + "multiversx-sc", +] + +[[package]] +name = "nibble_vec" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a5d83df9f36fe23f0c3648c6bbb8b0298bb5f1939c8f2704431371f4b84d43" +dependencies = [ + "smallvec", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radix_trie" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c069c179fcdc6a2fe24d8d18305cf085fdbd4f922c041943e203685d6a1c58fd" +dependencies = [ + "endian-type", + "nibble_vec", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "timestamp-oracle" +version = "0.0.0" +dependencies = [ + "common_structs", + "multiversx-sc", +] + +[[package]] +name = "timestamp-oracle-wasm" +version = "0.0.0" +dependencies = [ + "multiversx-sc-wasm-adapter", + "timestamp-oracle", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "unwrap-infallible" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "151ac09978d3c2862c4e39b557f4eceee2cc72150bc4cb4f16abf061b6e381fb" + +[[package]] +name = "unwrappable" +version = "0.0.0" +dependencies = [ + "multiversx-sc", +] diff --git a/energy-integration/timestamp-oracle/wasm/Cargo.toml b/energy-integration/timestamp-oracle/wasm/Cargo.toml new file mode 100644 index 000000000..b0d2c4c85 --- /dev/null +++ b/energy-integration/timestamp-oracle/wasm/Cargo.toml @@ -0,0 +1,34 @@ +# Code generated by the multiversx-sc build system. DO NOT EDIT. + +# ########################################## +# ############## AUTO-GENERATED ############# +# ########################################## + +[package] +name = "timestamp-oracle-wasm" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ["cdylib"] + +[profile.release] +codegen-units = 1 +opt-level = "z" +lto = true +debug = false +panic = "abort" +overflow-checks = false + +[profile.dev] +panic = "abort" + +[dependencies.timestamp-oracle] +path = ".." + +[dependencies.multiversx-sc-wasm-adapter] +version = "=0.53.2" + +[workspace] +members = ["."] diff --git a/energy-integration/timestamp-oracle/wasm/src/lib.rs b/energy-integration/timestamp-oracle/wasm/src/lib.rs new file mode 100644 index 000000000..a8caeb09e --- /dev/null +++ b/energy-integration/timestamp-oracle/wasm/src/lib.rs @@ -0,0 +1,27 @@ +// Code generated by the multiversx-sc build system. DO NOT EDIT. + +//////////////////////////////////////////////////// +////////////////// AUTO-GENERATED ////////////////// +//////////////////////////////////////////////////// + +// Init: 1 +// Upgrade: 1 +// Endpoints: 1 +// Async Callback (empty): 1 +// Total number of exported functions: 4 + +#![no_std] + +multiversx_sc_wasm_adapter::allocator!(); +multiversx_sc_wasm_adapter::panic_handler!(); + +multiversx_sc_wasm_adapter::endpoints! { + timestamp_oracle + ( + init => init + upgrade => upgrade + updateAndGetTimestampStartEpoch => update_and_get_timestamp_start_epoch + ) +} + +multiversx_sc_wasm_adapter::async_callback_empty! {}