Commit 507834d7 authored by Shawn Tabrizi's avatar Shawn Tabrizi Committed by Hero Bird
Browse files

Update ERC20 to match specification (#231)

* Update ERC20 to match specification

Some names and functions don't match specification.

https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md

* Cargo fmt
parent 216a79d0
Pipeline #56737 failed with stages
in 2 minutes and 52 seconds
fn main() -> Result<(), std::io::Error> {
let abi = <contract::Erc20 as ink_lang2::GenerateAbi>::generate_abi();
let contents = serde_json::to_string_pretty(&abi)?;
......
......@@ -30,23 +30,23 @@ mod erc20 {
}
#[ink(event)]
struct Transferred {
struct Transfer {
#[ink(topic)]
from: Option<AccountId>,
#[ink(topic)]
to: Option<AccountId>,
#[ink(topic)]
amount: Balance,
value: Balance,
}
#[ink(event)]
struct Approved {
struct Approval {
#[ink(topic)]
owner: AccountId,
#[ink(topic)]
spender: AccountId,
#[ink(topic)]
amount: Balance,
value: Balance,
}
impl Erc20 {
......@@ -55,10 +55,10 @@ mod erc20 {
let caller = self.env().caller();
self.total_supply.set(initial_supply);
self.balances.insert(caller, initial_supply);
self.env().emit_event(Transferred {
self.env().emit_event(Transfer {
from: None,
to: Some(caller),
amount: initial_supply,
value: initial_supply,
});
}
......@@ -73,29 +73,24 @@ mod erc20 {
}
#[ink(message)]
fn allowance(
&self,
owner: AccountId,
spender: AccountId,
) -> Balance {
let allowance = self.allowance_of_or_zero(&owner, &spender);
allowance
fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance {
self.allowance_of_or_zero(&owner, &spender)
}
#[ink(message)]
fn transfer(&mut self, to: AccountId, amount: Balance) -> bool {
fn transfer(&mut self, to: AccountId, value: Balance) -> bool {
let from = self.env().caller();
self.transfer_from_to(from, to, amount)
self.transfer_from_to(from, to, value)
}
#[ink(message)]
fn approve(&mut self, spender: AccountId, amount: Balance) -> bool {
fn approve(&mut self, spender: AccountId, value: Balance) -> bool {
let owner = self.env().caller();
self.allowances.insert((owner, spender), amount);
self.env().emit_event(Approved {
self.allowances.insert((owner, spender), value);
self.env().emit_event(Approval {
owner,
spender,
amount,
value,
});
true
}
......@@ -105,34 +100,34 @@ mod erc20 {
&mut self,
from: AccountId,
to: AccountId,
amount: Balance,
value: Balance,
) -> bool {
let caller = self.env().caller();
let allowance = self.allowance_of_or_zero(&from, &caller);
if allowance < amount {
return false
if allowance < value {
return false;
}
self.allowances.insert((from, caller), allowance - amount);
self.transfer_from_to(from, to, amount)
self.allowances.insert((from, caller), allowance - value);
self.transfer_from_to(from, to, value)
}
fn transfer_from_to(
&mut self,
from: AccountId,
to: AccountId,
amount: Balance,
value: Balance,
) -> bool {
let from_balance = self.balance_of_or_zero(&from);
if from_balance < amount {
return false
if from_balance < value {
return false;
}
let to_balance = self.balance_of_or_zero(&to);
self.balances.insert(from.clone(), from_balance - amount);
self.balances.insert(to.clone(), to_balance + amount);
self.env().emit_event(Transferred {
self.balances.insert(from.clone(), from_balance - value);
self.balances.insert(to.clone(), to_balance + value);
self.env().emit_event(Transfer {
from: Some(from),
to: Some(to),
amount,
value,
});
true
}
......
Supports Markdown
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