-
Notifications
You must be signed in to change notification settings - Fork 580
/
coin.go
47 lines (38 loc) · 1.72 KB
/
coin.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
package types
import (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// SenderChainIsSource returns false if the denomination originally came
// from the receiving chain and true otherwise.
func SenderChainIsSource(sourcePort, sourceChannel, denom string) bool {
// 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.
return !ReceiverChainIsSource(sourcePort, sourceChannel, denom)
}
// ReceiverChainIsSource returns true if the denomination originally came
// from the receiving chain and false otherwise.
func ReceiverChainIsSource(sourcePort, sourceChannel, denom string) bool {
// The prefix passed in should contain the SourcePort and SourceChannel.
// If the receiver chain originally sent the token to the sender chain
// the denom will have the sender's SourcePort and SourceChannel as the
// prefix.
voucherPrefix := GetDenomPrefix(sourcePort, sourceChannel)
return strings.HasPrefix(denom, voucherPrefix)
}
// GetDenomPrefix returns the receiving denomination prefix
func GetDenomPrefix(portID, channelID string) string {
return fmt.Sprintf("%s/%s/", portID, channelID)
}
// GetPrefixedDenom returns the denomination with the portID and channelID prefixed
func GetPrefixedDenom(portID, channelID, baseDenom string) string {
return fmt.Sprintf("%s/%s/%s", portID, channelID, baseDenom)
}
// GetTransferCoin creates a transfer coin with the port ID and channel ID
// prefixed to the base denom.
func GetTransferCoin(portID, channelID, baseDenom string, amount sdk.Int) sdk.Coin {
denomTrace := ParseDenomTrace(GetPrefixedDenom(portID, channelID, baseDenom))
return sdk.NewCoin(denomTrace.IBCDenom(), amount)
}