Skip to content

Commit

Permalink
add libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
youssefea committed Dec 17, 2023
1 parent fc86d53 commit b42612a
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ contract AdSpotContract is SuperAppBaseFlow {
// Getters
// ---------------------------------------------------------------------------------------------

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


/**
* @dev Returns the address of the pool.
*/
Expand Down Expand Up @@ -196,10 +188,9 @@ contract AdSpotContract is SuperAppBaseFlow {
if (highestBidder != address(0)) {
newCtx = acceptedToken.deleteFlowWithCtx(highestBidder, address(this), ctx);
uint128 halfShares = uint128(block.timestamp - lastUpdate) / 2;
if(pool.getUnits(owner)==1){
pool.updateMemberUnits(owner, halfShares + pool.getUnits(owner)-1);
}
else{
if (pool.getUnits(owner) == 1) {
pool.updateMemberUnits(owner, halfShares + pool.getUnits(owner) - 1);
} else {
pool.updateMemberUnits(owner, halfShares + pool.getUnits(owner));
}
pool.updateMemberUnits(highestBidder, halfShares + pool.getUnits(highestBidder));
Expand Down Expand Up @@ -240,7 +231,10 @@ contract AdSpotContract is SuperAppBaseFlow {
newCtx = ctx;
uint128 halfShares = uint128(block.timestamp - lastUpdate) / 2;
ISuperfluidPool(poolAddress).updateMemberUnits(owner, halfShares + pool.getUnits(owner));
ISuperfluidPool(poolAddress).updateMemberUnits(highestBidder,halfShares + pool.getUnits(highestBidder));
ISuperfluidPool(poolAddress).updateMemberUnits(
highestBidder,
halfShares + pool.getUnits(highestBidder)
);
newCtx = acceptedToken.distributeFlowWithCtx(address(this), pool, senderFlowRate, newCtx);
highestBidder = sender;
highestFlowRate = senderFlowRate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ contract AdSpotContractTest is Test {
uint256 mumbaiFork;
string MUMBAI_RPC_URL = vm.envString("MUMBAI_RPC_URL");


function setUp() public {
mumbaiFork = vm.createSelectFork(MUMBAI_RPC_URL);

daix= ISuperToken(0x5D8B4C2554aeB7e86F387B4d6c00Ac33499Ed01f);
daix = ISuperToken(0x5D8B4C2554aeB7e86F387B4d6c00Ac33499Ed01f);
adSpotContract = new AdSpotContract(daix);
account1 = address(0x72343b915f335B2af76CA703cF7a550C8701d5CD);
account2= address(0x61fFC0072D66cE2bC3b8D7654BF68690b2d7fDc4);
account2 = address(0x61fFC0072D66cE2bC3b8D7654BF68690b2d7fDc4);
vm.prank(account1);
daix.transfer(address(adSpotContract), 1e18);
vm.stopPrank();
Expand All @@ -37,9 +36,21 @@ contract AdSpotContractTest is Test {
function testInitialSetup() public {
vm.selectFork(mumbaiFork);
assertEq(vm.activeFork(), mumbaiFork);
assertEq(address(adSpotContract.getAcceptedToken()), address(daix), "Accepted token should be daix");
assertEq(adSpotContract.getOwner(), address(this), "Contract owner should be this contract");
assertEq(adSpotContract.getHighestBidder(), address(0), "Initial highest bidder should be address 0");
assertEq(
address(adSpotContract.getAcceptedToken()),
address(daix),
"Accepted token should be daix"
);
assertEq(
adSpotContract.getOwner(),
address(this),
"Contract owner should be this contract"
);
assertEq(
adSpotContract.getHighestBidder(),
address(0),
"Initial highest bidder should be address 0"
);
}

function testFlowCreation() public {
Expand All @@ -51,8 +62,16 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);
}

function testFlowUpdate() public {
Expand All @@ -64,16 +83,27 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

vm.startPrank(account1);
daix.updateFlow(address(adSpotContract), 2*flowRate);
daix.updateFlow(address(adSpotContract), 2 * flowRate);
vm.stopPrank();

// Verify that the flow rate is updated correctly
assertEq(adSpotContract.getHighestFlowRate(), 2*flowRate, "Highest flow rate should match the set flow rate");

assertEq(
adSpotContract.getHighestFlowRate(),
2 * flowRate,
"Highest flow rate should match the set flow rate"
);
}

function testFlowDeletion() public {
Expand All @@ -85,17 +115,32 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

vm.startPrank(account1);
daix.deleteFlow(account1, address(adSpotContract));
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), address(0), "Initial highest bidder should be address 0");
assertEq(adSpotContract.getHighestFlowRate(), 0, "Highest flow rate should match the set flow rate");

assertEq(
adSpotContract.getHighestBidder(),
address(0),
"Initial highest bidder should be address 0"
);
assertEq(
adSpotContract.getHighestFlowRate(),
0,
"Highest flow rate should match the set flow rate"
);
}

function testHigherBidd() public {
Expand All @@ -107,18 +152,33 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

// Create a flow from account2 to the adSpotContract
vm.startPrank(account2);
daix.createFlow(address(adSpotContract), flowRate+2);
daix.createFlow(address(adSpotContract), flowRate + 2);
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account2, "Account2 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate+2, "Highest flow rate should match the set flow rate");

assertEq(
adSpotContract.getHighestBidder(),
account2,
"Account2 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate + 2,
"Highest flow rate should match the set flow rate"
);
}

function testNFTSetting() public {
Expand All @@ -130,18 +190,29 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

// Set an NFT to showcase
vm.startPrank(account1);
adSpotContract.setNftToShowcase(address(this), 1);
vm.stopPrank();

// Verify that the NFT address and token ID are updated correctly
assertEq(adSpotContract.getNftAddress(), address(this), "NFT address should be this contract");
assertEq(
adSpotContract.getNftAddress(),
address(this),
"NFT address should be this contract"
);
assertEq(adSpotContract.getNftTokenId(), 1, "NFT token ID should be 1");

}

function testOwnerUnitsFirstTime() public {
Expand All @@ -153,8 +224,16 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), 1, "Owner's shares should be 1e18");
Expand All @@ -169,23 +248,39 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), 1, "Owner's shares should be 1");

// Create a flow from account2 to the adSpotContract
vm.startPrank(account2);
daix.createFlow(address(adSpotContract), flowRate+2);
daix.createFlow(address(adSpotContract), flowRate + 2);
vm.stopPrank();

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getTotalShares()/2, "Owner's shares should be half of total shares");
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getMemberShares(account1), "Owner's shares should be same as account1's shares");
assertEq(
adSpotContract.getOwnerShares(),
adSpotContract.getTotalShares() / 2,
"Owner's shares should be half of total shares"
);
assertEq(
adSpotContract.getOwnerShares(),
adSpotContract.getMemberShares(account1),
"Owner's shares should be same as account1's shares"
);
}

testAdvancedMembersUnits() public {
function testAdvancedMembersUnits() public {
int96 flowRate = int96(1000); // example flow rate

// Create a flow from account1 to the adSpotContract
Expand All @@ -194,32 +289,53 @@ contract AdSpotContractTest is Test {
vm.stopPrank();

// Verify that the highest bidder and flow rate are updated correctly
assertEq(adSpotContract.getHighestBidder(), account1, "Account1 should be the highest bidder");
assertEq(adSpotContract.getHighestFlowRate(), flowRate, "Highest flow rate should match the set flow rate");
assertEq(
adSpotContract.getHighestBidder(),
account1,
"Account1 should be the highest bidder"
);
assertEq(
adSpotContract.getHighestFlowRate(),
flowRate,
"Highest flow rate should match the set flow rate"
);

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), 1, "Owner's shares should be 1");

// Create a flow from account2 to the adSpotContract
vm.startPrank(account2);
daix.createFlow(address(adSpotContract), flowRate+2);
daix.createFlow(address(adSpotContract), flowRate + 2);
vm.stopPrank();

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getTotalShares()/2, "Owner's shares should be half of total shares");
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getMemberShares(account1), "Owner's shares should be same as account1's shares");
assertEq(
adSpotContract.getOwnerShares(),
adSpotContract.getTotalShares() / 2,
"Owner's shares should be half of total shares"
);
assertEq(
adSpotContract.getOwnerShares(),
adSpotContract.getMemberShares(account1),
"Owner's shares should be same as account1's shares"
);

// Create a flow from account2 to the adSpotContract
vm.startPrank(account1);
daix.createFlow(address(adSpotContract), flowRate+2);
daix.createFlow(address(adSpotContract), flowRate + 4);
vm.stopPrank();

// Verify that the owner's shares are updated correctly
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getTotalShares()/3, "Owner's shares should be 1/3 of total shares");
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getMemberShares(account1), "Owner's shares should be same as account1's shares");
assertEq(adSpotContract.getOwnerShares(), adSpotContract.getMemberShares(account2), "Owner's shares should be same as account2's shares");

assertEq(
adSpotContract.getOwnerShares(),
adSpotContract.getTotalShares() / 2,
"Owner's shares should be 1/3 of total shares"
);
assertEq(
adSpotContract.getMemberShares(account1)+adSpotContract.getMemberShares(account2),
adSpotContract.getTotalShares() / 2,
"Owner's shares should be 1/3 of total shares"
);
}


}

0 comments on commit b42612a

Please sign in to comment.