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: Add ETH check to Claim #20

Open
wants to merge 2 commits 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
32 changes: 18 additions & 14 deletions test/Claim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { waffle } = require('hardhat');
const { provider } = waffle;
const { expect } = require('chai');
// ============ Internal Imports ============
const { eth, getBalances, contribute, placeBid } = require('./helpers/utils');
const { eth, getBalances, contribute, placeBid, claim } = require('./helpers/utils');
const { deployTestContractSetup, getTokenVault } = require('./helpers/deploy');
const {
MARKETS,
Expand All @@ -20,7 +20,7 @@ describe('Claim', async () => {
// get test case information
const { auctionReservePrice, contributions, bids, claims } = testCase;
// instantiate test vars
let partyBid, market, auctionId, token;
let partyBid, market, auctionId, token, gasPayingSigner;
const signers = provider.getWallets();
const firstSigner = signers[0];
const tokenId = 100;
Expand Down Expand Up @@ -61,12 +61,17 @@ describe('Claim', async () => {
);
}
}

const wallet = ethers.Wallet.createRandom()
gasPayingSigner = new ethers.Wallet(wallet.privateKey, provider);
await signers[0].sendTransaction({
to: gasPayingSigner.address,
value: eth(100)
});
});

it(`Reverts before Finalize`, async () => {
await expect(
partyBid.claim(firstSigner.address),
).to.be.revertedWith('PartyBid::claim: auction not finalized');
await expect(claim(gasPayingSigner, partyBid, firstSigner.address)).to.be.revertedWith('PartyBid::claim: auction not finalized');
});

it('Allows Finalize', async () => {
Expand All @@ -81,8 +86,8 @@ describe('Claim', async () => {
token = await getTokenVault(partyBid, firstSigner);
});

for (let claim of claims[marketName]) {
const { signerIndex, tokens, excessEth, totalContributed } = claim;
for (let expectedClaim of claims[marketName]) {
const { signerIndex, tokens, excessEth, totalContributed } = expectedClaim;
const contributor = signers[signerIndex];
it(`Allows Claim, transfers ETH and tokens to contributors after Finalize`, async () => {
const accounts = [
Expand All @@ -102,7 +107,7 @@ describe('Claim', async () => {
expect(before.contributor.tokens).to.equal(0);

// claim succeeds; event is emitted
await expect(partyBid.claim(contributor.address))
await expect(claim(gasPayingSigner, partyBid, contributor.address))
.to.emit(partyBid, 'Claimed')
.withArgs(
contributor.address,
Expand All @@ -117,10 +122,9 @@ describe('Claim', async () => {
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,
// );
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(
Expand All @@ -132,13 +136,13 @@ describe('Claim', async () => {
});

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

it(`Reverts on Claim for non-contributor`, async () => {
const randomAddress = '0xD115BFFAbbdd893A6f7ceA402e7338643Ced44a6';
await expect(partyBid.claim(randomAddress)).to.be.revertedWith(
await expect(claim(gasPayingSigner, partyBid, randomAddress)).to.be.revertedWith(
'PartyBid::claim: not a contributor',
);
});
Expand Down
173 changes: 43 additions & 130 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { expect } = require('chai');
const { FOURTY_EIGHT_HOURS_IN_SECONDS, MARKET_NAMES } = require('./constants');

function eth(num) {
Expand All @@ -13,105 +12,37 @@ function encodeData(contract, functionName, args) {
return contract.interface.encodeFunctionData(func, args);
}

async function getBalances(provider, token, accounts) {
const balances = {};
for (let account of accounts) {
const { name, address } = account;
balances[name] = {};
balances[name]['eth'] = parseFloat(
weiToEth(await provider.getBalance(address)),
);
let tokenBalance = 0;
if(token.address != ethers.constants.AddressZero) {
tokenBalance = parseFloat(
weiToEth(await token.balanceOf(address)),
);
}
balances[name]['tokens'] = tokenBalance;
}
return balances;
async function sendFromSigner(signer, contract, functionName, args, value = null) {
const data = encodeData(contract, functionName, args);
return signer.sendTransaction({
to: contract.address,
data,
value
});
}

function getTotalContributed(contributions) {
let totalContributed = 0;
contributions.map((contribution) => {
totalContributed += contribution.amount;
});
return totalContributed;
async function claim(signer, partyBidContract, contributor) {
return sendFromSigner(signer, partyBidContract, "claim", [contributor]);
}

async function approve(signer, tokenContract, to, tokenId) {
const data = encodeData(tokenContract, 'approve', [to, tokenId]);

return signer.sendTransaction({
to: tokenContract.address,
data,
});
return sendFromSigner(signer, tokenContract, "approve", [to, tokenId]);
}

async function placeBid(signer, marketContract, auctionId, value, marketName) {
let data;
if (marketName == MARKET_NAMES.ZORA) {
data = encodeData(marketContract, 'createBid', [auctionId, value]);
return sendFromSigner(signer, marketContract, "createBid", [auctionId, value], value);
} else {
data = encodeData(marketContract, 'placeBid', [auctionId]);
return sendFromSigner(signer, marketContract, "placeBid", [auctionId], value);
}

return signer.sendTransaction({
to: marketContract.address,
data,
value,
});
}

async function contribute(partyBidContract, contributorSigner, value) {
const data = encodeData(partyBidContract, 'contribute');

return contributorSigner.sendTransaction({
to: partyBidContract.address,
data,
value,
});
return sendFromSigner(contributorSigner, partyBidContract, "contribute", [], value);
}

async function redeem(partyBidContract, contributorSigner, amount) {
const data = encodeData(partyBidContract, 'redeem', [amount]);

return contributorSigner.sendTransaction({
to: partyBidContract.address,
data,
});
}

async function supportReseller(
partyBidContract,
contributorSigner,
reseller,
resellerCalldata,
) {
const data = encodeData(partyBidContract, 'supportReseller', [
reseller,
resellerCalldata,
]);

return contributorSigner.sendTransaction({
to: partyBidContract.address,
data,
});
}

async function transfer(
partyBidContract,
contributorSigner,
recipient,
amount,
) {
const data = encodeData(partyBidContract, 'transfer', [recipient, amount]);

return contributorSigner.sendTransaction({
to: partyBidContract.address,
data,
});
return sendFromSigner(contributorSigner, partyBidContract, "redeem", [amount]);
}

async function createZoraAuction(
Expand All @@ -123,7 +54,7 @@ async function createZoraAuction(
duration = FOURTY_EIGHT_HOURS_IN_SECONDS,
curatorFeePercentage = 0,
) {
const data = encodeData(marketContract, 'createAuction', [
return sendFromSigner(artist, marketContract, "createAuction", [
tokenId,
tokenContractAddress,
duration,
Expand All @@ -132,11 +63,6 @@ async function createZoraAuction(
curatorFeePercentage,
ethers.constants.AddressZero,
]);

return artist.sendTransaction({
to: marketContract.address,
data,
});
}

async function createReserveAuction(
Expand All @@ -146,16 +72,34 @@ async function createReserveAuction(
tokenId,
reservePrice,
) {
const data = encodeData(marketContract, 'createReserveAuction', [
nftContractAddress,
tokenId,
reservePrice,
]);
return sendFromSigner(artist, marketContract, "createReserveAuction", [nftContractAddress, tokenId, reservePrice]);
}

return artist.sendTransaction({
to: marketContract.address,
data,
async function getBalances(provider, token, accounts) {
const balances = {};
for (let account of accounts) {
const { name, address } = account;
balances[name] = {};
balances[name]['eth'] = parseFloat(
weiToEth(await provider.getBalance(address)),
);
let tokenBalance = 0;
if(token.address != ethers.constants.AddressZero) {
tokenBalance = parseFloat(
weiToEth(await token.balanceOf(address)),
);
}
balances[name]['tokens'] = tokenBalance;
}
return balances;
}

function getTotalContributed(contributions) {
let totalContributed = 0;
contributions.map((contribution) => {
totalContributed += contribution.amount;
});
return totalContributed;
}

function initExpectedTotalContributed(signers) {
Expand All @@ -166,35 +110,6 @@ function initExpectedTotalContributed(signers) {
return expectedTotalContributed;
}

// Validate state variables based on ETH amount added to contract
async function expectRedeemable(
provider,
partyBid,
ethAmountAdded,
ethAmountRedeemed,
) {
const redeemableEth = ethAmountAdded - ethAmountRedeemed;

// eth balance is equal to redeemableEth + excessContributions
const excessContributions = await partyBid.excessContributions();
const expectedBalance =
redeemableEth + parseFloat(weiToEth(excessContributions));
const ethBalance = await provider.getBalance(partyBid.address);
await expect(ethBalance).to.equal(eth(expectedBalance));

// redeemableEthBalance is equal to ethAmountAdded
const redeemableEthBalance = await partyBid.redeemableEthBalance();
await expect(redeemableEthBalance).to.equal(eth(redeemableEth));

// redeemAmount(tokenAmount) is expected portion
const tokenAmount = 100;
const totalSupply = await partyBid.totalSupply();
const expectedRedeemAmount =
redeemableEth * (tokenAmount / parseFloat(weiToEth(totalSupply)));
const redeemAmount = await partyBid.redeemAmount(eth(tokenAmount));
await expect(redeemAmount).to.equal(eth(expectedRedeemAmount));
}

module.exports = {
eth,
weiToEth,
Expand All @@ -204,11 +119,9 @@ module.exports = {
initExpectedTotalContributed,
approve,
contribute,
placeBid,
claim,
redeem,
supportReseller,
transfer,
placeBid,
createReserveAuction,
createZoraAuction,
expectRedeemable,
};