Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
cd07e1b
Remove cross-chain contracts
Amxx May 16, 2023
42b3aef
Remove Escrow & PullPayment contracts
Amxx May 16, 2023
ff68a57
Remove ERC777
Amxx May 16, 2023
18e63a7
Update documentation's nav
Amxx May 16, 2023
64bf67c
add changeset
Amxx May 16, 2023
aeb12fa
Remove the Timer libary
Amxx May 16, 2023
0ea8756
Remove deprecated draft-xxx files.
Amxx May 16, 2023
3344612
Remove getters with error strings
Amxx May 16, 2023
5624e52
Remove SafeERC20.safeApprove
Amxx May 16, 2023
0a3ccc2
Remove AccessControl._setupRole
Amxx May 16, 2023
f3e4e14
Remove deprecated getters in Proxy & deprecated error code in ECDSA
Amxx May 16, 2023
a06d55f
Remove GovernorProposalThreshold
Amxx May 16, 2023
a949b62
Remove ERC1820Implementer
Amxx May 16, 2023
6831f92
fix lint
Amxx May 16, 2023
7b385c6
remove tests for ERC1820Implementer
Amxx May 16, 2023
cd0407f
fix test/urils/Create2.test.js dependency on ERC1820Implementer
Amxx May 16, 2023
edc27fe
remove deprecated and duplicate storage
Amxx May 16, 2023
69c5dee
Remove Checkpoints.History
Amxx May 17, 2023
c974e7f
Remove SafeMath.sol and move tryXxx function to Math.sol
Amxx May 17, 2023
d39ab48
re-add PullPayment (with immutable ownership of the Escrow)
Amxx May 17, 2023
b2be20e
Revert "re-add PullPayment (with immutable ownership of the Escrow)"
Amxx May 17, 2023
f714926
Update "_Available since" for tryXxx operations
Amxx May 19, 2023
15639d7
Apply suggestions from code review
Amxx May 19, 2023
e519bc3
Update Math.test.js
Amxx May 19, 2023
2cd0d41
cleanup EnumerableMap.test.js
Amxx May 19, 2023
815bf2a
cleanup Checkpoints tests
Amxx May 19, 2023
a19eb6a
retreive checkpoint sizes from the generation scripts
Amxx May 19, 2023
d0e778f
fix foundry test
Amxx May 19, 2023
932ae59
Fix upgradeable.patch
Amxx May 19, 2023
b9991cc
Update .changeset/selfish-queens-rest.md
Amxx May 19, 2023
4d11483
add missing await
frangio May 19, 2023
32b6b60
Remove SignedSafeMath library and tests
Amxx May 19, 2023
e21211b
remove mention to SafeMath in SafeCast
Amxx May 19, 2023
9456fce
Update test/utils/Create2.test.js
Amxx May 19, 2023
2d2e8fa
Update test/utils/Checkpoints.test.js
frangio May 19, 2023
9193cea
remove mention of safemath from template
frangio May 19, 2023
41732d3
remove ERC-1820 docs and move interfaces
frangio May 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
re-add PullPayment (with immutable ownership of the Escrow)
  • Loading branch information
Amxx committed May 17, 2023
commit d39ab48fd146f6244599cfeeb4c4b83171f2acdc
70 changes: 70 additions & 0 deletions contracts/security/PullPayment.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (security/PullPayment.sol)

pragma solidity ^0.8.0;

import "./PullPaymentEscrow.sol";

/**
* @dev Simple implementation of a
* https://consensys.github.io/smart-contract-best-practices/development-recommendations/general/external-calls/#favor-pull-over-push-for-external-calls[pull-payment]
* strategy, where the paying contract doesn't interact directly with the
* receiver account, which must withdraw its payments itself.
*
* Pull-payments are often considered the best practice when it comes to sending
* Ether, security-wise. It prevents recipients from blocking execution, and
* eliminates reentrancy concerns.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*
* To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
* instead of Solidity's `transfer` function. Payees can query their due
* payments with {payments}, and retrieve them with {withdrawPayments}.
*/
abstract contract PullPayment {
PullPaymentEscrow private immutable _escrow = new PullPaymentEscrow();

/**
* @dev Withdraw accumulated payments, forwarding all gas to the recipient.
*
* Note that _any_ account can call this function, not just the `payee`.
* This means that contracts unaware of the `PullPayment` protocol can still
* receive funds this way, by having a separate account call
* {withdrawPayments}.
*
* WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
* Make sure you trust the recipient, or are either following the
* checks-effects-interactions pattern or using {ReentrancyGuard}.
*
* @param payee Whose payments will be withdrawn.
*
* Causes the `escrow` to emit a {Withdrawn} event.
*/
function withdrawPayments(address payable payee) public virtual {
_escrow.withdraw(payee);
}

/**
* @dev Returns the payments owed to an address.
* @param dest The creditor's address.
*/
function payments(address dest) public view returns (uint256) {
return _escrow.depositsOf(dest);
}

/**
* @dev Called by the payer to store the sent amount as credit to be pulled.
* Funds sent in this way are stored in an intermediate {Escrow} contract, so
* there is no danger of them being spent before withdrawal.
*
* @param dest The destination address of the funds.
* @param amount The amount to transfer.
*
* Causes the `escrow` to emit a {Deposited} event.
*/
function _asyncTransfer(address dest, uint256 amount) internal virtual {
_escrow.deposit{value: amount}(dest);
}
}
74 changes: 74 additions & 0 deletions contracts/security/PullPaymentEscrow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/escrow/Escrow.sol)

pragma solidity ^0.8.0;

import "../utils/Address.sol";
import "../utils/Context.sol";

/**
* @title Escrow
* @dev Base escrow contract, holds funds designated for a payee until they
* withdraw them.
*
* Intended usage: This contract (and derived escrow contracts) should be a
* standalone contract, that only interacts with the contract that instantiated
* it. That way, it is guaranteed that all Ether will be handled according to
* the `Escrow` rules, and there is no need to check for payable functions or
* transfers in the inheritance tree. The contract that uses the escrow as its
* payment method should be its owner, and provide public methods redirecting
* to the escrow's deposit and withdraw.
*/
contract PullPaymentEscrow is Context {
using Address for address payable;

address public immutable owner = msg.sender;

event Deposited(address indexed payee, uint256 weiAmount);
event Withdrawn(address indexed payee, uint256 weiAmount);

mapping(address => uint256) private _deposits;

modifier onlyOwner() {
require(owner == _msgSender(), "Ownable: caller is not the owner");
_;
}

function depositsOf(address payee) public view returns (uint256) {
return _deposits[payee];
}

/**
* @dev Stores the sent amount as credit to be withdrawn.
* @param payee The destination address of the funds.
*
* Emits a {Deposited} event.
*/
function deposit(address payee) public payable virtual onlyOwner {
uint256 amount = msg.value;
_deposits[payee] += amount;
emit Deposited(payee, amount);
}

/**
* @dev Withdraw accumulated balance for a payee, forwarding all gas to the
* recipient.
*
* WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
* Make sure you trust the recipient, or are either following the
* checks-effects-interactions pattern or using {ReentrancyGuard}.
*
* @param payee The address whose funds will be withdrawn and transferred to.
*
* Emits a {Withdrawn} event.
*/
function withdraw(address payable payee) public virtual onlyOwner {
uint256 payment = _deposits[payee];

_deposits[payee] = 0;

payee.sendValue(payment);

emit Withdrawn(payee, payment);
}
}
3 changes: 3 additions & 0 deletions contracts/security/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ NOTE: This document is better viewed at https://docs.openzeppelin.com/contracts/

These contracts aim to cover common security practices.

* {PullPayment}: A pattern that can be used to avoid reentrancy attacks.
* {ReentrancyGuard}: A modifier that can prevent reentrancy during certain functions.
* {Pausable}: A common emergency response mechanism that can pause functionality while a remediation is pending.

TIP: For an overview on reentrancy and the possible mechanisms to prevent it, read our article https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].

== Contracts

{{PullPayment}}

{{ReentrancyGuard}}

{{Pausable}}
8 changes: 4 additions & 4 deletions docs/modules/ROOT/pages/utilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Easy!

Want to split some payments between multiple people? Maybe you have an app that sends 30% of art purchases to the original creator and 70% of the profits to the current owner; you can build that with xref:api:finance.adoc#PaymentSplitter[`PaymentSplitter`]!

In Solidity, there are some security concerns with blindly sending money to accounts, since it allows them to execute arbitrary code. You can read up on these security concerns in the https://consensys.github.io/smart-contract-best-practices/[Ethereum Smart Contract Best Practices] website.
In Solidity, there are some security concerns with blindly sending money to accounts, since it allows them to execute arbitrary code. You can read up on these security concerns in the https://consensys.github.io/smart-contract-best-practices/[Ethereum Smart Contract Best Practices] website. One of the ways to fix reentrancy and stalling problems is, instead of immediately sending Ether to accounts that need it, you can use xref:api:security.adoc#PullPayment[`PullPayment`], which offers an xref:api:security.adoc#PullPayment-_asyncTransfer-address-uint256-[`_asyncTransfer`] function for sending money to something and requesting that they xref:api:security.adoc#PullPayment-withdrawPayments-address-payable-[`withdrawPayments()`] it later.

[[collections]]
== Collections
Expand All @@ -101,7 +101,7 @@ Want to keep track of some numbers that increment by 1 every time you want anoth

=== Base64

xref:api:utils.adoc#Base64[`Base64`] util allows you to transform `bytes32` data into its Base64 `string` representation.
xref:api:utils.adoc#Base64[`Base64`] util allows you to transform `bytes32` data into its Base64 `string` representation.

This is especially useful for building URL-safe tokenURIs for both xref:api:token/ERC721.adoc#IERC721Metadata-tokenURI-uint256-[`ERC721`] or xref:api:token/ERC1155.adoc#IERC1155MetadataURI-uri-uint256-[`ERC1155`]. This library provides a clever way to serve URL-safe https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/Data_URIs/[Data URI] compliant strings to serve on-chain data structures.

Expand All @@ -120,7 +120,7 @@ contract My721Token is ERC721 {
using Strings for uint256;

constructor() ERC721("My721Token", "MTK") {}

...

function tokenURI(uint256 tokenId)
Expand All @@ -138,7 +138,7 @@ contract My721Token is ERC721 {

return string(
abi.encodePacked(
"data:application/json;base64,",
"data:application/json;base64,",
Base64.encode(dataURI)
)
);
Expand Down
52 changes: 52 additions & 0 deletions test/security/PullPayment.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { balance, ether } = require('@openzeppelin/test-helpers');

const { expect } = require('chai');

const PullPaymentMock = artifacts.require('$PullPayment');

contract('PullPayment', function (accounts) {
const [payer, payee1, payee2] = accounts;

const amount = ether('17');

beforeEach(async function () {
this.contract = await PullPaymentMock.new();
await web3.eth.sendTransaction({ from: payer, to: this.contract.address, value: amount });
});

describe('payments', function () {
it('can record an async payment correctly', async function () {
await this.contract.$_asyncTransfer(payee1, 100, { from: payer });
expect(await this.contract.payments(payee1)).to.be.bignumber.equal('100');
});

it('can add multiple balances on one account', async function () {
await this.contract.$_asyncTransfer(payee1, 200, { from: payer });
await this.contract.$_asyncTransfer(payee1, 300, { from: payer });
expect(await this.contract.payments(payee1)).to.be.bignumber.equal('500');
});

it('can add balances on multiple accounts', async function () {
await this.contract.$_asyncTransfer(payee1, 200, { from: payer });
await this.contract.$_asyncTransfer(payee2, 300, { from: payer });

expect(await this.contract.payments(payee1)).to.be.bignumber.equal('200');

expect(await this.contract.payments(payee2)).to.be.bignumber.equal('300');
});
});

describe('withdrawPayments', function () {
it('can withdraw payment', async function () {
const balanceTracker = await balance.tracker(payee1);

await this.contract.$_asyncTransfer(payee1, amount, { from: payer });
expect(await this.contract.payments(payee1)).to.be.bignumber.equal(amount);

await this.contract.withdrawPayments(payee1);

expect(await balanceTracker.delta()).to.be.bignumber.equal(amount);
expect(await this.contract.payments(payee1)).to.be.bignumber.equal('0');
});
});
});