Commit 49617ad4 authored by Hero Bird's avatar Hero Bird

[examples/erc20] Adjust to newest allocator changes

parent 76f3d356
#![no_std]
#![cfg_attr(not(all(test, feature = "test-env")), no_std)]
use parity_codec::{
Encode,
Decode,
};
use pdsl_core::{
storage,
env::{Env, ContractEnv, srml::Address, srml::Balance},
storage::{
self,
alloc::{
BumpAlloc,
Allocator,
AllocateUsing,
Initialize,
},
Flush,
Key,
},
env::{
Env,
ContractEnv,
srml::Address,
srml::Balance,
},
};
use parity_codec::{Encode, Decode};
/// Returns the zero address.
fn zero_address() -> Address {
Address::from([0x0_u8; 32].as_ref())
}
/// The storage data that is hold by the ERC-20 token.
#[derive(Debug, Encode, Decode)]
......@@ -60,7 +73,6 @@ impl Erc20Token {
/// the spender's allowance to 0 and set the desired value afterwards:
/// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
pub fn approve(&mut self, spender: Address, value: Balance) -> bool {
assert_ne!(spender, zero_address());
let owner = ContractEnv::caller();
self.allowances.insert((owner, spender), value);
// emit event (not ready yet)
......@@ -81,39 +93,36 @@ impl Erc20Token {
/// Transfers token from a specified address to another address.
fn transfer_impl(&mut self, from: Address, to: Address, value: Balance) {
assert_ne!(to, zero_address());
self.balances[&from] -= value;
self.balances[&to] += value;
// emit transfer(from, to, value) (not ready yet)
}
}
// BELOW THIS EVERYTHING WILL EVENTUALLY BE GENERATED BY THE eDSL
use pdsl_core::{
storage::{
alloc::ForwardAlloc,
Flush,
Key,
},
};
impl Erc20Token {
/// Creates new ERC-20 token using the given allocator.
pub unsafe fn new_using_alloc<A>(alloc: &mut A) -> Self
impl AllocateUsing for Erc20Token {
unsafe fn allocate_using<A>(alloc: &mut A) -> Self
where
A: storage::Allocator
A: Allocator,
{
Self {
balances: storage::HashMap::new_using_alloc(alloc),
allowances: storage::HashMap::new_using_alloc(alloc),
total_supply: storage::Value::new_using_alloc(alloc, 0),
balances: storage::HashMap::allocate_using(alloc),
allowances: storage::HashMap::allocate_using(alloc),
total_supply: storage::Value::allocate_using(alloc),
}
}
}
impl Initialize for Erc20Token {
type Args = ();
fn initialize(&mut self, _params: Self::Args) {
//self.mint_for(alice_address(), 10_000);
//self.mint_for(bob_address(), 500);
}
}
impl Flush for Erc20Token {
fn flush(&mut self) {
self.balances.flush();
......@@ -140,23 +149,30 @@ where
ContractEnv::return_(&val.encode())
}
#[no_mangle]
pub extern "C" fn deploy() {}
const ALLOC_KEY: Key = Key([0x0; 32]);
fn instantiate() -> Erc20Token {
unsafe {
let mut alloc = BumpAlloc::from_raw_parts(ALLOC_KEY);
Erc20Token::allocate_using(&mut alloc)
}
}
#[no_mangle]
pub extern "C" fn call() {
use parity_codec::{Decode};
use pdsl_core::{
env::{Env, ContractEnv},
};
pub extern "C" fn deploy() {
instantiate().initialize_into(()).flush()
}
fn decode_params() -> Action {
let input = ContractEnv::input();
let action = Action::decode(&mut &input[..]).unwrap();
let mut alloc = unsafe {
ForwardAlloc::from_raw_parts(Key([0x0; 32]))
};
let mut erc20token = unsafe { Erc20Token::new_using_alloc(&mut alloc) };
match action {
action
}
#[no_mangle]
pub extern "C" fn call() {
let mut erc20token = instantiate();
match decode_params() {
Action::TotalSupply => {
let ret_val = erc20token.total_supply();
erc20token.flush();
......
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