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

feat(stride): Stride outpost types and events #1914

Merged
merged 8 commits into from
Oct 20, 2023
110 changes: 110 additions & 0 deletions precompiles/outposts/stride/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package stride

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm"
cmn "github.com/evmos/evmos/v15/precompiles/common"
)

const (
// EventLiquidStake is the event type emitted on a liquidStake transaction to Autopilot on Stride.
EventLiquidStake = "LiquidStake"
// EventRedeem is the event type emitted on a redeem transaction to Autopilot on Stride.
EventRedeem = "Redeem"
)

// EmitLiquidStakeEvent creates a new LiquidStake event on the EVM stateDB.
func (p Precompile) EmitLiquidStakeEvent(
ctx sdk.Context,
stateDB vm.StateDB,
sender,
token common.Address,
amount *big.Int,
) error {
// Prepare the event topics
event := p.ABI.Events[EventLiquidStake]
topics := make([]common.Hash, 3)

// The first topic is always the signature of the event.
topics[0] = event.ID

var err error
// sender and receiver are indexed
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
topics[1], err = cmn.MakeTopic(sender)
if err != nil {
return err
}

Check warning on line 44 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L31-L44

Added lines #L31 - L44 were not covered by tests

topics[2], err = cmn.MakeTopic(token)
if err != nil {
return err
}

Check warning on line 49 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L46-L49

Added lines #L46 - L49 were not covered by tests

// Prepare the event data: amount
arguments := abi.Arguments{event.Inputs[2]}
packed, err := arguments.Pack(amount)
if err != nil {
return err
}

Check warning on line 56 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L52-L56

Added lines #L52 - L56 were not covered by tests

stateDB.AddLog(&ethtypes.Log{
Address: p.Address(),
Topics: topics,
Data: packed,
BlockNumber: uint64(ctx.BlockHeight()),
Dismissed Show dismissed Hide dismissed
})

return nil

Check warning on line 65 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L58-L65

Added lines #L58 - L65 were not covered by tests
}

// EmitRedeemEvent creates a new Redeem event on the EVM stateDB.
func (p Precompile) EmitRedeemEvent(
ctx sdk.Context,
stateDB vm.StateDB,
sender,
token common.Address,
receiver string,
amount *big.Int,
) error {
// Prepare the event topics
event := p.ABI.Events[EventRedeem]
topics := make([]common.Hash, 3)

// The first topic is always the signature of the event.
topics[0] = event.ID

var err error
// sender and token are indexed
topics[1], err = cmn.MakeTopic(sender)
if err != nil {
return err
}
topics[2], err = cmn.MakeTopic(token)
if err != nil {
return err
}

Check warning on line 93 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L76-L93

Added lines #L76 - L93 were not covered by tests

// Prepare the event data: receiver, amount
arguments := abi.Arguments{event.Inputs[2], event.Inputs[3]}
packed, err := arguments.Pack(receiver, amount)
if err != nil {
return err
}

Check warning on line 100 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L96-L100

Added lines #L96 - L100 were not covered by tests

stateDB.AddLog(&ethtypes.Log{
Address: p.Address(),
Topics: topics,
Data: packed,
BlockNumber: uint64(ctx.BlockHeight()),
Dismissed Show dismissed Hide dismissed
})

return nil

Check warning on line 109 in precompiles/outposts/stride/events.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/events.go#L102-L109

Added lines #L102 - L109 were not covered by tests
}
116 changes: 116 additions & 0 deletions precompiles/outposts/stride/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)

package stride

import (
"encoding/json"
"fmt"
"math/big"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
cmn "github.com/evmos/evmos/v15/precompiles/common"
)

// StakeIBCPacketMetadata metadata info specific to StakeIBC (e.g. 1-click liquid staking)
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
type StakeIBCPacketMetadata struct {
Action string `json:"action"`
StrideAddress string
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
}

// Autopilot defines the receiver and IBC packet metadata info specific to the
// Stride Autopilot liquid staking behavior
type Autopilot struct {
Receiver string `json:"receiver"`
StakeIBC *StakeIBCPacketMetadata `json:"stakeibc,omitempty"`
}

// RawPacketMetadata is the raw packet metadata used to construct a JSON string
type RawPacketMetadata struct {
Autopilot *Autopilot `json:"autopilot"`
}

// parseLiquidStakeArgs parses the arguments from the Liquid Stake method call
//
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
//nolint:unused
func parseLiquidStakeArgs(args []interface{}) (common.Address, common.Address, *big.Int, string, error) {
if len(args) != 4 {
return common.Address{}, common.Address{}, nil, "", fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 4, len(args))
}

Check warning on line 42 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L39-L42

Added lines #L39 - L42 were not covered by tests

sender, ok := args[0].(common.Address)
if !ok {
return common.Address{}, common.Address{}, nil, "", fmt.Errorf(cmn.ErrInvalidType, "sender", "", args[0])
}

Check warning on line 47 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L44-L47

Added lines #L44 - L47 were not covered by tests

token, ok := args[1].(common.Address)
if !ok {
return common.Address{}, common.Address{}, nil, "", fmt.Errorf(cmn.ErrInvalidType, "token", "", args[1])
}

Check warning on line 52 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L49-L52

Added lines #L49 - L52 were not covered by tests

amount, ok := args[2].(*big.Int)
if !ok {
return common.Address{}, common.Address{}, nil, "", fmt.Errorf(cmn.ErrInvalidType, "amount", "", args[2])
}

Check warning on line 57 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L54-L57

Added lines #L54 - L57 were not covered by tests

receiver, ok := args[3].(string)
if !ok {
return common.Address{}, common.Address{}, nil, "", fmt.Errorf(cmn.ErrInvalidType, "receiver", "", args[3])
}

Check warning on line 62 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L59-L62

Added lines #L59 - L62 were not covered by tests

// Check if the receiver address has stride before
if receiver[:6] != "stride" {
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
return common.Address{}, common.Address{}, nil, "", fmt.Errorf("receiver is not a stride address")
}

Check warning on line 67 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L65-L67

Added lines #L65 - L67 were not covered by tests

// Check if account is a valid bech32 address
_, err := AccAddressFromBech32(receiver, "stride")
if err != nil {
return common.Address{}, common.Address{}, nil, "", sdkerrors.ErrInvalidAddress.Wrapf("invalid stride bech32 address: %s", err)
}

Check warning on line 73 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L70-L73

Added lines #L70 - L73 were not covered by tests

return sender, token, amount, receiver, nil

Check warning on line 75 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L75

Added line #L75 was not covered by tests
}

// AccAddressFromBech32 creates an AccAddress from a Bech32 string.
func AccAddressFromBech32(address string, bech32prefix string) (addr sdk.AccAddress, err error) {
Vvaradinov marked this conversation as resolved.
Show resolved Hide resolved
if len(strings.TrimSpace(address)) == 0 {
return sdk.AccAddress{}, fmt.Errorf("empty address string is not allowed")
}

Check warning on line 82 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L79-L82

Added lines #L79 - L82 were not covered by tests

bz, err := sdk.GetFromBech32(address, bech32prefix)
if err != nil {
return nil, err
}

Check warning on line 87 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L84-L87

Added lines #L84 - L87 were not covered by tests

err = sdk.VerifyAddressFormat(bz)
if err != nil {
return nil, err
}

Check warning on line 92 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L89-L92

Added lines #L89 - L92 were not covered by tests

return sdk.AccAddress(bz), nil

Check warning on line 94 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L94

Added line #L94 was not covered by tests
}

// CreateMemo creates the memo for the StakeIBC actions - LiquidStake and Redeem.
func CreateMemo(action, receiverAddress string) (string, error) {
// Create a new instance of the struct and populate it
data := &RawPacketMetadata{
Autopilot: &Autopilot{
Receiver: receiverAddress,
StakeIBC: &StakeIBCPacketMetadata{
Action: action,
},
},
}

// Convert the struct to a JSON string
jsonBytes, err := json.MarshalIndent(data, "", " ")
if err != nil {
return "", sdkerrors.ErrJSONMarshal.Wrap("autopilot packet")
}

Check warning on line 113 in precompiles/outposts/stride/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/stride/types.go#L112-L113

Added lines #L112 - L113 were not covered by tests

return string(jsonBytes), nil
}
44 changes: 44 additions & 0 deletions precompiles/outposts/stride/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package stride_test

import (
"testing"

strideoutpost "github.com/evmos/evmos/v15/precompiles/outposts/stride"
"github.com/stretchr/testify/require"
)

func TestCreateMemo(t *testing.T) {
t.Parallel()

testcases := []struct {
name string
action string
receiver string
expPass bool
errContains string
}{
{
name: "success - liquid stake",
action: strideoutpost.LiquidStakeAction,
receiver: "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5",
expPass: true,
},
}

for _, tc := range testcases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

memo, err := strideoutpost.CreateMemo(tc.action, tc.receiver)
if tc.expPass {
require.NoError(t, err, "expected no error while creating memo")
require.NotEmpty(t, memo, "expected memo not to be empty")
} else {
require.Error(t, err, "expected error while creating memo")
require.Contains(t, err.Error(), tc.errContains, "expected different error")
}
})
}
}