Commit e8cf49e1 authored by Hero Bird's avatar Hero Bird

[pdsl_core] Update Erc20 example code

parent 97696ec1
...@@ -6,13 +6,7 @@ edition = "2018" ...@@ -6,13 +6,7 @@ edition = "2018"
[dependencies] [dependencies]
pdsl_core = { path = "../../pdsl_core" } pdsl_core = { path = "../../pdsl_core" }
parity-codec = { version = "2.2", default-features = false, features = ["derive"] }
[dependencies.parity-codec]
version = "2.0"
default-features = false
features = ["derive"]
git = "https://github.com/paritytech/parity-codec/"
branch = "master"
[lib] [lib]
name = "erc20_token" name = "erc20_token"
......
...@@ -13,28 +13,17 @@ fn zero_address() -> Address { ...@@ -13,28 +13,17 @@ fn zero_address() -> Address {
Address::from([0x0_u8; 32].as_ref()) Address::from([0x0_u8; 32].as_ref())
} }
/// A pair for allowances.
///
/// Has a notion of owner and spender.
/// The owner allows the spender to spend
/// the owners tokens.
///
/// The amount of allowed tokens is stored separately.
#[derive(Debug, Hash, PartialEq, Eq, Encode, Decode)]
pub struct AllowancePair {
/// The owner of the tokens.
owner: Address,
/// The address that is allowed to spend the tokens of the owner.
spender: Address,
}
/// The storage data that is hold by the ERC-20 token. /// The storage data that is hold by the ERC-20 token.
#[derive(Debug, Encode, Decode)] #[derive(Debug, Encode, Decode)]
pub struct Erc20Token { pub struct Erc20Token {
/// All peeps done by all users. /// All peeps done by all users.
balances: storage::HashMap<Address, Balance>, balances: storage::HashMap<Address, Balance>,
/// Balances that are spendable by non-owners. /// Balances that are spendable by non-owners.
allowances: storage::HashMap<AllowancePair, Balance>, ///
/// # Note
///
/// Mapping: (from, to) -> allowed
allowances: storage::HashMap<(Address, Address), Balance>,
/// The total supply. /// The total supply.
total_supply: storage::Value<Balance>, total_supply: storage::Value<Balance>,
/// The allocator for newly allocated entities. /// The allocator for newly allocated entities.
...@@ -54,8 +43,8 @@ impl Erc20Token { ...@@ -54,8 +43,8 @@ impl Erc20Token {
/// Returns the amount of tokens that an owner allowed to a spender. /// Returns the amount of tokens that an owner allowed to a spender.
pub fn allowance(&self, owner: Address, spender: Address) -> Balance { pub fn allowance(&self, owner: Address, spender: Address) -> Balance {
let pair = AllowancePair{owner, spender}; // let pair = AllowancePair{owner, spender};
*self.allowances.get(&pair).unwrap_or(&0) *self.allowances.get(&(owner, spender)).unwrap_or(&0)
} }
/// Transfers token from the sender to the `to` address. /// Transfers token from the sender to the `to` address.
...@@ -78,8 +67,8 @@ impl Erc20Token { ...@@ -78,8 +67,8 @@ impl Erc20Token {
pub fn approve(&mut self, spender: Address, value: Balance) -> bool { pub fn approve(&mut self, spender: Address, value: Balance) -> bool {
assert_ne!(spender, zero_address()); assert_ne!(spender, zero_address());
let owner = ContractEnv::caller(); let owner = ContractEnv::caller();
let pair = AllowancePair{owner, spender}; // let pair = AllowancePair{owner, spender};
self.allowances.insert(pair, value); self.allowances.insert((owner, spender), value);
// emit event (not ready yet) // emit event (not ready yet)
true true
} }
...@@ -90,8 +79,9 @@ impl Erc20Token { ...@@ -90,8 +79,9 @@ impl Erc20Token {
/// this is not required as per the specification, /// this is not required as per the specification,
/// and other compliant implementations may not emit the event. /// and other compliant implementations may not emit the event.
pub fn transfer_from(&mut self, from: Address, to: Address, value: Balance) -> bool { pub fn transfer_from(&mut self, from: Address, to: Address, value: Balance) -> bool {
let pair = AllowancePair{owner: from, spender: to}; // let pair = AllowancePair{owner: from, spender: to};
self.allowances.mutate_with(&pair, |allowed| *allowed -= value); // self.allowances.mutate_with(&pair, |allowed| *allowed -= value);
self.allowances[&(from, to)] -= value;
self.transfer_impl(from, to, value); self.transfer_impl(from, to, value);
// emit approval(from, to, value) (not yet ready) // emit approval(from, to, value) (not yet ready)
true true
...@@ -101,8 +91,8 @@ impl Erc20Token { ...@@ -101,8 +91,8 @@ impl Erc20Token {
fn transfer_impl(&mut self, from: Address, to: Address, value: Balance) { fn transfer_impl(&mut self, from: Address, to: Address, value: Balance) {
assert_ne!(to, zero_address()); assert_ne!(to, zero_address());
self.balances.mutate_with(&from, |from| *from -= value); self.balances[&from] -= value;
self.balances.mutate_with(&to, |to| *to += value); self.balances[&to] += value;
// emit transfer(from, to, value) (not ready yet) // emit transfer(from, to, value) (not ready yet)
} }
......
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