Skip to content

Commit

Permalink
Merge pull request #388 from UnUniFi/feat/derivatives-fix-command-clo…
Browse files Browse the repository at this point in the history
…se-potision

Feat/derivatives fix command close potision
  • Loading branch information
mkXultra authored Feb 22, 2023
2 parents fcfb8db + 12ed797 commit a22fb11
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 19 deletions.
5 changes: 4 additions & 1 deletion scripts/commands/derivatives/msgs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ $BINARY tx derivatives open-position perpetual-futures 100ubtc ubtc uusdc long -
$BINARY tx derivatives open-position perpetual-futures 100uusdc ubtc uusdc short --from=$USER1 $conf | jq .raw_log | sed 's/\\n/\n/g'

# query positions
$BINARY q derivatives positions $(ununifid keys show -a $USER1) --home=$NOME_HOME
$BINARY q derivatives positions $USER_ADDRESS_1

# close potision
$BINARY tx derivatives close-position 1 --from=$USER1 $conf | jq .raw_log | sed 's/\\n/\n/g'
17 changes: 11 additions & 6 deletions scripts/setup/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,27 @@ jq '.app_state.pricefeed.params.markets = [
{ "market_id": "ubtc:usd:30", "base_asset": "ubtc", "quote_asset": "usd", "oracles": [ "'$PRICEFEED_ADDRESS'" ], "active": true },
{ "market_id": "uusdc:usd", "base_asset": "uusdc", "quote_asset": "usd", "oracles": [ "'$PRICEFEED_ADDRESS'" ], "active": true },
{ "market_id": "uusdc:usd:30", "base_asset": "uusdc", "quote_asset": "usd", "oracles": [ "'$PRICEFEED_ADDRESS'" ], "active": true },
{"market_id": "uusdc:ubtc", "base_asset":"uusdc", "quote_asset":"ubtc", "oracles": ["'$PRICEFEED'"], "active": true}
{ "market_id": "ubtc:uusdc", "base_asset":"ubtc", "quote_asset":"uusdc", "oracles": ["'$PRICEFEED_ADDRESS'"], "active": true},
{ "market_id": "ubtc:uusdc:30", "base_asset":"ubtc", "quote_asset":"uusdc", "oracles": ["'$PRICEFEED_ADDRESS'"], "active": true}
]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.pool.base_lpt_mint_fee = "0.001"' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.pool.base_lpt_redeem_fee = "0.001"' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.pool.accepted_assets = [{"denom":"ubtc", "target_weight": "0.6"}, {"denom":"uusd", "target_weight":"0.4"}]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.pool.accepted_assets = [{"denom":"ubtc", "target_weight": "0.6"}, {"denom":"uusdc", "target_weight":"0.4"}]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.perpetual_futures.commission_rate = "0.001"' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.perpetual_futures.margin_maintenance_rate = "0.5"' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.perpetual_futures.imaginary_funding_rate_proportional_coefficient = "0.0005"' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.perpetual_futures.markets = [{"base_denom": "ubtc", "quote_denom": "uusd" }]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.bank.denom_metadata = [{"base" : "ubtc" , "symbol": "ubtc"}, {"base" : "uusd", "symbol": "uusdc"}]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.derivatives.params.perpetual_futures.markets = [{"base_denom": "ubtc", "quote_denom": "uusdc" }]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.bank.denom_metadata = [
{"base" : "ubtc" , "symbol": "ubtc"},
{"base" : "uusdc", "symbol": "uusdc"}
]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;
jq '.app_state.pricefeed.posted_prices = [
{"expiry": "2024-02-20T12:02:01Z","market_id": "ubtc:usd","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "0.024508410211260500"},
{"expiry": "2024-02-20T12:02:47Z","market_id": "ubtc:usd:30","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "0.005779087260702010"},
{"expiry": "2024-02-20T12:03:30Z","market_id": "uusdc:usd","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "0.000001001479651825"},
{"expiry": "2024-02-20T12:04:11Z","market_id": "uusdc:usd:30","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "0.000001002011358752"},
{"expiry": "2024-02-20T12:00:38Z","market_id": "ubtc:uusdc","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "24528.185864015486004064"}
{"expiry": "2024-02-20T12:00:38Z","market_id": "ubtc:uusdc","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "24528.185864015486004064"},
{"expiry": "2024-02-20T12:00:38Z","market_id": "ubtc:uusdc:30","oracle_address": "ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr","price": "24528.185864015486004064"}
]' $NODE_HOME/config/genesis.json > temp.json ; mv temp.json $NODE_HOME/config/genesis.json;

ununifid start --home=$NODE_HOME
# ununifid start --home=$NODE_HOME
5 changes: 5 additions & 0 deletions scripts/setup/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ USER_MNEMONIC_3="among follow tooth egg unhappy city road expire solution visit
USER_MNEMONIC_4="charge split umbrella day gauge two orphan random human clerk buzz funny cabin purse fluid lecture blouse keen twist loud animal supply hat scare"
PRICEFEED_MNEMONIC="jelly fortune hire delay impose daughter praise amazing patch gesture easy achieve intact genre swamp gossip aisle arrest item seek inherit cradle hover involve"

VAL_ADDRESS_1=ununifi1a8jcsmla6heu99ldtazc27dna4qcd4jygsthx6
USER_ADDRESS_1=ununifi155u042u8wk3al32h3vzxu989jj76k4zcu44v6w
USER_ADDRESS_2=ununifi1v0h8j7x7kfys29kj4uwdudcc9y0nx6twwxahla
USER_ADDRESS_3=ununifi1y3t7sp0nfe2nfda7r9gf628g6ym6e7d44evfv6
USER_ADDRESS_4=ununifi1pp2ruuhs0k7ayaxjupwj4k5qmgh0d72wrdyjyu
PRICEFEED_ADDRESS=ununifi1h7ulktk5p2gt7tnxwhqzlq0yegq47hum0fahcr

conf="--home=$NODE_HOME --chain-id=$CHAINID_1 --keyring-backend=test -y --broadcast-mode=block"
7 changes: 3 additions & 4 deletions x/derivatives/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,17 @@ func CmdClosePosition() *cobra.Command {
Example:
$ %s tx %s close-position --from myKeyName --chain-id ununifi-x
`, version.AppName, types.ModuleName)),
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

sender := clientCtx.GetFromAddress()
potisionId := args[0]

msg := types.MsgClosePosition{
Sender: ununifiType.StringAccAddress(sender),
}
msg := types.NewMsgClosePosition(sender, potisionId)

if err := msg.ValidateBasic(); err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion x/derivatives/keeper/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (k Keeper) GetPairRate(ctx sdk.Context, pair types.Market) (*sdk.Dec, error
if err != nil {
return nil, err
}
fmt.Printf("%s", marketId)
price, err := k.pricefeedKeeper.GetCurrentPrice(ctx, marketId)

return &price.Price, err
Expand Down
6 changes: 5 additions & 1 deletion x/derivatives/keeper/perpetual_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import (
// "time"

// cdcTypes "github.com/cosmos/cosmos-sdk/codec/types"
ununifiTypes "github.com/UnUniFi/chain/types"
sdk "github.com/cosmos/cosmos-sdk/types"

ununifiTypes "github.com/UnUniFi/chain/types"

"github.com/UnUniFi/chain/x/derivatives/types"
)

func (k Keeper) OpenPerpetualOptionsPosition(ctx sdk.Context, positionId string, sender ununifiTypes.StringAccAddress, margin sdk.Coin, market types.Market, positionInstance types.PerpetualOptionsPositionInstance) (*types.Position, error) {
// todo implement
return nil, nil
}

func (k Keeper) ClosePerpetualOptionsPosition(ctx sdk.Context, position types.Position, positionInstance types.PerpetualOptionsPositionInstance) error {
// todo implement
return nil
}
func (k Keeper) ReportLiquidationNeededPerpetualOptionsPosition(ctx sdk.Context, rewardRecipient ununifiTypes.StringAccAddress, position types.Position, positionInstance types.PerpetualOptionsPositionInstance) error {
// todo implement
return nil
}
33 changes: 29 additions & 4 deletions x/derivatives/keeper/positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"errors"
"strconv"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -17,6 +18,23 @@ func (k Keeper) GetLastPositionId(ctx sdk.Context) string {
return string(bz)
}

func (k Keeper) GetLastPosition(ctx sdk.Context) types.Position {
store := ctx.KVStore(k.storeKey)

position := types.Position{}

it := sdk.KVStoreReversePrefixIterator(store, []byte(types.KeyPrefixPosition))
defer it.Close()

for ; it.Valid(); it.Next() {
position := types.Position{}
k.cdc.Unmarshal(it.Value(), &position)
return position
}

return position
}

func (k Keeper) IncreaseLastPositionId(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)

Expand Down Expand Up @@ -105,11 +123,18 @@ func (k Keeper) DeletePosition(ctx sdk.Context, address sdk.AccAddress, id strin
}

func (k Keeper) OpenPosition(ctx sdk.Context, msg *types.MsgOpenPosition) error {
sender := msg.Sender.AccAddress()
lastPositionId := k.GetLastPositionId(ctx)
// todo check sender amount for margin

lastPosition := k.GetLastPosition(ctx)

positionKey := types.AddressPositionWithIdKeyPrefix(sender, lastPositionId)
positionId := string(positionKey)
var positionId string
if lastPosition.Id == "" {
positionId = "0"
} else {
// increment position id
lastPositionId, _ := strconv.Atoi(lastPosition.Id)
positionId = strconv.Itoa(lastPositionId + 1)
}

if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, msg.Sender.AccAddress(), types.ModuleName, sdk.NewCoins(msg.Margin)); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions x/derivatives/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ func (msg MsgOpenPosition) GetSigners() []sdk.AccAddress {
// ensure Msg interface compliance at compile time
var _ sdk.Msg = &MsgClosePosition{}

func NewMsgClosePosition(sender ununifiTypes.StringAccAddress, positionId string) MsgClosePosition {
func NewMsgClosePosition(sender sdk.AccAddress, positionId string) MsgClosePosition {
return MsgClosePosition{
Sender: sender,
Sender: sender.Bytes(),
PositionId: positionId,
}
}
Expand Down

0 comments on commit a22fb11

Please sign in to comment.