Skip to content

Commit

Permalink
timestamp sc
Browse files Browse the repository at this point in the history
  • Loading branch information
dorin-iancu committed Oct 22, 2024
1 parent af382ad commit 64b63c2
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions common/common_structs/src/alias_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M> = ManagedVec<M, EsdtTokenPayment<M>>;
pub type UnlockPeriod<M> = UnlockSchedule<M>;
pub type OldLockedTokenAttributes<M> = LockedAssetTokenAttributesEx<M>;
Expand Down
21 changes: 21 additions & 0 deletions energy-integration/timestamp-oracle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 12 additions & 0 deletions energy-integration/timestamp-oracle/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions energy-integration/timestamp-oracle/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta_lib::cli_main::<timestamp_oracle::AbiProvider>();
}
3 changes: 3 additions & 0 deletions energy-integration/timestamp-oracle/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
29 changes: 29 additions & 0 deletions energy-integration/timestamp-oracle/src/epoch_to_timestamp.rs
Original file line number Diff line number Diff line change
@@ -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<Epoch>;

#[storage_mapper("timestampStartEpochLastInter")]
fn timestamp_start_epoch_last_interaction(&self) -> SingleValueMapper<Timestamp>;
}
21 changes: 21 additions & 0 deletions energy-integration/timestamp-oracle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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) {}
}
228 changes: 228 additions & 0 deletions energy-integration/timestamp-oracle/wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 64b63c2

Please sign in to comment.