github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/api_contract.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    11  	"github.com/machinefi/w3bstream/pkg/errors/status"
    12  	"github.com/machinefi/w3bstream/pkg/models"
    13  	"github.com/machinefi/w3bstream/pkg/types"
    14  )
    15  
    16  type CreateContractLogReq struct {
    17  	ProjectName string `json:"-"`
    18  	models.ContractLogInfo
    19  }
    20  
    21  func CreateContractLog(ctx context.Context, r *CreateContractLogReq) (*models.ContractLog, error) {
    22  	d := types.MustMonitorDBExecutorFromContext(ctx)
    23  	idg := confid.MustSFIDGeneratorFromContext(ctx)
    24  
    25  	if err := checkChainID(ctx, r.ChainID); err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	r.BlockCurrent = r.BlockStart
    30  	m := &models.ContractLog{
    31  		RelContractLog: models.RelContractLog{ContractLogID: idg.MustGenSFID()},
    32  		ContractLogData: models.ContractLogData{
    33  			ProjectName:     r.ProjectName,
    34  			Uniq:            chainUniqFlag,
    35  			ContractLogInfo: r.ContractLogInfo,
    36  		},
    37  	}
    38  	if err := m.Create(d); err != nil {
    39  		if sqlx.DBErr(err).IsConflict() {
    40  			return nil, status.ContractLogConflict
    41  		}
    42  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    43  	}
    44  	return m, nil
    45  }
    46  
    47  func checkChainID(ctx context.Context, id uint64) error {
    48  	ethcli := types.MustETHClientConfigFromContext(ctx)
    49  	if _, ok := ethcli.Clients[uint32(id)]; !ok {
    50  		return status.BlockchainNotFound
    51  	}
    52  	return nil
    53  }
    54  
    55  func GetContractLogBySFID(ctx context.Context, id types.SFID) (*models.ContractLog, error) {
    56  	d := types.MustMonitorDBExecutorFromContext(ctx)
    57  
    58  	m := &models.ContractLog{RelContractLog: models.RelContractLog{ContractLogID: id}}
    59  	if err := m.FetchByContractLogID(d); err != nil {
    60  		if sqlx.DBErr(err).IsNotFound() {
    61  			return nil, status.ContractLogNotFound
    62  		}
    63  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    64  	}
    65  	return m, nil
    66  }
    67  
    68  func ListContractLogBySFIDs(ctx context.Context, ids []types.SFID) ([]models.ContractLog, error) {
    69  	d := types.MustMonitorDBExecutorFromContext(ctx)
    70  	m := &models.ContractLog{}
    71  
    72  	data, err := m.List(d, m.ColContractLogID().In(ids))
    73  	if err != nil {
    74  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    75  	}
    76  	return data, nil
    77  }
    78  
    79  func RemoveContractLogBySFID(ctx context.Context, id types.SFID) error {
    80  	d := types.MustMonitorDBExecutorFromContext(ctx)
    81  
    82  	m := &models.ContractLog{RelContractLog: models.RelContractLog{ContractLogID: id}}
    83  	if err := m.DeleteByContractLogID(d); err != nil {
    84  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
    85  	}
    86  	return nil
    87  }
    88  
    89  func BatchUpdateContractLogPausedBySFIDs(ctx context.Context, ids []types.SFID, s datatypes.Bool) error {
    90  	d := types.MustMonitorDBExecutorFromContext(ctx)
    91  	m := &models.ContractLog{
    92  		ContractLogData: models.ContractLogData{
    93  			ContractLogInfo: models.ContractLogInfo{
    94  				Paused: s,
    95  			},
    96  		},
    97  	}
    98  
    99  	expr := builder.Update(d.T(m)).Set(
   100  		m.ColPaused().ValueBy(s),
   101  		m.ColUpdatedAt().ValueBy(types.Timestamp{Time: time.Now()}),
   102  	).Where(m.ColContractLogID().In(ids))
   103  
   104  	if _, err := d.Exec(expr); err != nil {
   105  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
   106  	}
   107  	return nil
   108  }