Skip to content

Commit

Permalink
remove increase/decrease allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrowDom committed Nov 13, 2024
1 parent 2c96306 commit 4bf3360
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 161 deletions.
36 changes: 0 additions & 36 deletions contracts/contracts/echidna/EchidnaHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,42 +131,6 @@ contract EchidnaHelper is EchidnaSetup {
ousd.approve(spender, amount);
}

/**
* @notice Increase the allowance of an account to spend OUSD
* @param ownerAcc Account that owns the OUSD
* @param spenderAcc Account that is approved to spend the OUSD
* @param amount Amount to increase the allowance by
*/
function increaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);
hevm.prank(owner);
// slither-disable-next-line unused-return
ousd.increaseAllowance(spender, amount);
}

/**
* @notice Decrease the allowance of an account to spend OUSD
* @param ownerAcc Account that owns the OUSD
* @param spenderAcc Account that is approved to spend the OUSD
* @param amount Amount to decrease the allowance by
*/
function decreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);
hevm.prank(owner);
// slither-disable-next-line unused-return
ousd.decreaseAllowance(spender, amount);
}

/**
* @notice Get the sum of all OUSD balances
* @return total Total balance
Expand Down
42 changes: 0 additions & 42 deletions contracts/contracts/echidna/EchidnaTestApproval.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,4 @@ contract EchidnaTestApproval is EchidnaTestMintBurn {

assert(allowanceAfter2 == amount / 2);
}

/**
* @notice Increasing the allowance should raise it by the amount provided
* @param ownerAcc The account that is approving
* @param spenderAcc The account that is being approved
* @param amount The amount to approve
*/
function testIncreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);

uint256 allowanceBefore = ousd.allowance(owner, spender);
increaseAllowance(ownerAcc, spenderAcc, amount);
uint256 allowanceAfter = ousd.allowance(owner, spender);

assert(allowanceAfter == allowanceBefore + amount);
}

/**
* @notice Decreasing the allowance should lower it by the amount provided
* @param ownerAcc The account that is approving
* @param spenderAcc The account that is being approved
* @param amount The amount to approve
*/
function testDecreaseAllowance(
uint8 ownerAcc,
uint8 spenderAcc,
uint256 amount
) public {
address owner = getAccount(ownerAcc);
address spender = getAccount(spenderAcc);

uint256 allowanceBefore = ousd.allowance(owner, spender);
decreaseAllowance(ownerAcc, spenderAcc, amount);
uint256 allowanceAfter = ousd.allowance(owner, spender);

assert(allowanceAfter == allowanceBefore - amount);
}
}
4 changes: 2 additions & 2 deletions contracts/contracts/mocks/MockNonRebasing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ contract MockNonRebasing {
oUSD.transferFrom(_from, _to, _value);
}

function increaseAllowance(address _spender, uint256 _addedValue) public {
oUSD.increaseAllowance(_spender, _addedValue);
function approve(address _spender, uint256 _addedValue) public {
oUSD.approve(_spender, _addedValue);
}

function mintOusd(
Expand Down
42 changes: 0 additions & 42 deletions contracts/contracts/token/OUSD.sol
Original file line number Diff line number Diff line change
Expand Up @@ -364,48 +364,6 @@ contract OUSD is Governable {
return true;
}

/**
* @dev Increase the amount of tokens that an owner has allowed to
* `_spender`.
* This method should be used instead of approve() to avoid the double
* approval vulnerability described above.
* @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by.
*/
function increaseAllowance(address _spender, uint256 _addedValue)
external
returns (bool)
{
uint256 updatedAllowance = _allowances[msg.sender][_spender] +
_addedValue;
_allowances[msg.sender][_spender] = updatedAllowance;
emit Approval(msg.sender, _spender, updatedAllowance);
return true;
}

/**
* @dev Decrease the amount of tokens that an owner has allowed to
`_spender`.
* @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance
* by.
*/
function decreaseAllowance(address _spender, uint256 _subtractedValue)
external
returns (bool)
{
uint256 oldValue = _allowances[msg.sender][_spender];
uint256 newValue;
if (_subtractedValue >= oldValue) {
newValue = 0;
} else {
newValue = oldValue - _subtractedValue;
}
_allowances[msg.sender][_spender] = newValue;
emit Approval(msg.sender, _spender, newValue);
return true;
}

/**
* @dev Mints new tokens, increasing totalSupply.
*/
Expand Down
44 changes: 5 additions & 39 deletions contracts/test/token/ousd.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ describe("Token", function () {
// Give Josh an allowance to move Matt's OUSD
await ousd
.connect(matt)
.increaseAllowance(await josh.getAddress(), ousdUnits("100"));
.approve(await josh.getAddress(), ousdUnits("100"));

// Give contract 100 OUSD from Matt via Josh
await ousd
.connect(josh)
Expand Down Expand Up @@ -288,7 +289,7 @@ describe("Token", function () {
// Give Josh an allowance to move Matt's OUSD
await ousd
.connect(matt)
.increaseAllowance(await josh.getAddress(), ousdUnits("150"));
.approve(await josh.getAddress(), ousdUnits("150"));
// Give contract 100 OUSD from Matt via Josh
await ousd
.connect(josh)
Expand Down Expand Up @@ -333,7 +334,7 @@ describe("Token", function () {
await expect(matt).has.an.approxBalanceOf("100.00", ousd);
await expect(josh).has.an.approxBalanceOf("0", ousd);
await expect(mockNonRebasing).has.an.approxBalanceOf("100.00", ousd);
await mockNonRebasing.increaseAllowance(
await mockNonRebasing.approve(
await matt.getAddress(),
ousdUnits("100")
);
Expand Down Expand Up @@ -380,7 +381,7 @@ describe("Token", function () {
await expect(matt).has.an.approxBalanceOf("250", ousd);
await expect(mockNonRebasing).has.an.approxBalanceOf("150.00", ousd);
// Transfer contract balance to Josh
await mockNonRebasing.increaseAllowance(
await mockNonRebasing.approve(
await matt.getAddress(),
ousdUnits("150")
);
Expand Down Expand Up @@ -628,39 +629,6 @@ describe("Token", function () {
).to.be.revertedWith("panic code 0x11");
});

it("Should allow to increase/decrease allowance", async () => {
const { ousd, anna, matt } = fixture;
// Approve OUSD
await ousd.connect(matt).approve(anna.getAddress(), ousdUnits("1000"));
expect(
await ousd.allowance(await matt.getAddress(), await anna.getAddress())
).to.equal(ousdUnits("1000"));

// Decrease allowance
await ousd
.connect(matt)
.decreaseAllowance(await anna.getAddress(), ousdUnits("100"));
expect(
await ousd.allowance(await matt.getAddress(), await anna.getAddress())
).to.equal(ousdUnits("900"));

// Increase allowance
await ousd
.connect(matt)
.increaseAllowance(await anna.getAddress(), ousdUnits("20"));
expect(
await ousd.allowance(await matt.getAddress(), await anna.getAddress())
).to.equal(ousdUnits("920"));

// Decrease allowance more than what's there
await ousd
.connect(matt)
.decreaseAllowance(await anna.getAddress(), ousdUnits("950"));
expect(
await ousd.allowance(await matt.getAddress(), await anna.getAddress())
).to.equal(ousdUnits("0"));
});

it("Should increase users balance on supply increase", async () => {
const { ousd, usdc, vault, anna, matt } = fixture;
// Transfer 1 to Anna, so we can check different amounts
Expand Down Expand Up @@ -893,9 +861,7 @@ describe("Token", function () {
await expect(matt).has.an.approxBalanceOf("80.00", ousd);
await expect(anna).has.an.balanceOf("90", ousd);

console.log("Matt transfering to josh");
await ousd.connect(matt).transfer(josh.address, ousdUnits("80"));
console.log("Anna transfering to josh");
await ousd.connect(anna).transfer(josh.address, ousdUnits("90"));

await expect(josh).has.an.approxBalanceOf("400", ousd);
Expand Down

0 comments on commit 4bf3360

Please sign in to comment.