|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#![cfg_attr(target_arch = "wasm32", no_main)] |
| 5 | + |
| 6 | +mod state; |
| 7 | + |
| 8 | +use linera_sdk::{ |
| 9 | + linera_base_types::WithContractAbi, |
| 10 | + views::{RootView, View}, |
| 11 | + Contract, ContractRuntime, |
| 12 | +}; |
| 13 | +use time_expiry::{TimeExpiryAbi, TimeExpiryOperation}; |
| 14 | + |
| 15 | +use self::state::TimeExpiryState; |
| 16 | + |
| 17 | +pub struct TimeExpiryContract { |
| 18 | + state: TimeExpiryState, |
| 19 | + runtime: ContractRuntime<Self>, |
| 20 | +} |
| 21 | + |
| 22 | +linera_sdk::contract!(TimeExpiryContract); |
| 23 | + |
| 24 | +impl WithContractAbi for TimeExpiryContract { |
| 25 | + type Abi = TimeExpiryAbi; |
| 26 | +} |
| 27 | + |
| 28 | +impl Contract for TimeExpiryContract { |
| 29 | + type Message = (); |
| 30 | + type InstantiationArgument = (); |
| 31 | + type Parameters = (); |
| 32 | + type EventValue = (); |
| 33 | + |
| 34 | + async fn load(runtime: ContractRuntime<Self>) -> Self { |
| 35 | + let state = TimeExpiryState::load(runtime.root_view_storage_context()) |
| 36 | + .await |
| 37 | + .expect("Failed to load state"); |
| 38 | + TimeExpiryContract { state, runtime } |
| 39 | + } |
| 40 | + |
| 41 | + async fn instantiate(&mut self, _argument: ()) { |
| 42 | + self.runtime.application_parameters(); |
| 43 | + } |
| 44 | + |
| 45 | + async fn execute_operation(&mut self, operation: TimeExpiryOperation) { |
| 46 | + let TimeExpiryOperation::ExpireAfter(delta) = operation; |
| 47 | + |
| 48 | + // Get the current block timestamp. |
| 49 | + let block_timestamp = self.runtime.system_time(); |
| 50 | + |
| 51 | + // Calculate the expiry timestamp. |
| 52 | + let expiry_timestamp = block_timestamp.saturating_add(delta); |
| 53 | + |
| 54 | + // Assert that the validation happens before the expiry timestamp. |
| 55 | + self.runtime.assert_before(expiry_timestamp); |
| 56 | + } |
| 57 | + |
| 58 | + async fn execute_message(&mut self, _message: ()) { |
| 59 | + panic!("TimeExpiry application doesn't support any cross-chain messages"); |
| 60 | + } |
| 61 | + |
| 62 | + async fn store(mut self) { |
| 63 | + self.state.save().await.expect("Failed to save state"); |
| 64 | + } |
| 65 | +} |
0 commit comments