Skip to content

Commit

Permalink
Proper weight charging
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzuk-neti committed Jun 25, 2024
1 parent d05db42 commit 7234ccc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions liberland-extension/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ std = [
"sp-runtime/std",
"frame-system/std",
"log/std",
"pallet-llm/std",
]
25 changes: 17 additions & 8 deletions liberland-extension/runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::traits::OriginTrait;
use log::{error, trace};
use pallet_contracts::chain_extension::{ChainExtension, Environment, Ext, InitState, RetVal};
use sp_runtime::DispatchError;
Expand All @@ -23,7 +22,8 @@ impl LiberlandExtension {
env: Environment<E, InitState>,
) -> Result<RetVal, DispatchError>
where
E::T: pallet_llm::Config,
E::T: pallet_llm::Config + pallet_contracts::Config,
<E::T as pallet_contracts::Config>::RuntimeCall: From<pallet_llm::Call<E::T>>,
{
trace!(
target: "runtime",
Expand All @@ -32,18 +32,27 @@ impl LiberlandExtension {
let mut env = env.buf_in_buf_out();
let args: LLMForceTransferArguments<E::T> = env.read_as()?;
let ext = env.ext();
let contract_address = ext.address();
let origin =
<E::T as frame_system::Config>::RuntimeOrigin::signed(contract_address.clone());
pallet_llm::Pallet::<E::T>::force_transfer(origin, args.from, args.to, args.amount)?;
let call: <E::T as pallet_contracts::Config>::RuntimeCall =
pallet_llm::Call::<E::T>::force_transfer {
from: args.from,
to: args.to,
amount: args.amount,
}
.into();
ext.call_runtime(call).map_err(|e| e.error)?;
Ok(RetVal::Converging(0))
}
}

impl<T: pallet_contracts::Config + pallet_llm::Config> ChainExtension<T> for LiberlandExtension {
impl<T> ChainExtension<T> for LiberlandExtension
where
T: pallet_llm::Config + pallet_contracts::Config,
<T as pallet_contracts::Config>::RuntimeCall: From<pallet_llm::Call<T>>,
{
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>
where
E::T: pallet_llm::Config,
E::T: pallet_llm::Config + pallet_contracts::Config,
<E::T as pallet_contracts::Config>::RuntimeCall: From<pallet_llm::Call<E::T>>,
{
let func_id = env.func_id();
match func_id {
Expand Down

0 comments on commit 7234ccc

Please sign in to comment.