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

imp(osmosis-outpost): use wrapper structure for parsed Swap inputs #2025

Merged
merged 7 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (utils) [#2010](https://github.com/evmos/evmos/pull/2010) Add utils function to create ibc denom trace.
- (erc20) [#2012](https://github.com/evmos/evmos/pull/2012) Adjust ERC20 extension approvals to handle multiple denominations.
- (osmosis-outpost) [#2017](https://github.com/evmos/evmos/pull/2017) Refactor types, errors and precompile struct.
- (osmosis-outpost) [#2025](https://github.com/evmos/evmos/pull/2025) Use a struct to wrap parsed parameters from Solidity.

### Bug Fixes

Expand Down
27 changes: 22 additions & 5 deletions precompiles/outposts/osmosis/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ func (p Precompile) Swap(
method *abi.Method,
args []interface{},
) ([]byte, error) {
sender, input, output, amount, slippagePercentage, windowSeconds, swapReceiver, err := ParseSwapPacketData(args)
swapPacketData, err := ParseSwapPacketData(args)
if err != nil {
return nil, err
}

input := swapPacketData.Input
output := swapPacketData.Output
amount := swapPacketData.Amount
swapReceiver := swapPacketData.SwapReceiver

// The provided sender address should always be equal to the origin address.
// In case the contract caller address is the same as the sender address provided,
// update the sender address to be equal to the origin address.
// Otherwise, if the provided sender address is different from the origin address,
// return an error because is a forbidden operation
sender, err = ics20.CheckOriginAndSender(contract, origin, sender)
sender, err := ics20.CheckOriginAndSender(contract, origin, swapPacketData.Sender)
if err != nil {
return nil, err
}
Expand All @@ -76,7 +81,13 @@ func (p Precompile) Swap(
// in the Osmosis chain as a recovery address for the contract.
onFailedDelivery := CreateOnFailedDeliveryField(sender.String())
packet := CreatePacketWithMemo(
outputDenom, swapReceiver, XCSContract, slippagePercentage, windowSeconds, onFailedDelivery, NextMemo,
outputDenom,
swapPacketData.SwapReceiver,
XCSContract,
swapPacketData.SlippagePercentage,
swapPacketData.WindowSeconds,
onFailedDelivery,
NextMemo,
)

err = packet.Memo.Validate()
Expand All @@ -102,7 +113,13 @@ func (p Precompile) Swap(

// No need to have authorization when the contract caller is the same as
// origin (owner of funds) and the sender is the origin
accept, expiration, err := ics20.CheckAndAcceptAuthorizationIfNeeded(ctx, contract, origin, p.AuthzKeeper, msg)
accept, expiration, err := ics20.CheckAndAcceptAuthorizationIfNeeded(
ctx,
contract,
origin,
p.AuthzKeeper,
msg,
)
if err != nil {
return nil, err
}
Expand All @@ -122,7 +139,7 @@ func (p Precompile) Swap(
if err := ics20.EmitIBCTransferEvent(
ctx,
stateDB,
p.ABI.Events[ics20.EventTypeIBCTransfer],
p.Events[ics20.EventTypeIBCTransfer],
p.Address(),
sender,
msg.Receiver,
Expand Down
60 changes: 39 additions & 21 deletions precompiles/outposts/osmosis/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,54 +212,72 @@
return nil
}

// SwapPacketData is an utility structure used to wrap args reiceived by the
// Solidity interface of the Swap function.
0xstepit marked this conversation as resolved.
Show resolved Hide resolved
type SwapPacketData struct {
Sender common.Address
Input common.Address
Output common.Address
Amount *big.Int
SlippagePercentage uint8
WindowSeconds uint64
SwapReceiver string
}

// ParseSwapPacketData parses the packet data for the Osmosis swap function.
func ParseSwapPacketData(args []interface{}) (
sender, input, output common.Address,
amount *big.Int,
slippagePercentage uint8,
windowSeconds uint64,
receiver string,
swapPacketData SwapPacketData,
err error,
) {
if len(args) != 7 {
return common.Address{}, common.Address{}, common.Address{}, nil, 0, 0, "", fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 7, len(args))
return SwapPacketData{}, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 7, len(args))
}

var ok bool
0xstepit marked this conversation as resolved.
Show resolved Hide resolved
sender, ok = args[0].(common.Address)
sender, ok := args[0].(common.Address)
if !ok {
return common.Address{}, common.Address{}, common.Address{}, nil, 0, 0, "", fmt.Errorf(cmn.ErrInvalidType, "sender", common.Address{}, args[0])
return SwapPacketData{}, fmt.Errorf(cmn.ErrInvalidType, "sender", common.Address{}, args[0])
}

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

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

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

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

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

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

Check warning on line 270 in precompiles/outposts/osmosis/types.go

View check run for this annotation

Codecov / codecov/patch

precompiles/outposts/osmosis/types.go#L269-L270

Added lines #L269 - L270 were not covered by tests

swapPacketData = SwapPacketData{
Sender: sender,
Input: input,
Output: output,
Amount: amount,
SlippagePercentage: slippagePercentage,
WindowSeconds: windowSeconds,
SwapReceiver: receiver,
}

return sender, input, output, amount, slippagePercentage, windowSeconds, receiver, nil
return swapPacketData, nil
}
15 changes: 14 additions & 1 deletion precompiles/outposts/osmosis/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,23 @@ func TestParseSwapPacketData(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

_, _, _, _, _, _, _, err := osmosisoutpost.ParseSwapPacketData(tc.args)
swapPacketData, err := osmosisoutpost.ParseSwapPacketData(tc.args)

if tc.expPass {
require.NoError(t, err, "expected no error while creating memo")
require.Equal(
t,
osmosisoutpost.SwapPacketData{
Sender: testSender,
Input: testInput,
Output: testOutput,
Amount: testAmount,
SlippagePercentage: testSlippagePercentage,
WindowSeconds: testWindowSeconds,
SwapReceiver: testReceiver,
},
swapPacketData,
)
} else {
require.Error(t, err, "expected error while validating the memo")
require.Contains(t, err.Error(), tc.errContains, "expected different error")
Expand Down