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

switch to externally manged pool for FlightPool, amend tests #752

Merged
merged 1 commit into from
Oct 18, 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
36 changes: 28 additions & 8 deletions contracts/examples/flight/FlightOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,19 @@ contract FlightOracle is
_respond(requestId, responseData);

// TODO decide if the code below should be moved to GIF
// check callback result
bool requestFulfilled = _getInstanceReader().getRequestState(
requestId) == FULFILLED();

// remove from active requests when successful
if (requestFulfilled) {
LibRequestIdSet.remove(_activeRequests, requestId);
}
_updateRequestState(requestId);
}


function updateRequestState(
RequestId requestId
)
external
restricted()
{
_updateRequestState(requestId);
}

//--- view functions ----------------------------------------------------//

// TODO decide if the code below should be moved to GIF
Expand Down Expand Up @@ -172,6 +174,24 @@ contract FlightOracle is

//--- internal functions ------------------------------------------------//


// TODO decide if the code below should be moved to GIF
// check callback result
function _updateRequestState(
RequestId requestId
)
internal
{
bool requestFulfilled = _getInstanceReader().getRequestState(
requestId) == FULFILLED();

// remove from active requests when successful
if (requestFulfilled && LibRequestIdSet.contains(_activeRequests, requestId)) {
LibRequestIdSet.remove(_activeRequests, requestId);
}
}


/// @dev use case specific handling of oracle requests
/// for now only log is emitted to verify that request has been received by oracle component
function _request(
Expand Down
6 changes: 5 additions & 1 deletion contracts/examples/flight/FlightOracleAuthorization.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {IAccess} from "../../../contracts/authorization/IAccess.sol";
import {AccessAdminLib} from "../../authorization/AccessAdminLib.sol";
import {BasicOracleAuthorization} from "../../oracle/BasicOracleAuthorization.sol";
import {FlightOracle} from "./FlightOracle.sol";
import {RoleId, ADMIN_ROLE} from "../../../contracts/type/RoleId.sol";
import {RoleId, ADMIN_ROLE, PUBLIC_ROLE} from "../../../contracts/type/RoleId.sol";

contract FlightOracleAuthorization
is BasicOracleAuthorization
Expand Down Expand Up @@ -48,6 +48,10 @@ contract FlightOracleAuthorization

functions = _authorizeForTarget(getMainTargetName(), STATUS_PROVIDER_ROLE);
_authorize(functions, FlightOracle.respondWithFlightStatus.selector, "respondWithFlightStatus");

// authorize public role (additional authz via onlyOwner)
functions = _authorizeForTarget(getMainTargetName(), PUBLIC_ROLE());
_authorize(functions, FlightOracle.updateRequestState.selector, "updateRequestState");
}
}

2 changes: 1 addition & 1 deletion contracts/examples/flight/FlightPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract FlightPool is
maxBalanceAmount: AmountLib.max(),
isInterceptingBundleTransfers: false,
isProcessingConfirmedClaims: false,
isExternallyManaged: false,
isExternallyManaged: true,
isVerifyingApplications: false,
collateralizationLevel: UFixedLib.one(),
retentionLevel: UFixedLib.one()
Expand Down
24 changes: 24 additions & 0 deletions test/examples/flight/FlightBase.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ contract FlightBaseTest is GifTest {
address public statisticsProvider = makeAddr("statisticsProvider");
address public statusProvider = makeAddr("statusProvider");

address public flightPoolWallet = makeAddr("flightPoolWallet");

uint256 public customerPrivateKey = 0xB0B;

address public dataSigner;
Expand Down Expand Up @@ -246,6 +248,11 @@ contract FlightBaseTest is GifTest {
flightProduct,
address(flightPool),
"flightPool");

// set external wallet
vm.startPrank(flightOwner);
flightPool.setWallet(flightPoolWallet);
vm.stopPrank();
}


Expand Down Expand Up @@ -303,6 +310,23 @@ contract FlightBaseTest is GifTest {
}


function _transferTokenToPoolWallet(uint256 amount, bool setMatchingAllowance) internal {
vm.startPrank(flightOwner);
flightUSD.transfer(
flightPool.getWallet(),
amount);
vm.stopPrank();

vm.startPrank(flightPoolWallet);
if (setMatchingAllowance) {
flightUSD.approve(
address(flightPool.getTokenHandler()),
amount);
}
vm.stopPrank();
}


function _createInitialBundle() internal returns (NftId bundleNftId) {
vm.startPrank(flightOwner);
Amount investAmount = AmountLib.toAmount(10000000 * 10 ** 6);
Expand Down
104 changes: 102 additions & 2 deletions test/examples/flight/FlightPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ contract FlightPoolTest is FlightBaseTest {
console.log("");
console.log("flight pool", flightPoolNftId.toInt(), address(flightPool));
console.log("flight pool wallet", flightPool.getWallet());
console.log("flight pool wallet balance [$]", flightUSD.balanceOf(flightPool.getWallet()) / 10 ** flightUSD.decimals());
console.log("flight pool wallet allowance [$] (token handler)", flightUSD.allowance(flightPool.getWallet(), address(flightPool.getTokenHandler())) / 10 ** flightUSD.decimals());
console.log("flight pool token handler", address(flightPool.getTokenHandler()));
console.log("");
console.log("flight owner", flightOwner);
console.log("flight owner balance [$]", flightUSD.balanceOf(flightOwner) / 10 ** flightUSD.decimals());
console.log("flight owner allowance [$] (token handler)", flightUSD.allowance(flightOwner, address(flightPool.getTokenHandler())) / 10 ** flightUSD.decimals());
Expand All @@ -44,7 +47,7 @@ contract FlightPoolTest is FlightBaseTest {
assertTrue(flightUSD.allowance(flightOwner, address(flightPool.getTokenHandler())) > 0, "pool allowance zero");
assertEq(registry.getNftIdForAddress(address(flightPool)).toInt(), flightPoolNftId.toInt(), "unexpected pool nft id");
assertEq(registry.ownerOf(flightPoolNftId), flightOwner, "unexpected pool owner");
assertEq(flightPool.getWallet(), address(flightPool.getTokenHandler()), "unexpected pool wallet address");
assertEq(flightPool.getWallet(), flightPoolWallet, "unexpected pool wallet address");
}


Expand All @@ -54,9 +57,15 @@ contract FlightPoolTest is FlightBaseTest {
uint256 balanceBefore = flightUSD.balanceOf(flightOwner);
Amount bundleAmount = AmountLib.toAmount(10000 * 10 ** flightUSD.decimals());

console.log("balance before", balanceBefore / 10 ** flightUSD.decimals());

// WHEN
vm.prank(flightOwner);
vm.startPrank(flightOwner);
NftId bundleNftId = flightPool.createBundle(bundleAmount);
vm.stopPrank();

// extrnally managed, need to also transfer token to pool
_transferTokenToPoolWallet(bundleAmount.toInt(), false);

// THEN
assertEq(instanceReader.getBundleState(bundleNftId).toInt(), ACTIVE().toInt(), "unexpected bundle state");
Expand Down Expand Up @@ -88,4 +97,95 @@ contract FlightPoolTest is FlightBaseTest {
vm.prank(outsider);
bundleNftId = flightPool.createBundle(bundleAmount);
}


function test_flightPoolStakeAndUnstake() public {

// GIVEN
Amount bundleAmount = AmountLib.toAmount(10000 * 10 ** flightUSD.decimals());
uint256 bundleAmountInt = bundleAmount.toInt();

vm.startPrank(flightOwner);
NftId bundleNftId = flightPool.createBundle(bundleAmount);
vm.stopPrank();

assertEq(flightUSD.balanceOf(flightPool.getWallet()), 0, "unexpected pool wallet balance (before)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt, "unexpected pool balance (before)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (before)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt, "unexpected bundle balance (before)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (before)");

// WHEN - increase bundle stake
Amount stakeAmount = AmountLib.toAmount(3000 * 10 ** flightUSD.decimals());
uint256 stakeAmountInt = stakeAmount.toInt();

vm.startPrank(flightOwner);
flightPool.stake(bundleNftId, stakeAmount);
vm.stopPrank();

// THEN
assertEq(flightUSD.balanceOf(flightPool.getWallet()), 0, "unexpected pool wallet balance (after)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt + stakeAmountInt, "unexpected pool balance (after)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (after)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt + stakeAmountInt, "unexpected bundle balance (after)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (after)");

// WHEN - decrease bundle stake
Amount unstakeAmount = AmountLib.toAmount(6000 * 10 ** flightUSD.decimals());
uint256 unstakeAmountInt = unstakeAmount.toInt();

vm.startPrank(flightOwner);
flightPool.unstake(bundleNftId, unstakeAmount);
vm.stopPrank();

// THEN
assertEq(flightUSD.balanceOf(flightPool.getWallet()), 0, "unexpected pool wallet balance (after 2)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt + stakeAmountInt - unstakeAmountInt, "unexpected pool balance (after 2)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (after 2)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt + stakeAmountInt - unstakeAmountInt, "unexpected bundle balance (after 2)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (after 2)");

assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), 7000 * 10 ** flightUSD.decimals(), "unexpected pool balance (after 2b)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), 7000 * 10 ** flightUSD.decimals(), "unexpected bundle balance (after 2b)");
}


function test_flightPoolTransferTokenToAndFromPool() public {

// GIVEN
Amount bundleAmount = AmountLib.toAmount(10000 * 10 ** flightUSD.decimals());
uint256 bundleAmountInt = bundleAmount.toInt();

vm.startPrank(flightOwner);
NftId bundleNftId = flightPool.createBundle(bundleAmount);
vm.stopPrank();

assertEq(flightUSD.balanceOf(flightPool.getWallet()), 0, "unexpected pool wallet balance (before)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt, "unexpected pool balance (before)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (before)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt, "unexpected bundle balance (before)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (before)");

// WHEN - transfer token to pool wallet
_transferTokenToPoolWallet(bundleAmountInt, false);

// THEN
assertEq(flightUSD.balanceOf(flightPool.getWallet()), bundleAmountInt, "unexpected pool wallet balance (after)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt, "unexpected pool balance (after)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (after)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt, "unexpected bundle balance (after)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (after)");

// WHEN - transfer token from pool wallet
uint256 transferAmountInt = 100 * 10 ** flightUSD.decimals();
vm.prank(flightPoolWallet);
flightUSD.transfer(flightOwner, transferAmountInt);

// THEN
assertEq(flightUSD.balanceOf(flightPool.getWallet()), bundleAmountInt - transferAmountInt, "unexpected pool wallet balance (after 2)");
assertEq(instanceReader.getBalanceAmount(flightPoolNftId).toInt(), bundleAmountInt, "unexpected pool balance (after 2)");
assertEq(instanceReader.getLockedAmount(flightPoolNftId).toInt(), 0, "unexpected pool locked amount (after 2)");
assertEq(instanceReader.getBalanceAmount(bundleNftId).toInt(), bundleAmountInt, "unexpected bundle balance (after 2)");
assertEq(instanceReader.getLockedAmount(bundleNftId).toInt(), 0, "unexpected bundle locked amount (after 2)");
}
}
Loading
Loading