Skip to content

calculate amount of collateral automatically when an order is placed#117

Merged
just-in-chang merged 2 commits intomasterfrom
feat/auto-collateral
Jul 18, 2022
Merged

calculate amount of collateral automatically when an order is placed#117
just-in-chang merged 2 commits intomasterfrom
feat/auto-collateral

Conversation

@just-in-chang
Copy link
Contributor

No description provided.

return nil, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFunds, "insufficient funds to place order")
}
msg.Funds[0].Amount = calculatedCollateral.Mul(sdk.NewDecFromInt(sdk.NewInt(1000000))).RoundInt()
msg.Funds[0].Denom = msg.Orders[0].PriceDenom
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally the fund denom would be micro unit whereas price denom would be whole unit. We can add a helper like this in some util files:

func ConvertMicroToWholeDenom(amount sdk.Dec, denom string) (sdk.Dec, string) {
    assert denom starts with a `u`
    wholeDenom := denom[1:]
    assert wholeDenom is a valid denom (whitelisted in AssetList)
    return amount * 1000000, wholeDenom
}


if msg.AutoCalculateDeposit {
// throw error if current funds amount is less than calculatedCollateral
if msg.Funds[0].Amount.LT(calculatedCollateral.Mul(sdk.NewDecFromInt(sdk.NewInt(1000000))).RoundInt()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably didn't describe this clearly enough, but we don't really care what's passed in msg.Funds by the user. As long as msg. AutoCalculateDeposit is true, and there are enough funds in the user's bank, we will execute line 58-59. To check user's bank balance you can do k.Keeper.BankKeeper.GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string)

@just-in-chang just-in-chang force-pushed the feat/auto-collateral branch from fa64a30 to 92a57a3 Compare July 18, 2022 18:24

ctx := sdk.UnwrapSDKContext(goCtx)

calculatedCollateral := sdk.NewDecFromBigInt(big.NewInt(0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move inside the if block below


if msg.AutoCalculateDeposit {
// throw error if current funds amount is less than calculatedCollateral
if msg.Funds[0].Amount.LT(k.Keeper.BankKeeper.GetBalance(ctx, sdk.AccAddress(msg.GetCreator()), msg.Orders[0].PriceDenom).Amount) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if calculatedCollateral.GT(k.Keeper.BankKeeper.GetBalance(...))

@just-in-chang just-in-chang merged commit 24c0a46 into master Jul 18, 2022
philipsu522 pushed a commit that referenced this pull request Jul 19, 2022
…117)

* calculate amount of collateral automatically when an order is placed

* address comments
alg747 pushed a commit to UXDProtocol/sei-chain that referenced this pull request Jul 20, 2022
…ei-protocol#117)

* calculate amount of collateral automatically when an order is placed

* address comments
@masih masih deleted the feat/auto-collateral branch October 31, 2025 15:48
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

Successfully merging this pull request may close these issues.

2 participants