token.gno
1.31 Kb · 49 lines
1package transfer
2
3import (
4 "chain"
5 "strconv"
6
7 "gno.land/p/nt/ufmt/v0"
8)
9
10// Token defines a struct which represents a token to be transferred.
11type Token struct {
12 // the token denomination
13 Denom Denom
14 // the token amount to be transferred
15 Amount string
16}
17
18// NewToken constructs a Token with the given native base denom and amount
19// (no trace, no prefix hops). Provided so external callers — which cannot
20// construct foreign-stamped composite literals under v2 borrow rule #1 —
21// can still build Token values for tests and packet assembly.
22func NewToken(baseDenom, amount string) Token {
23 return Token{
24 Denom: NewDenom(baseDenom),
25 Amount: amount,
26 }
27}
28
29// ToCoin converts a Token to an chain.Coin.
30//
31// The function parses the Token.Amount field and returns a new sdk.Coin with
32// the IBCDenom of the Token's Denom field and the parsed Amount.
33// If the Amount cannot be parsed, an error is returned with a wrapped error
34// message.
35func (t Token) ToCoin() (chain.Coin, error) {
36 amt, err := t.AmountInt64()
37 if err != nil {
38 return chain.Coin{}, err
39 }
40 return chain.NewCoin(t.Denom.IBCDenom(), amt), nil
41}
42
43func (t Token) AmountInt64() (int64, error) {
44 amt, err := strconv.ParseInt(t.Amount, 10, 64)
45 if err != nil {
46 return 0, ufmt.Errorf("unable to parse amount %q into int64", t.Amount)
47 }
48 return amt, nil
49}