Commit e61f3e3f authored by Hero Bird's avatar Hero Bird

[examples/erc20] Add missing flushing functionality

For now kept super ugly but it should work at least ...
parent b7f65d4f
#![no_std] #![no_std]
use pdsl_core::{ use pdsl_core::{
storage::{ storage,
self,
Key,
alloc::ForwardAlloc,
},
env::{Env, ContractEnv, srml::Address, srml::Balance}, env::{Env, ContractEnv, srml::Address, srml::Balance},
}; };
use parity_codec::{Encode, Decode}; use parity_codec::{Encode, Decode};
...@@ -94,6 +90,16 @@ impl Erc20Token { ...@@ -94,6 +90,16 @@ impl Erc20Token {
} }
} }
// BELOW THIS EVERYTHING WILL EVENTUALLY BE GENERATED BY THE eDSL
use pdsl_core::{
storage::{
alloc::ForwardAlloc,
Flush,
Key,
},
};
impl Erc20Token { impl Erc20Token {
/// Creates new ERC-20 token using the given allocator. /// Creates new ERC-20 token using the given allocator.
pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self
...@@ -108,6 +114,14 @@ impl Erc20Token { ...@@ -108,6 +114,14 @@ impl Erc20Token {
} }
} }
impl Flush for Erc20Token {
fn flush(&mut self) {
self.balances.flush();
self.allowances.flush();
self.total_supply.flush();
}
}
/// Erc20Token API. /// Erc20Token API.
#[derive(Encode, Decode)] #[derive(Encode, Decode)]
enum Action { enum Action {
...@@ -144,22 +158,34 @@ pub extern "C" fn call() { ...@@ -144,22 +158,34 @@ pub extern "C" fn call() {
let mut erc20token = unsafe { Erc20Token::new_using_alloc(&mut alloc) }; let mut erc20token = unsafe { Erc20Token::new_using_alloc(&mut alloc) };
match action { match action {
Action::TotalSupply => { Action::TotalSupply => {
ret(erc20token.total_supply()) let ret_val = erc20token.total_supply();
erc20token.flush();
ret(ret_val);
} }
Action::BalanceOf{owner} => { Action::BalanceOf{owner} => {
ret(erc20token.balance_of(owner)) let ret_val = erc20token.balance_of(owner);
erc20token.flush();
ret(ret_val);
} }
Action::Allowance{owner, spender} => { Action::Allowance{owner, spender} => {
ret(erc20token.allowance(owner, spender)) let ret_val = erc20token.allowance(owner, spender);
erc20token.flush();
ret(ret_val);
} }
Action::Transfer{to, value} => { Action::Transfer{to, value} => {
ret(erc20token.transfer(to, value)) let ret_val = erc20token.transfer(to, value);
erc20token.flush();
ret(ret_val);
} }
Action::Approve{spender, value} => { Action::Approve{spender, value} => {
ret(erc20token.approve(spender, value)) let ret_val = erc20token.approve(spender, value);
erc20token.flush();
ret(ret_val);
} }
Action::TransferFrom{from, to, value} => { Action::TransferFrom{from, to, value} => {
ret(erc20token.transfer_from(from, to, value)) let ret_val = erc20token.transfer_from(from, to, value);
erc20token.flush();
ret(ret_val);
} }
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment