diff --git a/runtime-sdk/src/modules/accounts/mod.rs b/runtime-sdk/src/modules/accounts/mod.rs index bda154829b..bb69f7ef70 100644 --- a/runtime-sdk/src/modules/accounts/mod.rs +++ b/runtime-sdk/src/modules/accounts/mod.rs @@ -212,6 +212,11 @@ pub trait API { amount: &token::BaseUnits, ) -> Result<(), modules::core::Error>; + fn mint_into_fee_accumulator( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error>; + /// Move amount from fee accumulator into address. fn move_from_fee_accumulator( ctx: &mut C, @@ -587,6 +592,21 @@ impl API for Module { Ok(()) } + fn mint_into_fee_accumulator( + ctx: &mut C, + amount: &token::BaseUnits, + ) -> Result<(), modules::core::Error> { + if ctx.is_simulation() { + return Ok(()); + } + + ctx.value::(CONTEXT_KEY_FEE_ACCUMULATOR) + .or_default() + .add(amount); + + Ok(()) + } + fn move_from_fee_accumulator( ctx: &mut C, to: Address, diff --git a/runtime-sdk/src/modules/consensus_accounts/mod.rs b/runtime-sdk/src/modules/consensus_accounts/mod.rs index f16ce0c711..4d133dc51b 100644 --- a/runtime-sdk/src/modules/consensus_accounts/mod.rs +++ b/runtime-sdk/src/modules/consensus_accounts/mod.rs @@ -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::{ @@ -20,9 +26,10 @@ use crate::{ storage::Prefix, types::{ address::Address, + in_msg::IncomingMessageData, message::{MessageEvent, MessageEventHookInvocation}, token, - transaction::AuthInfo, + transaction::{AuthInfo, Transaction}, }, }; @@ -416,6 +423,37 @@ impl { } +impl + module::IncomingMessageHandler for Module +{ + fn prefetch_in_msg( + prefixes: &mut BTreeSet, + _in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> Result<(), CoreError> { + // todo: their account + Ok(()) + } + + fn execute_in_msg( + ctx: &mut C, + in_msg: &roothash::IncomingMessage, + _data: &IncomingMessageData, + _tx: &Option, + ) -> 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 module::BlockHandler for Module {