Skip to content

Commit

Permalink
update faucetsERC1155 constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
sxcamacho committed Sep 19, 2023
1 parent db1138b commit a4c030e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import {skipUnlessTestnet} from '../../utils/network';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployments, getNamedAccounts} = hre;
const {deploy} = deployments;
const {deployer, sandAdmin} = await getNamedAccounts();
await deploy('FaucetsERC1155', {
from: deployer,
log: true,
skipIfAlreadyDeployed: true,
args: [sandAdmin],
});
};

func.tags = ['FaucetsERC1155', 'FaucetsERC1155_deploy'];
func.skip = skipUnlessTestnet;

export default func;
5 changes: 4 additions & 1 deletion packages/core/src/solc_0.8/faucet/FaucetsERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ contract FaucetsERC1155 is Ownable, ERC1155Holder, ReentrancyGuard {
// Mapping from faucet address to its information.
mapping(address => FaucetInfo) private faucets;

constructor(address owner) Ownable() {
_transferOwnership(owner);
}

/**
* @dev Gets the period of a given faucet.
* @param faucet The address of the faucet.
Expand Down Expand Up @@ -84,7 +88,6 @@ contract FaucetsERC1155 is Ownable, ERC1155Holder, ReentrancyGuard {
emit LimitUpdated(faucet, newLimit);
}


// Modifier to check if the faucet exists.
modifier exists(address faucet) {
require(faucets[faucet].isFaucet, "Faucets: FAUCET_DOES_NOT_EXIST");
Expand Down
227 changes: 169 additions & 58 deletions packages/core/test/faucet/faucetsERC1155.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ethers } from 'hardhat';
import { expect } from '../chai-setup';
import { setupFaucetERC1155 } from './fixtures';
import { BigNumber, Contract, Signer } from 'ethers';
import { increaseTime } from '../utils';

describe("FaucetsERC1155", function () {
import {ethers} from 'hardhat';
import {expect} from '../chai-setup';
import {setupFaucetERC1155} from './fixtures';
import {BigNumber, Contract, Signer} from 'ethers';
import {increaseTime} from '../utils';

describe('FaucetsERC1155', function () {
let faucetsERC1155: Contract;
let mockAssetERC1155: Contract
let mockAssetERC1155: Contract;
let owner: Signer;
let user1: Signer;
let erc1155TokenIds: Array<BigNumber> = [];
Expand All @@ -16,145 +15,257 @@ describe("FaucetsERC1155", function () {
const faucetPeriod = 3600;
const faucetLimit = 100;


beforeEach(async function () {
const setup = await setupFaucetERC1155();
faucetsERC1155 = setup.faucetsERC1155;
mockAssetERC1155 = setup.mockAssetERC1155;
const { mintAssetERC1155 } = setup;
const {mintAssetERC1155} = setup;

[owner, user1] = await ethers.getSigners();
const ownerAddress = await owner.getAddress();

const { tokenId: tokenIdA } = await mintAssetERC1155({
const {tokenId: tokenIdA} = await mintAssetERC1155({
creatorAddress: ownerAddress,
packId: 1,
hash: '0x78b9f42c22c3c8b260b781578da3151e8200c741c6b7437bafaff5a9df9b403e',
hash:
'0x78b9f42c22c3c8b260b781578da3151e8200c741c6b7437bafaff5a9df9b403e',
supply: erc1155Amounts[0],
ownerAddress: ownerAddress,
data: '0x',
});

const { tokenId: tokenIdB } = await mintAssetERC1155({
const {tokenId: tokenIdB} = await mintAssetERC1155({
creatorAddress: ownerAddress,
packId: 2,
hash: '0x78b9f42c22c3c8b260b781578da3151e8200c741c6b7437bafaff5a9df9b404e',
hash:
'0x78b9f42c22c3c8b260b781578da3151e8200c741c6b7437bafaff5a9df9b404e',
supply: erc1155Amounts[1],
ownerAddress: ownerAddress,
data: '0x',
});

erc1155TokenIds = [tokenIdA, tokenIdB];

await faucetsERC1155.connect(owner).addFaucet(mockAssetERC1155.address, faucetPeriod, faucetLimit, erc1155TokenIds);
await mockAssetERC1155.connect(owner).safeBatchTransferFrom(ownerAddress, faucetsERC1155.address, erc1155TokenIds, erc1155Amounts, '0x');
await faucetsERC1155
.connect(owner)
.addFaucet(
mockAssetERC1155.address,
faucetPeriod,
faucetLimit,
erc1155TokenIds
);
await mockAssetERC1155
.connect(owner)
.safeBatchTransferFrom(
ownerAddress,
faucetsERC1155.address,
erc1155TokenIds,
erc1155Amounts,
'0x'
);
});

it("Should allow a user to claim ERC1155 tokens", async function () {
await faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
const user1Balance = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[0]);
it('Should allow a user to claim ERC1155 tokens', async function () {
await faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
const user1Balance = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[0]
);
expect(user1Balance).to.equal(faucetLimit);
});

it("Should not allow a user to claim more than the limit", async function () {
await expect(faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit + 1)).to.be.revertedWith("Faucets: AMOUNT_TOO_HIGH");
it('Should not allow a user to claim more than the limit', async function () {
await expect(
faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit + 1)
).to.be.revertedWith('Faucets: AMOUNT_TOO_HIGH');
});

it("Should not allow a user to claim before the faucet period expires", async function () {
await faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
await expect(faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit)).to.be.revertedWith("Faucets: CLAIM_PERIOD_NOT_PASSED");
it('Should not allow a user to claim before the faucet period expires', async function () {
await faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
await expect(
faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit)
).to.be.revertedWith('Faucets: CLAIM_PERIOD_NOT_PASSED');
});

it("Should allow a user to claim after the faucet period expires", async function () {
await faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
it('Should allow a user to claim after the faucet period expires', async function () {
await faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);

await increaseTime(faucetPeriod + 1, true);

await faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
const user1Balance = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[0]);
await faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[0], faucetLimit);
const user1Balance = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[0]
);
expect(user1Balance).to.equal(faucetLimit * 2);
});

it("Should not allow a user to claim if the faucet doesn't have enough tokens", async function () {
const highAmount = erc1155Amounts[1] + 1;
await expect(faucetsERC1155.connect(user1).claim(mockAssetERC1155.address, erc1155TokenIds[1], highAmount)).to.be.revertedWith("Faucets: BALANCE_IS_NOT_ENOUGH");
await expect(
faucetsERC1155
.connect(user1)
.claim(mockAssetERC1155.address, erc1155TokenIds[1], highAmount)
).to.be.revertedWith('Faucets: BALANCE_IS_NOT_ENOUGH');
});

it("Should correctly get the faucet's period", async function () {
const period = await faucetsERC1155.getPeriod(mockAssetERC1155.address);
expect(period).to.equal(faucetPeriod);
});

it("Should allow the owner to set a new faucet period", async function () {
it('Should allow the owner to set a new faucet period', async function () {
const newPeriod = 7200;
await faucetsERC1155.connect(owner).setPeriod(mockAssetERC1155.address, newPeriod);
await faucetsERC1155
.connect(owner)
.setPeriod(mockAssetERC1155.address, newPeriod);

const updatedPeriod = await faucetsERC1155.getPeriod(mockAssetERC1155.address);
const updatedPeriod = await faucetsERC1155.getPeriod(
mockAssetERC1155.address
);
expect(updatedPeriod).to.equal(newPeriod);
});

it("Should not allow a non-owner to set a new faucet period", async function () {
it('Should not allow a non-owner to set a new faucet period', async function () {
const newPeriod = 7200;
await expect(faucetsERC1155.connect(user1).setPeriod(mockAssetERC1155.address, newPeriod)).to.be.revertedWith("Ownable: caller is not the owner");
await expect(
faucetsERC1155
.connect(user1)
.setPeriod(mockAssetERC1155.address, newPeriod)
).to.be.revertedWith('Ownable: caller is not the owner');
});

it("Should allow the owner to withdraw all the faucet's tokens", async function () {
const faucetBalanceBefore = await mockAssetERC1155.balanceOf(faucetsERC1155.address, erc1155TokenIds[0]);
const faucetBalanceBefore = await mockAssetERC1155.balanceOf(
faucetsERC1155.address,
erc1155TokenIds[0]
);
expect(faucetBalanceBefore).to.be.gt(0);

await faucetsERC1155.connect(owner).withdraw(mockAssetERC1155.address, await owner.getAddress(), erc1155TokenIds);

const faucetBalanceAfter = await mockAssetERC1155.balanceOf(faucetsERC1155.address, erc1155TokenIds[0]);
const ownerBalanceAfter = await mockAssetERC1155.balanceOf(await owner.getAddress(), erc1155TokenIds[0]);
await faucetsERC1155
.connect(owner)
.withdraw(
mockAssetERC1155.address,
await owner.getAddress(),
erc1155TokenIds
);

const faucetBalanceAfter = await mockAssetERC1155.balanceOf(
faucetsERC1155.address,
erc1155TokenIds[0]
);
const ownerBalanceAfter = await mockAssetERC1155.balanceOf(
await owner.getAddress(),
erc1155TokenIds[0]
);

expect(faucetBalanceAfter).to.equal(0);
expect(ownerBalanceAfter).to.equal(faucetBalanceBefore);
});

it("Should not allow a non-owner to withdraw the faucet's tokens", async function () {
await expect(faucetsERC1155.connect(user1).withdraw(mockAssetERC1155.address, await user1.getAddress(), erc1155TokenIds)).to.be.revertedWith("Ownable: caller is not the owner");
await expect(
faucetsERC1155
.connect(user1)
.withdraw(
mockAssetERC1155.address,
await user1.getAddress(),
erc1155TokenIds
)
).to.be.revertedWith('Ownable: caller is not the owner');
});

it("Should allow a user to batch claim multiple ERC1155 tokens from a single faucet", async function () {
it('Should allow a user to batch claim multiple ERC1155 tokens from a single faucet', async function () {
const claimAmounts = [faucetLimit, erc1155Amounts[1] - 1];

await faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, claimAmounts);
await faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, claimAmounts);

const user1BalanceTokenA = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[0]);
const user1BalanceTokenB = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[1]);
const user1BalanceTokenA = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[0]
);
const user1BalanceTokenB = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[1]
);

expect(user1BalanceTokenA).to.equal(claimAmounts[0]);
expect(user1BalanceTokenB).to.equal(claimAmounts[1]);
});

it("Should not allow a user to batch claim amounts greater than the set limits", async function () {
it('Should not allow a user to batch claim amounts greater than the set limits', async function () {
const excessiveAmounts = [faucetLimit + 1, erc1155Amounts[1] + 1];

await expect(faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, excessiveAmounts)).to.be.revertedWith("Faucets: AMOUNT_TOO_HIGH");
await expect(
faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, excessiveAmounts)
).to.be.revertedWith('Faucets: AMOUNT_TOO_HIGH');
});

it("Should not allow a user to batch claim before the faucet period expires", async function () {
await faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);

await expect(faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10])).to.be.revertedWith("Faucets: CLAIM_PERIOD_NOT_PASSED");
it('Should not allow a user to batch claim before the faucet period expires', async function () {
await faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);

await expect(
faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, [
faucetLimit,
10,
])
).to.be.revertedWith('Faucets: CLAIM_PERIOD_NOT_PASSED');
});

it("Should allow a user to batch claim after the faucet period expires", async function () {
await faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);
it('Should allow a user to batch claim after the faucet period expires', async function () {
await faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);

await increaseTime(faucetPeriod + 1, true);

await faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);
await faucetsERC1155
.connect(user1)
.claimBatch(mockAssetERC1155.address, erc1155TokenIds, [faucetLimit, 10]);

const user1BalanceTokenA = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[0]);
const user1BalanceTokenB = await mockAssetERC1155.balanceOf(await user1.getAddress(), erc1155TokenIds[1]);
const user1BalanceTokenA = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[0]
);
const user1BalanceTokenB = await mockAssetERC1155.balanceOf(
await user1.getAddress(),
erc1155TokenIds[1]
);

expect(user1BalanceTokenA).to.equal(faucetLimit * 2);
expect(user1BalanceTokenB).to.equal(20);
});

it("Should revert if trying to batch claim for tokens not in the faucet", async function () {
const notInFaucetTokenId = BigNumber.from("9999");
await expect(faucetsERC1155.connect(user1).claimBatch(mockAssetERC1155.address, [notInFaucetTokenId], [faucetLimit])).to.be.revertedWith("Faucets: TOKEN_DOES_NOT_EXIST");
it('Should revert if trying to batch claim for tokens not in the faucet', async function () {
const notInFaucetTokenId = BigNumber.from('9999');
await expect(
faucetsERC1155
.connect(user1)
.claimBatch(
mockAssetERC1155.address,
[notInFaucetTokenId],
[faucetLimit]
)
).to.be.revertedWith('Faucets: TOKEN_DOES_NOT_EXIST');
});
});
Loading

1 comment on commit a4c030e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

82.96%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/core/src/solc_0.8.15/common/BaseWithStorage/ERC2771
   ERC2771HandlerUpgradeable.sol44.44%25%60%44.44%23, 27, 30, 39, 39, 39–40, 42
packages/core/src/solc_0.8.15/common/OperatorFilterer
   IOperatorFilterRegistry.sol100%100%100%100%
   UpdatableOperatorFiltererUpgradeable.sol11.11%10%25%9.52%18, 23–24, 24, 24–25, 27, 27, 27–28, 30, 38, 38, 38, 42, 42, 42–44, 46, 46, 46–47, 50, 55, 55, 55–56, 56, 56–57, 60, 68, 68, 68–69, 71
packages/core/src/solc_0.8.15/raffle
   DanceFight.sol75%50%100%100%20
   FistOfTheNorthStar.sol75%50%100%100%20
   GenericRaffle.sol61.32%41.11%58.62%81.72%177, 188–194, 224–228, 251–258, 265–266, 269, 287, 287–288, 288, 288–289, 307, 335–336, 346, 346–347, 347, 347–349, 357–358, 380, 389, 397–398, 408, 408–409, 418, 418, 420, 429, 429, 431, 441, 441–442, 452, 452–453, 464, 464–465, 565, 574, 625–626, 629, 632
   HellsKitchen.sol75%50%100%100%20
   MadBalls.sol75%50%100%100%20
   ParisHilton.sol75%50%100%100%20
   Rabbids.sol75%50%100%100%20
packages/core/src/solc_0.8/Game
   GameBaseToken.sol87.78%74.26%100%95.60%101, 139–140, 142–143, 185, 189, 189–190, 193–194, 213, 216–217, 231, 248, 251, 266, 286, 352, 415, 420, 436, 441–442, 442, 442–443, 485–486, 507–508, 510, 523, 523, 523, 523, 523, 542, 547, 55, 618, 81
   GameMinter.sol96.15%83.33%100%100%77
   GameV1.sol100%100%100%100%
packages/core/src/solc_0.8/OperatorFilterer/contracts
   OperatorFilterRegistrant.sol62.50%50%100%66.67%17, 19–20
packages/core/src/solc_0.8/OperatorFilterer/contracts/upgradeable
   DefaultOperatorFiltererUpgradeable.sol0%0%0%0%12, 12–13
   OperatorFiltererUpgradeable.sol88.64%85%100%90%16, 52–53, 62–63
packages/core/src/solc_0.8/OperatorFilterer/interfaces
   IOperatorFilterRegistry.sol100%100%100%100%
packages/core/src/solc_0.8/Sand
   SandBaseToken.sol100%100%100%100%
packages/core/src/solc_0.8/StarterPack
   PurchaseValidator.sol95.12%83.33%100%100%29, 37
   StarterPackV2.sol92.96%82.35%95.65%99.07%108–111, 309, 378, 67–72, 84
packages/core/src/solc_0.8/Utils
   Batch.sol85.48%59.09%100%100%34, 38, 44, 50, 56, 65, 75, 82, 88
packages/core/src/solc_0.8/asset
   AssetAttributesRegistry.sol66.91%56%83.33%71.43%103, 103, 103–104, 104, 109, 109, 109–112, 112, 112–113, 116, 118–119, 121, 123, 151–152, 160–161, 169–171, 185, 200–201, 201, 201–202, 202, 202–203, 205, 205, 205–206, 228, 232, 67, 69–70
   AssetMinter.sol85.71%73.75%87.50%94.34%113, 117, 137, 137, 137–138, 138, 141–142, 226, 235, 248, 284, 284–285, 287, 415, 44–46, 53–54, 65, 70, 75, 80, 85
   AssetSignedAuctionWithAuth.sol78%65.15%84.62%88.73%109, 111, 180, 180, 193, 202, 211, 227, 227, 240, 249, 258, 272, 285, 298, 303, 307–308, 311–312, 316, 346–347, 347, 347, 351–352, 352, 352, 373, 89
   AssetUpgrader.sol80.81%61.76%84.62%92.31%113–114, 122, 127, 168, 192–193, 195, 221, 221–222, 224, 232, 76–77, 96–97
packages/core/src/solc_0.8/asset/libraries
   AssetHelper.sol0%0%0%0%16–17, 17, 17–18, 18, 18–19, 35–38, 40, 47, 49, 51, 59–60, 62, 65–67, 67, 67–72, 72, 72–74, 78, 86, 88–90, 90, 90–91, 94
   ERC1155ERC721Helper.sol100%100%100%100%
packages/core/src/solc_0.8/assetERC1155
   AssetBaseERC1155.sol80.12%65.45%83.72%88.76%110, 112, 133, 152, 152, 152, 152, 152–153, 179, 190, 196, 204, 204, 204, 204, 204, 216, 218, 263, 301, 301, 301–304, 317–318, 339, 355, 355, 355–356, 358, 367, 369–370, 390, 437, 447–449, 449, 449–450, 452, 491,

Please sign in to comment.