diff --git a/contracts/src/auth.rs b/contracts/src/auth.rs
index a244526ddd83c..98b4ed65b2d98 100644
--- a/contracts/src/auth.rs
+++ b/contracts/src/auth.rs
@@ -15,13 +15,14 @@
// along with Polkadot. If not, see .
use primitives::Address;
+use primitives::hash::H256;
use state_machine::StaticExternalities;
use error::Result;
use executor::RustExecutor;
/// Data and some sort of Authentication Data
-type DataAndAuth = (Vec, Vec);
+type DataAndAuth = (H256, Vec);
/// Authentication contract rust implementation.
#[derive(Debug, Default)]
diff --git a/contracts/src/balances.rs b/contracts/src/balances.rs
index d5e3fd6b662fb..f9d5ece00f452 100644
--- a/contracts/src/balances.rs
+++ b/contracts/src/balances.rs
@@ -14,14 +14,25 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see .
-use primitives::Address;
+use primitives::contract::CallData;
use primitives::uint::U256;
-use state_machine::{Externalities, StaticExternalities};
+use primitives::{hash, Address};
+use serializer;
+use state_machine::{self, Externalities, StaticExternalities};
-use error::Result;
+use error::{Error, ErrorKind, Result, ResultExt};
use executor::RustExecutor;
-#[derive(Debug, Serialize, Deserialize)]
+/// Account entry
+#[derive(Default, Debug, Serialize, Deserialize)]
+struct Account {
+ /// Account balance
+ balance: U256,
+ /// Account nonce
+ nonce: U256,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transfer {
/// Transfer value
value: U256,
@@ -33,18 +44,36 @@ pub struct Transfer {
authentication_data: Vec,
}
+fn externalities_error(e: E) -> Error {
+ ErrorKind::Externalities(Box::new(e)).into()
+}
+
+fn address(x: u8) -> Address {
+ [x].as_ref().into()
+}
+
+fn account>(ext: &E, address: Address) -> Result {
+ ext.storage(&address.into())
+ .map_err(externalities_error)
+ .and_then(|x| if x.is_empty() {
+ Ok(Account::default())
+ } else {
+ serializer::from_slice(x).chain_err(|| "Invalid internal structure.")
+ })
+}
+
/// Balances contract rust implementation.
#[derive(Debug, Default)]
pub struct Contract;
impl Contract {
/// Returns a balance of given address.
- pub fn balance_of>(&self, _ext: &E, _data: Address) -> Result {
- unimplemented!()
+ pub fn balance_of>(&self, ext: &E, data: Address) -> Result {
+ account(ext, data).map(|acc| acc.balance)
}
/// Returns the next nonce to authorize the transfer from given address.
- pub fn next_nonce>(&self, _ext: &E, _data: Address) -> Result {
- unimplemented!()
+ pub fn next_nonce>(&self, ext: &E, data: Address) -> Result {
+ account(ext, data).map(|acc| acc.nonce)
}
/// Checks preconditions for transfer.
@@ -52,13 +81,172 @@ impl Contract {
/// - signature
/// - replay protection
/// - enough balance
- pub fn transfer_preconditions>(&self, _db: &E, _data: Transfer) -> Result {
- unimplemented!()
+ pub fn transfer_preconditions>(&self, ext: &E, data: Transfer) -> Result