9 log "github.com/sirupsen/logrus"
11 "github.com/vapor/account"
12 "github.com/vapor/blockchain/txbuilder"
13 "github.com/vapor/errors"
14 "github.com/vapor/protocol/bc"
15 "github.com/vapor/protocol/bc/types"
19 defaultTxTTL = 30 * time.Minute
20 defaultBaseRate = float64(100000)
23 func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, error), bool) {
24 decoders := map[string]func([]byte) (txbuilder.Action, error){
25 "control_address": txbuilder.DecodeControlAddressAction,
26 "control_program": txbuilder.DecodeControlProgramAction,
27 "cross_chain_out": txbuilder.DecodeCrossOutAction,
28 "vote_output": txbuilder.DecodeVoteOutputAction,
29 "retire": txbuilder.DecodeRetireAction,
30 "cross_chain_in": txbuilder.DecodeCrossInAction,
31 "spend_account": a.wallet.AccountMgr.DecodeSpendAction,
32 "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
33 "veto": a.wallet.AccountMgr.DecodeVetoAction,
35 decoder, ok := decoders[action]
39 func onlyHaveInputActions(req *BuildRequest) (bool, error) {
41 for i, act := range req.Actions {
42 actionType, ok := act["type"].(string)
44 return false, errors.WithDetailf(ErrBadActionType, "no action type provided on action %d", i)
47 if strings.HasPrefix(actionType, "spend") || actionType == "issue" {
52 return count == len(req.Actions), nil
55 func (a *API) buildSingle(ctx context.Context, req *BuildRequest) (*txbuilder.Template, error) {
56 if err := a.checkRequestValidity(ctx, req); err != nil {
59 actions, err := a.mergeSpendActions(req)
64 maxTime := time.Now().Add(req.TTL.Duration)
65 tpl, err := txbuilder.Build(ctx, req.Tx, actions, maxTime, req.TimeRange)
66 if errors.Root(err) == txbuilder.ErrAction {
67 // append each of the inner errors contained in the data.
70 for i, innerErr := range errors.Data(err)["actions"].([]error) {
72 rootErr = errors.Root(innerErr)
74 Errs = Errs + innerErr.Error()
76 err = errors.WithDetail(rootErr, Errs)
82 // ensure null is never returned for signing instructions
83 if tpl.SigningInstructions == nil {
84 tpl.SigningInstructions = []*txbuilder.SigningInstruction{}
89 // POST /build-transaction
90 func (a *API) build(ctx context.Context, buildReqs *BuildRequest) Response {
91 tmpl, err := a.buildSingle(ctx, buildReqs)
93 return NewErrorResponse(err)
96 return NewSuccessResponse(tmpl)
98 func (a *API) checkRequestValidity(ctx context.Context, req *BuildRequest) error {
99 if err := a.completeMissingIDs(ctx, req); err != nil {
103 if req.TTL.Duration == 0 {
104 req.TTL.Duration = defaultTxTTL
107 if ok, err := onlyHaveInputActions(req); err != nil {
110 return errors.WithDetail(ErrBadActionConstruction, "transaction contains only input actions and no output actions")
115 func (a *API) mergeSpendActions(req *BuildRequest) ([]txbuilder.Action, error) {
116 actions := make([]txbuilder.Action, 0, len(req.Actions))
117 for i, act := range req.Actions {
118 typ, ok := act["type"].(string)
120 return nil, errors.WithDetailf(ErrBadActionType, "no action type provided on action %d", i)
122 decoder, ok := a.actionDecoder(typ)
124 return nil, errors.WithDetailf(ErrBadActionType, "unknown action type %q on action %d", typ, i)
127 // Remarshal to JSON, the action may have been modified when we
129 b, err := json.Marshal(act)
133 action, err := decoder(b)
135 return nil, errors.WithDetailf(ErrBadAction, "%s on action %d", err.Error(), i)
137 actions = append(actions, action)
139 actions = account.MergeSpendAction(actions)
143 func (a *API) buildTxs(ctx context.Context, req *BuildRequest) ([]*txbuilder.Template, error) {
144 if err := a.checkRequestValidity(ctx, req); err != nil {
147 actions, err := a.mergeSpendActions(req)
152 builder := txbuilder.NewBuilder(time.Now().Add(req.TTL.Duration))
153 tpls := []*txbuilder.Template{}
154 for _, action := range actions {
155 if action.ActionType() == "spend_account" {
156 tpls, err = account.SpendAccountChain(ctx, builder, action)
158 err = action.Build(ctx, builder)
167 tpl, _, err := builder.Build()
173 tpls = append(tpls, tpl)
177 // POST /build-chain-transactions
178 func (a *API) buildChainTxs(ctx context.Context, buildReqs *BuildRequest) Response {
179 tmpls, err := a.buildTxs(ctx, buildReqs)
181 return NewErrorResponse(err)
183 return NewSuccessResponse(tmpls)
186 type submitTxResp struct {
187 TxID *bc.Hash `json:"tx_id"`
190 // POST /submit-transaction
191 func (a *API) submit(ctx context.Context, ins struct {
192 Tx types.Tx `json:"raw_transaction"`
194 if err := txbuilder.FinalizeTx(ctx, a.chain, &ins.Tx); err != nil {
195 return NewErrorResponse(err)
198 log.WithField("tx_id", ins.Tx.ID.String()).Info("submit single tx")
199 return NewSuccessResponse(&submitTxResp{TxID: &ins.Tx.ID})
202 type submitTxsResp struct {
203 TxID []*bc.Hash `json:"tx_id"`
206 // POST /submit-transactions
207 func (a *API) submitTxs(ctx context.Context, ins struct {
208 Tx []types.Tx `json:"raw_transactions"`
210 txHashs := []*bc.Hash{}
211 for i := range ins.Tx {
212 if err := txbuilder.FinalizeTx(ctx, a.chain, &ins.Tx[i]); err != nil {
213 return NewErrorResponse(err)
215 log.WithField("tx_id", ins.Tx[i].ID.String()).Info("submit single tx")
216 txHashs = append(txHashs, &ins.Tx[i].ID)
218 return NewSuccessResponse(&submitTxsResp{TxID: txHashs})
221 // POST /estimate-transaction-gas
222 func (a *API) estimateTxGas(ctx context.Context, in struct {
223 TxTemplate txbuilder.Template `json:"transaction_template"`
225 txGasResp, err := txbuilder.EstimateTxGas(in.TxTemplate)
227 return NewErrorResponse(err)
229 return NewSuccessResponse(txGasResp)