Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
I
ink
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Environments
Packages & Registries
Packages & Registries
Package Registry
Container Registry
Analytics
Analytics
CI / CD
Insights
Issue
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
parity
ink
Commits
e8cf49e1
Commit
e8cf49e1
authored
Jan 23, 2019
by
Hero Bird
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[pdsl_core] Update Erc20 example code
parent
97696ec1
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
31 deletions
+15
-31
examples/erc20/Cargo.toml
examples/erc20/Cargo.toml
+1
-7
examples/erc20/src/lib.rs
examples/erc20/src/lib.rs
+14
-24
No files found.
examples/erc20/Cargo.toml
View file @
e8cf49e1
...
...
@@ -6,13 +6,7 @@ edition = "2018"
[dependencies]
pdsl_core
=
{
path
=
"../../pdsl_core"
}
[dependencies.parity-codec]
version
=
"2.0"
default-features
=
false
features
=
["derive"]
git
=
"https://github.com/paritytech/parity-codec/"
branch
=
"master"
parity-codec
=
{
version
=
"2.2"
,
default-features
=
false
,
features
=
["derive"]
}
[lib]
name
=
"erc20_token"
...
...
examples/erc20/src/lib.rs
View file @
e8cf49e1
...
...
@@ -13,28 +13,17 @@ fn zero_address() -> Address {
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.
#[derive(Debug,
Encode,
Decode)]
pub
struct
Erc20Token
{
/// All peeps done by all users.
balances
:
storage
::
HashMap
<
Address
,
Balance
>
,
/// 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.
total_supply
:
storage
::
Value
<
Balance
>
,
/// The allocator for newly allocated entities.
...
...
@@ -54,8 +43,8 @@ impl Erc20Token {
/// Returns the amount of tokens that an owner allowed to a spender.
pub
fn
allowance
(
&
self
,
owner
:
Address
,
spender
:
Address
)
->
Balance
{
let
pair
=
AllowancePair
{
owner
,
spender
};
*
self
.allowances
.get
(
&
pair
)
.unwrap_or
(
&
0
)
//
let pair = AllowancePair{owner, spender};
*
self
.allowances
.get
(
&
(
owner
,
spender
)
)
.unwrap_or
(
&
0
)
}
/// Transfers token from the sender to the `to` address.
...
...
@@ -78,8 +67,8 @@ impl Erc20Token {
pub
fn
approve
(
&
mut
self
,
spender
:
Address
,
value
:
Balance
)
->
bool
{
assert_ne!
(
spender
,
zero_address
());
let
owner
=
ContractEnv
::
caller
();
let
pair
=
AllowancePair
{
owner
,
spender
};
self
.allowances
.insert
(
pair
,
value
);
//
let pair = AllowancePair{owner, spender};
self
.allowances
.insert
(
(
owner
,
spender
)
,
value
);
// emit event (not ready yet)
true
}
...
...
@@ -90,8 +79,9 @@ impl Erc20Token {
/// this is not required as per the specification,
/// and other compliant implementations may not emit the event.
pub
fn
transfer_from
(
&
mut
self
,
from
:
Address
,
to
:
Address
,
value
:
Balance
)
->
bool
{
let
pair
=
AllowancePair
{
owner
:
from
,
spender
:
to
};
self
.allowances
.mutate_with
(
&
pair
,
|
allowed
|
*
allowed
-=
value
);
// let pair = AllowancePair{owner: from, spender: to};
// self.allowances.mutate_with(&pair, |allowed| *allowed -= value);
self
.allowances
[
&
(
from
,
to
)]
-=
value
;
self
.transfer_impl
(
from
,
to
,
value
);
// emit approval(from, to, value) (not yet ready)
true
...
...
@@ -101,8 +91,8 @@ impl Erc20Token {
fn
transfer_impl
(
&
mut
self
,
from
:
Address
,
to
:
Address
,
value
:
Balance
)
{
assert_ne!
(
to
,
zero_address
());
self
.balances
.mutate_with
(
&
from
,
|
from
|
*
from
-=
value
)
;
self
.balances
.mutate_with
(
&
to
,
|
to
|
*
to
+=
value
)
;
self
.balances
[
&
from
]
-=
value
;
self
.balances
[
&
to
]
+=
value
;
// emit transfer(from, to, value) (not ready yet)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment