Rename palette -> frame (#41)

This commit is contained in:
Andrew Jones
2019-11-22 18:35:36 +00:00
committed by GitHub
parent 0b39bbbc30
commit 55e067a294
10 changed files with 27 additions and 27 deletions
+116
View File
@@ -0,0 +1,116 @@
//! Implements support for the pallet_balances module.
use crate::{
error::Error,
frame::{
Call,
system::System,
},
Client,
};
use futures::future::{
self,
Future,
};
use parity_scale_codec::{Encode, Codec};
use runtime_primitives::traits::{
MaybeSerialize,
Member,
SimpleArithmetic,
};
use runtime_support::Parameter;
use std::fmt::Debug;
/// The subset of the `pallet_balances::Trait` that a client must implement.
pub trait Balances: System {
/// The balance of an account.
type Balance: Parameter
+ Member
+ SimpleArithmetic
+ Codec
+ Default
+ Copy
+ MaybeSerialize
+ Debug
+ From<<Self as System>::BlockNumber>;
}
/// Blanket impl for using existing runtime types
impl<T: frame_system::Trait + pallet_balances::Trait + Debug> Balances for T
where
<T as frame_system::Trait>::Header: serde::de::DeserializeOwned,
{
type Balance = T::Balance;
}
/// The Balances extension trait for the Client.
pub trait BalancesStore {
/// Balances type.
type Balances: Balances;
/// The 'free' balance of a given account.
///
/// This is the only balance that matters in terms of most operations on
/// tokens. It alone is used to determine the balance when in the contract
/// execution environment. When this balance falls below the value of
/// `ExistentialDeposit`, then the 'current account' is deleted:
/// specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback
/// is invoked, giving a chance to external modules to clean up data
/// associated with the deleted account.
///
/// `system::AccountNonce` is also deleted if `ReservedBalance` is also
/// zero. It also gets collapsed to zero if it ever becomes less than
/// `ExistentialDeposit`.
fn free_balance(
&self,
account_id: <Self::Balances as System>::AccountId,
) -> Box<dyn Future<Item = <Self::Balances as Balances>::Balance, Error = Error> + Send>;
}
impl<T: Balances + 'static, S: 'static> BalancesStore for Client<T, S> {
type Balances = T;
fn free_balance(
&self,
account_id: <Self::Balances as System>::AccountId,
) -> Box<dyn Future<Item = <Self::Balances as Balances>::Balance, Error = Error> + Send>
{
let free_balance_map = || {
Ok(self
.metadata()
.module("Balances")?
.storage("FreeBalance")?
.get_map::<
<Self::Balances as System>::AccountId,
<Self::Balances as Balances>::Balance>()?)
};
let map = match free_balance_map() {
Ok(map) => map,
Err(err) => return Box::new(future::err(err)),
};
Box::new(self.fetch_or(map.key(account_id), map.default()))
}
}
const MODULE: &str = "Balances";
const TRANSFER: &str = "transfer";
/// Arguments for transferring a balance
#[derive(Encode)]
pub struct TransferArgs<T: Balances> {
to: <T as System>::Address,
#[codec(compact)]
amount: <T as Balances>::Balance
}
/// Transfer some liquid free balance to another account.
///
/// `transfer` will set the `FreeBalance` of the sender and receiver.
/// It will decrease the total issuance of the system by the `TransferFee`.
/// If the sender's account is below the existential deposit as a result
/// of the transfer, the account will be reaped.
pub fn transfer<T: Balances>(
to: <T as System>::Address,
amount: <T as Balances>::Balance,
) -> Call<TransferArgs<T>> {
Call::new(MODULE, TRANSFER, TransferArgs { to, amount })
}
+95
View File
@@ -0,0 +1,95 @@
//! Implements support for the pallet_contracts module.
use crate::{
frame::{
Call,
balances::Balances,
system::System,
},
};
use parity_scale_codec::Encode;
const MODULE: &str = "Contracts";
const PUT_CODE: &str = "put_code";
const CREATE: &str = "create";
const CALL: &str = "call";
/// Gas units are chosen to be represented by u64 so that gas metering
/// instructions can operate on them efficiently.
pub type Gas = u64;
/// The subset of the `pallet_contracts::Trait` that a client must implement.
pub trait Contracts: System + Balances {}
/// Arguments for uploading contract code to the chain
#[derive(Encode)]
pub struct PutCodeArgs {
#[codec(compact)]
gas_limit: Gas,
code: Vec<u8>,
}
/// Arguments for creating an instance of a contract
#[derive(Encode)]
pub struct CreateArgs<T: Contracts> {
endowment: <T as Balances>::Balance,
#[codec(compact)]
gas_limit: Gas,
code_hash: <T as System>::Hash,
data: Vec<u8>,
}
/// Arguments for calling a contract
#[derive(Encode)]
pub struct CallArgs<T: Contracts> {
dest: <T as System>::Address,
value: <T as Balances>::Balance,
#[codec(compact)]
gas_limit: Gas,
data: Vec<u8>,
}
/// Stores the given binary Wasm code into the chain's storage and returns
/// its `codehash`.
/// You can instantiate contracts only with stored code.
pub fn put_code(gas_limit: Gas, code: Vec<u8>) -> Call<PutCodeArgs> {
Call::new(MODULE, PUT_CODE, PutCodeArgs { gas_limit, code })
}
/// Creates a new contract from the `codehash` generated by `put_code`,
/// optionally transferring some balance.
///
/// Creation is executed as follows:
///
/// - The destination address is computed based on the sender and hash of
/// the code.
/// - The smart-contract account is created at the computed address.
/// - The `ctor_code` is executed in the context of the newly-created
/// account. Buffer returned after the execution is saved as the `code`https://www.bbc.co.uk/
/// of the account. That code will be invoked upon any call received by
/// this account.
/// - The contract is initialized.
pub fn create<T: Contracts>(
endowment: <T as Balances>::Balance,
gas_limit: Gas,
code_hash: <T as System>::Hash,
data: Vec<u8>,
) -> Call<CreateArgs<T>> {
Call::new(MODULE, CREATE, CreateArgs { endowment, gas_limit, code_hash, data })
}
/// Makes a call to an account, optionally transferring some balance.
///
/// * If the account is a smart-contract account, the associated code will
/// be executed and any value will be transferred.
/// * If the account is a regular account, any value will be transferred.
/// * If no account exists and the call value is not less than
/// `existential_deposit`, a regular account will be created and any value
/// will be transferred.
pub fn call<T: Contracts>(
dest: <T as System>::Address,
value: <T as Balances>::Balance,
gas_limit: Gas,
data: Vec<u8>,
) -> Call<CallArgs<T>> {
Call::new(MODULE, CALL, CallArgs { dest, value, gas_limit, data })
}
+20
View File
@@ -0,0 +1,20 @@
//! Implements support for built-in runtime modules.
use parity_scale_codec::Encode;
pub mod balances;
pub mod contracts;
pub mod system;
/// Creates module calls
pub struct Call<T: Encode> {
pub module: &'static str,
pub function: &'static str,
pub args: T,
}
impl<T: Encode> Call<T> {
pub fn new(module: &'static str, function: &'static str, args: T) -> Self {
Call { module, function, args }
}
}
+159
View File
@@ -0,0 +1,159 @@
//! Implements support for the frame_system module.
use crate::{
error::Error,
frame::{
Call,
balances::Balances,
},
Client,
};
use futures::future::{
self,
Future,
};
use parity_scale_codec::Codec;
use runtime_primitives::traits::{
Bounded,
CheckEqual,
Hash,
Header,
MaybeDisplay,
MaybeSerialize,
MaybeSerializeDeserialize,
Member,
SimpleArithmetic,
SimpleBitOps,
StaticLookup,
};
use runtime_support::Parameter;
use serde::de::DeserializeOwned;
use std::fmt::Debug;
/// The subset of the `frame::Trait` that a client must implement.
pub trait System: 'static + Eq + Clone + Debug {
/// Account index (aka nonce) type. This stores the number of previous
/// transactions associated with a sender account.
type Index: Parameter
+ Member
+ MaybeSerialize
+ Debug
+ Default
+ MaybeDisplay
+ SimpleArithmetic
+ Copy;
/// The block number type used by the runtime.
type BlockNumber: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ MaybeDisplay
+ SimpleArithmetic
+ Default
+ Bounded
+ Copy
+ std::hash::Hash;
/// The output of the `Hashing` function.
type Hash: Parameter
+ Member
+ MaybeSerializeDeserialize
+ Debug
+ MaybeDisplay
+ SimpleBitOps
+ Default
+ Copy
+ CheckEqual
+ std::hash::Hash
+ AsRef<[u8]>
+ AsMut<[u8]>;
/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
type Hashing: Hash<Output = Self::Hash>;
/// The user account identifier type for the runtime.
type AccountId: Parameter
+ Member
+ MaybeSerialize
+ Debug
+ MaybeDisplay
+ Ord
+ Default;
/// The address type. This instead of `<frame_system::Trait::Lookup as StaticLookup>::Source`.
type Address: Codec + Clone + PartialEq + Debug;
/// The block header.
type Header: Parameter
+ Header<Number = Self::BlockNumber, Hash = Self::Hash>
+ DeserializeOwned;
}
/// Blanket impl for using existing runtime types
impl<T: frame_system::Trait + Debug> System for T
where
<T as frame_system::Trait>::Header: serde::de::DeserializeOwned,
{
type Index = T::Index;
type BlockNumber = T::BlockNumber;
type Hash = T::Hash;
type Hashing = T::Hashing;
type AccountId = T::AccountId;
type Address = <T::Lookup as StaticLookup>::Source;
type Header = T::Header;
}
/// The System extension trait for the Client.
pub trait SystemStore {
/// System type.
type System: System;
/// Returns the account nonce for an account_id.
fn account_nonce(
&self,
account_id: <Self::System as System>::AccountId,
) -> Box<dyn Future<Item = <Self::System as System>::Index, Error = Error> + Send>;
}
impl<T: System + Balances + 'static, S: 'static> SystemStore for Client<T, S> {
type System = T;
fn account_nonce(
&self,
account_id: <Self::System as System>::AccountId,
) -> Box<dyn Future<Item = <Self::System as System>::Index, Error = Error> + Send>
{
let account_nonce_map = || {
Ok(self
.metadata
.module("System")?
.storage("AccountNonce")?
.get_map()?)
};
let map = match account_nonce_map() {
Ok(map) => map,
Err(err) => return Box::new(future::err(err)),
};
Box::new(self.fetch_or(map.key(account_id), map.default()))
}
}
const MODULE: &str = "System";
const SET_CODE: &str = "set_code";
/// Arguments for updating the runtime code
pub type SetCode = Vec<u8>;
/// Sets the new code.
pub fn set_code(code: Vec<u8>) -> Call<SetCode> {
Call::new(MODULE, SET_CODE, code)
}
/// Event for the System module.
#[derive(Clone, Debug, parity_scale_codec::Decode)]
pub enum SystemEvent {
/// An extrinsic completed successfully.
ExtrinsicSuccess,
/// An extrinsic failed.
ExtrinsicFailed(runtime_primitives::DispatchError),
}