Skip to content

Commit

Permalink
fix: Allow higher for sale price on reclaim (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
codynhat authored Sep 15, 2022
1 parent 35ca6d4 commit ebc97e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
18 changes: 0 additions & 18 deletions contracts/pco-license/facets/CFAReclaimerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,6 @@ contract CFAReclaimerFacet is CFABasePCOFacetModifiers {
}

ISuperToken paymentToken = ds.paramsStore.getPaymentToken();
{
uint256 requiredBuffer = cs.cfaV1.cfa.getDepositRequiredForFlowRate(
paymentToken,
newContributionRate
);

require(
paymentToken.balanceOf(msg.sender) >=
newForSalePrice + requiredBuffer,
"CFAReclaimerFacet: Insufficient balance"
);
require(
paymentToken.allowance(msg.sender, address(this)) >=
newForSalePrice + requiredBuffer,
"CFAReclaimerFacet: Insufficient allowance"
);
}

{
// Check operator permissions
(, uint8 permissions, int96 flowRateAllowance) = cs
Expand Down
102 changes: 61 additions & 41 deletions test/pco-license/CFAReclaimerFacet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe("CFAReclaimerFacet", async function () {
).to.have.been.calledWith(user, bidder, await basePCOFacet.licenseId());
});

it("should revert if insufficient balance", async () => {
it("should succeed if for sale price is higher than claim price", async () => {
const {
basePCOFacet,
mockParamsStore,
Expand All @@ -74,6 +74,50 @@ describe("CFAReclaimerFacet", async function () {
} = await BaseFixtures.afterPayerDelete();
const { bidder, user } = await getNamedAccounts();

const contributionRate = BigNumber.from(200);
const forSalePrice = await rateToPurchasePrice(
mockParamsStore,
contributionRate
);
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaimPrice();

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: reclaimPrice.add(requiredBuffer),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));

// Approve flow creation
const op3 = ethersjsSf.cfaV1.updateFlowOperatorPermissions({
superToken: paymentToken.address,
flowOperator: basePCOFacet.address,
permissions: 1,
flowRateAllowance: contributionRate.toString(),
});
await op3.exec(await ethers.getSigner(bidder));

const txn = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaim(contributionRate, forSalePrice);
await txn.wait();
await expect(txn).to.emit(basePCOFacet, "LicenseReclaimed");
expect(
mockLicense["safeTransferFrom(address,address,uint256)"]
).to.have.been.calledWith(user, bidder, await basePCOFacet.licenseId());
});

it("should revert if insufficient balance", async () => {
const { basePCOFacet, mockParamsStore, paymentToken, ethersjsSf } =
await BaseFixtures.afterPayerDelete();
const { bidder } = await getNamedAccounts();

const contributionRate = BigNumber.from(100);
const forSalePrice = await rateToPurchasePrice(
mockParamsStore,
Expand All @@ -82,15 +126,14 @@ describe("CFAReclaimerFacet", async function () {
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaimPrice();

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: reclaimPrice.add(totalCollateral),
amount: reclaimPrice.add(requiredBuffer),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));
Expand Down Expand Up @@ -118,28 +161,19 @@ describe("CFAReclaimerFacet", async function () {
basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaim(contributionRate, forSalePrice)
).to.be.revertedWith("CFAReclaimerFacet: Insufficient balance");
).to.be.revertedWith("SuperfluidToken: move amount exceeds balance");
});

it("should revert if insufficient allowance", async () => {
const {
basePCOFacet,
mockParamsStore,
paymentToken,
ethersjsSf,
mockLicense,
} = await BaseFixtures.afterPayerDelete();
const { bidder, user } = await getNamedAccounts();
const { basePCOFacet, mockParamsStore, paymentToken, ethersjsSf } =
await BaseFixtures.afterPayerDelete();
const { bidder } = await getNamedAccounts();

const contributionRate = BigNumber.from(100);
const forSalePrice = await rateToPurchasePrice(
mockParamsStore,
contributionRate
);
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
Expand All @@ -165,18 +199,13 @@ describe("CFAReclaimerFacet", async function () {
basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaim(contributionRate, forSalePrice)
).to.be.revertedWith("CFAReclaimerFacet: Insufficient allowance");
).to.be.revertedWith("SuperToken: transfer amount exceeds allowance");
});

it("should revert if permission not granted to create flow", async () => {
const {
basePCOFacet,
mockParamsStore,
paymentToken,
ethersjsSf,
mockLicense,
} = await BaseFixtures.afterPayerDelete();
const { bidder, user } = await getNamedAccounts();
const { basePCOFacet, mockParamsStore, paymentToken, ethersjsSf } =
await BaseFixtures.afterPayerDelete();
const { bidder } = await getNamedAccounts();

const contributionRate = BigNumber.from(100);
const forSalePrice = await rateToPurchasePrice(
Expand All @@ -186,15 +215,14 @@ describe("CFAReclaimerFacet", async function () {
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaimPrice();

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: reclaimPrice.add(totalCollateral),
amount: reclaimPrice.add(requiredBuffer),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));
Expand All @@ -209,14 +237,9 @@ describe("CFAReclaimerFacet", async function () {
});

it("should revert if flow permission doesn't have enough allowance", async () => {
const {
basePCOFacet,
mockParamsStore,
paymentToken,
ethersjsSf,
mockLicense,
} = await BaseFixtures.afterPayerDelete();
const { bidder, user } = await getNamedAccounts();
const { basePCOFacet, mockParamsStore, paymentToken, ethersjsSf } =
await BaseFixtures.afterPayerDelete();
const { bidder } = await getNamedAccounts();

const contributionRate = BigNumber.from(100);
const forSalePrice = await rateToPurchasePrice(
Expand All @@ -226,15 +249,14 @@ describe("CFAReclaimerFacet", async function () {
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaimPrice();

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: reclaimPrice.add(totalCollateral),
amount: reclaimPrice.add(requiredBuffer),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));
Expand Down Expand Up @@ -270,11 +292,10 @@ describe("CFAReclaimerFacet", async function () {
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: totalCollateral.toString(),
amount: requiredBuffer.toString(),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));
Expand Down Expand Up @@ -309,15 +330,14 @@ describe("CFAReclaimerFacet", async function () {
const requiredBuffer = await ethersjsSf.cfaV1.contract
.connect(await ethers.getSigner(bidder))
.getDepositRequiredForFlowRate(paymentToken.address, contributionRate);
const totalCollateral = forSalePrice.add(requiredBuffer);

const reclaimPrice = await basePCOFacet
.connect(await ethers.getSigner(bidder))
.reclaimPrice();

// Allow spending of reclaimPrice
const op2 = paymentToken.approve({
amount: reclaimPrice.add(totalCollateral),
amount: reclaimPrice.add(requiredBuffer),
receiver: basePCOFacet.address,
});
await op2.exec(await ethers.getSigner(bidder));
Expand Down

0 comments on commit ebc97e9

Please sign in to comment.