Skip to content

Commit

Permalink
test: create mock contracts to move TS tests in LSP16 package
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Jan 29, 2024
1 parent d5e32e2 commit 3529b55
Show file tree
Hide file tree
Showing 15 changed files with 10,602 additions and 2,522 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ jobs:
"lsp20",
"lsp20init",
"lsp23",
"universalfactory",
"reentrancy",
"reentrancyinit",
"mocks",
Expand Down
5 changes: 5 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ max_test_rejects = 200_000
src = 'packages/LSP2ERC725YJSONSchema/contracts'
test = 'packages/LSP2ERC725YJSONSchema/foundry'
out = 'packages/LSP2ERC725YJSONSchema/contracts/foundry_artifacts'

[profile.lsp16]
src = 'packages/LSP16UniversalFactory/contracts'
test = 'packages/LSP16UniversalFactory/foundry'
out = 'packages/LSP16UniversalFactory/contracts/foundry_artifacts'
12,735 changes: 10,376 additions & 2,359 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@
"test:lsp20init": "hardhat test --no-compile tests/LSP20CallVerification/LSP6/LSP20WithLSP6Init.test.ts",
"test:lsp23": "hardhat test --no-compile tests/LSP23LinkedContractsDeployment/LSP23LinkedContractsDeployment.test.ts",
"test:lsp25": "hardhat test --no-compile tests/LSP25ExecuteRelayCall/LSP25MultiChannelNonce.test.ts",
"test:universalfactory": "hardhat test --no-compile tests/LSP16UniversalFactory/LSP16UniversalFactory.test.ts",
"test:reentrancy": "hardhat test --no-compile tests/Reentrancy/Reentrancy.test.ts",
"test:reentrancyinit": "hardhat test --no-compile tests/Reentrancy/ReentrancyInit.test.ts",
"test:foundry": "forge test --no-match-test Skip -vvv --gas-report > gasreport.ansi",
Expand Down
9 changes: 9 additions & 0 deletions packages/LSP16UniversalFactory/contracts/Mocks/Account.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import {ERC725} from "@erc725/smart-contracts/contracts/ERC725.sol";

contract Account is ERC725 {
// solhint-disable-next-line no-empty-blocks
constructor(address contractOwner) ERC725(contractOwner) {}
}
13 changes: 13 additions & 0 deletions packages/LSP16UniversalFactory/contracts/Mocks/AccountInit.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import {
ERC725InitAbstract
} from "@erc725/smart-contracts/contracts/ERC725InitAbstract.sol";

// solhint-disable-next-line no-empty-blocks
contract AccountInit is ERC725InitAbstract {
function initialize(address newOwner) public virtual initializer {
ERC725InitAbstract._initialize(newOwner);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.4;

contract ContractNoConstructor {
uint256 private _number = 5;

function getNumber() public view returns (uint256) {
return _number;
}

function setNumber(uint256 newNumber) public {
_number = newNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.4;

/**
* @dev sample contract used for testing
*/
contract FallbackInitializer {
address public caller;

receive() external payable {
_initialize();
}

fallback() external payable {
_initialize();
}

function _initialize() internal {
caller = msg.sender;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

contract NonPayableContract {
address private _owner;

constructor(address newOwner) {
_owner = newOwner;
}

function getOwner() public view returns (address) {
return _owner;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ import "forge-std/console.sol";

import {Address} from "@openzeppelin/contracts/utils/Address.sol";

import "lsp16/contracts/LSP16UniversalFactory.sol";
import "../../../contracts/Mocks/NonPayableFallback.sol";
import "../../../contracts/Mocks/FallbackInitializer.sol";
import "../../../contracts/LSP0ERC725Account/LSP0ERC725Account.sol";
import "../../../contracts/LSP0ERC725Account/LSP0ERC725AccountInit.sol";
import {LSP16UniversalFactory} from "../contracts/LSP16UniversalFactory.sol";
import {
NonPayableFallback
} from "../../../contracts/Mocks/NonPayableFallback.sol";
import {
FallbackInitializer
} from "../../../contracts/Mocks/FallbackInitializer.sol";
import {Account} from "../contracts/Mocks/Account.sol";
import {AccountInit} from "../contracts/Mocks/AccountInit.sol";

contract LSP16UniversalProfileTest is Test {
LSP16UniversalFactory public lsp16;
NonPayableFallback public nonPayableFallbackContract;
FallbackInitializer public fallbackInitializer;
LSP0ERC725Account public lsp0;
LSP0ERC725AccountInit public lsp0Init;

Account public account;
AccountInit public accountInit;

bytes public nonPayableFallbackBytecode =
type(NonPayableFallback).creationCode;
Expand All @@ -35,8 +40,9 @@ contract LSP16UniversalProfileTest is Test {

nonPayableFallbackContract = new NonPayableFallback();
fallbackInitializer = new FallbackInitializer();
lsp0Init = new LSP0ERC725AccountInit();
lsp0 = new LSP0ERC725Account(address(20));

account = new Account(address(20));
accountInit = new AccountInit();

uniqueInitializableSalt = lsp16.generateSalt(
randomBytes32ForSalt,
Expand Down Expand Up @@ -103,13 +109,13 @@ contract LSP16UniversalProfileTest is Test {
assert(salt != uniqueInitializableSalt);
}

function testdeployERC1167ProxyWithUPInit() public {
function testdeployERC1167ProxyWithAccountInit() public {
bytes32 salt = lsp16.generateSalt(bytes32(++testCounter), false, "");

(bool success, bytes memory returnData) = address(lsp16).call(
abi.encodeWithSignature(
"deployERC1167Proxy(address,bytes32)",
address(lsp0Init),
address(accountInit),
salt
)
);
Expand All @@ -120,7 +126,7 @@ contract LSP16UniversalProfileTest is Test {
);
}

function testdeployERC1167ProxyAndInitializeShouldNotKeepValueWithUPInit(
function testdeployERC1167ProxyAndInitializeShouldNotKeepValueWithAccountInit(
uint256 valueToTransfer,
bytes memory initializeCalldata
) public {
Expand All @@ -143,7 +149,7 @@ contract LSP16UniversalProfileTest is Test {
}(
abi.encodeWithSignature(
"deployERC1167ProxyAndInitialize(address,bytes32,bytes)",
address(lsp0Init),
address(accountInit),
salt,
lsp0Initbytes
)
Expand All @@ -156,7 +162,7 @@ contract LSP16UniversalProfileTest is Test {
assert(address(lsp16).balance == 0);
}

function testDeployCreate2ShouldNotKeepValueWithUP(
function testDeployCreate2ShouldNotKeepValueWithAccount(
uint256 valueToTransfer
) public {
vm.deal(address(this), valueToTransfer);
Expand All @@ -170,7 +176,7 @@ contract LSP16UniversalProfileTest is Test {
abi.encodeWithSignature(
"deployCreate2(bytes,bytes32)",
abi.encodePacked(
type(LSP0ERC725Account).creationCode,
type(Account).creationCode,
abi.encode(address(this))
),
salt
Expand All @@ -187,7 +193,7 @@ contract LSP16UniversalProfileTest is Test {
);
}

function testdeployCreate2AndInitializeShouldNotKeepValueWithUPInit(
function testdeployCreate2AndInitializeShouldNotKeepValueWithAccountInit(
uint128 valueForInitializer,
bytes4 initilializerBytes
) public {
Expand All @@ -205,7 +211,7 @@ contract LSP16UniversalProfileTest is Test {
}(
abi.encodeWithSignature(
"deployCreate2AndInitialize(bytes,bytes32,bytes,uint256,uint256)",
type(LSP0ERC725AccountInit).creationCode,
type(AccountInit).creationCode,
salt,
_removeRandomByteFromBytes4(initilializerBytes),
0, // constructor is not payable
Expand Down Expand Up @@ -267,7 +273,7 @@ contract LSP16UniversalProfileTest is Test {
(bool success, ) = address(lsp16).call{value: valueToTransfer}(
abi.encodeWithSignature(
"deployERC1167ProxyAndInitialize(address,bytes32,bytes)",
address(lsp0Init),
address(accountInit),
salt,
initializeCalldata
)
Expand Down Expand Up @@ -342,7 +348,7 @@ contract LSP16UniversalProfileTest is Test {
);
}

function testcomputeAddressShouldReturnCorrectUPAddressWithdeployCreate2AndInitialize(
function testcomputeAddressShouldReturnCorrectAccountAddressWithdeployCreate2AndInitialize(
bytes32 providedSalt,
uint256 valueForInitializer,
bytes4 initilializerBytes
Expand All @@ -355,7 +361,7 @@ contract LSP16UniversalProfileTest is Test {
);

address expectedAddress = lsp16.computeAddress(
keccak256(type(LSP0ERC725AccountInit).creationCode),
keccak256(type(AccountInit).creationCode),
providedSalt,
true,
initializeCallData
Expand All @@ -365,7 +371,7 @@ contract LSP16UniversalProfileTest is Test {
}(
abi.encodeWithSignature(
"deployCreate2AndInitialize(bytes,bytes32,bytes,uint256,uint256)",
type(LSP0ERC725AccountInit).creationCode,
type(AccountInit).creationCode,
providedSalt,
initializeCallData,
0,
Expand All @@ -383,7 +389,7 @@ contract LSP16UniversalProfileTest is Test {
assert(expectedAddress == returnedAddress);
}

function testcomputeAddressShouldReturnCorrectUPAddressWithDeployCreate2(
function testcomputeAddressShouldReturnCorrectAccountAddressWithDeployCreate2(
bytes32 providedSalt,
uint256 valueForConstructor
) public {
Expand All @@ -393,7 +399,7 @@ contract LSP16UniversalProfileTest is Test {
address expectedAddress = lsp16.computeAddress(
keccak256(
abi.encodePacked(
type(LSP0ERC725Account).creationCode,
type(Account).creationCode,
abi.encode(address(this))
)
),
Expand All @@ -407,7 +413,7 @@ contract LSP16UniversalProfileTest is Test {
abi.encodeWithSignature(
"deployCreate2(bytes,bytes32)",
abi.encodePacked(
type(LSP0ERC725Account).creationCode,
type(Account).creationCode,
abi.encode(address(this))
),
providedSalt
Expand Down Expand Up @@ -436,7 +442,7 @@ contract LSP16UniversalProfileTest is Test {
);

address expectedAddress = lsp16.computeERC1167Address(
address(lsp0Init),
address(accountInit),
providedSalt,
true,
initializeCallData
Expand All @@ -446,7 +452,7 @@ contract LSP16UniversalProfileTest is Test {
}(
abi.encodeWithSignature(
"deployERC1167ProxyAndInitialize(address,bytes32,bytes)",
address(lsp0Init),
address(accountInit),
providedSalt,
initializeCallData
)
Expand All @@ -465,15 +471,15 @@ contract LSP16UniversalProfileTest is Test {
bytes32 providedSalt
) public {
address expectedAddress = lsp16.computeERC1167Address(
address(lsp0),
address(account),
providedSalt,
false,
""
);
(bool success, bytes memory returnedData) = address(lsp16).call(
abi.encodeWithSignature(
"deployERC1167Proxy(address,bytes32)",
address(lsp0),
address(account),
providedSalt
)
);
Expand Down
6 changes: 5 additions & 1 deletion packages/LSP16UniversalFactory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
"clean": "hardhat clean",
"format": "prettier --write .",
"lint": "eslint . --ext .ts,.js",
"lint:solidity": "solhint 'contracts/**/*.sol' && prettier --check 'contracts/**/*.sol'"
"lint:solidity": "solhint 'contracts/**/*.sol' && prettier --check 'contracts/**/*.sol'",
"test": "hardhat test --no-compile tests/*.test.ts",
"test:foundry": "FOUNDRY_PROFILE=lsp16 forge test --no-match-test Skip -vvv"
},
"dependencies": {
"@openzeppelin/contracts": "^4.9.2"
},
"devDependencies": {
"@erc725/smart-contracts": "7.0.0",
"@openzeppelin/contracts-upgradeable": "4.9.3",
"@nomicfoundation/hardhat-toolbox": "^2.0.2",
"@typechain/ethers-v5": "^10.2.0",
"dotenv": "^16.0.3",
Expand Down
Loading

0 comments on commit 3529b55

Please sign in to comment.