Skip to content

Commit

Permalink
Merge pull request #30 from 1inch/fix/p
Browse files Browse the repository at this point in the history
Fix/p
  • Loading branch information
ZumZoom committed Aug 28, 2023
2 parents 8c2b804 + 2e0e4bd commit 585cad4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
34 changes: 16 additions & 18 deletions contracts/ERC20Plugins.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ abstract contract ERC20Plugins is ERC20, IERC20Plugins, ReentrancyGuardExt {
}

function _removeAllPlugins(address account) internal virtual {
address[] memory items = _plugins[account].items.get();
address[] memory pluginItems = _plugins[account].items.get();
uint256 balance = balanceOf(account);
unchecked {
for (uint256 i = items.length; i > 0; i--) {
address item = items[i-1];
for (uint256 i = pluginItems.length; i > 0; i--) {
address item = pluginItems[i-1];
_plugins[account].remove(item);
emit PluginRemoved(account, item);
if (balance > 0) {
Expand Down Expand Up @@ -168,39 +168,37 @@ abstract contract ERC20Plugins is ERC20, IERC20Plugins, ReentrancyGuardExt {
}
}

// ERC20 Overrides

function _afterTokenTransfer(address from, address to, uint256 amount) internal nonReentrant(_guard) override virtual {
super._afterTokenTransfer(from, to, amount);

unchecked {
if (amount > 0 && from != to) {
address[] memory a = _plugins[from].items.get();
address[] memory b = _plugins[to].items.get();
uint256 aLength = a.length;
uint256 bLength = b.length;
address[] memory pluginsFrom = _plugins[from].items.get();
address[] memory pluginsTo = _plugins[to].items.get();
uint256 pluginsFromLength = pluginsFrom.length;
uint256 pluginsToLength = pluginsTo.length;

for (uint256 i = 0; i < aLength; i++) {
address plugin = a[i];
for (uint256 i = 0; i < pluginsFromLength; i++) {
address plugin = pluginsFrom[i];

uint256 j;
for (j = 0; j < bLength; j++) {
if (plugin == b[j]) {
// Both parties are participating of the same plugin
for (j = 0; j < pluginsToLength; j++) {
if (plugin == pluginsTo[j]) {
// Both parties are participating in the same plugin
_updateBalances(plugin, from, to, amount);
b[j] = address(0);
pluginsTo[j] = address(0);
break;
}
}

if (j == bLength) {
if (j == pluginsToLength) {
// Sender is participating in a plugin, but receiver is not
_updateBalances(plugin, from, address(0), amount);
}
}

for (uint256 j = 0; j < bLength; j++) {
address plugin = b[j];
for (uint256 j = 0; j < pluginsToLength; j++) {
address plugin = pluginsTo[j];
if (plugin != address(0)) {
// Receiver is participating in a plugin, but sender is not
_updateBalances(plugin, address(0), to, amount);
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IERC20Plugins.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface IERC20Plugins is IERC20 {

/**
* @dev Returns the address of a plugin at a specified index for a given account.
* The function will revert if index is greater or equal than `pluginsCount(account)`.
* @param account The address of the account.
* @param index The index of the plugin to retrieve.
* @return plugin The address of the plugin.
Expand Down
12 changes: 8 additions & 4 deletions contracts/interfaces/IPlugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ pragma solidity ^0.8.0;
import { IERC20Plugins } from "./IERC20Plugins.sol";

interface IPlugin {
function token() external view returns(IERC20Plugins);
/**
* @dev Returns the token which this plugin belongs to.
* @return erc20 The IERC20Plugins token.
*/
function token() external view returns(IERC20Plugins erc20);

/**
* @dev Updates the balances of two addresses in the plugin as a result of any balance changes.
* Only the Token contract is allowed to call this function.
* @param from The address from which tokens were transferred
* @param to The address to which tokens were transferred
* @param amount The amount of tokens transferred
* @param from The address from which tokens were transferred.
* @param to The address to which tokens were transferred.
* @param amount The amount of tokens transferred.
*/
function updateBalances(address from, address to, uint256 amount) external;
}
4 changes: 2 additions & 2 deletions contracts/libs/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract ReentrancyGuardExt {
using ReentrancyGuardLib for ReentrancyGuardLib.Data;

/**
* @dev Modifier that ensures a function can only be called once in the same block.
* @dev Modifier that prevents a contract from calling itself, directly or indirectly.
* @param self The storage reference to the struct.
*/
modifier nonReentrant(ReentrancyGuardLib.Data storage self) {
Expand All @@ -72,7 +72,7 @@ contract ReentrancyGuardExt {
}

/**
* @dev Modifier that ensures a function can only be called once in the same block in view mode.
* @dev Modifier that prevents calls to a function from `nonReentrant` functions, directly or indirectly.
* @param self The storage reference to the struct.
*/
modifier nonReentrantView(ReentrancyGuardLib.Data storage self) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/token-plugins",
"version": "1.2.0",
"version": "1.2.1",
"description": "ERC20 extension enabling external smart contract based plugins to track balances of those users who opted-in to those plugins",
"repository": {
"type": "git",
Expand Down

0 comments on commit 585cad4

Please sign in to comment.