Skip to content

Commit

Permalink
refactor: update deploy scripts and integration tests to make better …
Browse files Browse the repository at this point in the history
…use of common scripts
  • Loading branch information
bagelface committed Dec 26, 2024
1 parent e5f942f commit 69bacfd
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 290 deletions.
46 changes: 24 additions & 22 deletions script/Common.sol
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import {IERC20} from '@openzeppelin/token/ERC20/IERC20.sol';
import {Greeter, IGreeter} from 'contracts/Greeter.sol';
import {ProxyAdmin} from '@openzeppelin/proxy/transparent/ProxyAdmin.sol';
import {TransparentUpgradeableProxy} from '@openzeppelin/proxy/transparent/TransparentUpgradeableProxy.sol';
import {Script} from 'forge-std/Script.sol';
// solhint-disable-next-line
import 'script/Registry.sol';

import {SavingCircles} from '../src/contracts/SavingCircles.sol';

/**
* @title Common Contract
* @author Breadchain
* @notice This contract is used to deploy the Greeter contract
* @notice This contract is used to deploy an upgradable Saving Circles contract
* @dev This contract is intended for use in Scripts and Integration Tests
*/
contract Common is Script {
struct DeploymentParams {
string greeting;
IERC20 token;
}

IGreeter public greeter;
function setUp() public virtual {}

/// @notice Deployment parameters for each chain
mapping(uint256 _chainId => DeploymentParams _params) internal _deploymentParams;

function setUp() public virtual {
// Optimism
_deploymentParams[10] = DeploymentParams('Hello, Optimism!', IERC20(OPTIMISM_DAI));
function _deploySavingCircles() internal returns (SavingCircles) {
return new SavingCircles();
}

// Gnosis
_deploymentParams[100] = DeploymentParams('Hello, Gnosis!', IERC20(GNOSIS_BREAD));
function _deployProxyAdmin(address _admin) internal returns (ProxyAdmin) {
return new ProxyAdmin(_admin);
}

function _deployContracts() internal {
DeploymentParams memory _params = _deploymentParams[block.chainid];
function _deployTransparentProxy(
address _implementation,
address _proxyAdmin,
bytes memory _initData
) internal returns (TransparentUpgradeableProxy) {
return new TransparentUpgradeableProxy(_implementation, _proxyAdmin, _initData);
}

greeter = new Greeter(_params.greeting, _params.token);
function _deployContracts(address _admin) internal returns (TransparentUpgradeableProxy) {
return _deployTransparentProxy(
address(_deploySavingCircles()),
address(_deployProxyAdmin(_admin)),
abi.encodeWithSelector(SavingCircles.initialize.selector, _admin)
);
}
}
4 changes: 2 additions & 2 deletions script/Deploy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ pragma solidity 0.8.28;
import {Common} from 'script/Common.sol';

contract Deploy is Common {
function run() public {
function run(address _admin) public {
vm.startBroadcast();

_deployContracts();
_deployContracts(_admin);

vm.stopBroadcast();
}
Expand Down
30 changes: 0 additions & 30 deletions script/DeploySavingsCircle.s.sol

This file was deleted.

28 changes: 0 additions & 28 deletions test/integration/Greeter.t.sol

This file was deleted.

69 changes: 61 additions & 8 deletions test/integration/IntegrationBase.sol
Original file line number Diff line number Diff line change
@@ -1,30 +1,83 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import {IERC20} from '@openzeppelin/token/ERC20/IERC20.sol';
import {Test} from 'forge-std/Test.sol';
import {Common} from 'script/Common.sol';

import {SavingCircles} from '../../src/contracts/SavingCircles.sol';
import {ISavingCircles} from '../../src/interfaces/ISavingCircles.sol';
import {MockERC20} from '../mocks/MockERC20.sol';

// solhint-disable-next-line
import 'script/Registry.sol';

contract IntegrationBase is Common, Test {
uint256 public constant INIT_BALANCE = 1 ether;
SavingCircles public circle;
MockERC20 public token;

address public user = makeAddr('user');
address public alice = makeAddr('alice');
address public bob = makeAddr('bob');
address public carol = makeAddr('carol');
address public owner = makeAddr('owner');
address[] public members;

ISavingCircles.Circle public baseCircle;

IERC20 public bread = IERC20(GNOSIS_BREAD);
string public constant BASE_CIRCLE_NAME = 'Test Circle';
uint256 public constant DEPOSIT_AMOUNT = 1000e18;
uint256 public constant DEPOSIT_INTERVAL = 7 days;
uint256 public constant BASE_CURRENT_INDEX = 0;
uint256 public constant BASE_MAX_DEPOSITS = 1000;
bytes32 public constant BASE_CIRCLE_ID = keccak256(abi.encodePacked(BASE_CIRCLE_NAME));

function setUp() public virtual override {
super.setUp();
vm.createSelectFork(vm.rpcUrl('gnosis'));

vm.startPrank(owner);
circle = SavingCircles(address(_deployContracts(owner)));
token = new MockERC20('Test Token', 'TEST');
vm.stopPrank();

_setUpAccounts();

baseCircle = ISavingCircles.Circle({
owner: alice,
name: BASE_CIRCLE_NAME,
members: members,
currentIndex: BASE_CURRENT_INDEX,
circleStart: block.timestamp,
token: address(token),
depositAmount: DEPOSIT_AMOUNT,
depositInterval: DEPOSIT_INTERVAL,
maxDeposits: BASE_MAX_DEPOSITS
});
}

/// @dev deploy contracts methods are located in script/Common.sol
_deployContracts();
function createBaseCircle() public {
vm.prank(owner);
circle.setTokenAllowed(address(token), true);

vm.prank(alice);
circle.create(baseCircle);
}

function _setUpAccounts() internal {
vm.startPrank(alice);
token.mint(alice, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(alice);
vm.stopPrank();

vm.startPrank(bob);
token.mint(bob, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(bob);
vm.stopPrank();

vm.startPrank(carol);
token.mint(carol, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(carol);
vm.stopPrank();
deal(GNOSIS_BREAD, address(greeter), INIT_BALANCE);
}
}
78 changes: 4 additions & 74 deletions test/integration/SavingCircles.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,14 @@ import {ProxyAdmin} from '@openzeppelin/proxy/transparent/ProxyAdmin.sol';
import {TransparentUpgradeableProxy} from '@openzeppelin/proxy/transparent/TransparentUpgradeableProxy.sol';

Check warning on line 6 in test/integration/SavingCircles.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "TransparentUpgradeableProxy" is unused
import {Test} from 'forge-std/Test.sol';

Check warning on line 7 in test/integration/SavingCircles.t.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Variable "Test" is unused

import {SavingCircles} from '../../src/contracts/SavingCircles.sol';
import {ISavingCircles} from '../../src/interfaces/ISavingCircles.sol';
import {MockERC20} from '../mocks/MockERC20.sol';
import {IntegrationBase} from 'test/integration/IntegrationBase.sol';

/* solhint-disable func-name-mixedcase */

contract SavingCirclesIntegration is Test {
SavingCircles public circle;
MockERC20 public token;

address public alice = makeAddr('alice');
address public bob = makeAddr('bob');
address public carol = makeAddr('carol');
address public owner = makeAddr('owner');
address[] public members;

string public constant BASE_CIRCLE_NAME = 'Test Circle';
uint256 public constant DEPOSIT_AMOUNT = 1000e18;
uint256 public constant DEPOSIT_INTERVAL = 7 days;
uint256 public constant BASE_CURRENT_INDEX = 0;
uint256 public constant BASE_MAX_DEPOSITS = 1000;
bytes32 public constant BASE_CIRCLE_ID = keccak256(abi.encodePacked(BASE_CIRCLE_NAME));

ISavingCircles.Circle public baseCircle;

function setUp() public {
vm.startPrank(owner);
circle = SavingCircles(
address(
new TransparentUpgradeableProxy(
address(new SavingCircles()),
address(new ProxyAdmin(owner)),
abi.encodeWithSelector(SavingCircles.initialize.selector, owner)
)
)
);

token = new MockERC20('Test Token', 'TEST');
vm.stopPrank();

// Setup test accounts
vm.startPrank(alice);
token.mint(alice, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(alice);
vm.stopPrank();

vm.startPrank(bob);
token.mint(bob, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(bob);
vm.stopPrank();

vm.startPrank(carol);
token.mint(carol, DEPOSIT_AMOUNT * 10);
token.approve(address(circle), type(uint256).max);
members.push(carol);
vm.stopPrank();

baseCircle = ISavingCircles.Circle({
owner: alice,
name: BASE_CIRCLE_NAME,
members: members,
currentIndex: BASE_CURRENT_INDEX,
circleStart: block.timestamp,
token: address(token),
depositAmount: DEPOSIT_AMOUNT,
depositInterval: DEPOSIT_INTERVAL,
maxDeposits: BASE_MAX_DEPOSITS
});
}

function createBaseCircle() public {
vm.prank(owner);
circle.setTokenAllowed(address(token), true);

vm.prank(alice);
circle.create(baseCircle);
contract SavingCirclesIntegration is IntegrationBase {
function setUp() public override {
super.setUp();
}

function test_SetTokenAllowed() public {
Expand Down
Loading

0 comments on commit 69bacfd

Please sign in to comment.