-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into egor/maxborrow
- Loading branch information
Showing
16 changed files
with
318 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ibc | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types" | ||
"github.com/cosmos/ibc-go/v5/modules/core/exported" | ||
) | ||
|
||
// GetFundsFromPacket returns transfer amount and denom | ||
func GetFundsFromPacket(packet exported.PacketI) (sdkmath.Int, string, error) { | ||
var packetData transfertypes.FungibleTokenPacketData | ||
err := json.Unmarshal(packet.GetData(), &packetData) | ||
if err != nil { | ||
return sdkmath.Int{}, "", err | ||
} | ||
|
||
amount, ok := sdkmath.NewIntFromString(packetData.Amount) | ||
if !ok { | ||
return sdkmath.Int{}, "", sdkerrors.ErrInvalidRequest.Wrapf("invalid transfer amount %s", packetData.Amount) | ||
} | ||
|
||
return amount, GetLocalDenom(packetData.Denom), nil | ||
} | ||
|
||
// GetLocalDenom retruns ibc denom | ||
// Expected denoms in the following cases: | ||
// | ||
// send non-native: transfer/channel-0/denom -> ibc/xxx | ||
// send native: denom -> denom | ||
// recv (B)non-native: denom | ||
// recv (B)native: transfer/channel-0/denom | ||
func GetLocalDenom(denom string) string { | ||
if strings.HasPrefix(denom, "transfer/") { | ||
denomTrace := transfertypes.ParseDenomTrace(denom) | ||
return denomTrace.IBCDenom() | ||
} | ||
|
||
return denom | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ibc | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types" | ||
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types" | ||
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" | ||
"github.com/tendermint/tendermint/crypto" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestGetFundsFromPacket(t *testing.T) { | ||
denom := strings.Join([]string{ | ||
"transfer", | ||
"dest_chain", | ||
"quark", | ||
}, "/") | ||
|
||
amount := strconv.Itoa(1) | ||
data := ibctransfertypes.NewFungibleTokenPacketData( | ||
denom, | ||
amount, | ||
AddressFromString("a3"), | ||
AddressFromString("a4"), | ||
) | ||
|
||
packet := channeltypes.NewPacket( | ||
data.GetBytes(), | ||
uint64(1), | ||
"transfer", | ||
"channel-0", | ||
"transfer", | ||
"channel-0", | ||
clienttypes.NewHeight(0, 100), | ||
0, | ||
) | ||
|
||
famount, fdenom, err := GetFundsFromPacket(packet) | ||
|
||
assert.NilError(t, err) | ||
assert.Equal(t, denom, fdenom) | ||
assert.Equal(t, famount.String(), amount) | ||
} | ||
|
||
func TestGetLocalDenom(t *testing.T) { | ||
denom := strings.Join([]string{ | ||
"transfer", | ||
"dest_chain", | ||
"quark", | ||
}, "/") | ||
|
||
rdenom := GetLocalDenom(denom) | ||
assert.Equal(t, rdenom, denom) | ||
} | ||
|
||
func AddressFromString(address string) string { | ||
return sdk.AccAddress(crypto.AddressHash([]byte(address))).String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.