Skip to content

Commit

Permalink
Autopilot JSON Memo (#689)
Browse files Browse the repository at this point in the history
Co-authored-by: shellvish <104537253+shellvish@users.noreply.github.com>
  • Loading branch information
sampocs and shellvish committed Mar 25, 2023
1 parent 31c5dc4 commit d16d7f2
Show file tree
Hide file tree
Showing 24 changed files with 1,225 additions and 179 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Expand Up @@ -544,7 +544,8 @@ func NewStrideApp(
appCodec,
keys[autopilottypes.StoreKey],
app.GetSubspace(autopilottypes.ModuleName),
app.StakeibcKeeper)
app.StakeibcKeeper,
app.ClaimKeeper)
autopilotModule := autopilot.NewAppModule(appCodec, app.AutopilotKeeper)

// Register Gov (must be registerd after stakeibc)
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v8/upgrades_test.go
Expand Up @@ -133,7 +133,8 @@ func (s *UpgradeTestSuite) CheckStoreAfterUpgrade() {

// Check autopilot params
expectedAutoPilotParams := autopilottypes.Params{
Active: false,
StakeibcActive: false,
ClaimActive: true,
}
actualAutopilotParams := s.App.AutopilotKeeper.GetParams(s.Ctx)
s.Require().Equal(expectedAutoPilotParams, actualAutopilotParams, "autopilot params")
Expand Down
20 changes: 16 additions & 4 deletions dockernet/tests/integration_tests.bats
Expand Up @@ -100,8 +100,8 @@ setup_file() {
hval_token_balance_start=$($HOST_MAIN_CMD q bank balances $HOST_VAL_ADDRESS --denom $HOST_DENOM | GETBAL)

# do IBC transfer
$STRIDE_MAIN_CMD tx ibc-transfer transfer transfer $STRIDE_TRANFER_CHANNEL $HOST_VAL_ADDRESS ${TRANSFER_AMOUNT}${STRIDE_DENOM} --from $STRIDE_VAL -y &
$HOST_MAIN_CMD tx ibc-transfer transfer transfer $HOST_TRANSFER_CHANNEL $(STRIDE_ADDRESS) ${TRANSFER_AMOUNT}${HOST_DENOM} --from $HOST_VAL -y &
$STRIDE_MAIN_CMD tx ibc-transfer transfer transfer $STRIDE_TRANFER_CHANNEL $HOST_VAL_ADDRESS ${TRANSFER_AMOUNT}${STRIDE_DENOM} --from $STRIDE_VAL -y
$HOST_MAIN_CMD tx ibc-transfer transfer transfer $HOST_TRANSFER_CHANNEL $(STRIDE_ADDRESS) ${TRANSFER_AMOUNT}${HOST_DENOM} --from $HOST_VAL -y

WAIT_FOR_BLOCK $STRIDE_LOGS 8

Expand Down Expand Up @@ -160,11 +160,23 @@ setup_file() {

@test "[INTEGRATION-BASIC-$CHAIN_NAME] packet forwarding automatically liquid stakes" {
skip "DefaultActive set to false, skip test"
memo='{ "autopilot": { "receiver": "'"$(STRIDE_ADDRESS)"'", "stakeibc": { "stride_address": "'"$(STRIDE_ADDRESS)"'", "action": "LiquidStake" } } }'

# get initial balances
sttoken_balance_start=$($STRIDE_MAIN_CMD q bank balances $(STRIDE_ADDRESS) --denom st$HOST_DENOM | GETBAL)

# do IBC transfer
$HOST_MAIN_CMD tx ibc-transfer transfer transfer $HOST_TRANSFER_CHANNEL $(STRIDE_ADDRESS)'|stakeibc/LiquidStake' ${PACKET_FORWARD_STAKE_AMOUNT}${HOST_DENOM} --from $HOST_VAL -y &
# Send the IBC transfer with the JSON memo
transfer_msg_prefix="$HOST_MAIN_CMD tx ibc-transfer transfer transfer $HOST_TRANSFER_CHANNEL"
if [[ "$CHAIN_NAME" == "GAIA" ]]; then
# For GAIA (ibc-v3), pass the memo into the receiver field
$transfer_msg_prefix "$memo" ${PACKET_FORWARD_STAKE_AMOUNT}${HOST_DENOM} --from $HOST_VAL -y
elif [[ "$CHAIN_NAME" == "HOST" ]]; then
# For HOST (ibc-v5), pass an address for a receiver and the memo in the --memo field
$transfer_msg_prefix $(STRIDE_ADDRESS) ${PACKET_FORWARD_STAKE_AMOUNT}${HOST_DENOM} --memo "$memo" --from $HOST_VAL -y
else
# For all other hosts, skip this test
skip "Packet forward liquid stake test is only run on GAIA and HOST"
fi

# Wait for the transfer to complete
WAIT_FOR_BALANCE_CHANGE STRIDE $(STRIDE_ADDRESS) st$HOST_DENOM
Expand Down
5 changes: 3 additions & 2 deletions proto/stride/autopilot/params.proto
Expand Up @@ -9,6 +9,7 @@ option go_package = "github.com/Stride-Labs/stride/v8/x/autopilot/types";
// next id: 1
message Params {
option (gogoproto.goproto_stringer) = false;
// optionally, turn off this module
bool active = 1;
// optionally, turn off each module
bool stakeibc_active = 1;
bool claim_active = 2;
}
42 changes: 41 additions & 1 deletion x/autopilot/README.md
Expand Up @@ -14,10 +14,50 @@ With current implementation of Autopilot module, it supports:

Note: This will support more functions that can reduce number of users' operations.

## Memo
### Format
```json
{
"autopilot": {
"receiver": "strideXXX",
"{module_name}": { "{additiional_field}": "{value}" }
}
}
```

### Example (1-Click Liquid Stake)
```json
{
"autopilot": {
"receiver": "strideXXX",
"stakeibc": {
"stride_address": "strideXXX",
"action": "LiquidStake",
}
}
}
```
### Example (Update Airdrop Address)
```json
{
"autopilot": {
"receiver": "strideXXX",
"claim": {
"stride_address": "strideXXX",
"airdrop_id": "evmos",
}
}
}
```

### A Note on Parsing
Since older versions of IBC do not have a `Memo` field, they must pass the routing information in the `Receiver` attribute of the IBC packet. To make autopilot backwards compatible with all older IBC versions, the receiver address must be specified in the JSON string. Before passing the packet down the stack to the transfer module, the address in the JSON string will replace the `Receiver` field in the packet data, regardless of the IBC version.

## Params

```
Active (default bool = true)
StakeibcActive (default bool = false)
ClaimActive (default bool = false)
```

## Keeper functions
Expand Down
5 changes: 4 additions & 1 deletion x/autopilot/genesis_test.go
Expand Up @@ -12,7 +12,10 @@ import (

func TestGenesis(t *testing.T) {
expectedGenesisState := types.GenesisState{
Params: types.Params{Active: true},
Params: types.Params{
StakeibcActive: true,
ClaimActive: true,
},
}

s := apptesting.SetupSuitelessTestHelper()
Expand Down
40 changes: 40 additions & 0 deletions x/autopilot/keeper/airdrop.go
@@ -0,0 +1,40 @@
package keeper

import (
"errors"
"fmt"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"

"github.com/Stride-Labs/stride/v8/utils"
"github.com/Stride-Labs/stride/v8/x/autopilot/types"
)

func (k Keeper) TryUpdateAirdropClaim(
ctx sdk.Context,
data transfertypes.FungibleTokenPacketData,
packetMetadata types.ClaimPacketMetadata,
) error {
params := k.GetParams(ctx)
if !params.ClaimActive {
return errors.New("packet forwarding param is not active")
}

// grab relevant addresses
senderStrideAddress := utils.ConvertAddressToStrideAddress(data.Sender)
if senderStrideAddress == "" {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, fmt.Sprintf("invalid sender address (%s)", data.Sender))
}
newStrideAddress := packetMetadata.StrideAddress

// update the airdrop
airdropId := packetMetadata.AirdropId
k.Logger(ctx).Info(fmt.Sprintf("updating airdrop address %s (orig %s) to %s for airdrop %s",
senderStrideAddress, data.Sender, newStrideAddress, airdropId))

return k.claimKeeper.UpdateAirdropAddress(ctx, senderStrideAddress, newStrideAddress, airdropId)
}

0 comments on commit d16d7f2

Please sign in to comment.