github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/tx/create.go (about) 1 package tx 2 3 import ( 4 "context" 5 6 txcrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/tx" 7 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/tx" 8 9 "github.com/NpoolPlatform/chain-middleware/pkg/db" 10 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 11 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 12 13 "github.com/google/uuid" 14 ) 15 16 func (h *Handler) CreateTx(ctx context.Context) (*npool.Tx, error) { 17 id := uuid.New() 18 if h.EntID == nil { 19 h.EntID = &id 20 } 21 22 err := db.WithClient(ctx, func(_ctx context.Context, cli *ent.Client) error { 23 if _, err := txcrud.CreateSet( 24 cli.Tran.Create(), 25 &txcrud.Req{ 26 EntID: h.EntID, 27 CoinTypeID: h.CoinTypeID, 28 FromAccountID: h.FromAccountID, 29 ToAccountID: h.ToAccountID, 30 Amount: h.Amount, 31 FeeAmount: h.FeeAmount, 32 ChainTxID: h.ChainTxID, 33 State: h.State, 34 Extra: h.Extra, 35 Type: h.Type, 36 }, 37 ).Save(ctx); err != nil { 38 return err 39 } 40 return nil 41 }) 42 if err != nil { 43 return nil, err 44 } 45 46 return h.GetTx(ctx) 47 } 48 49 func (h *Handler) CreateTxs(ctx context.Context) ([]*npool.Tx, error) { 50 ids := []uuid.UUID{} 51 52 err := db.WithTx(ctx, func(_ctx context.Context, tx *ent.Tx) error { 53 for _, req := range h.Reqs { 54 info, err := txcrud.CreateSet(tx.Tran.Create(), req).Save(_ctx) 55 if err != nil { 56 return err 57 } 58 ids = append(ids, info.EntID) 59 } 60 return nil 61 }) 62 if err != nil { 63 return nil, err 64 } 65 66 h.Conds = &txcrud.Conds{ 67 EntIDs: &cruder.Cond{Op: cruder.IN, Val: ids}, 68 } 69 h.Offset = 0 70 h.Limit = int32(len(ids)) 71 72 infos, _, err := h.GetTxs(ctx) 73 return infos, err 74 }