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

use dealProvider as base logic #11

Merged
merged 3 commits into from
Mar 21, 2024
Merged
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
66 changes: 5 additions & 61 deletions contracts/DispenserProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,14 @@ pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "./DispenserView.sol";
import "@poolzfinance/lockdeal-nft/contracts/SimpleProviders/DealProvider/DealProvider.sol";
import "./DispenserState.sol";

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

constructor(ILockDealNFT _lockDealNFT) {
require(
address(_lockDealNFT) != address(0),
"DispenserProvider: Invalid lockDealNFT address"
);
constructor(ILockDealNFT _lockDealNFT) DealProvider(_lockDealNFT) {
name = "DispenserProvider";
lockDealNFT = _lockDealNFT;
}

function deposit(
address signer,
IERC20 tokenAddress,
uint256 amount,
bytes calldata data
) external returns (uint256 poolId) {
require(
signer != address(0),
"DispenserProvider: Invalid signer address"
);
require(
address(tokenAddress) != address(0),
"DispenserProvider: Invalid token address"
);
require(amount > 0, "DispenserProvider: Invalid amount");
poolId = lockDealNFT.safeMintAndTransfer(
signer,
address(tokenAddress),
msg.sender,
amount,
this,
data
);
uint256[] memory params = new uint256[](1);
params[0] = amount;
_registerPool(poolId, params);
}

function registerPool(
uint256 poolId,
uint256[] calldata params
)
external
onlyProvider
validProviderId(poolId)
validParamsLength(params.length, currentParamsTargetLength())
{
_registerPool(poolId, params);
}

function _registerPool(uint256 poolId, uint256[] memory params) internal {
leftAmount[poolId] = params[0];
}

function createLock(
Expand Down Expand Up @@ -111,7 +63,7 @@ contract DispenserProvider is DispenserView {
uint256 poolId = lockDealNFT.mintForProvider(owner, data[i].simpleProvider);
data[i].simpleProvider.registerPool(poolId, data[i].params);
lockDealNFT.cloneVaultId(poolId, tokenPoolId);
leftAmount[tokenPoolId] -= data[i].params[0];
poolIdToAmount[tokenPoolId] -= data[i].params[0];
_withdrawIfAvailable(data[i].simpleProvider, poolId, owner);
}
}
Expand All @@ -136,12 +88,4 @@ contract DispenserProvider is DispenserView {
address recoveredSigner = hash.recover(signature);
success = recoveredSigner == signer;
}

function withdraw(uint256) external pure override returns (uint256, bool) {
require(false, "DispenserProvider: Not implemented yet");
}

function split(uint256, uint256, uint256) external pure override {
require(false, "DispenserProvider: Not implemented yet");
}
}
1 change: 0 additions & 1 deletion contracts/DispenserState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import "@poolzfinance/poolz-helper-v2/contracts/interfaces/ISimpleProvider.sol";

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

struct Builder {
ISimpleProvider simpleProvider;
Expand Down
16 changes: 0 additions & 16 deletions contracts/DispenserView.sol

This file was deleted.

53 changes: 16 additions & 37 deletions test/DispenserProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ describe("Dispenser Provider tests", function () {
let lockProvider: LockDealProvider
let timedProvider: TimedDealProvider
let vaultManager: VaultManager
let packedData: string
let poolId: number
let addresses: string[]
let params: [BigNumber]
let validTime: BigNumber
const builderType = ["uint256", "uint256", "address", "tuple(address,uint256[])[]"]
const creationSignature: Bytes = ethers.utils.toUtf8Bytes("signature")
const amount = ethers.utils.parseUnits("10", 18)
const ONE_DAY = 86400
Expand Down Expand Up @@ -58,33 +58,29 @@ describe("Dispenser Provider tests", function () {
beforeEach(async () => {
const ERC20Token = await ethers.getContractFactory("ERC20Token")
token = await ERC20Token.deploy("Test", "TST")
params = [amount]
addresses = [signer.address, token.address]
poolId = (await lockDealNFT.totalSupply()).toNumber()
await token.approve(vaultManager.address, amount)
await dispenserProvider.connect(owner).deposit(signer.address, token.address, amount, creationSignature)
await dispenserProvider.connect(owner).createNewPool(addresses, params, creationSignature)
validTime = ethers.BigNumber.from((await time.latest()) + ONE_DAY)
userData = { simpleProvider: lockProvider.address, params: [amount.div(2), validTime] }
usersData = [{ simpleProvider: lockProvider.address, params: [amount.div(2), validTime] }]
packedData = ethers.utils.defaultAbiCoder.encode(builderType, [
poolId,
validTime,
user.address,
[[dealProvider.address, [amount]]],
])
})

it("should return name of contract", async () => {
expect(await dispenserProvider.name()).to.equal("DispenserProvider")
})

it("should increase leftAmount after creation", async () => {
expect(await dispenserProvider.leftAmount(poolId)).to.equal(amount)
expect(await dispenserProvider.poolIdToAmount(poolId)).to.equal(amount)
})

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)
expect(await dispenserProvider.leftAmount(poolId)).to.equal(amount.div(2))
expect(await dispenserProvider.poolIdToAmount(poolId)).to.equal(amount.div(2))
})

it("should transfer if available", async () => {
Expand Down Expand Up @@ -127,9 +123,10 @@ describe("Dispenser Provider tests", function () {
})

it("should revert invalid signer address", async () => {
addresses = [constants.AddressZero, token.address]
await expect(
dispenserProvider.connect(owner).deposit(constants.AddressZero, token.address, amount, creationSignature)
).to.be.revertedWith("DispenserProvider: Invalid signer address")
dispenserProvider.connect(owner).createNewPool(addresses, params, creationSignature)
).to.be.revertedWith("Zero Address is not allowed")
})

it("should revert if sender is invalid", async () => {
Expand All @@ -141,34 +138,16 @@ describe("Dispenser Provider tests", function () {
})

it("should revert invalid signer address", async () => {
addresses = [signer.address, constants.AddressZero]
await expect(
dispenserProvider.connect(owner).deposit(owner.address, constants.AddressZero, amount, creationSignature)
).to.be.revertedWith("DispenserProvider: Invalid token address")
dispenserProvider.connect(owner).createNewPool(addresses, params, creationSignature)
).to.be.revertedWith("Zero Address is not allowed")
})

it("should revert invalid signer address", async () => {
params = [BigNumber.from(0)]
await expect(
dispenserProvider.connect(owner).deposit(owner.address, token.address, 0, creationSignature)
).to.be.revertedWith("DispenserProvider: Invalid amount")
})

it("should revert split", async () => {
const halfRatio = ethers.utils.parseUnits("1", 21).div(2)
const packedData = ethers.utils.defaultAbiCoder.encode(["uint256", "address"], [halfRatio, owner.address])
await expect(
lockDealNFT
.connect(signer)
[
"safeTransferFrom(address,address,uint256,bytes)"
](signer.address, lockDealNFT.address, poolId, packedData)
).to.be.revertedWith("DispenserProvider: Not implemented yet")
})

it("should revert withdraw", async () => {
await expect(
lockDealNFT
.connect(signer)
["safeTransferFrom(address,address,uint256)"](signer.address, lockDealNFT.address, poolId)
).to.be.revertedWith("DispenserProvider: Not implemented yet")
dispenserProvider.connect(owner).createNewPool(addresses, params, creationSignature)
).to.be.revertedWith("amount must be greater than 0")
})
})