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

Add shortcut to operator for disburse and cancel pending order #534

Open
lucasvo opened this issue Mar 27, 2021 · 3 comments
Open

Add shortcut to operator for disburse and cancel pending order #534

lucasvo opened this issue Mar 27, 2021 · 3 comments

Comments

@lucasvo
Copy link
Member

lucasvo commented Mar 27, 2021

We should add an extra method to the operator contract that will disburse and cancel any pending order in one transaction. It can be done as follows:

    function disburseAndCancel(uint endEpoch) external
        returns(uint payoutCurrencyAmount, uint payoutTokenAmount, uint remainingSupplyCurrency,  uint remainingRedeemToken)
    {
        require((token.hasMember(msg.sender) == true), "user-not-allowed-to-hold-token");
        (payoutCurrencyAmount, payoutTokenAmount, remainingSupplyCurrency, remainingRedeemToken ) = tranche.disburse(msg.sender, endEpoch);
        if (remainingRedeemToken) {
            tranche.redeemOrder(msg.sender, 0)
        }
        if (remainingSupplyCurrency) {
            tranche.supplyOrder(msg.sender, 0)
        }
    }
@xmxanuel
Copy link
Member

nice idea.

@lucasvo
Copy link
Member Author

lucasvo commented Apr 6, 2021

I just looked at disburse and the operator and I think we can further improve it by doing adding one order method that is able to handle any user state in one method call/transaction. It would update/cancel any existing orders you have and call disburse on a previously filled one if necessary.

I reviewed the tranche code and it seems like one could have both a supply order & redeem order active at the same time. Is this correct? If not, the code would get slightly more complicated.

The below method would mean that no matter what action you wanted to do, you could always do it in one transaction. As they are written now, they only use existing public methods and should not pose any additional security risk and need in-depth testing.

I posted both variations:

    function order(uint supplyAmount, uint redeemAmount) public note {
         require(supplyAmount == 0 || redeemAmount == 0, "tinlake/operator-invalid-args"
         payoutCurrencyAmount, payoutTokenAmount, remainingSupplyCurrency, remainingRedeemToken = tranche.calcDisburse()
         if (payoutCurrencyAmount > 0 || payoutTokenAmount > 0) {
           disburse()
         }

         if (remainingRedeemToken > 0 || redeemAmount> 0) {
           redeemOrder(redeemAmount);
         }

         if (remainingSupplyCurrency > 0 || supplyAmount > 0) {
           supplyOrder(supplyAmount);
         }
    }
    function order(uint supplyAmount, uint redeemAmount) public note {
         require(supplyAmount == 0 || redeemAmount == 0, "tinlake/operator-invalid-args"
         payoutCurrencyAmount, payoutTokenAmount, remainingSupplyCurrency, remainingRedeemToken = tranche.calcDisburse()
         if (payoutCurrencyAmount > 0 || payoutTokenAmount > 0) {
           tranche.disburse(msg.sender)
         }

         bool triggered;
         if (remainingRedeemToken > 0) {
           triggered = true;
           redeemOrder(redeemAmount);
         }

         if (remainingSupplyCurrency > 0) {
           triggered = true;
           supplyOrder(supplyAmount);
         }

         if (triggered) return;
         if (redeemAmount > 0) redeemOrder(redeemAmount);
         if (supplyAmount > 0) supplyOrder(supplyOrder);
    }

@xmxanuel
Copy link
Member

xmxanuel commented Apr 6, 2021

We could use option A. It is possible to make an redeem and a supply order at the same time even if it makes logical no sense but otherwise we would have additional code to prevent it.

I think you can also do it like that to make it more gas efficient. The calcDisburse logic is only executed once.

    function order(uint supplyAmount, uint redeemAmount) public note {
         require(supplyAmount == 0 || redeemAmount == 0, "tinlake/operator-invalid-args");
         uint remainingSupplyCurrency = 0;
         uint remainingRedeemToken = 0;
         if (tranche.users(msg.sender).orderedInEpoch() <= epochTicker.lastEpochExecuted()) {
         ( , ,  remainingSupplyCurrency, remainingRedeemToken = tranche.disburse();
         }
         if (remainingRedeemToken > 0 || redeemAmount > 0) {
           redeemOrder(redeemAmount);
         }
         if (remainingSupplyCurrency > 0 || supplyAmount > 0) {
           supplyOrder(supplyAmount);
         }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants