Skip to content

Commit

Permalink
Feature/xcontracts (paritytech#106)
Browse files Browse the repository at this point in the history
* add contracts module and remove rent part

* add contracts into runtime Cargo.toml

* use xrml_assets as Currency impl

* add xrc20 convert interface

* fix ensure_with_errorlog in bitcoin module

* and xrc20 call for runtime-api and rpc

* integrate xcontracts into runtime and add genesis
  • Loading branch information
atenjin authored Jun 19, 2020
1 parent be12db2 commit 6fd9679
Show file tree
Hide file tree
Showing 52 changed files with 13,542 additions and 54 deletions.
155 changes: 153 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ frame-support = { git = "https://github.com/paritytech/substrate.git", tag = "v2
pallet-transaction-payment-rpc = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc3" }
substrate-frame-rpc-system = { git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc3" }

xrml-contracts-rpc = { path = "../xrml/contracts/rpc" }
xrml-contracts-rpc-runtime-api = { path = "../xrml/contracts/rpc/runtime-api" }

chainx-primitives = { path = "../primitives" }
chainx-runtime = { path = "../runtime" }

Expand Down
6 changes: 6 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ chainx-primitives = { path = "../primitives", default-features = false }

xrml-assets = { path = "../xrml/assets", default-features = false }
xrml-bridge-bitcoin = { path = "../xrml/bridge/bitcoin", default-features = false }
xrml-contracts = { path = "../xrml/contracts", default-features = false }
xrml-contracts-primitives = { path = "../xrml/contracts/common", default-features = false }
xrml-contracts-rpc-runtime-api = { path = "../xrml/contracts/rpc/runtime-api", default-features = false }

[build-dependencies]
wasm-builder-runner = { package = "substrate-wasm-builder-runner", git = "https://github.com/paritytech/substrate.git", tag = "v2.0.0-rc3", default-features = false }
Expand Down Expand Up @@ -81,4 +84,7 @@ std = [

"xrml-assets/std",
"xrml-bridge-bitcoin/std",
"xrml-contracts/std",
"xrml-contracts-primitives/std",
"xrml-contracts-rpc-runtime-api/std",
]
63 changes: 61 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ use pallet_transaction_payment_rpc_runtime_api::RuntimeDispatchInfo;
pub use chainx_primitives::{
AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Moment, Signature,
};

use xrml_contracts_rpc_runtime_api::ContractExecResult;
// A few exports that help ease life for downstream crates.
use chainx_primitives::Token;
pub use chainx_primitives::Token;
pub use frame_support::{
construct_runtime, parameter_types,
traits::{KeyOwnerProofSystem, Randomness},
Expand All @@ -49,6 +49,8 @@ pub use sp_runtime::{Perbill, Permill};
#[cfg(feature = "std")]
pub use xrml_bridge_bitcoin::h256_conv_endian_from_str;
pub use xrml_bridge_bitcoin::{BTCHeader, BTCNetwork, BTCParams, Compact, H256 as BTCHash};
pub use xrml_contracts::Schedule as ContractsSchedule;
pub use xrml_contracts_primitives::XRC20Selector;

impl_opaque_keys! {
pub struct SessionKeys {
Expand Down Expand Up @@ -226,6 +228,18 @@ impl xrml_bridge_bitcoin::Trait for Runtime {
type Event = Event;
}

impl xrml_contracts::Trait for Runtime {
type Time = Timestamp;
type Randomness = RandomnessCollectiveFlip;
type Call = Call;
type Event = Event;
type DetermineContractAddress = xrml_contracts::SimpleAddressDeterminer<Runtime>;
type TrieIdGenerator = xrml_contracts::TrieIdFromParentCounter<Runtime>;
type StorageSizeOffset = xrml_contracts::DefaultStorageSizeOffset;
type MaxDepth = xrml_contracts::DefaultMaxDepth;
type MaxValueSize = xrml_contracts::DefaultMaxValueSize;
}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand All @@ -242,6 +256,7 @@ construct_runtime!(
TransactionPayment: pallet_transaction_payment::{Module, Storage},
XAssets: xrml_assets::{Module, Call, Storage, Event<T>},
XBridgeBitcoin: xrml_bridge_bitcoin::{Module, Call, Storage, Event<T>, Config},
XContracts: xrml_contracts::{Module, Call, Config, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -402,4 +417,48 @@ impl_runtime_apis! {
TransactionPayment::query_info(uxt, len)
}
}

impl xrml_contracts_rpc_runtime_api::ContractsApi<Block, AccountId, Balance, BlockNumber>
for Runtime
{
fn call(
origin: AccountId,
dest: AccountId,
value: Balance,
gas_limit: u64,
input_data: Vec<u8>,
) -> ContractExecResult {
let exec_result =
XContracts::bare_call(origin, dest.into(), value, gas_limit, input_data);
match exec_result {
Ok(v) => ContractExecResult::Success {
status: v.status,
data: v.data,
},
Err(_) => ContractExecResult::Error,
}
}

fn get_storage(
address: AccountId,
key: [u8; 32],
) -> xrml_contracts_primitives::GetStorageResult {
XContracts::get_storage(address, key)
}

fn xrc20_call(
token: Token,
selector: XRC20Selector,
data: Vec<u8>,
) -> ContractExecResult {
let exec_result = XContracts::call_xrc20(token, selector, data);
match exec_result {
Ok(v) => ContractExecResult::Success {
status: v.status,
data: v.data,
},
Err(_) => ContractExecResult::Error,
}
}
}
}
12 changes: 9 additions & 3 deletions src/chain_spec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chainx_runtime::{
h256_conv_endian_from_str, AccountId, AuraConfig, BTCHeader, BTCNetwork, BTCParams, Compact,
GenesisConfig, GrandpaConfig, Signature, SudoConfig, SystemConfig, XBridgeBitcoinConfig,
WASM_BINARY,
ContractsSchedule, GenesisConfig, GrandpaConfig, Signature, SudoConfig, SystemConfig,
XBridgeBitcoinConfig, XContractsConfig, WASM_BINARY,
};
use sc_service::ChainType;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand Down Expand Up @@ -107,7 +107,7 @@ fn testnet_genesis(
initial_authorities: Vec<(AuraId, GrandpaId)>,
root_key: AccountId,
endowed_accounts: Vec<AccountId>,
_enable_println: bool,
enable_println: bool,
) -> GenesisConfig {
GenesisConfig {
frame_system: Some(SystemConfig {
Expand Down Expand Up @@ -158,5 +158,11 @@ fn testnet_genesis(
btc_withdrawal_fee: 500000,
max_withdrawal_count: 100,
}),
xrml_contracts: Some(XContractsConfig {
current_schedule: ContractsSchedule {
enable_println, // this should only be enabled on development chains
..Default::default()
},
}),
}
}
Loading

0 comments on commit 6fd9679

Please sign in to comment.