forked from iotexproject/iotex-core
/
actiondeploy.go
74 lines (68 loc) · 2.08 KB
/
actiondeploy.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
66
67
68
69
70
71
72
73
74
// Copyright (c) 2019 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package action
import (
"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"
)
// actionDeployCmd represents the action deploy command
var actionDeployCmd = &cobra.Command{
Use: "deploy -s SIGNER -b BYTE_CODE -l GAS_LIMIT [-p GAS_PRICE]",
Short: "Deploy smart contract on IoTeX blockchain",
Args: cobra.MaximumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
output, err := deploy()
if err == nil {
fmt.Println(output)
}
return err
},
}
// deploy deploys smart contract on IoTeX blockchain
func deploy() (string, error) {
executor, err := alias.Address(signer)
if err != nil {
return "", err
}
if nonce == 0 {
accountMeta, err := account.GetAccountMeta(executor)
if err != nil {
return "", err
}
nonce = accountMeta.PendingNonce
}
var gasPriceRau *big.Int
if len(gasPrice) == 0 {
gasPriceRau, err = GetGasPrice()
if err != nil {
return "", err
}
} else {
gasPriceRau, err = util.StringToRau(gasPrice, util.GasPriceDecimalNum)
if err != nil {
return "", err
}
}
tx, err := action.NewExecution("", nonce, big.NewInt(0), gasLimit, gasPriceRau, bytecode)
if err != nil {
log.L().Error("cannot make a Execution instance", zap.Error(err))
return "", err
}
bd := &action.EnvelopeBuilder{}
elp := bd.SetNonce(nonce).
SetGasPrice(gasPriceRau).
SetGasLimit(gasLimit).
SetAction(tx).Build()
return sendAction(elp)
}