Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
f1427d51
Commit
f1427d51
authored
5 years ago
by
Gavin Wood
Committed by
GitHub
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
EVM should store contracts' balances in an account. (#4090)
parent
5504b4b2
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
substrate/srml/evm/src/lib.rs
+27
-10
27 additions, 10 deletions
substrate/srml/evm/src/lib.rs
with
27 additions
and
10 deletions
substrate/srml/evm/src/lib.rs
+
27
−
10
View file @
f1427d51
...
...
@@ -27,13 +27,16 @@ use rstd::vec::Vec;
use
support
::{
dispatch
::
Result
,
decl_module
,
decl_storage
,
decl_event
};
use
support
::
traits
::{
Currency
,
WithdrawReason
,
ExistenceRequirement
};
use
system
::
ensure_signed
;
use
sr_primitives
::
ModuleId
;
use
sr_primitives
::
weights
::
SimpleDispatchInfo
;
use
sr_primitives
::
traits
::
UniqueSaturatedInto
;
use
sr_primitives
::
traits
::
{
UniqueSaturatedInto
,
AccountIdConversion
}
;
use
primitives
::{
U256
,
H256
,
H160
};
use
evm
::{
ExitReason
,
ExitSucceed
,
ExitError
};
use
evm
::
executor
::
StackExecutor
;
use
evm
::
backend
::
ApplyBackend
;
const
MODULE_ID
:
ModuleId
=
ModuleId
(
*
b"py/ethvm"
);
/// Type alias for currency balance.
pub
type
BalanceOf
<
T
>
=
<<
T
as
Trait
>
::
Currency
as
Currency
<<
T
as
system
::
Trait
>
::
AccountId
>>
::
Balance
;
...
...
@@ -117,12 +120,13 @@ decl_module! {
fn
deposit_balance
(
origin
,
value
:
BalanceOf
<
T
>
)
->
Result
{
let
sender
=
ensure_signed
(
origin
)
?
;
T
::
Currency
::
withdraw
(
let
imbalance
=
T
::
Currency
::
withdraw
(
&
sender
,
value
,
WithdrawReason
::
Reserve
.into
(),
ExistenceRequirement
::
KeepAlive
,
ExistenceRequirement
::
AllowDeath
,
)
?
;
T
::
Currency
::
resolve_creating
(
&
Self
::
account_id
(),
imbalance
);
let
bvalue
=
U256
::
from
(
UniqueSaturatedInto
::
<
u128
>
::
unique_saturated_into
(
value
));
let
address
=
T
::
ConvertAccountId
::
convert_account_id
(
&
sender
);
...
...
@@ -139,15 +143,20 @@ decl_module! {
let
address
=
T
::
ConvertAccountId
::
convert_account_id
(
&
sender
);
let
bvalue
=
U256
::
from
(
UniqueSaturatedInto
::
<
u128
>
::
unique_saturated_into
(
value
));
if
Accounts
::
get
(
&
address
)
.balance
<
bvalue
{
return
Err
(
"Not enough balance to withdraw"
)
}
let
mut
account
=
Accounts
::
get
(
&
address
)
;
account
.balance
=
account
.balance
.checked_sub
(
bvalue
)
.ok_or
(
"Not enough balance to withdraw"
)
?
;
Accounts
::
mutate
(
&
address
,
|
account
|
{
account
.balance
-=
bvalue
;
});
let
imbalance
=
T
::
Currency
::
withdraw
(
&
Self
::
account_id
(),
value
,
WithdrawReason
::
Reserve
.into
(),
ExistenceRequirement
::
AllowDeath
)
?
;
Accounts
::
insert
(
&
address
,
account
);
T
::
Currency
::
deposit
_creating
(
&
sender
,
valu
e
);
T
::
Currency
::
resolve
_creating
(
&
sender
,
imbalanc
e
);
Ok
(())
}
...
...
@@ -256,6 +265,14 @@ decl_module! {
}
impl
<
T
:
Trait
>
Module
<
T
>
{
/// The account ID of the EVM module.
///
/// This actually does computation. If you need to keep using it, then make sure you cache the
/// value and only call this once.
pub
fn
account_id
()
->
T
::
AccountId
{
MODULE_ID
.into_account
()
}
/// Check whether an account is empty.
pub
fn
is_account_empty
(
address
:
&
H160
)
->
bool
{
let
account
=
Accounts
::
get
(
address
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment