Skip to content

Commit

Permalink
Merge pull request #344 from scorpioborn/refactor/rename-reimbursed
Browse files Browse the repository at this point in the history
Refactor / Rename reimbursement liquidity to returned amount
  • Loading branch information
3eyedraga authored Jan 25, 2024
2 parents fbff8e3 + 3431f1a commit c4f3ec1
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 85 deletions.
30 changes: 19 additions & 11 deletions app/upgrades/v7/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/sge-network/sge/app/keepers"
"github.com/sge-network/sge/x/market/types"
markettypes "github.com/sge-network/sge/x/market/types"
)

func CreateUpgradeHandler(
Expand All @@ -21,28 +21,36 @@ func CreateUpgradeHandler(
panic(err)
}

for _, p := range participationList {
if !p.IsSettled {
for _, bp := range participationList {
if !bp.IsSettled {
continue
}

market, found := k.MarketKeeper.GetMarket(ctx, p.OrderBookUID)
market, found := k.MarketKeeper.GetMarket(ctx, bp.OrderBookUID)
if !found {
panic(fmt.Errorf("market not found %s", p.OrderBookUID))
panic(fmt.Errorf("market not found %s", bp.OrderBookUID))
}

reimburseFee := false
if market.Status == types.MarketStatus_MARKET_STATUS_CANCELED ||
market.Status == types.MarketStatus_MARKET_STATUS_ABORTED {
switch market.Status {
case markettypes.MarketStatus_MARKET_STATUS_RESULT_DECLARED:
bp.ReturnedAmount = bp.Liquidity.Add(bp.ActualProfit)
if bp.NotParticipatedInBetFulfillment() {
reimburseFee = true
}

case markettypes.MarketStatus_MARKET_STATUS_CANCELED,
markettypes.MarketStatus_MARKET_STATUS_ABORTED:
bp.ReturnedAmount = bp.Liquidity
reimburseFee = true
p.ReimbursedLiquidity = p.Liquidity
}

if reimburseFee || p.NotParticipatedInBetFulfillment() {
p.ReimbursedFee = p.Fee
if reimburseFee {
bp.ReimbursedFee = bp.Fee
bp.ReturnedAmount = bp.ReturnedAmount.Add(bp.ReimbursedFee)
}

k.OrderbookKeeper.SetOrderBookParticipation(ctx, p)
k.OrderbookKeeper.SetOrderBookParticipation(ctx, bp)
}

return mm.RunMigrations(ctx, configurator, fromVM)
Expand Down
6 changes: 3 additions & 3 deletions proto/sge/orderbook/participation.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ message OrderBookParticipation {
// is_settled represents if the participation is settled or not.
bool is_settled = 14 [ (gogoproto.moretags) = "yaml:\"is_settled\"" ];

// reimbursed_liquidity is the liquidity reimbursed because of reasons such as market calcellation.
string reimbursed_liquidity = 15 [
// returned_amount is the total returned amount to the user's account including reimbursed fees.
string returned_amount = 15 [
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"reimbursed_liquidity\""
(gogoproto.moretags) = "yaml:\"returned_amount\""
];

// reimbursed_fee is the fee reimbursed because of reasons such as market calcellation.
Expand Down
2 changes: 1 addition & 1 deletion x/orderbook/client/cli/query_participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func networkWithParticipationObjects(t *testing.T, n int) (*network.Network, []t
CurrentRoundMaxLossOddsUID: "6db09053-2901-4110-8fb5-c14e21f8d666",
ActualProfit: sdkmath.NewInt(0),
IsSettled: false,
ReimbursedLiquidity: sdkmath.NewInt(10),
ReturnedAmount: sdkmath.NewInt(1010),
ReimbursedFee: sdkmath.NewInt(10),
}
nullify.Fill(&participation)
Expand Down
13 changes: 7 additions & 6 deletions x/orderbook/keeper/orderbook_settle.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ func (k Keeper) settleParticipation(

switch market.Status {
case markettypes.MarketStatus_MARKET_STATUS_RESULT_DECLARED:
depositPlusProfit := bp.Liquidity.Add(bp.ActualProfit)
bp.ReturnedAmount = bp.Liquidity.Add(bp.ActualProfit)
// refund participant's account from orderbook liquidity pool.
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, depositorAddress, depositPlusProfit); err != nil {
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, depositorAddress, bp.ReturnedAmount); err != nil {
return err
}
if bp.NotParticipatedInBetFulfillment() {
Expand All @@ -131,13 +131,13 @@ func (k Keeper) settleParticipation(

case markettypes.MarketStatus_MARKET_STATUS_CANCELED,
markettypes.MarketStatus_MARKET_STATUS_ABORTED:
bp.ReturnedAmount = bp.Liquidity
// refund participant's account from orderbook liquidity pool.
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, depositorAddress, bp.Liquidity); err != nil {
if err := k.refund(types.OrderBookLiquidityFunder{}, ctx, depositorAddress, bp.ReturnedAmount); err != nil {
return err
}
refundHouseDepositFeeToDepositor = true
bp.ReimbursedLiquidity = bp.Liquidity
k.hooks.AfterHouseRefund(ctx, depositorAddress, bp.Liquidity)
k.hooks.AfterHouseRefund(ctx, depositorAddress, bp.ReturnedAmount)
default:
return sdkerrors.Wrapf(
types.ErrUnknownMarketStatus,
Expand All @@ -153,6 +153,7 @@ func (k Keeper) settleParticipation(
return err
}
bp.ReimbursedFee = bp.Fee
bp.ReturnedAmount = bp.ReturnedAmount.Add(bp.ReimbursedFee)
k.hooks.AfterHouseFeeRefund(ctx, depositorAddress, bp.Fee)
} else {
// refund participant's account from house fee collector.
Expand All @@ -176,7 +177,7 @@ func (k Keeper) settleParticipation(
sdk.NewAttribute(types.AttributeValueMarketUID, market.UID),
sdk.NewAttribute(types.AttributeValueParticipationIndex, cast.ToString(bp.Index)),
sdk.NewAttribute(types.AttributeValueParticipationReimbursedFees, bp.ReimbursedFee.String()),
sdk.NewAttribute(types.AttributeValueParticipationReimbursedLiquidity, bp.ReimbursedLiquidity.String()),
sdk.NewAttribute(types.AttributeValueParticipationReturnedAmount, bp.ReturnedAmount.String()),
sdk.NewAttribute(types.AttributeValueParticipationActualProfit, bp.ActualProfit.String()),
),
)
Expand Down
2 changes: 1 addition & 1 deletion x/orderbook/keeper/participation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func createNParticipation(
items[i].MaxLoss = sdkmath.NewInt(100)
items[i].TotalBetAmount = sdkmath.NewInt(100)
items[i].ReimbursedFee = sdkmath.NewInt(10)
items[i].ReimbursedLiquidity = sdkmath.NewInt(10)
items[i].ReturnedAmount = sdkmath.NewInt(1010)

keeper.SetOrderBookParticipation(ctx, items[i])
}
Expand Down
10 changes: 5 additions & 5 deletions x/orderbook/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const (

TypeParticipationSettlement = "participation_settlement"

AttributeValueMarketUID = "market_uid"
AttributeValueParticipationIndex = "participation_index"
AttributeValueParticipationReimbursedFees = "participation_reimbursed_fee"
AttributeValueParticipationReimbursedLiquidity = "participation_reimbursed_liquidity"
AttributeValueParticipationActualProfit = "participation_actual_profit"
AttributeValueMarketUID = "market_uid"
AttributeValueParticipationIndex = "participation_index"
AttributeValueParticipationReimbursedFees = "participation_reimbursed_fee"
AttributeValueParticipationReturnedAmount = "participation_returned_amount"
AttributeValueParticipationActualProfit = "participation_actual_profit"
)
117 changes: 59 additions & 58 deletions x/orderbook/types/participation.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c4f3ec1

Please sign in to comment.