Skip to content

Commit

Permalink
Merge pull request #174 from open-dollar/transferOwner
Browse files Browse the repository at this point in the history
test transferOwnership
  • Loading branch information
daopunk authored Oct 11, 2023
2 parents 467b072 + 7dbe64b commit af45836
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 109 deletions.
4 changes: 2 additions & 2 deletions src/contracts/proxies/ODSafeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ contract ODSafeManager is IODSafeManager {
if (_dst == _sData.owner) revert AlreadySafeOwner();

_usrSafes[_sData.owner].remove(_safe);
_usrSafesPerCollat[_sData.owner][_sData.collateralType].remove(_safeId);
_usrSafesPerCollat[_sData.owner][_sData.collateralType].remove(_safe);

_usrSafes[_dst].add(_safe);
_usrSafesPerCollat[_dst][_sData.collateralType].add(_safeId);
_usrSafesPerCollat[_dst][_sData.collateralType].add(_safe);

_safeData[_safe].owner = _dst;

Expand Down
33 changes: 31 additions & 2 deletions test/nft/anvil/AnvilFork.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ contract AnvilFork is AnvilDeployment, Test {
debtCeiling = setDebtCeiling();
}

// setup functions
/**
* @dev setup functions
*/
function deployProxies() public {
proxies[0] = deployOrFind(ALICE);
proxies[1] = deployOrFind(BOB);
Expand Down Expand Up @@ -118,7 +120,9 @@ contract AnvilFork is AnvilDeployment, Test {
_debtCeiling = params.safeDebtCeiling;
}

// helper functions
/**
* @dev public helper functions
*/
function deployOrFind(address owner) public returns (address) {
address proxy = vault721.getProxy(owner);
if (proxy == address(0)) {
Expand Down Expand Up @@ -165,4 +169,29 @@ contract AnvilFork is AnvilDeployment, Test {
);
ODProxy(_proxy).execute(address(basicActions), payload);
}

/**
* @dev internal helper functions
*/
function checkProxyAddress() public {
for (uint256 i = 0; i < users.length; i++) {
assertEq(proxies[i], vault721.getProxy(users[i]));
}
}

function checkVaultIds() public {
uint256 vaultId = 1;

for (uint256 i = 0; i < users.length; i++) {
address user = users[i];
address proxy = vault721.getProxy(user);
assertEq(totalVaults / 3, vault721.balanceOf(user));

for (uint256 j = 0; j < cTypes.length; j++) {
assertEq(vaultId, vaultIds[proxy][cTypes[j]]);
assertEq(user, vault721.ownerOf(vaultId));
vaultId++;
}
}
}
}
29 changes: 2 additions & 27 deletions test/nft/anvil/NFTLocal.t.sol → test/nft/anvil/NFT.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ contract NFTAnvil is AnvilFork {
*/
function test_setup() public {
assertEq(totalVaults, vault721.totalSupply());
_checkProxyAddress();
_checkVaultIds();
checkProxyAddress();
checkVaultIds();
}

/**
Expand Down Expand Up @@ -147,29 +147,4 @@ contract NFTAnvil is AnvilFork {

assertEq(initBal, vault721.balanceOf(owner));
}

/**
* @dev internal helper functions
*/
function _checkProxyAddress() internal {
for (uint256 i = 0; i < users.length; i++) {
assertEq(proxies[i], vault721.getProxy(users[i]));
}
}

function _checkVaultIds() internal {
uint256 vaultId = 1;

for (uint256 i = 0; i < users.length; i++) {
address user = users[i];
address proxy = vault721.getProxy(user);
assertEq(totalVaults / 3, vault721.balanceOf(user));

for (uint256 j = 0; j < cTypes.length; j++) {
assertEq(vaultId, vaultIds[proxy][cTypes[j]]);
assertEq(user, vault721.ownerOf(vaultId));
vaultId++;
}
}
}
}
156 changes: 156 additions & 0 deletions test/nft/anvil/TransferOwnership.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;

import {AnvilFork} from '@test/nft/anvil/AnvilFork.t.sol';
import {WSTETH, ARB, CBETH, RETH, MAGIC} from '@script/GoerliParams.s.sol';
import {IERC20} from '@openzeppelin/token/ERC20/IERC20.sol';
import {SafeERC20} from '@openzeppelin/token/ERC20/utils/SafeERC20.sol';
import {Vault721} from '@contracts/proxies/Vault721.sol';
import {IODSafeManager} from '@interfaces/proxies/IODSafeManager.sol';
import {ISAFEEngine} from '@interfaces/ISAFEEngine.sol';

// forge t --fork-url $URL --match-contract TransferOwnershipAnvil -vvvvv

contract TransferOwnershipAnvil is AnvilFork {
using SafeERC20 for IERC20;

/**
* @dev enfore correct setup
*/
function test_setup() public {
assertEq(totalVaults, vault721.totalSupply());
checkProxyAddress();
checkVaultIds();
}

/**
* @dev unit tests
*/
function test_unit_transferVault() public {
uint256 vaultId = 1;
address owner = vault721.ownerOf(vaultId);
address proxy = vault721.getProxy(owner);
uint256 initBal = vault721.balanceOf(owner);
uint256[] memory safesBefore = safeManager.getSafes(proxy);
uint256 safesBeforeL = safesBefore.length;
emit log_named_uint('Safes length', safesBeforeL);

for (uint256 i = 0; i < safesBeforeL; i++) {
emit log_named_uint('Safe', safesBefore[i]);
}

address reciever = newUsers[0];

vm.startPrank(owner);
vault721.transferFrom(owner, reciever, vaultId);
vm.stopPrank();

uint256[] memory safesAfter = safeManager.getSafes(owner);
uint256 safesAfterL = safesAfter.length;
emit log_named_uint('Safes length', safesAfterL);

// this fails, but it should pass
assertEq(safesBeforeL - 1, safesAfterL);

// reciever should own transfered safe
assertEq(reciever, vault721.ownerOf(vaultId));

// owner should still own other safes
assertEq(owner, vault721.ownerOf(vaultId + 1));
assertEq(owner, vault721.ownerOf(vaultId + 2));
assertEq(owner, vault721.ownerOf(vaultId + 3));
assertEq(owner, vault721.ownerOf(vaultId + 4));

assertEq(initBal - 1, vault721.balanceOf(owner));
assertEq(1, vault721.balanceOf(reciever));
}

function test_unit_transferVault_toZero_Fail() public {
uint256 vaultId = 1;
address owner = vault721.ownerOf(vaultId);
uint256 initBal = vault721.balanceOf(owner);
uint256[] memory safesBefore = safeManager.getSafes(owner);
uint256 safesBeforeL = safesBefore.length;

address reciever = address(0);

vm.startPrank(owner);
vm.expectRevert('ERC721: transfer to the zero address');
vault721.transferFrom(owner, reciever, vaultId);
vm.stopPrank();

uint256[] memory safesAfter = safeManager.getSafes(owner);
uint256 safesAfterL = safesAfter.length;

assertEq(safesBeforeL, safesAfterL);
assertEq(initBal, vault721.balanceOf(owner));

// owner should still own all safes
assertEq(owner, vault721.ownerOf(vaultId));
assertEq(owner, vault721.ownerOf(vaultId + 1));
assertEq(owner, vault721.ownerOf(vaultId + 2));
assertEq(owner, vault721.ownerOf(vaultId + 3));
assertEq(owner, vault721.ownerOf(vaultId + 4));
}

/**
* @dev fuzz tests
*/
function test_fuzz_transferVault(uint256 vaultId) public {
vaultId = bound(vaultId, 1, totalVaults - 1);
address owner = vault721.ownerOf(vaultId);
address proxy = vault721.getProxy(owner);
uint256 initBal = vault721.balanceOf(owner);
uint256[] memory safesBefore = safeManager.getSafes(proxy);
uint256 safesBeforeL = safesBefore.length;
emit log_named_uint('Safes length', safesBeforeL);

for (uint256 i = 0; i < safesBeforeL; i++) {
emit log_named_uint('Safe', safesBefore[i]);
}

address reciever = newUsers[0];

vm.startPrank(owner);
vault721.transferFrom(owner, reciever, vaultId);
vm.stopPrank();

uint256[] memory safesAfter = safeManager.getSafes(owner);
uint256 safesAfterL = safesAfter.length;
emit log_named_uint('Safes length', safesAfterL);

for (uint256 i = 0; i < safesAfterL; i++) {
emit log_named_uint('Safe', safesAfter[i]);
}

// this fails, but it should pass
assertEq(safesBeforeL - 1, safesAfterL);

// reciever should own transfered safe
assertEq(reciever, vault721.ownerOf(vaultId));

assertEq(initBal - 1, vault721.balanceOf(owner));
assertEq(1, vault721.balanceOf(reciever));
}

function test_fuzz_transferVault_toZero_Fail(uint256 vaultId) public {
vaultId = bound(vaultId, 1, totalVaults - 1);
address owner = vault721.ownerOf(vaultId);
uint256 initBal = vault721.balanceOf(owner);
uint256[] memory safesBefore = safeManager.getSafes(owner);
uint256 safesBeforeL = safesBefore.length;

address reciever = address(0);

vm.startPrank(owner);
vm.expectRevert('ERC721: transfer to the zero address');
vault721.transferFrom(owner, reciever, vaultId);
vm.stopPrank();

uint256[] memory safesAfter = safeManager.getSafes(owner);
uint256 safesAfterL = safesAfter.length;

assertEq(safesBeforeL, safesAfterL);
assertEq(initBal, vault721.balanceOf(owner));
}
}
Loading

0 comments on commit af45836

Please sign in to comment.