Skip to content

Commit

Permalink
Merge pull request #34 from schnetzlerjoe/main
Browse files Browse the repository at this point in the history
Finalize stage 2 details
  • Loading branch information
schnetzlerjoe authored Nov 3, 2022
2 parents 18b3fe1 + 65655a5 commit 98d44cf
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 118 deletions.
2 changes: 1 addition & 1 deletion cmd/defundd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func initRootCmd(
options rootOptions,
) {
rootCmd.AddCommand(
genutilcli.InitCmd(moduleBasics, defaultNodeHome),
InitCmd(moduleBasics, defaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, defaultNodeHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.GenTxCmd(
Expand Down
8 changes: 4 additions & 4 deletions network/hermes/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ address_type = { derivation = 'cosmos' }
[chains.packet_filter]
policy = 'allow'
list = [
['transfer', 'channel-1367'],
['icahost', 'channel-1368'],
['icahost', 'channel-1369'],
['icahost', 'channel-1370'],
['transfer', 'channel-1386'],
['icahost', 'channel-1387'],
['icahost', 'channel-1388'],
['icahost', 'channel-1389'],
]
2 changes: 1 addition & 1 deletion network/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ CHAINID_1=defund

echo "Starting $CHAINID_1 in $CHAIN_DIR..."
echo "Creating log file at $CHAIN_DIR/$CHAINID_1.log"
$BINARY start --log_level info --log_format json --home $CHAIN_DIR/$CHAINID_1 --pruning=nothing > $CHAIN_DIR/$CHAINID_1.log 2>&1 &
$BINARY start --log_level debug --log_format json --home $CHAIN_DIR/$CHAINID_1 --pruning=nothing > $CHAIN_DIR/$CHAINID_1.log 2>&1 &
2 changes: 0 additions & 2 deletions network/ts-relayer/.ibc-setup/app.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
src: defund
dest: osmosis
mnemonic: alley afraid soup fall idea toss can goose become valve initial strong forward bright dish figure check leopard decide warfare hub unusual join cart
srcConnection: connection-0
destConnection: connection-2407
6 changes: 6 additions & 0 deletions network/ts-relayer/.ibc-setup/last-queried-heights.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"packetHeightA": 1283,
"packetHeightB": 7505811,
"ackHeightA": 1322,
"ackHeightB": 7505820
}
4 changes: 2 additions & 2 deletions network/ts-relayer/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MNEMONIC="alley afraid soup fall idea toss can goose become valve initial strong

# create connections
echo "Creating connections..."
ibc-setup connect --interactive --home $RELAY_DIR/.ibc-setup --log-level debug
ibc-setup connect --home $RELAY_DIR/.ibc-setup --log-level debug

echo "What is the source connection to relay?"
read srcconnection
Expand All @@ -19,4 +19,4 @@ read destconnection

# start up relayer polling every 6 seconds
echo "Starting Defund ts-relayer for src:$srcconnection dest:$destconnection..."
ibc-relayer start --home $RELAY_DIR/.ibc-setup --src-connection $srcconnection --dest-connection $destconnection --poll 6 --log-level debug --interactive > ./relayer.log 2>&1 &
ibc-relayer start --home $RELAY_DIR/.ibc-setup --src-connection $srcconnection --dest-connection $destconnection --poll 6 --log-level debug > ./relayer.log 2>&1 &
8 changes: 2 additions & 6 deletions x/broker/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import (
)

// Create a list of all pool ids supported on osmosis at init/genesis
// Pool 678 is USCD/OSMO so base denom pool option.
var poolsOsmosis = []uint64{
1, 497, 674, 604, 9, 498, 584, 3, 10, 601, 2, 611, 585, 13, 4, 482, 481, 6, 577, 5, 463,
629, 641, 15, 461, 560, 586, 587, 42, 600, 627, 608, 571, 631, 548, 7, 605, 572, 648,
606, 643, 8, 597, 619, 553, 625, 602, 618, 574, 578, 651, 626, 573, 22, 555, 637, 464,
645, 644, 596, 547, 616, 558, 621, 613, 197, 617, 670, 612, 638, 561, 567, 649, 653,
633, 557, 662, 615, 565, 562, 592, 151, 183, 673, 549, 624, 642, 678,
1, 2, 560, 604, 561, 497, 584, 3, 9, 601, 605, 627, 481, 5, 629, 15, 619, 608, 7, 463, 571, 42, 586,
577, 621, 573, 553, 602, 197, 597, 651, 645, 557, 626, 631, 613, 647, 624, 637, 11, 641,
}

// AddOsmosisToBrokers adds Osmosis as a broker to state manually
Expand Down
12 changes: 2 additions & 10 deletions x/etf/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,8 @@ func (im IBCModule) OnAcknowledgementPacket(
if err := proto.Unmarshal(ack.GetResult(), txMsgData); err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal Broker tx message data: %v", err)
}
// if the length of the msg data is 0 skip/return, otherwise run through logic
switch len(txMsgData.Data) {
case 0:
return nil
default:
err := im.keeper.OnAcknowledgementPacketICA(ctx, packet, ack, txMsgData)
if err != nil {
return err
}
}
im.keeper.OnAcknowledgementPacketICA(ctx, packet, ack, txMsgData)

return nil
}

Expand Down
124 changes: 47 additions & 77 deletions x/etf/keeper/ica.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
proto "github.com/gogo/protobuf/proto"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
connectiontypes "github.com/cosmos/ibc-go/v4/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"

transfertypes "github.com/cosmos/ibc-go/v4/modules/apps/transfer/types"
osmosisgammtypes "github.com/osmosis-labs/osmosis/v8/x/gamm/types"
)

Expand Down Expand Up @@ -55,7 +52,7 @@ func (k Keeper) GetChannel(ctx sdk.Context, portID string, channelID string) (ch
//
// If the ICA message is an ICA Swap Message, we know it is a rebalance workflow, and we mark the rebalance
// from pending to complete.
func (k Keeper) OnAcknowledgementPacketSuccess(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) error {
func (k Keeper) OnAcknowledgementPacketSuccess(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) {
// loop through each ICA msg in the tx (one ack respresents one tx)
for _, msgData := range txMsgData.Data {
switch msgData.MsgType {
Expand All @@ -64,102 +61,75 @@ func (k Keeper) OnAcknowledgementPacketSuccess(ctx sdk.Context, packet channelty
redeem, found := k.brokerKeeper.GetRedeem(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Redeem %s not found. Skipping redeem success logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
return nil
return
}
msgResponse := &transfertypes.MsgTransferResponse{}
if err := proto.Unmarshal(msgData.Data, msgResponse); err != nil {
err = sdkerrors.Wrapf(sdkerrors.ErrJSONUnmarshal, "cannot unmarshal ica transfer response message: %s", err.Error())
k.Logger(ctx).Error("Failed running redeem success logic during msgResponse unmarshalling. ===>>> Error: %s", err.Error())
}
k.Logger(ctx).Info("Redeem shares ICA transfer msg ran successfully. Running redeem success logic.", "response", msgResponse.String())
k.Logger(ctx).Info("Redeem shares ICA transfer msg ran successfully. Running redeem success logic.")
// Run redeem success logic
k.OnRedeemSuccess(ctx, packet, redeem)
err := k.OnRedeemSuccess(ctx, packet, redeem)
if err != nil {
k.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRedeemSuccess", "error", err.Error())
}

return nil
return
case sdk.MsgTypeURL(&osmosisgammtypes.MsgSwapExactAmountIn{}):
// get the rebalance from the store. If not found return nil and do not run logic
rebalance, found := k.brokerKeeper.GetRebalance(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Fund rebalance %s not found. Skipping rebalance success logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
return nil
return
}
msgResponse := &osmosisgammtypes.MsgSwapExactAmountInResponse{}
if err := proto.Unmarshal(msgData.Data, msgResponse); err != nil {
err = sdkerrors.Wrapf(sdkerrors.ErrJSONUnmarshal, "cannot unmarshal Osmosis swap in response message: %s", err.Error())
k.Logger(ctx).Error("Failed running rebalance success logic during msgResponse unmarshalling. ===>>> Error: %s", err.Error())
}
k.Logger(ctx).Info("Fund rebalance ICA msg ran successfully. Running rebalance success logic.", "response", msgResponse.String())
k.Logger(ctx).Info("Fund rebalance ICA msg ran successfully. Running rebalance success logic.")
// Run rebalance success logic
k.OnRebalanceSuccess(ctx, rebalance, rebalance.Fund)
err := k.OnRebalanceSuccess(ctx, rebalance, rebalance.Fund)
if err != nil {
k.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRebalanceSuccess", "error", err.Error())
}

return nil
return
default:
return nil
k.Logger(ctx).Error("Received ICA failure msg type we do not recognize.", "typeUrl", msgData.MsgType)

return
}
}
return nil
}

func (k Keeper) OnAcknowledgementPacketFailure(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) error {
// loop through each ICA msg in the tx (one ack respresents one tx)
for _, msgData := range txMsgData.Data {
switch msgData.MsgType {
case sdk.MsgTypeURL(&banktypes.MsgSend{}):
// get the redeem from the store. If not found return nil and do not run logic
redeem, found := k.brokerKeeper.GetRedeem(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Redeem %s not found. Skipping redeem failure logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
return nil
}
msgResponse := &transfertypes.MsgTransferResponse{}
if err := proto.Unmarshal(msgData.Data, msgResponse); err != nil {
err = sdkerrors.Wrapf(sdkerrors.ErrJSONUnmarshal, "cannot unmarshal ica transfer response message: %s", err.Error())
k.Logger(ctx).Error("Failed running redeem failure logic during msgResponse unmarshalling. ===>>> Error: %s", err.Error())
}
k.Logger(ctx).Error("Redeem shares ICA transfer msg ran unsuccessfully. Running redeem failure logic.", "response: ", msgResponse.String())

// Run redeem failure logic
k.OnRedeemFailure(ctx, packet, redeem)

return nil
case sdk.MsgTypeURL(&osmosisgammtypes.MsgSwapExactAmountIn{}):
// get the rebalance from the store. If not found return nil and do not run logic
rebalance, found := k.brokerKeeper.GetRebalance(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Error(fmt.Sprintf("Fund rebalance %s not found. Skipping rebalance failure logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
return nil
}
fund := rebalance.Fund
switch rebalance.Broker {
case "osmosis":
msgResponse := &osmosisgammtypes.MsgSwapExactAmountInResponse{}
if err := proto.Unmarshal(msgData.Data, msgResponse); err != nil {
err = sdkerrors.Wrapf(sdkerrors.ErrJSONUnmarshal, "cannot unmarshal Osmosis swap in response message: %s", err.Error())
k.Logger(ctx).Error("Failed running rebalance failure logic during msgResponse unmarshalling. ===>>> Error: %s", err.Error())
}
k.Logger(ctx).Error("Fund rebalance ICA msg ran unsuccessfully. Running rebalance failure logic.", "response: ", msgResponse.String())
}

// Run rebalance failure logic
k.OnRebalanceFailure(ctx, rebalance, fund)

return nil
default:
return nil
func (k Keeper) OnAcknowledgementPacketFailure(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) {
// get the redeem from the store. If not found skip redeem logic
redeem, found := k.brokerKeeper.GetRedeem(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Debug(fmt.Sprintf("Redeem %s not found. Skipping redeem failure logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
} else {
k.Logger(ctx).Error("Redeem shares ICA transfer msg ran unsuccessfully. Running redeem failure logic.")
// Run redeem failure logic
err := k.OnRedeemFailure(ctx, packet, redeem)
if err != nil {
k.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRedeemFailure", "error", err.Error())
}
}
// get the rebalance from the store. If not found skip rebalance logic
rebalance, found := k.brokerKeeper.GetRebalance(ctx, fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence))
if !found {
k.Logger(ctx).Debug(fmt.Sprintf("Fund rebalance %s not found. Skipping rebalance failure logic.", fmt.Sprintf("%s-%d", packet.SourceChannel, packet.Sequence)))
} else {
k.Logger(ctx).Error("Fund rebalance ICA msg ran unsuccessfully. Running rebalance failure logic.")
// Run rebalance failure logic
err := k.OnRebalanceFailure(ctx, rebalance, rebalance.Fund)
if err != nil {
k.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRebalanceFailure", "error", err.Error())
}
}
return nil
}

func (k Keeper) OnAcknowledgementPacketICA(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) error {
func (k Keeper) OnAcknowledgementPacketICA(ctx sdk.Context, packet channeltypes.Packet, ack channeltypes.Acknowledgement, txMsgData *sdk.TxMsgData) {
switch ack.Response.(type) {
// on successful ack
case *channeltypes.Acknowledgement_Result:
return k.OnAcknowledgementPacketSuccess(ctx, packet, ack, txMsgData)
// on failure ack
case *channeltypes.Acknowledgement_Error:
return k.OnAcknowledgementPacketFailure(ctx, packet, ack, txMsgData)
ctx.Logger().Info("received successful ICA acknowledgement. running ICA successful acknowledgement logic.", "channel", packet.SourceChannel, "sequence", packet.Sequence, "ack", ack.GetResponse())
k.OnAcknowledgementPacketSuccess(ctx, packet, ack, txMsgData)
// on failure ack. defaults to failure in switch statement
default:
return nil
ctx.Logger().Info("received error ICA acknowledgement. running ICA failure acknowledgement logic.", "channel", packet.SourceChannel, "sequence", packet.Sequence, "ack", ack.GetError())
k.OnAcknowledgementPacketFailure(ctx, packet, ack, txMsgData)
}
}
26 changes: 13 additions & 13 deletions x/etf/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (k Keeper) RedeemShares(ctx sdk.Context, creator string, fund types.Fund, a
return err
}

receiveAddress, err := sdk.AccAddressFromBech32(addressMap.OsmosisAddress)
receiveAddress, err := AccAddressFromBech32Osmo(addressMap.OsmosisAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -540,10 +540,6 @@ func (k Keeper) CreateRebalanceMsgs(ctx sdk.Context, fund types.Fund) (types.Reb
if holding.Token == fund.BaseDenom.OnBroker {
continue
}
// if the token in amount or token out amount is negative continue
if !tokenIn.Amount.IsPositive() || !tokenOut.IsPositive() {
continue
}
// get the routes needed to swap for from this current denom to base denom
routes, err := k.getOsmosisRoutes(ctx, holding.Token, fund.BaseDenom.OnBroker)
if err != nil {
Expand All @@ -553,9 +549,13 @@ func (k Keeper) CreateRebalanceMsgs(ctx sdk.Context, fund types.Fund) (types.Reb
if err != nil {
return msgs, err
}
// if the token in amount or token out amount is negative skip
if !msg.TokenIn.Amount.IsPositive() || !msg.TokenOutMinAmount.IsPositive() {
continue
}
// append the new message for the broker
msgs.Osmosis = append(msgs.Osmosis, msg)
}
// append the new message for the broker
msgs.Osmosis = append(msgs.Osmosis, msg)
}
}

Expand Down Expand Up @@ -607,10 +607,6 @@ func (k Keeper) CreateRebalanceMsgs(ctx sdk.Context, fund types.Fund) (types.Reb
if holding.Token == fund.BaseDenom.OnBroker {
continue
}
// if the token in amount or token out amount is negative continue
if !tokenIn.Amount.IsPositive() || !tokenOut.IsPositive() {
continue
}
// get the routes needed to swap for from this current denom to base denom
routes, err := k.getOsmosisRoutes(ctx, fund.BaseDenom.OnBroker, holding.Token)
if err != nil {
Expand All @@ -620,9 +616,13 @@ func (k Keeper) CreateRebalanceMsgs(ctx sdk.Context, fund types.Fund) (types.Reb
if err != nil {
return msgs, err
}
// if the token in amount or token out amount is negative skip
if !msg.TokenIn.Amount.IsPositive() || !msg.TokenOutMinAmount.IsPositive() {
continue
}
// append the new message for the broker
msgs.Osmosis = append(msgs.Osmosis, msg)
}
// append the new message for the broker
msgs.Osmosis = append(msgs.Osmosis, msg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/etf/keeper/osmosis.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (k Keeper) SendOsmosisTrades(ctx sdk.Context, msgs []*osmosisgammtypes.MsgS

// timeoutTimestamp set to max value with the unsigned bit shifted to sastisfy hermes timestamp conversion
// it is the responsibility of the auth module developer to ensure an appropriate timeout timestamp
timeoutTimestamp := uint64(time.Now().Add(1 * time.Minute).UnixNano())
timeoutTimestamp := uint64(ctx.BlockTime().Add(1 * time.Minute).UnixNano())
sequence, err = k.icaControllerKeeper.SendTx(ctx, chanCap, connectionID, portID, packetData, uint64(timeoutTimestamp))
if err != nil {
return channel, sequence, err
Expand Down
2 changes: 1 addition & 1 deletion x/query/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (k Keeper) CreateInterqueryRequest(ctx sdk.Context, chainid string, storeid
}
k.SetInterquery(ctx, interquery)

k.Logger(ctx).Info(fmt.Sprintf("Interquery request for path %s on connection %s has been initiated", path, connectionid))
k.Logger(ctx).Debug(fmt.Sprintf("Interquery request for path %s on connection %s has been initiated", path, connectionid))

return nil
}
Expand Down

0 comments on commit 98d44cf

Please sign in to comment.