Skip to content

Commit

Permalink
💚
Browse files Browse the repository at this point in the history
  • Loading branch information
lajarre committed Jan 31, 2023
1 parent 4e558d6 commit 0e42a74
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"name": "@buttergov/molten-core",
"version": "0.1.0",
"description": "Molten core contracts",
"files": ["out/"],
"files": [
"out/"
],
"directories": {
"lib": "lib",
"test": "test"
Expand Down
12 changes: 6 additions & 6 deletions src/MoltenCampaign.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract MoltenCampaignMarket {
// Threshold in daoToken-weis.
uint256 public threshold;

constructor (address daoTokenAddress, uint256 _threshold) {
constructor(address daoTokenAddress, uint256 _threshold) {
daoToken = IERC20Votes(daoTokenAddress);
threshold = _threshold;
}
Expand All @@ -19,11 +19,11 @@ contract MoltenCampaignMarket {
contract MoltenCampaign {
address public representative;
MoltenCampaignMarket public market;

uint256 public totalDeposited;
mapping(address => uint256) public deposited;

constructor (address marketAddress) {
constructor(address marketAddress) {
representative = msg.sender;
market = MoltenCampaignMarket(marketAddress);
}
Expand All @@ -32,14 +32,14 @@ contract MoltenCampaign {
return IERC20Votes(market.daoToken());
}

function deposit (uint256 amount) public {
function deposit(uint256 amount) public {
deposited[msg.sender] += amount;
totalDeposited += amount;

_getDaoToken().transferFrom(msg.sender, address(this), amount);
}
function refund () public {

function refund() public {
uint256 _deposited = deposited[msg.sender];

deposited[msg.sender] = 0;
Expand Down
61 changes: 34 additions & 27 deletions test/MoltenCampaign.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {ERC20VotesMintableMock, ERC20VotesMintableFailedMock} from "./helpers/ER
import {MoltenCampaign, MoltenCampaignMarket} from "../src/MoltenCampaign.sol";

contract CreationTest is Test {
MoltenCampaignMarket mcm;
ERC20VotesMintableMock daoToken;
uint256 threshold;
MoltenCampaignMarket public mcm;
ERC20VotesMintableMock public daoToken;
uint256 public threshold;

function setUp() public {
daoToken = new ERC20VotesMintableMock("DAO governance token", "GT");
Expand All @@ -31,42 +31,47 @@ contract CreationTest is Test {
}

abstract contract TestBase is Test {
ERC20VotesMintableMock daoToken;
uint256 threshold;
address representative = address(0x123);
MoltenCampaign mc;
ERC20VotesMintableMock public daoToken;
uint256 public threshold;
address public representative = address(0x123);
MoltenCampaign public mc;

function setUp() public virtual {
daoToken = new ERC20VotesMintableMock("DAO governance token", "GT");
threshold = 1;
MoltenCampaignMarket mcm = new MoltenCampaignMarket(
address(daoToken),
threshold);
threshold
);
vm.prank(representative);
mc = new MoltenCampaign(address(mcm));
}
}

abstract contract TestBaseFailing is Test {
ERC20VotesMintableFailedMock daoToken;
uint256 threshold;
address representative = address(0x123);
MoltenCampaign mc;
ERC20VotesMintableFailedMock public daoToken;
uint256 public threshold;
address public representative = address(0x123);
MoltenCampaign public mc;

function setUp() public virtual {
daoToken = new ERC20VotesMintableFailedMock("DAO governance token", "GT");
daoToken = new ERC20VotesMintableFailedMock(
"DAO governance token",
"GT"
);
threshold = 1;
MoltenCampaignMarket mcm = new MoltenCampaignMarket(
address(daoToken),
threshold);
threshold
);
vm.prank(representative);
mc = new MoltenCampaign(address(mcm));
}
}

contract DepositTest is TestBase {
address depositor;
address depositor2;
address public depositor;
address public depositor2;

function setUp() public override {
super.setUp();
Expand All @@ -78,7 +83,7 @@ contract DepositTest is TestBase {
function testSuccessfulDepositUpdatesDeposited() public {
vm.prank(depositor);
mc.deposit(333);

assertEq(mc.deposited(depositor), 333);
}

Expand All @@ -88,31 +93,33 @@ contract DepositTest is TestBase {

vm.prank(depositor2);
mc.deposit(222);

assertEq(mc.totalDeposited(), 555);
}

function testSuccessfulDepositCallsTransfer() public {
vm.prank(depositor);
mc.deposit(333);

(address from, address to, uint256 amount) = daoToken.transferFromCalledWith();
(address from, address to, uint256 amount) = daoToken
.transferFromCalledWith();
assertEq(from, depositor);
assertEq(to, address(mc));
assertEq(amount, 333);
}
}

contract DepositFailTest is TestBaseFailing {
address depositor;
address depositor2;
address public depositor;
address public depositor2;

function setUp() public override {
super.setUp();

depositor = address(0x331);
depositor2 = address(0x332);
}

function testUnsuccessfulDepositDoesntUpdate() public {
vm.prank(depositor);
vm.expectRevert("ERC20VotesMintableFailedMock transferFrom");
Expand All @@ -124,8 +131,8 @@ contract DepositFailTest is TestBaseFailing {
}

contract RefundTest is TestBase {
address depositor;
address depositor2;
address public depositor;
address public depositor2;

function setUp() public override {
super.setUp();
Expand All @@ -142,7 +149,6 @@ contract RefundTest is TestBase {
function testSuccessfulRefundUpdatesDeposited() public {
vm.prank(depositor);
mc.refund();

assertEq(mc.deposited(depositor), 0);
}

Expand All @@ -152,15 +158,16 @@ contract RefundTest is TestBase {

vm.prank(depositor2);
mc.refund();

assertEq(mc.totalDeposited(), 0);
}

function testSuccessfulRefundCallsTransfer() public {
vm.prank(depositor);
mc.refund();

(address from, address to, uint256 amount) = daoToken.transferFromCalledWith();
(address from, address to, uint256 amount) = daoToken
.transferFromCalledWith();
assertEq(from, address(mc));
assertEq(to, depositor);
assertEq(amount, 333);
Expand Down
10 changes: 8 additions & 2 deletions test/MoltenCampaignMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ contract CreationTest is Test {
}

function testHasDaoToken() public {
MoltenCampaignMarket mcm = new MoltenCampaignMarket(address(daoToken), 1);
MoltenCampaignMarket mcm = new MoltenCampaignMarket(
address(daoToken),
1
);

assertEq(address(mcm.daoToken()), address(daoToken));
}

function testHasThreshold(uint256 threshold) public {
MoltenCampaignMarket mcm = new MoltenCampaignMarket(address(daoToken), threshold);
MoltenCampaignMarket mcm = new MoltenCampaignMarket(
address(daoToken),
threshold
);

assertEq(mcm.threshold(), threshold);
}
Expand Down
14 changes: 11 additions & 3 deletions test/helpers/ERC20VotesMintable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ contract ERC20VotesMintableMock is ERC20Votes {
address to,
uint256 amount
) public override returns (bool) {
transferFromCalledWith = TransferFromCall({from: from, to: to, amount: amount});
transferFromCalledWith = TransferFromCall({
from: from,
to: to,
amount: amount
});
return true;
}
}
Expand All @@ -74,8 +78,12 @@ contract ERC20VotesMintableFailedMock is ERC20Votes {
address to,
uint256 amount
) public override returns (bool) {
transferFromCalledWith = TransferFromCall({from: from, to: to, amount: amount});
require(false, "ERC20VotesMintableFailedMock transferFrom");
transferFromCalledWith = TransferFromCall({
from: from,
to: to,
amount: amount
});
require(false, "ERC20VMFM transferFrom");
return true;
}
}

0 comments on commit 0e42a74

Please sign in to comment.