Skip to content

Commit

Permalink
Merge branch 'next-v5.0' into refator/remove/TokenTimelock
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio authored May 26, 2023
2 parents 9e5047b + 4448c13 commit e51e218
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-birds-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': major
---

`Checkpoints`: library moved from `utils` to `utils/structs`
5 changes: 5 additions & 0 deletions .changeset/wild-windows-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': major
---

`SafeERC20`: Refactor `safeDecreaseAllowance` and `safeIncreaseAllowance` to support USDT-like tokens.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
pragma solidity ^0.8.0;

import "./GovernorVotes.sol";
import "../../utils/Checkpoints.sol";
import "../../utils/math/SafeCast.sol";
import "../../utils/structs/Checkpoints.sol";

/**
* @dev Extension of {Governor} for voting weight extraction from an {ERC20Votes} token and a quorum expressed as a
Expand Down
2 changes: 1 addition & 1 deletion contracts/governance/utils/Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pragma solidity ^0.8.0;
import "../../interfaces/IERC5805.sol";
import "../../utils/Context.sol";
import "../../utils/Nonces.sol";
import "../../utils/Checkpoints.sol";
import "../../utils/cryptography/EIP712.sol";
import "../../utils/structs/Checkpoints.sol";

/**
* @dev This is a base abstract contract that tracks voting units, which are a measure of voting power that can be
Expand Down
4 changes: 2 additions & 2 deletions contracts/token/ERC20/utils/SafeERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ library SafeERC20 {
*/
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
forceApprove(token, spender, oldAllowance + value);
}

/**
Expand All @@ -52,7 +52,7 @@ library SafeERC20 {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
forceApprove(token, spender, oldAllowance - value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/token/ERC721/extensions/ERC721Consecutive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pragma solidity ^0.8.1;

import "../ERC721.sol";
import "../../../interfaces/IERC2309.sol";
import "../../../utils/Checkpoints.sol";
import "../../../utils/structs/BitMaps.sol";
import "../../../utils/structs/Checkpoints.sol";

/**
* @dev Implementation of the ERC2309 "Consecutive Transfer Extension" as defined in
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/Checkpoints.sol)
// OpenZeppelin Contracts (last updated v4.9.0) (utils/structs/Checkpoints.sol)
// This file was procedurally generated from scripts/generate/templates/Checkpoints.js.

pragma solidity ^0.8.0;

import "./math/Math.sol";
import "./math/SafeCast.sol";
import "../math/Math.sol";
import "../math/SafeCast.sol";

/**
* @dev This library defines the `History` struct, for checkpointing values as they change at different points in
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ for (const [file, template] of Object.entries({
'utils/math/SafeCast.sol': './templates/SafeCast.js',
'utils/structs/EnumerableSet.sol': './templates/EnumerableSet.js',
'utils/structs/EnumerableMap.sol': './templates/EnumerableMap.js',
'utils/Checkpoints.sol': './templates/Checkpoints.js',
'utils/structs/Checkpoints.sol': './templates/Checkpoints.js',
'utils/StorageSlot.sol': './templates/StorageSlot.js',
})) {
generateFromTemplate(file, template, './contracts/');
}

// Tests
for (const [file, template] of Object.entries({
'utils/Checkpoints.t.sol': './templates/Checkpoints.t.js',
'utils/structs/Checkpoints.t.sol': './templates/Checkpoints.t.js',
})) {
generateFromTemplate(file, template, './test/');
}
4 changes: 2 additions & 2 deletions scripts/generate/templates/Checkpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const { OPTS } = require('./Checkpoints.opts.js');
const header = `\
pragma solidity ^0.8.0;
import "./math/Math.sol";
import "./math/SafeCast.sol";
import "../math/Math.sol";
import "../math/SafeCast.sol";
/**
* @dev This library defines the \`History\` struct, for checkpointing values as they change at different points in
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate/templates/Checkpoints.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const header = `\
pragma solidity ^0.8.0;
import "forge-std/Test.sol";
import "../../contracts/utils/Checkpoints.sol";
import "../../contracts/utils/math/SafeCast.sol";
import "../../../contracts/utils/math/SafeCast.sol";
import "../../../contracts/utils/structs/Checkpoints.sol";
`;

/* eslint-disable max-len */
Expand Down
11 changes: 7 additions & 4 deletions test/token/ERC20/utils/SafeERC20.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,19 @@ contract('SafeERC20', function (accounts) {
await this.token.$_approve(this.mock.address, spender, 100);
});

it('safeApprove can increase approval', async function () {
await expectRevert(this.mock.$safeIncreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
it('safeIncreaseAllowance works', async function () {
await this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
expect(this.token.allowance(this.mock.address, spender, 90));
});

it('safeApprove can decrease approval', async function () {
await expectRevert(this.mock.$safeDecreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
it('safeDecreaseAllowance works', async function () {
await this.mock.$safeDecreaseAllowance(this.token.address, spender, 10);
expect(this.token.allowance(this.mock.address, spender, 110));
});

it('forceApprove works', async function () {
await this.mock.$forceApprove(this.token.address, spender, 200);
expect(this.token.allowance(this.mock.address, spender, 200));
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "../../contracts/utils/Checkpoints.sol";
import "../../contracts/utils/math/SafeCast.sol";
import "../../../contracts/utils/math/SafeCast.sol";
import "../../../contracts/utils/structs/Checkpoints.sol";

contract CheckpointsTrace224Test is Test {
using Checkpoints for Checkpoints.Trace224;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const { VALUE_SIZES } = require('../../scripts/generate/templates/Checkpoints.opts.js');
const { VALUE_SIZES } = require('../../../scripts/generate/templates/Checkpoints.opts.js');

const $Checkpoints = artifacts.require('$Checkpoints');

Expand Down

0 comments on commit e51e218

Please sign in to comment.