github.com/DapperCollectives/CAST/backend@v0.0.0-20230921221157-1350c8be7c96/main/models/balance.go (about)

     1  package models
     2  
     3  import (
     4  	"time"
     5  
     6  	s "github.com/DapperCollectives/CAST/backend/main/shared"
     7  	"github.com/georgysavva/scany/pgxscan"
     8  	"github.com/google/uuid"
     9  	"github.com/rs/zerolog/log"
    10  )
    11  
    12  type Balance struct {
    13  	ID                      string    `json:"id"`
    14  	Addr                    string    `json:"addr"`
    15  	PrimaryAccountBalance   uint64    `json:"primaryAccountBalance"`
    16  	SecondaryAddress        string    `json:"secondaryAddress"`
    17  	SecondaryAccountBalance uint64    `json:"secondaryAccountBalance"`
    18  	StakingBalance          uint64    `json:"stakingBalance"`
    19  	ScriptResult            string    `json:"scriptResult"`
    20  	Stakes                  []string  `json:"stakes"`
    21  	BlockHeight             uint64    `json:"blockHeight"`
    22  	Proposal_id             int       `json:"proposal_id"`
    23  	NFTCount                int       `json:"nftCount"`
    24  	CreatedAt               time.Time `json:"createdAt"`
    25  }
    26  
    27  func (b *Balance) GetBalanceByAddressAndBlockHeight(db *s.Database) error {
    28  	sql := `
    29  	SELECT * from balances as b
    30  	WHERE b.addr = $1 and b.block_height = $2
    31  	`
    32  	return pgxscan.Get(db.Context, db.Conn, b, sql, b.Addr, b.BlockHeight)
    33  }
    34  
    35  func (b *Balance) CreateBalance(db *s.Database) error {
    36  	sql := `
    37  	INSERT INTO balances (addr, primary_account_balance, secondary_address,
    38  	    secondary_account_balance, staking_balance, script_result, stakes, block_height, id)
    39  	VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
    40  	`
    41  
    42  	_, err := db.Conn.Exec(db.Context, sql,
    43  		b.Addr, b.PrimaryAccountBalance, b.SecondaryAddress, b.SecondaryAccountBalance,
    44  		b.StakingBalance, b.ScriptResult, b.Stakes, b.BlockHeight, uuid.New(),
    45  	)
    46  
    47  	if err != nil {
    48  		log.Debug().Err(err).Msg("error inserting balance into DB")
    49  		return err
    50  	}
    51  
    52  	return nil
    53  }