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/net/http/reqid"
15 "github.com/vapor/protocol/bc"
16 "github.com/vapor/protocol/bc/types"
20 defaultTxTTL = 30 * time.Minute
21 defaultBaseRate = float64(100000)
24 func (a *API) actionDecoder(action string) (func([]byte) (txbuilder.Action, error), bool) {
25 decoders := map[string]func([]byte) (txbuilder.Action, error){
26 "control_address": txbuilder.DecodeControlAddressAction,
27 "control_program": txbuilder.DecodeControlProgramAction,
28 "cross_chain_out": txbuilder.DecodeCrossOutAction,
29 "vote_output": txbuilder.DecodeVoteOutputAction,
30 "retire": txbuilder.DecodeRetireAction,
31 "cross_chain_in": txbuilder.DecodeCrossInAction,
32 "spend_account": a.wallet.AccountMgr.DecodeSpendAction,
33 "spend_account_unspent_output": a.wallet.AccountMgr.DecodeSpendUTXOAction,
34 "veto": a.wallet.AccountMgr.DecodeVetoAction,
36 decoder, ok := decoders[action]
40 func onlyHaveInputActions(req *BuildRequest) (bool, error) {
42 for i, act := range req.Actions {
43 actionType, ok := act["type"].(string)
45 return false, errors.WithDetailf(ErrBadActionType, "no action type provided on action %d", i)
48 if strings.HasPrefix(actionType, "spend") || actionType == "issue" {
53 return count == len(req.Actions), nil
56 func (a *API) buildSingle(ctx context.Context, req *BuildRequest) (*txbuilder.Template, error) {
57 if err := a.checkRequestValidity(ctx, req); err != nil {
60 actions, err := a.mergeSpendActions(req)
65 maxTime := time.Now().Add(req.TTL.Duration)
66 tpl, err := txbuilder.Build(ctx, req.Tx, actions, maxTime, req.TimeRange)
67 if errors.Root(err) == txbuilder.ErrAction {
68 // append each of the inner errors contained in the data.
71 for i, innerErr := range errors.Data(err)["actions"].([]error) {
73 rootErr = errors.Root(innerErr)
75 Errs = Errs + innerErr.Error()
77 err = errors.WithDetail(rootErr, Errs)
83 // ensure null is never returned for signing instructions
84 if tpl.SigningInstructions == nil {
85 tpl.SigningInstructions = []*txbuilder.SigningInstruction{}
90 // POST /build-transaction
91 func (a *API) build(ctx context.Context, buildReqs *BuildRequest) Response {
92 subctx := reqid.NewSubContext(ctx, reqid.New())
93 tmpl, err := a.buildSingle(subctx, buildReqs)
95 return NewErrorResponse(err)
98 return NewSuccessResponse(tmpl)
100 func (a *API) checkRequestValidity(ctx context.Context, req *BuildRequest) error {
101 if err := a.completeMissingIDs(ctx, req); err != nil {
105 if req.TTL.Duration == 0 {
106 req.TTL.Duration = defaultTxTTL
109 if ok, err := onlyHaveInputActions(req); err != nil {
112 return errors.WithDetail(ErrBadActionConstruction, "transaction contains only input actions and no output actions")
117 func (a *API) mergeSpendActions(req *BuildRequest) ([]txbuilder.Action, error) {
118 actions := make([]txbuilder.Action, 0, len(req.Actions))
119 for i, act := range req.Actions {
120 typ, ok := act["type"].(string)
122 return nil, errors.WithDetailf(ErrBadActionType, "no action type provided on action %d", i)
124 decoder, ok := a.actionDecoder(typ)
126 return nil, errors.WithDetailf(ErrBadActionType, "unknown action type %q on action %d", typ, i)
129 // Remarshal to JSON, the action may have been modified when we
131 b, err := json.Marshal(act)
135 action, err := decoder(b)
137 return nil, errors.WithDetailf(ErrBadAction, "%s on action %d", err.Error(), i)
139 actions = append(actions, action)
141 actions = account.MergeSpendAction(actions)
145 func (a *API) buildTxs(ctx context.Context, req *BuildRequest) ([]*txbuilder.Template, error) {
146 if err := a.checkRequestValidity(ctx, req); err != nil {
149 actions, err := a.mergeSpendActions(req)
154 builder := txbuilder.NewBuilder(time.Now().Add(req.TTL.Duration))
155 tpls := []*txbuilder.Template{}
156 for _, action := range actions {
157 if action.ActionType() == "spend_account" {
158 tpls, err = account.SpendAccountChain(ctx, builder, action)
160 err = action.Build(ctx, builder)
169 tpl, _, err := builder.Build()
175 tpls = append(tpls, tpl)
179 // POST /build-chain-transactions
180 func (a *API) buildChainTxs(ctx context.Context, buildReqs *BuildRequest) Response {
181 subctx := reqid.NewSubContext(ctx, reqid.New())
182 tmpls, err := a.buildTxs(subctx, buildReqs)
184 return NewErrorResponse(err)
186 return NewSuccessResponse(tmpls)
189 type submitTxResp struct {
190 TxID *bc.Hash `json:"tx_id"`
193 // POST /submit-transaction
194 func (a *API) submit(ctx context.Context, ins struct {
195 Tx types.Tx `json:"raw_transaction"`
197 if err := txbuilder.FinalizeTx(ctx, a.chain, &ins.Tx); err != nil {
198 return NewErrorResponse(err)
201 log.WithField("tx_id", ins.Tx.ID.String()).Info("submit single tx")
202 return NewSuccessResponse(&submitTxResp{TxID: &ins.Tx.ID})
205 type submitTxsResp struct {
206 TxID []*bc.Hash `json:"tx_id"`
209 // POST /submit-transactions
210 func (a *API) submitTxs(ctx context.Context, ins struct {
211 Tx []types.Tx `json:"raw_transactions"`
213 txHashs := []*bc.Hash{}
214 for i := range ins.Tx {
215 if err := txbuilder.FinalizeTx(ctx, a.chain, &ins.Tx[i]); err != nil {
216 return NewErrorResponse(err)
218 log.WithField("tx_id", ins.Tx[i].ID.String()).Info("submit single tx")
219 txHashs = append(txHashs, &ins.Tx[i].ID)
221 return NewSuccessResponse(&submitTxsResp{TxID: txHashs})
224 // POST /estimate-transaction-gas
225 func (a *API) estimateTxGas(ctx context.Context, in struct {
226 TxTemplate txbuilder.Template `json:"transaction_template"`
228 txGasResp, err := txbuilder.EstimateTxGas(in.TxTemplate)
230 return NewErrorResponse(err)
232 return NewSuccessResponse(txGasResp)