-
Notifications
You must be signed in to change notification settings - Fork 202
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
feat(perp): MsgWithdrawFromPerpFund sudo call as part of #1642 #1734
Changes from 2 commits
240dffe
596a840
5121341
0090d2f
e43672a
d3f0ce3
1316f18
cc89084
40aec00
b7fc955
1e2e226
bf89c80
a9938d4
11cb148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,11 @@ service Msg { | |
// [Admin] Only callable by sudoers. | ||
rpc ShiftSwapInvariant(MsgShiftSwapInvariant) | ||
returns (MsgShiftSwapInvariantResponse) {} | ||
|
||
// WithdrawFromPerpFund: gRPC tx msg to withdraw from the perp fund module | ||
// account. [Admin] Only callable by sudoers. | ||
rpc WithdrawFromPerpFund(MsgWithdrawFromPerpFund) | ||
returns (MsgWithdrawFromPerpFundResponse) {} | ||
} | ||
|
||
// -------------------------- Settle Position -------------------------- | ||
|
@@ -429,3 +434,19 @@ message MsgShiftSwapInvariant { | |
} | ||
|
||
message MsgShiftSwapInvariantResponse {} | ||
|
||
// -------------------------- WithdrawFromPerpFund -------------------------- | ||
|
||
// WithdrawFromPerpFund: gRPC tx msg for changing the swap invariant. | ||
// Admin-only. | ||
message MsgWithdrawFromPerpFund { | ||
string sender = 1; | ||
string amount = 2 [ | ||
k-yang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", | ||
(gogoproto.nullable) = false | ||
]; | ||
string denom = 3; | ||
string to_addr = 4; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit) it might be safer to just withdraw to whoever sends the message, i.e. the message I can foresee possible human error in the future where somebody sends the message but accidentally sets the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so too. The problem we might encounter here is when the claim goes to a smart contract that doesn't expose a function to withdraw its balance, or if there's routing happening between multiple contracts like the ones used in Mars Protocol or in future Nibiru-developed contracts.
I'm trying to avoid this ^ deadlock. Adding an explicit Another option is to make that field nullable, so that empty means, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, I didn't even consider that scenario. In that case, then I think it's better to be explicit. Thinking about it more, in the general case, we'll likely be sending these A more sophisticated design could be to only execute the message if the |
||
} | ||
|
||
message MsgWithdrawFromPerpFundResponse {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,34 +23,42 @@ import ( | |
// function is being used when it's called from the PerpKeeper.Admin struct. | ||
type admin struct{ *Keeper } | ||
|
||
/* | ||
WithdrawFromInsuranceFund sends funds from the Insurance Fund to the given "to" | ||
address. | ||
|
||
Args: | ||
- ctx: Blockchain context holding the current state | ||
- amount: Amount of micro-NUSD to withdraw. | ||
- to: Recipient address | ||
*/ | ||
func (k admin) WithdrawFromInsuranceFund( | ||
ctx sdk.Context, amount sdkmath.Int, to sdk.AccAddress, | ||
// WithdrawFromPerpFund sends funds from the Perp Fund to the "to" address. | ||
// | ||
// Args: | ||
// - ctx: Blockchain context holding the current state | ||
// - amount: Amount of micro-NUSD to withdraw. | ||
// - sender: Admin address registered in x/sudo | ||
// - to: Recipient address | ||
func (k admin) WithdrawFromPerpFund( | ||
ctx sdk.Context, amount sdkmath.Int, sender, to sdk.AccAddress, denom string, | ||
) (err error) { | ||
collateral, err := k.Collateral.Get(ctx) | ||
if err != nil { | ||
if err := k.SudoKeeper.CheckPermissions(sender, ctx); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have some opinions related to checking permissions here. I created some PR here: #1751 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a stateful validation, reading from the KV store of another module, so it's not really for the MsgServer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. It does not change that principle if this call is still in the message server. MsgServer delegates validation of the request to the SudoKeeper (that he really is the one reading the KVStore, not the MsgServer), and if it is correct, it calls the Admin function. What I don't believe is in the admin function to validate the permissions. It is a thin line and a matter of convention, but I can show you some benefits of this slight separation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking a whitelist is not a message field validation. If the keeper method doesn't have the permission check, other modules that import the methods can sidestep the sudo, defeating the purpose for the check. It's not just a difference by convention. Switching There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message field validation should be done by default out of the message server as long as you implement ValidateBasic(). Validating any field in the message server is duplicating computing cycles. What, then, is the use case of the MsgServer if we remove the ValidateBasic? There are two paradigms here: the MsgServer as orchestrator of keepers (my ideal) or the Keepers as holders of all the logic and being pure defensive (your ideal; this removes the need for MsgServer in my opinion as it becomes only a wrapper of the keeper). It is a matter of convention from the moment you agree on what it means to share some Keeper method. Is there any use case at this stage for CloseMarket to be public in the keeper? Try to avoid the what-if thing. I am open to anything as long as it makes sense, and we will follow the convention from now on. |
||
return err | ||
} | ||
|
||
coinToSend := sdk.NewCoin(collateral, amount) | ||
var collateralDenom string | ||
if denom == "" { | ||
denomFromState, err := k.Collateral.Get(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
collateralDenom = denomFromState | ||
} else { | ||
collateralDenom = denom | ||
} | ||
|
||
coinToSend := sdk.NewCoin(collateralDenom, amount) | ||
if err = k.BankKeeper.SendCoinsFromModuleToAccount( | ||
ctx, | ||
/* from */ types.PerpEFModuleAccount, | ||
/* from */ types.PerpFundModuleAccount, | ||
/* to */ to, | ||
/* amount */ sdk.NewCoins(coinToSend), | ||
); err != nil { | ||
return err | ||
} | ||
ctx.EventManager().EmitEvent(sdk.NewEvent( | ||
"withdraw_from_if", | ||
"withdraw_from_perp_fund", | ||
sdk.NewAttribute("to", to.String()), | ||
sdk.NewAttribute("funds", coinToSend.String()), | ||
)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A little bit of a more descriptive message like the one in the title would be nice. You will appreciate it when you search for this in the changelog in four years!