Skip to content

Commit

Permalink
AdSpot contract update functions
Browse files Browse the repository at this point in the history
  • Loading branch information
youssefea committed Nov 16, 2023
1 parent bf83a1e commit 9774d09
Showing 1 changed file with 129 additions and 61 deletions.
190 changes: 129 additions & 61 deletions projects/gda-advertisement-auction/contracts/AdSpotContract.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;
Expand All @@ -13,11 +12,9 @@ import {ISuperfluid, ISuperToken, ISuperApp, SuperAppDefinitions} from "./contra
import {ISuperfluidPool} from "./contracts/interfaces/agreements/gdav1/ISuperfluidPool.sol";
import {SuperTokenV1Library} from "./contracts/apps/SuperTokenV1Library.sol";
import {SuperAppBaseFlow} from "./contracts/apps/SuperAppBaseFlow.sol";
import {IGeneralDistributionAgreementV1,ISuperfluidPool,PoolConfig} from "./contracts/interfaces/agreements/gdav1/IGeneralDistributionAgreementV1.sol";
import {IGeneralDistributionAgreementV1, ISuperfluidPool, PoolConfig} from "./contracts/interfaces/agreements/gdav1/IGeneralDistributionAgreementV1.sol";


contract AdSpotContract is SuperAppBaseFlow {

using SuperTokenV1Library for ISuperToken;

uint256 private number;
Expand All @@ -30,8 +27,10 @@ contract AdSpotContract is SuperAppBaseFlow {
uint private lastUpdate;
PoolConfig private poolConfig;
IGeneralDistributionAgreementV1 private gda;
address public nftAddress; // Address of the NFT contract
uint256 public nftTokenId; // Token ID of the NFT

event newHighestBidder(address highestBidder, int96 flowRate );
event newHighestBidder(address highestBidder, int96 flowRate);

/**
* @dev Constructor to initialize the contract with necessary Superfluid interfaces and parameters.
Expand All @@ -42,34 +41,101 @@ contract AdSpotContract is SuperAppBaseFlow {
constructor(
ISuperToken _acceptedToken,
IGeneralDistributionAgreementV1 _gda
) SuperAppBaseFlow(
ISuperfluid(ISuperToken(_acceptedToken).getHost()),
true,
true,
true,
string("")
) {
gda=_gda;
acceptedToken=_acceptedToken;
poolConfig.transferabilityForUnitsOwner=true;
poolConfig.distributionFromAnyAddress=true;
pool=SuperTokenV1Library.createPool(acceptedToken, address(this), poolConfig );
poolAddress=address(pool);
owner=msg.sender;
highestFlowRate=acceptedToken.getFlowRate(owner, address(this));
lastUpdate=block.timestamp;
highestBidder=address(0);
)
SuperAppBaseFlow(
ISuperfluid(ISuperToken(_acceptedToken).getHost()),
true,
true,
true,
string("")
)
{
gda = _gda;
acceptedToken = _acceptedToken;
poolConfig.transferabilityForUnitsOwner = true;
poolConfig.distributionFromAnyAddress = true;
pool = SuperTokenV1Library.createPool(acceptedToken, address(this), poolConfig);
poolAddress = address(pool);
owner = msg.sender;
highestFlowRate = acceptedToken.getFlowRate(owner, address(this));
lastUpdate = block.timestamp;
highestBidder = address(0);
}

function isAcceptedSuperToken(ISuperToken superToken) public view override returns (bool) {
return superToken == acceptedToken;
}

/**
* @dev Allows the highest bidder to set an NFT to showcase.
* @param _nftAddress The address of the NFT contract.
* @param _tokenId The token ID of the NFT.
*/

function setNftToShowcase(address _nftAddress, uint256 _tokenId) external {
require(msg.sender == highestBidder, "Only the highest bidder can set the NFT");
nftAddress = _nftAddress;
nftTokenId = _tokenId;
}

// ---------------------------------------------------------------------------------------------
// GETTERS
// ---------------------------------------------------------------------------------------------

/**
* @dev Returns the address of the pool.
*/
function getPoolAddress() public view returns (address) {
return poolAddress;
}

/**
* @dev Returns the accepted token for streaming payments.
*/
function getAcceptedToken() public view returns (ISuperToken) {
return acceptedToken;
}

/**
* @dev Returns the address of the contract owner.
*/
function getOwner() public view returns (address) {
return owner;
}

/**
* @dev Returns the address of the highest bidder.
*/
function getHighestBidder() public view returns (address) {
return highestBidder;
}

/**
* @dev Returns the highest flow rate.
*/
function getHighestFlowRate() public view returns (int96) {
return highestFlowRate;
}

/**
* @dev Returns the last update timestamp.
*/
function getLastUpdate() public view returns (uint) {
return lastUpdate;
}

/**
* @dev Returns the address of the General Distribution Agreement.
*/
function getGDA() public view returns (IGeneralDistributionAgreementV1) {
return gda;
}

// ---------------------------------------------------------------------------------------------
// SUPER APP CALLBACKS
// ---------------------------------------------------------------------------------------------

/*
/**
* @dev Callback function that gets executed when a new flow is created to this contract.
* It handles logic for updating the highest bidder and distributing shares.
* @param sender The address of the sender creating the flow.
Expand All @@ -81,21 +147,21 @@ contract AdSpotContract is SuperAppBaseFlow {
address sender,
bytes calldata ctx
) internal override returns (bytes memory newCtx) {
int96 senderFlowRate= acceptedToken.getFlowRate(sender, address(this));
require(senderFlowRate>highestFlowRate, "Sender flowrate lower than current flowRate");
newCtx=ctx;
if(highestBidder!=address(0)){
newCtx=acceptedToken.deleteFlowWithCtx(highestBidder,address(this), ctx);
int96 senderFlowRate = acceptedToken.getFlowRate(sender, address(this));
require(senderFlowRate > highestFlowRate, "Sender flowrate lower than current flowRate");
newCtx = ctx;
if (highestBidder != address(0)) {
newCtx = acceptedToken.deleteFlowWithCtx(highestBidder, address(this), ctx);
}
uint128 halfShares=uint128(block.timestamp-lastUpdate)/2;
pool.updateMemberUnits(owner,halfShares);
if (highestBidder!=address(0)){
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder,halfShares);
uint128 halfShares = uint128(block.timestamp - lastUpdate) / 2;
pool.updateMemberUnits(owner, halfShares);
if (highestBidder != address(0)) {
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder, halfShares);
}
newCtx=acceptedToken.distributeFlowWithCtx(pool,address(this),senderFlowRate,newCtx);
highestBidder=sender;
highestFlowRate=senderFlowRate;
lastUpdate=block.timestamp;
newCtx = acceptedToken.distributeFlowWithCtx(pool, address(this), senderFlowRate, newCtx);
highestBidder = sender;
highestFlowRate = senderFlowRate;
lastUpdate = block.timestamp;
return newCtx;
}

Expand All @@ -108,28 +174,34 @@ contract AdSpotContract is SuperAppBaseFlow {
* @param ctx The context of the current flow transaction.
* @return bytes Returns the new transaction context.
*/
function onFlowUpdated(
function onFlowUpdated(
ISuperToken,
address sender,
int96 previousflowRate,
uint256 lastUpdated,
bytes calldata ctx
) internal override returns (bytes memory newCtx) {
int96 senderFlowRate= acceptedToken.getFlowRate(sender, address(this));
require(senderFlowRate>previousflowRate, "Sender flowRate is lower than the previous one");
require(senderFlowRate>highestFlowRate, "You already have a flowrate that is higher than this one");
newCtx=ctx;
uint128 halfShares=uint128(block.timestamp-lastUpdate)/2;
ISuperfluidPool(poolAddress).updateMemberUnits(owner,halfShares);
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder,halfShares);
newCtx=acceptedToken.distributeFlowWithCtx(pool,address(this),senderFlowRate,newCtx);
highestBidder=sender;
highestFlowRate=senderFlowRate;
lastUpdate=block.timestamp;
int96 senderFlowRate = acceptedToken.getFlowRate(sender, address(this));
require(
senderFlowRate > previousflowRate,
"Sender flowRate is lower than the previous one"
);
require(
senderFlowRate > highestFlowRate,
"You already have a flowrate that is higher than this one"
);
newCtx = ctx;
uint128 halfShares = uint128(block.timestamp - lastUpdate) / 2;
ISuperfluidPool(poolAddress).updateMemberUnits(owner, halfShares);
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder, halfShares);
newCtx = acceptedToken.distributeFlowWithCtx(pool, address(this), senderFlowRate, newCtx);
highestBidder = sender;
highestFlowRate = senderFlowRate;
lastUpdate = block.timestamp;
return newCtx;
}

/*
/**
* @dev Callback function that gets executed when a flow to this contract is deleted.
* Handles the removal of a bidder and adjustment of shares.
* @param sender The address of the sender deleting the flow.
Expand All @@ -145,19 +217,15 @@ contract AdSpotContract is SuperAppBaseFlow {
uint256 /*lastUpdated*/,
bytes calldata ctx
) internal override returns (bytes memory newCtx) {

require(sender==highestBidder, "You don't have an active stream");
highestBidder=address(0);
uint128 halfShares=uint128(block.timestamp-lastUpdate)/2;
pool.updateMemberUnits(owner,halfShares);
if (highestBidder!=address(0)){
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder,halfShares);
require(sender == highestBidder, "You don't have an active stream");
highestBidder = address(0);
uint128 halfShares = uint128(block.timestamp - lastUpdate) / 2;
pool.updateMemberUnits(owner, halfShares);
if (highestBidder != address(0)) {
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder, halfShares);
}
newCtx=gda.distributeFlow(acceptedToken, address(this),pool,0,newCtx);
newCtx = gda.distributeFlow(acceptedToken, address(this), pool, 0, newCtx);

return newCtx;

}


}
}

0 comments on commit 9774d09

Please sign in to comment.