Skip to content

Commit

Permalink
Merge pull request #12 from thesis/renames
Browse files Browse the repository at this point in the history
Small renames: s/nonces/nonce, s/sender/spender
  • Loading branch information
dimpar authored Aug 27, 2021
2 parents a1384af + d809f74 commit 4985bcf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
34 changes: 17 additions & 17 deletions contracts/token/ERC20WithPermit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
/// @notice Returns the current nonce for EIP2612 permission for the
/// provided token owner for a replay protection. Used to construct
/// EIP2612 signature provided to `permit` function.
mapping(address => uint256) public override nonces;
mapping(address => uint256) public override nonce;

uint256 public immutable cachedChainId;
bytes32 public immutable cachedDomainSeparator;
Expand Down Expand Up @@ -73,29 +73,29 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
return true;
}

/// @notice Moves `amount` tokens from `sender` to `recipient` using the
/// @notice Moves `amount` tokens from `spender` to `recipient` using the
/// allowance mechanism. `amount` is then deducted from the caller's
/// allowance unless the allowance was made for `type(uint256).max`.
/// @return True if the operation succeeded, reverts otherwise.
/// @dev Requirements:
/// - `sender` and `recipient` cannot be the zero address,
/// - `sender` must have a balance of at least `amount`,
/// - the caller must have allowance for `sender`'s tokens of at least
/// - `spender` and `recipient` cannot be the zero address,
/// - `spender` must have a balance of at least `amount`,
/// - the caller must have allowance for `spender`'s tokens of at least
/// `amount`.
function transferFrom(
address sender,
address spender,
address recipient,
uint256 amount
) external override returns (bool) {
uint256 currentAllowance = allowance[sender][msg.sender];
uint256 currentAllowance = allowance[spender][msg.sender];
if (currentAllowance != type(uint256).max) {
require(
currentAllowance >= amount,
"Transfer amount exceeds allowance"
);
_approve(sender, msg.sender, currentAllowance - amount);
_approve(spender, msg.sender, currentAllowance - amount);
}
_transfer(sender, recipient, amount);
_transfer(spender, recipient, amount);
return true;
}

Expand Down Expand Up @@ -141,7 +141,7 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
owner,
spender,
amount,
nonces[owner]++,
nonce[owner]++,
deadline
)
)
Expand Down Expand Up @@ -289,21 +289,21 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
}

function _transfer(
address sender,
address spender,
address recipient,
uint256 amount
) private {
require(sender != address(0), "Transfer from the zero address");
require(spender != address(0), "Transfer from the zero address");
require(recipient != address(0), "Transfer to the zero address");
require(recipient != address(this), "Transfer to the token address");

beforeTokenTransfer(sender, recipient, amount);
beforeTokenTransfer(spender, recipient, amount);

uint256 senderBalance = balanceOf[sender];
require(senderBalance >= amount, "Transfer amount exceeds balance");
balanceOf[sender] = senderBalance - amount;
uint256 spenderBalance = balanceOf[spender];
require(spenderBalance >= amount, "Transfer amount exceeds balance");
balanceOf[spender] = spenderBalance - amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
emit Transfer(spender, recipient, amount);
}

function _approve(
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/IERC20WithPermit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface IERC20WithPermit is IERC20, IERC20Metadata, IApproveAndCall {
/// @notice Returns the current nonce for EIP2612 permission for the
/// provided token owner for a replay protection. Used to construct
/// EIP2612 signature provided to `permit` function.
function nonces(address owner) external view returns (uint256);
function nonce(address owner) external view returns (uint256);

/// @notice Returns EIP2612 Permit message hash. Used to construct EIP2612
/// signature provided to `permit` function.
Expand Down
16 changes: 8 additions & 8 deletions test/token/ERC20WithPermit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("ERC20WithPermit", () => {

describe("transfer", () => {
context("when the recipient is not the zero address", () => {
context("when the sender does not have enough balance", () => {
context("when the spender does not have enough balance", () => {
const amount = initialSupply.add(1)

it("should revert", async () => {
Expand All @@ -113,7 +113,7 @@ describe("ERC20WithPermit", () => {
})
})

context("when the sender transfers all balance", () => {
context("when the spender transfers all balance", () => {
const amount = initialSupply

let tx
Expand Down Expand Up @@ -142,7 +142,7 @@ describe("ERC20WithPermit", () => {
})
})

context("when the sender transfers zero tokens", () => {
context("when the spender transfers zero tokens", () => {
const amount = ethers.BigNumber.from(0)

let tx
Expand Down Expand Up @@ -401,7 +401,7 @@ describe("ERC20WithPermit", () => {

describe("approve", () => {
context("when the spender is not the zero address", () => {
context("when the sender has enough balance", () => {
context("when the spender has enough balance", () => {
const allowance = initialSupply

it("should emit an approval event", async () => {
Expand Down Expand Up @@ -452,7 +452,7 @@ describe("ERC20WithPermit", () => {
})
})

context("when the sender does not have enough balance", () => {
context("when the spender does not have enough balance", () => {
const allowance = initialSupply.add(1)

it("should emit an approval event", async () => {
Expand Down Expand Up @@ -729,7 +729,7 @@ describe("ERC20WithPermit", () => {

const domainSeparator = await token.DOMAIN_SEPARATOR()
const permitTypehash = await token.PERMIT_TYPEHASH()
const nonce = await token.nonces(permittingHolder.address)
const nonce = await token.nonce(permittingHolder.address)

const approvalDigest = ethers.utils.keccak256(
ethers.utils.solidityPack(
Expand Down Expand Up @@ -816,7 +816,7 @@ describe("ERC20WithPermit", () => {
})

context("when the spender is not the zero address", () => {
context("when the sender has enough balance", () => {
context("when the spender has enough balance", () => {
const allowance = permittingHolderBalance
it("should emit an approval event", async () => {
const deadline = tomorrow
Expand Down Expand Up @@ -930,7 +930,7 @@ describe("ERC20WithPermit", () => {
})
})

context("when the sender does not have enough balance", () => {
context("when the spender does not have enough balance", () => {
const allowance = permittingHolderBalance.add(1)
it("should emit an approval event", async () => {
const deadline = tomorrow
Expand Down

0 comments on commit 4985bcf

Please sign in to comment.