Skip to content

Commit

Permalink
modules/consensus_accounts: handle incoming message tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Mar 11, 2022
1 parent 3e5a88d commit d7092d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
20 changes: 20 additions & 0 deletions runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ pub trait API {
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error>;

fn mint_into_fee_accumulator<C: Context>(
ctx: &mut C,
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error>;

/// Move amount from fee accumulator into address.
fn move_from_fee_accumulator<C: Context>(
ctx: &mut C,
Expand Down Expand Up @@ -587,6 +592,21 @@ impl API for Module {
Ok(())
}

fn mint_into_fee_accumulator<C: Context>(
ctx: &mut C,
amount: &token::BaseUnits,
) -> Result<(), modules::core::Error> {
if ctx.is_simulation() {
return Ok(());
}

ctx.value::<FeeAccumulator>(CONTEXT_KEY_FEE_ACCUMULATOR)
.or_default()
.add(amount);

Ok(())
}

fn move_from_fee_accumulator<C: Context>(
ctx: &mut C,
to: Address,
Expand Down
42 changes: 40 additions & 2 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
//! while keeping track of amount deposited per account.
use std::{collections::BTreeSet, convert::TryInto};

use num_traits::Zero;
use once_cell::sync::Lazy;
use thiserror::Error;

use oasis_core_runtime::consensus::staking::Account as ConsensusAccount;
use oasis_core_runtime::{
consensus::{
staking::Account as ConsensusAccount,
roothash,
},
};
use oasis_runtime_sdk_macros::{handler, sdk_derive};

use crate::{
Expand All @@ -20,9 +26,10 @@ use crate::{
storage::Prefix,
types::{
address::Address,
in_msg::IncomingMessageData,
message::{MessageEvent, MessageEventHookInvocation},
token,
transaction::AuthInfo,
transaction::{AuthInfo, Transaction},
},
};

Expand Down Expand Up @@ -416,6 +423,37 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
{
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
module::IncomingMessageHandler for Module<Accounts, Consensus>
{
fn prefetch_in_msg(
prefixes: &mut BTreeSet<Prefix>,
_in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), CoreError> {
// todo: their account
Ok(())
}

fn execute_in_msg<C: Context>(
ctx: &mut C,
in_msg: &roothash::IncomingMessage,
_data: &IncomingMessageData,
_tx: &Option<Transaction>,
) -> Result<(), CoreError> {
if !in_msg.fee.is_zero() {
Accounts::mint_into_fee_accumulator(ctx, &token::BaseUnits(Consensus::amount_from_consensus(in_msg.fee.try_into()?)?, Consensus::consensus_denomination(ctx)?))?;
// TODO: Emit event that fee has been paid.
}
if !in_msg.tokens.is_zero() {
Accounts::mint(ctx, in_msg.caller.into(), &token::BaseUnits(Consensus::amount_from_consensus(in_msg.tokens.into())?, Consensus::consensus_denomination(ctx)?))?;
// TODO: Emit event.
}
Ok(())
}
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> module::BlockHandler
for Module<Accounts, Consensus>
{
Expand Down

0 comments on commit d7092d8

Please sign in to comment.