Skip to content

Commit

Permalink
add symbol psm
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Nov 11, 2024
1 parent 2a3b39c commit 31f6672
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 89 deletions.
1 change: 1 addition & 0 deletions proto/reserve/psm/v1/psm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ message StablecoinInfo {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string sym_bol = 7;
}
2 changes: 2 additions & 0 deletions proto/reserve/psm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ message MsgAddStableCoin {
(gogoproto.nullable) = false
];
int64 oracle_script = 6;
string sym_bol = 7;
}

message MsgAddStableCoinResponse {}
Expand Down Expand Up @@ -101,6 +102,7 @@ message MsgUpdatesStableCoin {
];

int64 oracle_script = 6;
string sym_bol = 7;
}

message MsgUpdatesStableCoinResponse {}
Expand Down
2 changes: 1 addition & 1 deletion x/psm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {

func (k Keeper) UpdatesStablecoinEpoch(ctx context.Context) error {
updatePrice := func(info types.StablecoinInfo) bool {
price := k.OracleKeeper.GetPrice(ctx, info.Denom, types.ReserveStableCoinDenom)
price := k.OracleKeeper.GetPrice(ctx, info.SymBol, types.ReserveStableCoinDenom)
if price == nil || price.IsNil() {
return false
}
Expand Down
1 change: 1 addition & 0 deletions x/psm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (s *KeeperTestSuite) TestUpdatesStablecoinEpoch() {
s.Run(t.name, func() {
sc := types.GetMsgStablecoin(&types.MsgAddStableCoin{
Denom: usdt,
SymBol: usdt,
LimitTotal: limitUSDT,
FeeIn: t.feeIn,
FeeOut: t.feeOut,
Expand Down
3 changes: 3 additions & 0 deletions x/psm/keeper/proposals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (s *KeeperTestSuite) TestAddStableCoinProposal() {
proAdd := types.MsgAddStableCoin{
Denom: usdt,
LimitTotal: limitUSDT,
SymBol: usdt,
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand All @@ -34,6 +35,7 @@ func (s *KeeperTestSuite) TestUpdateStableCoinProposal() {
proAdd := types.MsgAddStableCoin{
Denom: usdt,
LimitTotal: limitUSDT,
SymBol: usdt,
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand All @@ -57,6 +59,7 @@ func (s *KeeperTestSuite) TestUpdateStableCoinProposal() {
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
SymBol: usdt,
OracleScript: 44,
}

Expand Down
28 changes: 14 additions & 14 deletions x/psm/keeper/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
// SwapToStablecoin return receiveAmount, fee, error
func (k Keeper) SwapToOtherStablecoin(ctx context.Context, addr sdk.AccAddress, offerCoin sdk.Coin, expectedDenom string) error {
// check stablecoin is suport
ok, err := k.StablecoinInfos.Has(ctx, expectedDenom)
if err != nil || !ok {
stablecoin, err := k.StablecoinInfos.Get(ctx, expectedDenom)
if err != nil {
return fmt.Errorf("%s not in list stablecoin supported", expectedDenom)
}

Expand All @@ -27,7 +27,7 @@ func (k Keeper) SwapToOtherStablecoin(ctx context.Context, addr sdk.AccAddress,
}

// check balace and calculate amount of coins received
receiveAmountStablecoin, fee_out, err := k.calculateSwapToStablecoin(ctx, offerCoin.Amount, expectedDenom)
receiveAmountStablecoin, fee_out, err := k.calculateSwapToStablecoin(ctx, offerCoin.Amount, stablecoin)
if err != nil {
return err
}
Expand Down Expand Up @@ -76,8 +76,8 @@ func (k Keeper) SwapToOtherStablecoin(ctx context.Context, addr sdk.AccAddress,

func (k Keeper) SwapToOnomyStableToken(ctx context.Context, accAddress sdk.AccAddress, offerCoin sdk.Coin, expectedDenom string) error {
// check stablecoin is suport
ok, err := k.StablecoinInfos.Has(ctx, offerCoin.Denom)
if err != nil || !ok {
stablecoin, err := k.StablecoinInfos.Get(ctx, offerCoin.Denom)
if err != nil {
return fmt.Errorf("%s not in list stablecoin supported", offerCoin.Denom)
}

Expand All @@ -88,7 +88,7 @@ func (k Keeper) SwapToOnomyStableToken(ctx context.Context, accAddress sdk.AccAd
}

// check balance user and calculate amount of coins received
receiveAmountnomUSD, fee_in, err := k.calculateSwapToOnomyStableToken(ctx, offerCoin)
receiveAmountnomUSD, fee_in, err := k.calculateSwapToOnomyStableToken(ctx, offerCoin, stablecoin.SymBol)
if err != nil {
return err
}
Expand Down Expand Up @@ -150,26 +150,26 @@ func (k Keeper) PayFeesIn(ctx context.Context, amount math.Int, denom string) (m
}

// SwapToStablecoin return receiveAmount, fee, error
func (k Keeper) calculateSwapToStablecoin(ctx context.Context, amount math.Int, toDenom string) (math.Int, sdk.DecCoin, error) {
multiplier := k.OracleKeeper.GetPrice(ctx, toDenom, types.ReserveStableCoinDenom)
func (k Keeper) calculateSwapToStablecoin(ctx context.Context, amount math.Int, sc types.StablecoinInfo) (math.Int, sdk.DecCoin, error) {
multiplier := k.OracleKeeper.GetPrice(ctx, sc.SymBol, types.ReserveStableCoinDenom)
if multiplier == nil || multiplier.IsNil() {
return math.Int{}, sdk.DecCoin{}, errors.Wrapf(oracletypes.ErrInvalidOracle, "can not get price with base %s quote %s", toDenom, types.ReserveStableCoinDenom)
return math.Int{}, sdk.DecCoin{}, errors.Wrapf(oracletypes.ErrInvalidOracle, "can not get price with base %s quote %s", sc.SymBol, types.ReserveStableCoinDenom)
}
amountStablecoin := amount.ToLegacyDec().Quo(*multiplier)

fee, err := k.PayFeesOut(ctx, amountStablecoin.RoundInt(), toDenom)
fee, err := k.PayFeesOut(ctx, amountStablecoin.RoundInt(), sc.Denom)
if err != nil {
return math.Int{}, sdk.DecCoin{}, err
}

receiveAmount := amountStablecoin.Sub(fee)
return receiveAmount.RoundInt(), sdk.NewDecCoinFromDec(toDenom, fee), nil
return receiveAmount.RoundInt(), sdk.NewDecCoinFromDec(sc.Denom, fee), nil
}

func (k Keeper) calculateSwapToOnomyStableToken(ctx context.Context, stablecoin sdk.Coin) (math.Int, sdk.DecCoin, error) {
multiplier := k.OracleKeeper.GetPrice(ctx, stablecoin.Denom, types.ReserveStableCoinDenom)
func (k Keeper) calculateSwapToOnomyStableToken(ctx context.Context, stablecoin sdk.Coin, symBol string) (math.Int, sdk.DecCoin, error) {
multiplier := k.OracleKeeper.GetPrice(ctx, symBol, types.ReserveStableCoinDenom)
if multiplier == nil || multiplier.IsNil() {
return math.Int{}, sdk.DecCoin{}, errors.Wrapf(oracletypes.ErrInvalidOracle, "can not get price with base %s quote %s", stablecoin.Denom, types.ReserveStableCoinDenom)
return math.Int{}, sdk.DecCoin{}, errors.Wrapf(oracletypes.ErrInvalidOracle, "can not get price with base %s quote %s", symBol, types.ReserveStableCoinDenom)
}

amountnomUSD := multiplier.Mul(stablecoin.Amount.ToLegacyDec())
Expand Down
4 changes: 4 additions & 0 deletions x/psm/keeper/swap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (s *KeeperTestSuite) TestSwapToOnomyStableToken() {
_, err = s.msgServer.AddStableCoinProposal(s.Ctx, &types.MsgAddStableCoin{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Denom: usdt,
SymBol: usdt,
LimitTotal: limitUSDT,
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand All @@ -54,6 +55,7 @@ func (s *KeeperTestSuite) TestSwapToOnomyStableToken() {
_, err := s.msgServer.AddStableCoinProposal(s.Ctx, &types.MsgAddStableCoin{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Denom: usdc,
SymBol: usdc,
LimitTotal: limitUSDC,
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand Down Expand Up @@ -113,6 +115,7 @@ func (s *KeeperTestSuite) TestSwapToOtherStablecoin() {
_, err = s.msgServer.AddStableCoinProposal(s.Ctx, &types.MsgAddStableCoin{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Denom: usdt,
SymBol: usdt,
LimitTotal: limitUSDT,
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand All @@ -135,6 +138,7 @@ func (s *KeeperTestSuite) TestSwapToOtherStablecoin() {
_, err := s.msgServer.AddStableCoinProposal(s.Ctx, &types.MsgAddStableCoin{
Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Denom: usdc,
SymBol: usdc,
LimitTotal: limitUSDC,
FeeIn: math.LegacyMustNewDecFromStr("0.001"),
FeeOut: math.LegacyMustNewDecFromStr("0.001"),
Expand Down
8 changes: 8 additions & 0 deletions x/psm/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (msg MsgAddStableCoin) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "empty denom")
}

if msg.SymBol == "" {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "empty symbol")
}

if msg.OracleScript <= 0 {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "empty oracle script")
}
Expand Down Expand Up @@ -122,6 +126,10 @@ func (msg MsgUpdatesStableCoin) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "empty denom")
}

if msg.SymBol == "" {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "empty symbol")
}

if msg.LimitTotal.LT(math.ZeroInt()) {
return sdkerrors.Wrap(ErrInvalidAddStableCoinProposal, "limittotal less than zero")
}
Expand Down
2 changes: 2 additions & 0 deletions x/psm/types/psm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func GetMsgStablecoin(msg getStablecoinFromMsg) StablecoinInfo {
FeeOut: msg.GetFeeOut(),
TotalStablecoinLock: math.ZeroInt(),
FeeMaxStablecoin: msg.GetFeeIn().Add(msg.GetFeeOut()),
SymBol: msg.GetSymBol(),
}
}

Expand All @@ -21,4 +22,5 @@ type getStablecoinFromMsg interface {
// GetPrice() math.LegacyDec
GetFeeIn() math.LegacyDec
GetFeeOut() math.LegacyDec
GetSymBol() string
}
104 changes: 78 additions & 26 deletions x/psm/types/psm.pb.go

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

Loading

0 comments on commit 31f6672

Please sign in to comment.