Skip to content

Commit

Permalink
chore: add buy now for in gbm facet
Browse files Browse the repository at this point in the history
  • Loading branch information
BromeRST committed May 9, 2024
1 parent d9e31f0 commit 773deac
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 7 deletions.
29 changes: 23 additions & 6 deletions contracts/facets/GBMFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ contract GBMFacet is IGBM, IERC1155TokenReceiver, IERC721TokenReceiver, Modifier
/// @param _auctionID The auctionId of the auction to complete
//No change necessary for this function code, but it use overriden internal and hence need overriding too in the diamond
function buyNow(uint256 _auctionID) public {
_buyNowImplementation(_auctionID, msg.sender, msg.sender);
}

/// @notice Attribute a token to a specified recipient and distribute the proceeds to the owner of this contract.
/// @param _auctionID The auctionId of the auction to complete
/// @param _recipient The address of the recipient who will receive the NFT
function buyNowFor(uint256 _auctionID, address _recipient) public {
require(_recipient != address(0), "Invalid recipient address");
_buyNowImplementation(_auctionID, msg.sender, _recipient);
}

/// @dev Internal function to handle the logic of buying an auction item.
/// @param _auctionID The ID of the auction.
/// @param _buyer The address of the buyer.
/// @param _recipient The address of the recipient of the NFT.
function _buyNowImplementation(uint256 _auctionID, address _buyer, address _recipient) internal {
_validateAuctionExistence(_auctionID);

Auction storage a = s.auctions[_auctionID];
Expand All @@ -160,25 +176,26 @@ contract GBMFacet is IGBM, IERC1155TokenReceiver, IERC721TokenReceiver, Modifier
address tokenContract = a.tokenContract;
if (s.contractBiddingAllowed[tokenContract] == false) revert("BiddingNotAllowed");

//Prevents re-entrancy
// Prevents re-entrancy
a.claimed = true;

//Transfer the money of the buyer to the GBM Diamond
IERC20(s.GHST).transferFrom(msg.sender, address(this), ae1bnp);
// Transfer the money of the buyer to the GBM Diamond
IERC20(s.GHST).transferFrom(_buyer, address(this), ae1bnp);

//Refund the highest bidder
// Refund the highest bidder
if (a.highestBid > 0) {
IERC20(s.GHST).transfer(a.highestBidder, a.highestBid + a.dueIncentives);
//emit incentive event and bidRemoval event
emit Auction_IncentivePaid(_auctionID, a.highestBidder, a.dueIncentives);
emit Auction_BidRemoved(_auctionID, a.highestBidder, a.highestBid);
}

emit Auction_BoughtNow(_auctionID, msg.sender);
emit Auction_BoughtNow(_auctionID, _recipient);

_calculateRoyaltyAndSend(_auctionID, msg.sender, ae1bnp, a.dueIncentives);
_calculateRoyaltyAndSend(_auctionID, _recipient, ae1bnp, a.dueIncentives);
}


/// @notice Allow/disallow bidding and claiming for a whole token contract address.
/// @param _contract The token contract the auctionned token belong to
/// @param _value True if bidding/claiming should be allowed.
Expand Down
70 changes: 69 additions & 1 deletion test/buyNowAndStartBidPriceTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe("Testing start bid price and buy now logic", async function () {
let auctionId: any;
let gotchiHolder: any;
let bidder: any;

const gotchiId = 13230;
const auctionPresetId = 1;
const backendSigner = new ethers.Wallet(process.env.SECRET);
Expand All @@ -45,16 +46,18 @@ describe("Testing start bid price and buy now logic", async function () {
.mul(70)
.div(100)
.add(ethers.utils.parseEther("1"));

const auctionInfoData = {
// startTime: Math.floor(Date.now() / 1000 + 200),
// endTime: Math.floor(Date.now() / 1000) + 8640,
tokenAmount: 1,
tokenKind: "0x73ad2146", //ERC721
tokenID: gotchiId,
category: 3,
buyItNowPrice: buyItNowPrice,
startingBid: startBidPrice,
buyItNowPrice: buyItNowPrice,
};

let auctionInfo;

before(async function () {
Expand Down Expand Up @@ -368,6 +371,7 @@ describe("Testing start bid price and buy now logic", async function () {
const auctionOwnerGhstBalanceBefore = await ghstERC20.balanceOf(
gotchiHolderAddress
);

const receipt = await (await gbmFacetWithBidder.buyNow(auctionId)).wait();
const event = receipt!.events!.find(
(e: any) => e.event === "Auction_BoughtNow"
Expand All @@ -380,6 +384,7 @@ describe("Testing start bid price and buy now logic", async function () {
const auctionOwnerGhstBalanceAfter = await ghstERC20.balanceOf(
gotchiHolderAddress
);

expect(
auctionOwnerGhstBalanceAfter.sub(auctionOwnerGhstBalanceBefore)
).to.equal(buyItNowPriceLow.mul(96).div(100));
Expand Down Expand Up @@ -476,4 +481,67 @@ describe("Testing start bid price and buy now logic", async function () {
);
});
});

describe("Testing buy now logic with specified recipient (buyNowFor)", async function () {
before(async function () {
// Reset the environment
await ethers.provider.send("evm_revert", [snapshot]);
snapshot = await ethers.provider.send("evm_snapshot", []);

// Define auction info with a buy it now price
auctionInfo = {
...auctionInfoData,
startTime: Math.floor(Date.now() / 1000) + 200,
endTime: Math.floor(Date.now() / 1000) + 8640,
startingBid: startBidPrice,
buyItNowPrice: buyItNowPrice,
};

// Create the auction
const receipt = await (
await gbmFacetWithGotchiHolder.createAuction(
auctionInfo,
gotchiDiamondAddress,
auctionPresetId
)
).wait();

// Extract auction ID from the creation event
const createEvent = receipt.events.find(
(e) => e.event === "Auction_Initialized"
);
auctionId = createEvent.args._auctionId;
});

it("Should allow buying an NFT for a specified recipient", async function () {
const recipient = "0xAd0CEb6Dc055477b8a737B630D6210EFa76a2265";

const auctionOwnerGhstBalanceBefore = await ghstERC20.balanceOf(
gotchiHolderAddress
);

// Ensure the recipient can receive the NFT
await expect(gbmFacetWithBidder.buyNowFor(auctionId, recipient))
.to.emit(gbmFacetWithBidder, "Auction_BoughtNow")
.withArgs(auctionId, recipient);

// Check that the recipient now owns the NFT
const newOwner = await gotchiDiamond.ownerOf(gotchiId);
expect(newOwner).to.equal(recipient);

// Check if funds were correctly transferred
const auctionOwnerGhstBalanceAfter = await ghstERC20.balanceOf(
gotchiHolderAddress
);
expect(
auctionOwnerGhstBalanceAfter.sub(auctionOwnerGhstBalanceBefore)
).to.equal(buyItNowPrice.mul(96).div(100)); // assuming the buy now price is taken at 96%
});

it("Should revert if the recipient address is invalid", async function () {
await expect(
gbmFacetWithBidder.buyNowFor(auctionId, ethers.constants.AddressZero)
).to.be.revertedWith("Invalid recipient address");
});
});
});

0 comments on commit 773deac

Please sign in to comment.