Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) Test: sending Raw ETH to contract #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ module.exports = {
},

networks: {
hardhat: {
gasPrice: 0,
},
hardhat: {},
localhost: {
url: 'http://localhost:8545',
},
Expand Down
167 changes: 167 additions & 0 deletions test/custom/RawETH.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// SPDX-License-Identifier: MIT
// ============ External Imports ============
const { waffle } = require('hardhat');
const { provider } = waffle;
const { expect } = require('chai');
// ============ Internal Imports ============
const { eth, getBalances, bidThroughParty, contribute, placeBid } = require('../helpers/utils');
const { deployTestContractSetup, getTokenVault } = require('../helpers/deploy');
const {
MARKETS,
FOURTY_EIGHT_HOURS_IN_SECONDS,
} = require('../helpers/constants');
const { testCases } = require('../testCases.json');

describe('Sending Raw ETH', async () => {
MARKETS.map((marketName) => {
describe(marketName, async () => {
testCases.map((testCase, i) => {
describe(`Case ${i}`, async () => {
// get test case information
const { auctionReservePrice, contributions, bids, claims } = testCase;
// instantiate test vars
let partyBid, market, auctionId, token;
const signers = provider.getWallets();
const firstSigner = signers[0];
const tokenId = 95;

before(async () => {
// DEPLOY NFT, MARKET, AND PARTY BID CONTRACTS
const contracts = await deployTestContractSetup(
marketName,
provider,
firstSigner,
tokenId,
auctionReservePrice,
);
partyBid = contracts.partyBid;
market = contracts.market;

auctionId = await partyBid.auctionId();
});

it(`Has zero balance to begin with`, async () => {
const balance = await provider.getBalance(partyBid.address);
await expect(balance).to.equal(eth(0));
});

it(`Has 100 balance after raw send, but 0 total contributed`, async () => {
// Send raw ETH into the contract
await signers[0].sendTransaction({
to: partyBid.address,
value: eth(100),
});

const balance = await provider.getBalance(partyBid.address);
await expect(balance).to.equal(eth(100));

const totalContributedToParty = await partyBid.totalContributedToParty();
await expect(totalContributedToParty).to.equal(eth(0));

const totalContributed = await partyBid.totalContributed(signers[0].address);
await expect(totalContributed).to.equal(eth(0));
});


it('Allows contribute, bid, and finalize', async () => {
// submit contributions before bidding begins
for (let contribution of contributions) {
const { signerIndex, amount } = contribution;
const signer = signers[signerIndex];
await contribute(partyBid, signer, eth(amount));
}

// submit the valid bids in order
for (let bid of bids) {
const { placedByPartyBid, amount, success } = bid;
if (success && placedByPartyBid) {
const { signerIndex } = contributions[0];
await bidThroughParty(partyBid, signers[signerIndex]);
} else if (success && !placedByPartyBid) {
await placeBid(
firstSigner,
market,
auctionId,
eth(amount),
marketName,
);
}
}

// increase time on-chain so that auction can be finalized
await provider.send('evm_increaseTime', [
FOURTY_EIGHT_HOURS_IN_SECONDS,
]);
await provider.send('evm_mine');

// finalize auction
await expect(partyBid.finalize()).to.emit(partyBid, 'Finalized');
token = await getTokenVault(partyBid, firstSigner);
});

for (let claim of claims[marketName]) {
const { signerIndex, tokens, excessEth, totalContributed } = claim;
const contributor = signers[signerIndex];
it(`Allows Claim, transfers ETH and tokens to contributors after Finalize`, async () => {
const accounts = [
{
name: 'partyBid',
address: partyBid.address,
},
{
name: 'contributor',
address: contributor.address,
},
];

const before = await getBalances(provider, token, accounts);

// signer has no PartyBid tokens before claim
expect(before.contributor.tokens).to.equal(0);

// claim succeeds; event is emitted
await expect(partyBid.claim(contributor.address))
.to.emit(partyBid, 'Claimed')
.withArgs(
contributor.address,
eth(totalContributed),
eth(excessEth),
eth(tokens),
);

const after = await getBalances(provider, token, accounts);

// ETH was transferred from PartyBid to contributor
await expect(after.partyBid.eth).to.equal(
before.partyBid.eth - excessEth,
);
// TODO: fix this test (hardhat gasPrice zero not working)
// await expect(after.contributor.eth).to.equal(
// before.contributor.eth + excessEth,
// );

// Tokens were transferred from PartyBid to contributor
await expect(after.partyBid.tokens).to.equal(
before.partyBid.tokens - tokens,
);
await expect(after.contributor.tokens).to.equal(
before.contributor.tokens + tokens,
);
});

it(`Does not allow a contributor to double-claim`, async () => {
await expect(partyBid.claim(contributor.address)).to.be.reverted;
});
}

it(`Reverts on Claim for non-contributor`, async () => {
const randomAddress = '0xD115BFFAbbdd893A6f7ceA402e7338643Ced44a6';
await expect(partyBid.claim(randomAddress)).to.be.revertedWith(
'PartyBid::claim: not a contributor',
);
});
});
});
});
});
});