/
parse.go
97 lines (82 loc) · 3.81 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package keeper
import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
fxtypes "github.com/functionx/fx-core/v7/types"
"github.com/functionx/fx-core/v7/x/ibc/applications/transfer/types"
)
func parseIBCCoinDenom(packet channeltypes.Packet, packetDenom string) string {
// This is the prefix that would have been prefixed to the denomination
// on sender chain IF and only if the token originally came from the
// receiving chain.
//
// NOTE: We use SourcePort and SourceChannel here, because the counterparty
// chain would have prefixed with DestPort and DestChannel when originally
// receiving this coin as seen in the "sender chain is the source" condition.
var receiveDenom string
if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), packetDenom) {
// sender chain is not the source, unescrow tokens
// remove prefix added by sender chain
voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())
unprefixedDenom := packetDenom[len(voucherPrefix):]
// coin denomination used in sending from the escrow address
denom := unprefixedDenom
// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom)
if denomTrace.Path != "" {
denom = denomTrace.IBCDenom()
}
receiveDenom = denom
} else {
// sender chain is the source, mint vouchers
// since SendPacket did not prefix the denomination, we must prefix denomination here
sourcePrefix := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel())
// NOTE: sourcePrefix contains the trailing "/"
prefixedDenom := sourcePrefix + packetDenom
// construct the denomination trace from the full raw denomination
denomTrace := transfertypes.ParseDenomTrace(prefixedDenom)
receiveDenom = denomTrace.IBCDenom()
}
return receiveDenom
}
func parseReceiveAndAmountByPacket(data types.FungibleTokenPacketData) (sdk.AccAddress, bool, sdkmath.Int, sdkmath.Int, error) {
// parse the transfer amount
transferAmount, ok := sdkmath.NewIntFromString(data.Amount)
if !ok {
return nil, false, sdkmath.Int{}, sdkmath.Int{}, errorsmod.Wrapf(transfertypes.ErrInvalidAmount, "unable to parse transfer amount (%s) into sdkmath.Int", data.Amount)
}
if data.Router != "" {
addressBytes, _, err := fxtypes.ParseAddress(data.Sender)
if err != nil {
return nil, false, sdkmath.Int{}, sdkmath.Int{}, err
}
feeAmount, ok := sdkmath.NewIntFromString(data.Fee)
if !ok || feeAmount.IsNegative() {
return nil, false, sdkmath.Int{}, sdkmath.Int{}, errorsmod.Wrapf(transfertypes.ErrInvalidAmount, "fee amount is invalid:%s", data.Fee)
}
return addressBytes, false, transferAmount, feeAmount, nil
}
// decode the receiver address
receiverAddr, isEvmAddr, err := fxtypes.ParseAddress(data.Receiver)
return receiverAddr, isEvmAddr, transferAmount, sdkmath.ZeroInt(), err
}
func parseAmountAndFeeByPacket(data types.FungibleTokenPacketData) (sdkmath.Int, sdkmath.Int, error) {
// parse the transfer amount
transferAmount, ok := sdkmath.NewIntFromString(data.Amount)
if !ok {
return sdkmath.Int{}, sdkmath.Int{}, errorsmod.Wrapf(transfertypes.ErrInvalidAmount, "unable to parse transfer amount (%s) into sdkmath.Int", data.Amount)
}
feeAmount := sdkmath.ZeroInt()
if data.Router != "" {
fee, ok := sdkmath.NewIntFromString(data.Fee)
if !ok || fee.IsNegative() {
return sdkmath.Int{}, sdkmath.Int{}, errorsmod.Wrapf(transfertypes.ErrInvalidAmount, "fee amount is invalid:%s", data.Fee)
}
feeAmount = fee
}
return transferAmount, feeAmount, nil
}