Skip to content

Commit

Permalink
add IDispenserProvider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
YouStillAlive committed Mar 21, 2024
1 parent ad14e28 commit 001bd07
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
13 changes: 10 additions & 3 deletions contracts/DispenserProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@poolzfinance/lockdeal-nft/contracts/SimpleProviders/DealProvider/DealProvider.sol";
import "./interfaces/IDispenserProvider.sol";
import "./DispenserState.sol";

contract DispenserProvider is DealProvider, DispenserState {
contract DispenserProvider is IDispenserProvider, DealProvider, DispenserState {
using ECDSA for bytes32;

constructor(ILockDealNFT _lockDealNFT) DealProvider(_lockDealNFT) {
name = "DispenserProvider";
}

function createLock(
function dispenseLock(
uint256 poolId,
uint256 validUntil,
address owner,
Builder[] calldata data,
bytes memory signature
bytes calldata signature
) external validProviderId(poolId) {
require(
msg.sender == owner ||
Expand Down Expand Up @@ -88,4 +89,10 @@ contract DispenserProvider is DealProvider, DispenserState {
address recoveredSigner = hash.recover(signature);
success = recoveredSigner == signer;
}

function supportsInterface(
bytes4 interfaceId
) public view virtual override returns (bool) {
return interfaceId == type(IDispenserProvider).interfaceId || interfaceId == type(IERC165).interfaceId;
}
}
6 changes: 0 additions & 6 deletions contracts/DispenserState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ pragma solidity ^0.8.0;

import "@poolzfinance/poolz-helper-v2/contracts/interfaces/ILockDealNFT.sol";
import "@poolzfinance/poolz-helper-v2/contracts/interfaces/IVaultManager.sol";
import "@poolzfinance/poolz-helper-v2/contracts/interfaces/ISimpleProvider.sol";

contract DispenserState {
mapping(uint256 => mapping(address => bool)) public isTaken;

struct Builder {
ISimpleProvider simpleProvider;
uint256[] params;
}
}
19 changes: 19 additions & 0 deletions contracts/interfaces/IDispenserProvider.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@poolzfinance/poolz-helper-v2/contracts/interfaces/ISimpleProvider.sol";

interface IDispenserProvider {
struct Builder {
ISimpleProvider simpleProvider;
uint256[] params;
}

function dispenseLock(
uint256 poolId,
uint256 validUntil,
address owner,
Builder[] calldata data,
bytes calldata signature
) external;
}
22 changes: 15 additions & 7 deletions test/DispenserProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("Dispenser Provider tests", function () {
it("should deacrease leftAmount after lock", async () => {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
await dispenserProvider.connect(user).createLock(poolId, validTime, user.address, usersData, signature)
await dispenserProvider.connect(user).dispenseLock(poolId, validTime, user.address, usersData, signature)
expect(await dispenserProvider.poolIdToAmount(poolId)).to.equal(amount.div(2))
})

Expand All @@ -89,7 +89,7 @@ describe("Dispenser Provider tests", function () {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
const beforeBalance = await token.balanceOf(user.address)
await dispenserProvider.connect(user).createLock(poolId, validTime, user.address, usersData, signature)
await dispenserProvider.connect(user).dispenseLock(poolId, validTime, user.address, usersData, signature)
// check if user has tokens after the transfer
expect(await token.balanceOf(user.address)).to.equal(beforeBalance.add(amount))
})
Expand All @@ -99,7 +99,7 @@ describe("Dispenser Provider tests", function () {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
await expect(
dispenserProvider.connect(owner).createLock(poolId, validTime, user.address, usersData, signature)
dispenserProvider.connect(owner).dispenseLock(poolId, validTime, user.address, usersData, signature)
).to.not.reverted
await lockDealNFT.connect(user).setApprovalForAll(owner.address, false)
})
Expand All @@ -109,16 +109,16 @@ describe("Dispenser Provider tests", function () {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
await expect(
dispenserProvider.connect(owner).createLock(poolId, validTime, user.address, usersData, signature)
dispenserProvider.connect(owner).dispenseLock(poolId, validTime, user.address, usersData, signature)
).to.not.reverted
})

it("should revert double creation", async () => {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
await dispenserProvider.connect(user).createLock(poolId, validTime, user.address, usersData, signature)
await dispenserProvider.connect(user).dispenseLock(poolId, validTime, user.address, usersData, signature)
await expect(
dispenserProvider.connect(user).createLock(poolId, validTime, user.address, usersData, signature)
dispenserProvider.connect(user).dispenseLock(poolId, validTime, user.address, usersData, signature)
).to.be.revertedWith("DispenserProvider: Tokens already taken")
})

Expand All @@ -133,7 +133,7 @@ describe("Dispenser Provider tests", function () {
const signatureData = [poolId, validTime, user.address, userData]
const signature = await createSignature(signer, signatureData)
await expect(
dispenserProvider.connect(owner).createLock(poolId, validTime, user.address, usersData, signature)
dispenserProvider.connect(owner).dispenseLock(poolId, validTime, user.address, usersData, signature)
).to.be.revertedWith("DispenserProvider: Caller is not approved")
})

Expand All @@ -150,4 +150,12 @@ describe("Dispenser Provider tests", function () {
dispenserProvider.connect(owner).createNewPool(addresses, params, creationSignature)
).to.be.revertedWith("amount must be greater than 0")
})

it("should support IERC165 interface", async () => {
expect(await dispenserProvider.supportsInterface('0x01ffc9a7')).to.equal(true)
})

it("should support IDispenserProvider interface", async () => {
expect(await dispenserProvider.supportsInterface('0xda28ff53')).to.equal(true)
})
})

0 comments on commit 001bd07

Please sign in to comment.