ioctl: add balance check when sending out any iotx (possibly check gas fee too) 1299
@@ -8,13 +8,17 @@ package action
import (
"encoding/hex"
+ "fmt"
+ "math/big"
+
+ "github.com/spf13/cobra"
+ "go.uber.org/zap"
"github.com/iotexproject/iotex-core/action"
+ "github.com/iotexproject/iotex-core/cli/ioctl/cmd/account"
"github.com/iotexproject/iotex-core/cli/ioctl/cmd/alias"
"github.com/iotexproject/iotex-core/cli/ioctl/util"
"github.com/iotexproject/iotex-core/pkg/log"
- "github.com/spf13/cobra"
- "go.uber.org/zap"
)
// actionTransferCmd represents the action transfer command
@@ -63,6 +67,25 @@ var actionTransferCmd = &cobra.Command{
log.L().Error("failed to make a Transfer instance", zap.Error(err))
return err
}
+ address, err := alias.Address(sender)
+ if err != nil {
+ return err
+ }
+ accountMeta, err := account.GetAccountMeta(address)
+ if err != nil {
+ return err
+ }
+ balance, ok := big.NewInt(0).SetString(accountMeta.Balance, 10)
+ if !ok {
+ return fmt.Errorf("failed to convert balance into big int")
+ }
+ cost, err := tx.Cost()
+ if err != nil {
+ return err
+ }
+ if balance.Cmp(cost) < 0 {
+ return fmt.Errorf("balance is not enough")
+ }
return sendAction(
(&action.EnvelopeBuilder{}).
SetNonce(nonce).
