Skip to content

Commit

Permalink
USDCx > ibAlluoUSD REX Market (#133)
Browse files Browse the repository at this point in the history
* Copy twoway market

* smartcontract integration for alluo

* add generated ibAlluo interface

* wip

* working tests for 2.1

* saving work in progress

* saving wip

* saving wip

* Alluo deploy script, misc. fixes, passing 1 and 2 tests

* reduce fee

* ibAlluoUSD<>ibAlluoBTC deployment

* saving wip

* refactor usdcx to iballuousd

* upgrade sf SDK

* working usdcx>>ibAlluoUSD market

* USDCx>iballuoUSD market deploy script

* Fix growing ration withdraw bug

* added constant, remove unused code, edit config for alluo deploy
  • Loading branch information
mikeghen authored Oct 19, 2022
1 parent 21c33a9 commit a88a814
Show file tree
Hide file tree
Showing 17 changed files with 4,418 additions and 1,114 deletions.
31 changes: 11 additions & 20 deletions contracts/REXMarket.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {

struct ShareholderUpdate {
address shareholder;
address affiliate;
int96 previousFlowRate;
int96 currentFlowRate;
ISuperToken token;
Expand Down Expand Up @@ -159,10 +160,6 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {

/// @dev Drain contract's input and output tokens balance to owner if SuperApp dont have any input streams.
function emergencyDrain(ISuperToken token) external virtual onlyOwner {
require(
cfa.getNetFlow(token, address(this)) == 0,
"!zeroStreamers"
);
require(host.isAppJailed(ISuperApp(address(this))), "!jailed");

token.transfer(
Expand Down Expand Up @@ -406,9 +403,7 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
) internal virtual returns (bytes memory _newCtx) {
// We need to go through all the output tokens and update their IDA shares
_newCtx = _ctx;

(uint128 userShares, uint128 daoShares, uint128 affiliateShares) = _getShareAllocations(_shareholderUpdate);

// updateOutputPools
for (uint32 _index = 0; _index < market.numOutputPools; _index++) {
_newCtx = _updateSubscriptionWithContext(
Expand All @@ -427,12 +422,11 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
daoShares,
market.outputPools[_index].token
);
address affiliate = referrals.getAffiliateAddress(_shareholderUpdate.shareholder);
if (affiliate != address(0)) {
if (_shareholderUpdate.affiliate != address(0)) {
_newCtx = _updateSubscriptionWithContext(
_newCtx,
_index,
affiliate,
_shareholderUpdate.affiliate,
// affiliate may get 0.2%
affiliateShares,
market.outputPools[_index].token
Expand All @@ -449,9 +443,8 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
(,,daoShares,) = getIDAShares(market.outputPoolIndicies[_shareholderUpdate.token], owner());
daoShares *= market.outputPools[market.outputPoolIndicies[_shareholderUpdate.token]].shareScaler;

address affiliateAddress = referrals.getAffiliateAddress(_shareholderUpdate.shareholder);
if (address(0) != affiliateAddress) {
(,,affiliateShares,) = getIDAShares(market.outputPoolIndicies[_shareholderUpdate.token], affiliateAddress);
if (address(0) != _shareholderUpdate.affiliate) {
(,,affiliateShares,) = getIDAShares(market.outputPoolIndicies[_shareholderUpdate.token], _shareholderUpdate.affiliate);
affiliateShares *= market.outputPools[market.outputPoolIndicies[_shareholderUpdate.token]].shareScaler;
}

Expand All @@ -463,7 +456,7 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
if(changeInFlowRate > 0) {
// Add new shares to the DAO
feeShares = uint128(uint256(int256(changeInFlowRate)) * market.feeRate / 1e6);
if (address(0) != affiliateAddress) {
if (address(0) != _shareholderUpdate.affiliate) {
affiliateShares += feeShares * market.affiliateFee / 1e6;
feeShares -= feeShares * market.affiliateFee / 1e6;
}
Expand All @@ -472,7 +465,7 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
// Make the rate positive
changeInFlowRate = -1 * changeInFlowRate;
feeShares = uint128(uint256(int256(changeInFlowRate)) * market.feeRate / 1e6);
if (address(0) != affiliateAddress) {
if (address(0) != _shareholderUpdate.affiliate) {
affiliateShares -= (feeShares * market.affiliateFee / 1e6 > affiliateShares) ? affiliateShares : feeShares * market.affiliateFee / 1e6;
feeShares -= feeShares * market.affiliateFee / 1e6;
}
Expand Down Expand Up @@ -743,9 +736,8 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
_registerReferral(_ctx, _shareholder);

ShareholderUpdate memory _shareholderUpdate = ShareholderUpdate(
_shareholder, 0, _flowRate, _superToken
_shareholder, referrals.getAffiliateAddress(_shareholder), 0, _flowRate, _superToken
);

_newCtx = _updateShareholder(_newCtx, _shareholderUpdate);

}
Expand Down Expand Up @@ -778,7 +770,6 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
bytes calldata _cbdata,
bytes calldata _ctx
) external virtual override returns (bytes memory _newCtx) {

_onlyHost();
if (!_isInputToken(_superToken) || !_isCFAv1(_agreementClass))
return _ctx;
Expand All @@ -798,11 +789,12 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
}

ShareholderUpdate memory _shareholderUpdate = ShareholderUpdate(
_shareholder, _beforeFlowRate, _flowRate, _superToken
_shareholder, referrals.getAffiliateAddress(_shareholder), _beforeFlowRate, _flowRate, _superToken
);

// TODO: Udpate shareholder needs before and after flow rate
_newCtx = _updateShareholder(_newCtx, _shareholderUpdate);

}

// We need before agreement to get the uninvested amount using the flowRate before update
Expand Down Expand Up @@ -845,15 +837,14 @@ abstract contract REXMarket is Ownable, SuperAppBase, Initializable {
(uint256 _uninvestAmount, int96 _beforeFlowRate ) = abi.decode(_cbdata, (uint256, int96));

ShareholderUpdate memory _shareholderUpdate = ShareholderUpdate(
_shareholder, _beforeFlowRate, 0, _superToken
_shareholder, referrals.getAffiliateAddress(_shareholder), _beforeFlowRate, 0, _superToken
);

_newCtx = _updateShareholder(_newCtx, _shareholderUpdate);
// Refund the unswapped amount back to the person who started the stream
try _superToken.transferFrom(address(this), _shareholder, _uninvestAmount)
// solhint-disable-next-line no-empty-blocks
{} catch {
// Nothing to do, pass
}
}
}
Loading

0 comments on commit a88a814

Please sign in to comment.