Last active
April 9, 2024 01:36
-
-
Save stackup-dev/91ce22ab477cb8d32f22c010e6e05909 to your computer and use it in GitHub Desktop.
Completed state.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Zefchain Labs, Inc. | |
// SPDX-License-Identifier: Apache-2.0 | |
use linera_sdk::base::{Amount, Owner}; | |
use linera_sdk::views::{linera_views, MapView, RootView, ViewStorageContext}; | |
use thiserror::Error; | |
#[derive(RootView)] | |
#[view(context = "ViewStorageContext")] | |
pub struct FungibleToken { | |
pub accounts: MapView<Owner, Amount>, | |
} | |
#[allow(dead_code)] | |
impl FungibleToken { | |
// INSERT fn initialize_accounts CODE HERE | |
pub async fn initialize_accounts(&mut self, account: Owner, amount: Amount) { | |
self.accounts | |
.insert(&account, amount) | |
.expect("Error in insert statement") | |
} | |
// INSERT fn balance CODE HERE | |
pub async fn balance(&self, account: &Owner) -> Amount { | |
self.accounts | |
.get(account) | |
.await | |
.expect("Failure in the retrieval") | |
.unwrap_or(Amount::ZERO) | |
} | |
// INSERT fn credit CODE HERE | |
pub async fn credit(&mut self, account: Owner, amount: Amount) { | |
let mut balance = self.balance(&account).await; | |
balance.saturating_add_assign(amount); | |
self.accounts | |
.insert(&account, balance) | |
.expect("Failed to insert"); | |
} | |
// INSERT fn debit CODE HERE | |
pub async fn debit( | |
&mut self, | |
account: Owner, | |
amount: Amount, | |
) -> Result<(), InsufficientBalanceError> { | |
let mut balance = self.balance(&account).await; | |
balance | |
.try_sub_assign(amount) | |
.map_err(|_| InsufficientBalanceError)?; | |
self.accounts | |
.insert(&account, balance) | |
.expect("Failed to insert"); | |
Ok(()) | |
} | |
} | |
// INSERT InsufficientBalanceError CODE HERE | |
#[derive(Clone, Copy, Debug, Error)] | |
#[error("Insufficient balance for transfer")] | |
pub struct InsufficientBalanceError; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment