Skip to content

Commit

Permalink
Merge pull request #35 from schnetzlerjoe/main
Browse files Browse the repository at this point in the history
Finalize for v0.1.0 officially
  • Loading branch information
schnetzlerjoe committed Nov 3, 2022
2 parents 98d44cf + 818dda5 commit 05390d0
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 32 deletions.
9 changes: 5 additions & 4 deletions network/hermes/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ address_type = { derivation = 'cosmos' }
[chains.packet_filter]
policy = 'allow'
list = [
['transfer', 'channel-1386'],
['icahost', 'channel-1387'],
['icahost', 'channel-1388'],
['icahost', 'channel-1389'],
['transfer', 'channel-1402'],
['icahost', 'channel-1403'],
['icahost', 'channel-1404'],
['icahost', 'channel-1405'],
['icahost', 'channel-1406'],
]
8 changes: 4 additions & 4 deletions network/ts-relayer/.ibc-setup/last-queried-heights.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"packetHeightA": 1283,
"packetHeightB": 7505811,
"ackHeightA": 1322,
"ackHeightB": 7505820
"packetHeightA": 837,
"packetHeightB": 7519078,
"ackHeightA": 886,
"ackHeightB": 7519089
}
2 changes: 1 addition & 1 deletion tests/etf/redeem-shares.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

defundd tx etf redeem test "2000000etf/test" "{osmosisAddress: 'osmo1m9l358xunhhwds0568za49mzhvuxx9uxtz8m2l'}" --from demowallet1 --keyring-backend test --home ./network/data/defund --gas auto -y
defundd tx etf redeem test '2000000etf/test' '{"osmosisAddress": "osmo1m9l358xunhhwds0568za49mzhvuxx9uxtz8m2l"}' --from demowallet1 --keyring-backend test --home ./network/data/defund --gas auto -y
31 changes: 29 additions & 2 deletions x/etf/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package etf
import (
"errors"
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -95,6 +96,16 @@ func (im IBCModule) OnChanCloseInit(
portID,
channelID string,
) error {
// on a ICA channel close we must ensure the fund rebalance is set to false
addr := strings.Split(portID, "icacontroller-")[1]
if len(addr) > 0 {
fund, err := im.keeper.GetFundByDefundAddr(ctx, addr)
if err != nil {
return err
}
fund.Rebalancing = false
im.keeper.SetFund(ctx, fund)
}
return nil
}

Expand Down Expand Up @@ -150,16 +161,32 @@ func (im IBCModule) OnTimeoutPacket(
// get the redeem from the store. If not found return nil and do not run logic if found run the redeem timeout logic
redeem, redeemExists := im.brokerKeeper.GetRedeem(ctx, id)
if redeemExists {
// on a timeout, ICA channel closes so automatically set the fund as not autorebalancing
fund, _ := im.keeper.GetFund(ctx, redeem.Fund)
fund.Rebalancing = false
im.keeper.SetFund(ctx, fund)

im.keeper.Logger(ctx).Error("redeem %s timed out. Running the redeem timeout logic.", id)
im.etfKeeper.OnRedeemFailure(ctx, packet, redeem)
err := im.etfKeeper.OnRedeemFailure(ctx, packet, redeem)
if err != nil {
im.keeper.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRedeemFailure", "error", err.Error())
}
} else {
im.keeper.Logger(ctx).Debug(fmt.Sprintf("Redeem %s not found. Skipping redeem timeout logic.", id))
}
// get the rebalance from the store. If not found return nil and do not run logic if found run the redeem timeout logic
rebalance, rebalanceExists := im.brokerKeeper.GetRebalance(ctx, id)
if rebalanceExists {
// on a timeout, ICA channel closes so automatically set the fund as not autorebalancing
fund, _ := im.keeper.GetFund(ctx, redeem.Fund)
fund.Rebalancing = false
im.keeper.SetFund(ctx, fund)

im.keeper.Logger(ctx).Error(fmt.Sprintf("rebalance %s timed out. Running the rebalance timeout logic.", id))
im.etfKeeper.OnRebalanceFailure(ctx, rebalance, rebalance.Fund)
err := im.etfKeeper.OnRebalanceFailure(ctx, rebalance, rebalance.Fund)
if err != nil {
im.keeper.Logger(ctx).Error("Error occured during run of ICA callback.", "callback", "OnRebalanceFailure", "error", err.Error())
}
} else {
im.keeper.Logger(ctx).Debug(fmt.Sprintf("Rebalance %s not found. Skipping rebalance timeout logic.", id))
}
Expand Down
17 changes: 17 additions & 0 deletions x/etf/keeper/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ func (k Keeper) GetFundBySymbol(ctx sdk.Context, symbol string) (types.Fund, err
return types.Fund{}, sdkerrors.Wrapf(types.ErrFundNotFound, "fund with the symbol %s does not exist", symbol)
}

// GetFundByDefundAddr returns a fund by the funds defund address
func (k Keeper) GetFundByDefundAddr(ctx sdk.Context, address string) (types.Fund, error) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.FundKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.Fund
k.cdc.MustUnmarshal(iterator.Value(), &val)
if val.Address == address {
return val, nil
}
}
return types.Fund{}, sdkerrors.Wrapf(types.ErrFundNotFound, "fund with the address %s does not exist", address)
}

// GetNextID gets the count of all funds and then adds 1 for the next fund id
func (k Keeper) GetNextID(ctx sdk.Context) (id string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.FundKeyPrefix))
Expand Down
24 changes: 8 additions & 16 deletions x/etf/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,37 +280,29 @@ func (k Keeper) RedeemShares(ctx sdk.Context, creator string, fund types.Fund, a
if err != nil {
ctx.Logger().Debug(err.Error())
}
addr, found := k.icaControllerKeeper.GetInterchainAccountAddress(ctx, broker.ConnectionId, portID)
addr, found := k.GetBrokerAccount(ctx, broker.ConnectionId, portID)
if !found {
err := status.Errorf(codes.NotFound, "no account found for portID %s on connection %s", portID, broker.ConnectionId)
return err
}
fundICAAddress, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return err
}

receiveAddress, err := AccAddressFromBech32Osmo(addressMap.OsmosisAddress)
if err != nil {
return err
}

msg := banktypes.NewMsgSend(fundICAAddress, receiveAddress, sdk.NewCoins(currentCoin))
if err != nil {
return err
msg := banktypes.MsgSend{
FromAddress: addr,
ToAddress: addressMap.OsmosisAddress,
Amount: sdk.NewCoins(currentCoin),
}

msgs[holding.BrokerId] = append(msgs[holding.BrokerId], msg)
msgs[holding.BrokerId] = append(msgs[holding.BrokerId], &msg)
}

// take the fund etf shares and escrow them in the module account. in the ack callback, on success
// of sequence we will burn proportionally. If unsuccessful the transfer is reattempted until successful.
// of sequence we will burn proportionally. If unsuccessful the escrow etf shares are returned.
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, creatorAcc, types.ModuleName, sdk.NewCoins(amount))
if err != nil {
return err
}

// send each ICA message and add it to the redeem which will be used in end blocker
// send each ICA message and add it to the redeem which will be used in callbacks
// to check status of ICA message
for brokerId, msg := range msgs {
// get the broker
Expand Down
5 changes: 3 additions & 2 deletions x/etf/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"testing"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -553,7 +554,7 @@ func (s *KeeperTestSuite) TestETFFundActions() {
s.Assert().True(found)
tokenInRedeem := sdk.NewCoin(fund.Shares.Denom, sdk.NewInt(89753))
addrMap := types.AddressMap{
OsmosisAddress: s.chainA.SenderAccount.GetAddress().String(),
OsmosisAddress: "osmo" + strings.Split(s.chainA.SenderAccount.GetAddress().String(), "defund")[1],
}

// mint and then send the etf coins to the redeemer
Expand All @@ -562,7 +563,7 @@ func (s *KeeperTestSuite) TestETFFundActions() {
s.GetDefundApp(s.chainA).BankKeeper.SendCoinsFromModuleToAccount(s.chainA.GetContext(), "etf", s.chainA.SenderAccounts[1].SenderAccount.GetAddress(), sdk.NewCoins(tokenInRedeem))

// redeem the created shares from above
err = s.GetDefundApp(s.chainA).EtfKeeper.RedeemShares(s.chainA.GetContext(), s.chainA.SenderAccounts[1].SenderAccount.GetAddress().String(), fund, tokenInRedeem, addrMap)
err = s.GetDefundApp(s.chainA).EtfKeeper.RedeemShares(s.chainA.GetContext(), "osmo"+strings.Split(s.chainA.SenderAccounts[1].SenderAccount.GetAddress().String(), "defund")[1], fund, tokenInRedeem, addrMap)
s.Assert().NoError(err)
})

Expand Down
5 changes: 2 additions & 3 deletions x/etf/keeper/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (k Keeper) CreateMultiSendMsg(ctx sdk.Context, fromAddress string, toAddres
func (k Keeper) SendIBCSend(ctx sdk.Context, msgs []*banktypes.MsgSend, owner string, connectionID string) (sequence uint64, channel string, err error) {
seralizeMsgs := []sdk.Msg{}
for _, msg := range msgs {
msg.ValidateBasic()
seralizeMsgs = append(seralizeMsgs, msg)
}

Expand Down Expand Up @@ -88,7 +87,7 @@ func (k Keeper) SendIBCSend(ctx sdk.Context, msgs []*banktypes.MsgSend, owner st

// 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(time.Minute).UnixNano())
timeoutTimestamp := uint64(ctx.BlockTime().Add(time.Minute).UnixNano())
sequence, err = k.icaControllerKeeper.SendTx(ctx, chanCap, connectionID, portID, packetData, uint64(timeoutTimestamp))
if err != nil {
return sequence, channel, err
Expand Down Expand Up @@ -132,7 +131,7 @@ func (k Keeper) SendIBCTransferICA(ctx sdk.Context, msgs []*ibctransfertypes.Msg

// 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(time.Minute).UnixNano())
timeoutTimestamp := uint64(ctx.BlockTime().Add(time.Minute).UnixNano())
sequence, err = k.icaControllerKeeper.SendTx(ctx, chanCap, connectionID, portID, packetData, uint64(timeoutTimestamp))
if err != nil {
return sequence, "", err
Expand Down
8 changes: 8 additions & 0 deletions x/etf/types/transfer.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package types

import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

type RedeemMsgs struct {
Osmosis []*banktypes.MsgSend
}

const (
TransferStateTransferring = "tranferring"
TransferStateFailed = "failed"
Expand Down

0 comments on commit 05390d0

Please sign in to comment.