Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
8a775cd8
Unverified
Commit
8a775cd8
authored
Jun 23, 2021
by
Hadrien Croubois
Committed by
GitHub
Jun 22, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Emit DelegateVotesChanged events after Transfer (#2733)
parent
4d0f8c1d
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
2 deletions
+39
-2
ERC20.sol
contracts/token/ERC20/ERC20.sol
+27
-1
ERC20Votes.sol
contracts/token/ERC20/extensions/ERC20Votes.sol
+3
-1
ERC20Votes.test.js
test/token/ERC20/extensions/ERC20Votes.test.js
+9
-0
No files found.
contracts/token/ERC20/ERC20.sol
View file @
8a775cd8
...
...
@@ -234,6 +234,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
...
...
@@ -253,6 +255,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
/**
...
...
@@ -279,6 +283,8 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
/**
...
...
@@ -313,7 +319,7 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be t
o t
ransferred to `to`.
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
...
...
@@ -325,4 +331,24 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
address to,
uint256 amount
) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}
contracts/token/ERC20/extensions/ERC20Votes.sol
View file @
8a775cd8
...
...
@@ -190,11 +190,13 @@ abstract contract ERC20Votes is ERC20Permit {
*
* Emits a {DelegateVotesChanged} event.
*/
function _
before
TokenTransfer(
function _
after
TokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override {
super._afterTokenTransfer(from, to, amount);
_moveVotingPower(delegates(from), delegates(to), amount);
}
...
...
test/token/ERC20/extensions/ERC20Votes.test.js
View file @
8a775cd8
...
...
@@ -306,6 +306,9 @@ contract('ERC20Votes', function (accounts) {
expectEvent
(
receipt
,
'Transfer'
,
{
from
:
holder
,
to
:
recipient
,
value
:
'1'
});
expectEvent
(
receipt
,
'DelegateVotesChanged'
,
{
delegate
:
holder
,
previousBalance
:
supply
,
newBalance
:
supply
.
subn
(
1
)
});
const
{
logIndex
:
transferLogIndex
}
=
receipt
.
logs
.
find
(({
event
})
=>
event
==
'Transfer'
);
expect
(
receipt
.
logs
.
filter
(({
event
})
=>
event
==
'DelegateVotesChanged'
).
every
(({
logIndex
})
=>
transferLogIndex
<
logIndex
)).
to
.
be
.
equal
(
true
);
this
.
holderVotes
=
supply
.
subn
(
1
);
this
.
recipientVotes
=
'0'
;
});
...
...
@@ -317,6 +320,9 @@ contract('ERC20Votes', function (accounts) {
expectEvent
(
receipt
,
'Transfer'
,
{
from
:
holder
,
to
:
recipient
,
value
:
'1'
});
expectEvent
(
receipt
,
'DelegateVotesChanged'
,
{
delegate
:
recipient
,
previousBalance
:
'0'
,
newBalance
:
'1'
});
const
{
logIndex
:
transferLogIndex
}
=
receipt
.
logs
.
find
(({
event
})
=>
event
==
'Transfer'
);
expect
(
receipt
.
logs
.
filter
(({
event
})
=>
event
==
'DelegateVotesChanged'
).
every
(({
logIndex
})
=>
transferLogIndex
<
logIndex
)).
to
.
be
.
equal
(
true
);
this
.
holderVotes
=
'0'
;
this
.
recipientVotes
=
'1'
;
});
...
...
@@ -330,6 +336,9 @@ contract('ERC20Votes', function (accounts) {
expectEvent
(
receipt
,
'DelegateVotesChanged'
,
{
delegate
:
holder
,
previousBalance
:
supply
,
newBalance
:
supply
.
subn
(
1
)
});
expectEvent
(
receipt
,
'DelegateVotesChanged'
,
{
delegate
:
recipient
,
previousBalance
:
'0'
,
newBalance
:
'1'
});
const
{
logIndex
:
transferLogIndex
}
=
receipt
.
logs
.
find
(({
event
})
=>
event
==
'Transfer'
);
expect
(
receipt
.
logs
.
filter
(({
event
})
=>
event
==
'DelegateVotesChanged'
).
every
(({
logIndex
})
=>
transferLogIndex
<
logIndex
)).
to
.
be
.
equal
(
true
);
this
.
holderVotes
=
supply
.
subn
(
1
);
this
.
recipientVotes
=
'1'
;
});
...
...
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