forked from ignite/cli
/
transfer.go
65 lines (53 loc) · 1.77 KB
/
transfer.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
package cosmosfaucet
import (
"context"
"fmt"
"strconv"
"strings"
chaincmdrunner "github.com/tendermint/starport/starport/pkg/chaincmd/runner"
)
// TotalTransferredAmount returns the total transferred amount from faucet account to toAccountAddress.
func (f Faucet) TotalTransferredAmount(ctx context.Context, toAccountAddress, denom string) (amount uint64, err error) {
fromAccount, err := f.runner.ShowAccount(ctx, f.accountName)
if err != nil {
return 0, err
}
events, err := f.runner.QueryTxEvents(ctx,
chaincmdrunner.NewEventSelector("message", "sender", fromAccount.Address),
chaincmdrunner.NewEventSelector("transfer", "recipient", toAccountAddress))
if err != nil {
return 0, err
}
for _, event := range events {
if event.Type == "transfer" {
for _, attr := range event.Attributes {
if attr.Key == "amount" {
if !strings.HasSuffix(attr.Value, denom) {
continue
}
amountStr := strings.TrimRight(attr.Value, denom)
if a, err := strconv.ParseUint(amountStr, 10, 64); err == nil {
amount += a
}
}
}
}
}
return amount, nil
}
// Transfer transfer amount of tokens from the faucet account to toAccountAddress.
func (f Faucet) Transfer(ctx context.Context, toAccountAddress string, amount uint64, denom string) error {
amountStr := fmt.Sprintf("%d%s", amount, denom)
totalSent, err := f.TotalTransferredAmount(ctx, toAccountAddress, denom)
if err != nil {
return err
}
if f.coinsMax[denom] != 0 && totalSent >= f.coinsMax[denom] {
return fmt.Errorf("account has reached maximum credit allowed per account (%d)", f.coinsMax[denom])
}
fromAccount, err := f.runner.ShowAccount(ctx, f.accountName)
if err != nil {
return err
}
return f.runner.BankSend(ctx, fromAccount.Address, toAccountAddress, amountStr)
}