Skip to content

Commit f64a6dd

Browse files
authored
ioctl: add balance check when sending out any iotx (possibly check gas fee too) 1299 (iotexproject#1302)
1 parent 694ef38 commit f64a6dd

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

cli/ioctl/cmd/action/actiontransfer.go

+25-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ package action
88

99
import (
1010
"encoding/hex"
11+
"fmt"
12+
"math/big"
13+
14+
"github.com/spf13/cobra"
15+
"go.uber.org/zap"
1116

1217
"github.com/iotexproject/iotex-core/action"
18+
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/account"
1319
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias"
1420
"github.com/iotexproject/iotex-core/cli/ioctl/util"
1521
"github.com/iotexproject/iotex-core/pkg/log"
16-
"github.com/spf13/cobra"
17-
"go.uber.org/zap"
1822
)
1923

2024
// actionTransferCmd represents the action transfer command
@@ -63,6 +67,25 @@ var actionTransferCmd = &cobra.Command{
6367
log.L().Error("failed to make a Transfer instance", zap.Error(err))
6468
return err
6569
}
70+
address, err := alias.Address(sender)
71+
if err != nil {
72+
return err
73+
}
74+
accountMeta, err := account.GetAccountMeta(address)
75+
if err != nil {
76+
return err
77+
}
78+
balance, ok := big.NewInt(0).SetString(accountMeta.Balance, 10)
79+
if !ok {
80+
return fmt.Errorf("failed to convert balance into big int")
81+
}
82+
cost, err := tx.Cost()
83+
if err != nil {
84+
return err
85+
}
86+
if balance.Cmp(cost) < 0 {
87+
return fmt.Errorf("balance is not enough")
88+
}
6689
return sendAction(
6790
(&action.EnvelopeBuilder{}).
6891
SetNonce(nonce).

0 commit comments

Comments
 (0)