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

Psm: allow multiple currency for psm #78

Merged
merged 19 commits into from
Nov 10, 2024
2 changes: 1 addition & 1 deletion proto/reserve/psm/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ message GenesisState {
(amino.dont_omitempty) = true
];

repeated Stablecoin stablecoins = 2
repeated StablecoinInfo stablecoins = 2
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];
}
2 changes: 1 addition & 1 deletion proto/reserve/psm/v1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "cosmos_proto/cosmos.proto";
option go_package = "github.com/onomyprotocol/reserve/x/psm/types";

message Params {
// total $nomUSD can mint
// total $nomX can mint
bytes limit_total = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
Expand Down
8 changes: 4 additions & 4 deletions proto/reserve/psm/v1/psm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/onomyprotocol/reserve/x/psm/types";

message Stablecoin {
message StablecoinInfo {
// stablecoin name
string denom = 1;
// limit total stablecoin module support
// limit total stablecoin
bytes limit_total = 2 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
// stablecoin to nomUSD exchange fee, fee_in when 1 stablecoin = 1nomUSD
// stablecoin to nom exchange fee, fee_in when 1 stablecoin = 1nomUSD
bytes fee_in = 3 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
// nomUSD to stablecoin exchange fee, fee_out when 1 stablecoin = 1nomUSD
// nom to stablecoin exchange fee, fee_out when 1 stablecoin = 1nomUSD
bytes fee_out = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
Expand Down
4 changes: 2 additions & 2 deletions proto/reserve/psm/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ message QueryStablecoinRequest {
}

message QueryStablecoinResponse {
Stablecoin stablecoin = 1 [(gogoproto.nullable) = false];
StablecoinInfo stablecoin = 1 [(gogoproto.nullable) = false];

bytes swapable_quantity = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
Expand All @@ -63,7 +63,7 @@ message QueryAllStablecoinResponse {
}

message StablecoinResponse {
Stablecoin stablecoin = 1 [(gogoproto.nullable) = false];
StablecoinInfo stablecoin = 1 [(gogoproto.nullable) = false];
bytes swapable_quantity = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
Expand Down
43 changes: 19 additions & 24 deletions proto/reserve/psm/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
rpc AddStableCoinProposal(MsgAddStableCoin) returns (MsgAddStableCoinResponse);
rpc UpdatesStableCoinProposal(MsgUpdatesStableCoin) returns (MsgUpdatesStableCoinResponse);
rpc SwapTonomUSD(MsgSwapTonomUSD) returns (MsgSwapTonomUSDResponse);
rpc SwapToStablecoin(MsgSwapToStablecoin) returns (MsgSwapToStablecoinResponse);
rpc StableSwap(MsgStableSwap) returns (MsgStableSwapResponse);
}

// MsgUpdateParams is the Msg/UpdateParams request type.
Expand All @@ -44,27 +43,6 @@ message MsgUpdateParams {
// MsgUpdateParams message.
message MsgUpdateParamsResponse {}

message MsgSwapTonomUSD {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
cosmos.base.v1beta1.Coin coin = 2;
}
message MsgSwapTonomUSDResponse {}

message MsgSwapToStablecoin {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
string to_denom = 2;
bytes amount = 3 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];
}
message MsgSwapToStablecoinResponse {}

message MsgAddStableCoin {
option (cosmos.msg.v1.signer) = "authority";
Expand All @@ -91,6 +69,7 @@ message MsgAddStableCoin {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
int64 oracle_script = 6;
}

message MsgAddStableCoinResponse {}
Expand Down Expand Up @@ -120,6 +99,22 @@ message MsgUpdatesStableCoin {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];

int64 oracle_script = 6;
}

message MsgUpdatesStableCoinResponse {}
message MsgUpdatesStableCoinResponse {}

// MsgUpdateParamsResponse defines the response structure for executing a
// MsgStableSwap message.

message MsgStableSwap {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
string expected_denom = 2;
cosmos.base.v1beta1.Coin offer_coin = 3
[ (gogoproto.nullable) = false, (amino.dont_omitempty) = true ];

}
message MsgStableSwapResponse {}
14 changes: 0 additions & 14 deletions script/proposal-2.json

This file was deleted.

23 changes: 23 additions & 0 deletions script/proposal-psm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"messages": [{
"@type": "/reserve.psm.v1.MsgAddStableCoin",
"authority":"onomy10d07y265gmmuvt4z0w9aw880jnsr700jqr8n8k",
"denom": "usdt",
"limit_total": "100000000000000000000000000000",
"fee_in": "0.001000000000000000",
"fee_out": "0.001000000000000000",
"oracle_script": "44"
},
{
"@type": "/reserve.psm.v1.MsgAddStableCoin",
"authority":"onomy10d07y265gmmuvt4z0w9aw880jnsr700jqr8n8k",
"denom": "usdc",
"limit_total": "100000000000000000000000000000",
"fee_in": "0.001000000000000000",
"fee_out": "0.001000000000000000",
"oracle_script": "44"
}],
"deposit": "100000000stake",
"title": "My proposal",
"summary": "A short summary of my proposal"
}
44 changes: 20 additions & 24 deletions script/psm-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ echo $mnemonic2| reserved keys add validator2 --recover --keyring-backend=test -
echo $mnemonic3| reserved keys add validator3 --recover --keyring-backend=test --home=$HOME/.reserved/validator3

# create validator node with tokens to transfer to the three other nodes
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator1
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator2
reserved genesis add-genesis-account $(reserved keys show validator1 -a --keyring-backend=test --home=$HOME/.reserved/validator1) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator2 -a --keyring-backend=test --home=$HOME/.reserved/validator2) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator3
reserved genesis add-genesis-account $(reserved keys show validator3 -a --keyring-backend=test --home=$HOME/.reserved/validator3) 10000000000000000000000000000000stake,10000000000000000000000000000000usdt,10000000000000000000000000000000usdc --home=$HOME/.reserved/validator3
reserved genesis gentx validator1 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator1 --chain-id=testing-1
reserved genesis gentx validator2 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator2 --chain-id=testing-1
reserved genesis gentx validator3 1000000000000000000000stake --keyring-backend=test --home=$HOME/.reserved/validator3 --chain-id=testing-1
Expand Down Expand Up @@ -118,7 +118,7 @@ screen -S onomy3 -t onomy3 -d -m reserved start --home=$HOME/.reserved/validator

# submit proposal add usdt
sleep 7
reserved tx gov submit-proposal ./script/proposal-2.json --home=$HOME/.reserved/validator1 --from validator1 --keyring-backend test --fees 20stake --chain-id testing-1 -y
reserved tx gov submit-proposal ./script/proposal-psm.json --home=$HOME/.reserved/validator1 --from validator1 --keyring-backend test --fees 20stake --chain-id testing-1 -y

# # # vote
sleep 7
Expand All @@ -130,22 +130,18 @@ reserved tx gov vote 1 yes --from validator3 --keyring-backend test --home ~/.r
sleep 15
# echo "========sleep=========="

# # check add usdt, balances
# reserved q psm all-stablecoin
# # # check add usdt, balances
# # reserved q psm all-stablecoin
reserved q bank balances $(reserved keys show validator1 -a --keyring-backend test --home /Users/donglieu/.reserved/validator1)

# # tx swap usdt to nomUSD
# echo "========swap==========="
reserved tx psm swap-to-nomUSD 100000000000000000000000usdt --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
# # # tx swap usdt to nomUSD
# # echo "========swap==========="
reserved tx psm swap 100000000000000000000000usdt nomUSD --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake

sleep 7
# # Check account after swap
reserved q bank balances $(reserved keys show validator1 -a --keyring-backend test --home /Users/donglieu/.reserved/validator1)

# # tx swap nomUSD to usdt
# reserved tx psm swap-to-stablecoin usdt 1000nomUSD --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake

# sleep 7
# # Check account after swap
# reserved q bank balances $(reserved keys show validator1 -a --keyring-backend test --home /Users/donglieu/.reserved/validator1)
# # killall reserved || true
reserved tx psm swap 1000000000usdc nomUSD --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
sleep 7
reserved tx psm swap 1000nomUSD usdc --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
sleep 7
reserved tx psm swap 1000nomUSD usdt --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake
51 changes: 9 additions & 42 deletions x/psm/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,68 +21,35 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(NewSwapToStablecoinCmd())
cmd.AddCommand(NewSwapTonomUSDCmd())
cmd.AddCommand(NewSwapToNomCmd())

return cmd
}

func NewSwapTonomUSDCmd() *cobra.Command {
func NewSwapToNomCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "swap-to-nomUSD [stablecoin]",
Args: cobra.ExactArgs(1),
Short: "swap stablecoin to $nomUSD ",
Long: `swap stablecoin to $nomUSD.

Example:
$ onomyd tx psm swap-to-nomUSD 1000usdt --from mykey
`,

RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
stablecoin, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}

addr := clientCtx.GetFromAddress()
msg := types.NewMsgSwapTonomUSD(addr.String(), &stablecoin)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func NewSwapToStablecoinCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "swap-to-stablecoin [stable-coin-type] [amount-nomUSD]",
Use: "swap [offer_stable_coin] [expected_denom]",
Args: cobra.ExactArgs(2),
Short: "swap $nomUSD to stablecoin ",
Long: `swap $nomUSD to stablecoin.
Short: "stable swap ",
Long: `swap between stable coins.

Example:
$ onomyd tx psm swap-to-stablecoin usdt 10000nomUSD --from mykey
$ onomyd tx psm swap 100000000000000000000000nomUSD ibc/xxxxx --from validator1 --keyring-backend test --home ~/.reserved/validator1 --chain-id testing-1 -y --fees 20stake

`,

RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
nomUSDcoin, err := sdk.ParseCoinNormalized(args[1])
offerCoin, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
}

addr := clientCtx.GetFromAddress()
msg := types.NewMsgSwapToStablecoin(addr.String(), args[0], nomUSDcoin.Amount)
msg := types.NewMsgStableSwap(addr.String(), offerCoin, args[1])

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
12 changes: 6 additions & 6 deletions x/psm/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ func (k Keeper) BeginBlocker(ctx context.Context) error {
}

func (k Keeper) UpdatesStablecoinEpoch(ctx context.Context) error {
updatePrice := func(red types.Stablecoin) bool {
price := k.OracleKeeper.GetPrice(ctx, red.Denom, types.DefaultMintDenom)
updatePrice := func(info types.StablecoinInfo) bool {
price := k.OracleKeeper.GetPrice(ctx, info.Denom, types.ReserveStableCoinDenom)
if price == nil || price.IsNil() {
return false
}

sc := k.stablecoinUpdate(ctx, *price, red)
err := k.Stablecoins.Set(ctx, sc.Denom, sc)
sc := k.stablecoinUpdate(ctx, *price, info)
err := k.StablecoinInfos.Set(ctx, sc.Denom, sc)
if err != nil {
return false
}
return false
}

return k.Stablecoins.Walk(ctx, nil, func(key string, value types.Stablecoin) (stop bool, err error) {
return k.StablecoinInfos.Walk(ctx, nil, func(key string, value types.StablecoinInfo) (stop bool, err error) {
return updatePrice(value), nil
}) //k.IterateStablecoin(ctx, updatePrice)
}
Expand Down Expand Up @@ -56,7 +56,7 @@ func (k Keeper) UpdatesStablecoinEpoch(ctx context.Context) error {
// newfeeOut = 0.02 - 0.014948314143157351 = 0.005051685856842649
// So $nomUSD swap to $USDT will be cheaper than $USDT swap to $nomUSD

func (k Keeper) stablecoinUpdate(ctx context.Context, newPrice math.LegacyDec, stablecoin types.Stablecoin) types.Stablecoin {
func (k Keeper) stablecoinUpdate(ctx context.Context, newPrice math.LegacyDec, stablecoin types.StablecoinInfo) types.StablecoinInfo {
params, err := k.GetParams(ctx)
if err != nil {
panic(err)
Expand Down
13 changes: 7 additions & 6 deletions x/psm/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ func (s *KeeperTestSuite) TestUpdatesStablecoinEpoch() {
for _, t := range tests {
s.Run(t.name, func() {
sc := types.GetMsgStablecoin(&types.MsgAddStableCoin{
Denom: usdt,
LimitTotal: limitUSDT,
FeeIn: t.feeIn,
FeeOut: t.feeOut,
Denom: usdt,
LimitTotal: limitUSDT,
FeeIn: t.feeIn,
FeeOut: t.feeOut,
OracleScript: 44,
})
s.mockOracleKeeper.SetPrice(s.Ctx, sc.Denom, t.priceCurrent)
err := s.k.Stablecoins.Set(s.Ctx, sc.Denom, sc)
err := s.k.StablecoinInfos.Set(s.Ctx, sc.Denom, sc)
s.Require().NoError(err)

s.mockOracleKeeper.SetPrice(s.Ctx, usdt, t.priceUpdate)

err = s.k.UpdatesStablecoinEpoch(s.Ctx)
s.Require().NoError(err)

scUpdate, err := s.k.Stablecoins.Get(s.Ctx, usdt)
scUpdate, err := s.k.StablecoinInfos.Get(s.Ctx, usdt)
s.Require().NoError(err)
// s.Require().Equal(t.priceUpdate, scUpdate.Price)
s.Require().Equal(t.expectFeeIn.String(), scUpdate.FeeIn.String())
Expand Down
Loading
Loading