mirror of
https://github.com/alexgo-io/libbrc20-indexer.git
synced 2026-01-12 22:43:25 +08:00
feat: record data UpdateHeight
This commit is contained in:
@@ -148,6 +148,10 @@ func (g *BRC20ModuleIndexer) ProcessDeploy(data *model.InscriptionBRC20Data) err
|
||||
// all history
|
||||
g.AllHistory = append(g.AllHistory, history)
|
||||
}
|
||||
|
||||
// mark update
|
||||
tokenInfo.UpdateHeight = data.Height
|
||||
|
||||
// init user tokens
|
||||
var userTokens map[string]*model.BRC20TokenBalance
|
||||
if tokens, ok := g.UserTokensBalanceData[string(data.PkScript)]; !ok {
|
||||
|
||||
@@ -85,6 +85,8 @@ func (g *BRC20ModuleIndexer) ProcessMint(data *model.InscriptionBRC20Data) error
|
||||
}
|
||||
|
||||
// update tinfo
|
||||
tokenInfo.UpdateHeight = data.Height
|
||||
|
||||
// minted
|
||||
balanceMinted := decimal.NewDecimalCopy(amt)
|
||||
if tinfo.TotalMinted.Add(amt).Cmp(tinfo.Max) > 0 {
|
||||
@@ -117,6 +119,8 @@ func (g *BRC20ModuleIndexer) ProcessMint(data *model.InscriptionBRC20Data) error
|
||||
mintInfo.Amount = balanceMinted
|
||||
|
||||
// update tokenBalance
|
||||
tokenBalance.UpdateHeight = data.Height
|
||||
|
||||
if data.BlockTime > 0 {
|
||||
tokenBalance.AvailableBalanceSafe = tokenBalance.AvailableBalanceSafe.Add(balanceMinted)
|
||||
}
|
||||
|
||||
@@ -129,6 +129,8 @@ func (g *BRC20ModuleIndexer) ProcessTransfer(data *model.InscriptionBRC20Data, t
|
||||
tokenUsers[receiverPkScript] = tokenBalance
|
||||
|
||||
// set from
|
||||
fromTokenBalance.UpdateHeight = data.Height
|
||||
|
||||
fromTokenBalance.TransferableBalance = fromTokenBalance.TransferableBalance.Sub(transferInfo.Amount)
|
||||
delete(fromTokenBalance.ValidTransferMap, data.CreateIdxKey)
|
||||
|
||||
@@ -143,6 +145,8 @@ func (g *BRC20ModuleIndexer) ProcessTransfer(data *model.InscriptionBRC20Data, t
|
||||
userHistoryFrom.History = append(userHistoryFrom.History, fromHistory)
|
||||
}
|
||||
// set to
|
||||
tokenBalance.UpdateHeight = data.Height
|
||||
|
||||
if data.BlockTime > 0 {
|
||||
tokenBalance.AvailableBalanceSafe = tokenBalance.AvailableBalanceSafe.Add(transferInfo.Amount)
|
||||
}
|
||||
@@ -203,6 +207,7 @@ func (g *BRC20ModuleIndexer) ProcessTransfer(data *model.InscriptionBRC20Data, t
|
||||
// get user's tokens to update
|
||||
|
||||
moduleTokenBalance := moduleInfo.GetUserTokenBalance(transferInfo.Tick, senderPkScript)
|
||||
moduleTokenBalance.UpdateHeight = data.Height
|
||||
// set module deposit
|
||||
if data.BlockTime > 0 { // how many confirmes ok
|
||||
moduleTokenBalance.SwapAccountBalanceSafe = moduleTokenBalance.SwapAccountBalanceSafe.Add(transferInfo.Amount)
|
||||
|
||||
@@ -99,6 +99,8 @@ func (g *BRC20ModuleIndexer) ProcessApprove(data *model.InscriptionBRC20Data, ap
|
||||
tokenBalance := moduleInfo.GetUserTokenBalance(approveInfo.Tick, receiverPkScript)
|
||||
|
||||
// set from
|
||||
fromTokenBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
fromTokenBalance.ApproveableBalance = fromTokenBalance.ApproveableBalance.Sub(approveInfo.Amount)
|
||||
delete(fromTokenBalance.ValidApproveMap, data.CreateIdxKey)
|
||||
|
||||
@@ -106,6 +108,7 @@ func (g *BRC20ModuleIndexer) ProcessApprove(data *model.InscriptionBRC20Data, ap
|
||||
fromTokenBalance.History = append(fromTokenBalance.History, fromHistory)
|
||||
|
||||
// set to
|
||||
tokenBalance.UpdateHeight = g.BestHeight
|
||||
if data.BlockTime > 0 {
|
||||
tokenBalance.SwapAccountBalanceSafe = tokenBalance.SwapAccountBalanceSafe.Add(approveInfo.Amount)
|
||||
}
|
||||
@@ -194,6 +197,7 @@ func (g *BRC20ModuleIndexer) ProcessInscribeApprove(data *model.InscriptionBRC20
|
||||
}
|
||||
moduleTokenBalance.ValidApproveMap[data.CreateIdxKey] = data
|
||||
|
||||
moduleTokenBalance.UpdateHeight = g.BestHeight
|
||||
// Update global approve lookup table
|
||||
g.InscriptionsValidApproveMap[data.CreateIdxKey] = approveInfo
|
||||
|
||||
|
||||
@@ -135,6 +135,10 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionAddLiquidity(moduleInfo *model
|
||||
// User Real-time Balance Update
|
||||
token0Balance.SwapAccountBalance = token0Balance.SwapAccountBalance.Sub(token0Amt)
|
||||
token1Balance.SwapAccountBalance = token1Balance.SwapAccountBalance.Sub(token1Amt)
|
||||
|
||||
token0Balance.UpdateHeight = g.BestHeight
|
||||
token1Balance.UpdateHeight = g.BestHeight
|
||||
|
||||
// fixme: User safety balance update
|
||||
|
||||
// lp balance update
|
||||
@@ -169,6 +173,8 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionAddLiquidity(moduleInfo *model
|
||||
pool.TickBalance[token1Idx] = pool.TickBalance[token1Idx].Add(token1Amt)
|
||||
pool.LpBalance = pool.LpBalance.Add(lpForPool)
|
||||
|
||||
pool.UpdateHeight = g.BestHeight
|
||||
|
||||
// update lastRootK
|
||||
pool.LastRootK = pool.TickBalance[token0Idx].Mul(pool.TickBalance[token1Idx]).Sqrt()
|
||||
|
||||
|
||||
@@ -25,6 +25,8 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionDecreaseApproval(moduleInfo *m
|
||||
tokenBalance.SwapAccountBalance = tokenBalance.SwapAccountBalance.Sub(tokenAmt)
|
||||
tokenBalance.AvailableBalance = tokenBalance.AvailableBalance.Add(tokenAmt)
|
||||
|
||||
tokenBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
log.Printf("pool decreaseApproval [%s] available: %s, swappable: %s", token, tokenBalance.AvailableBalance, tokenBalance.SwapAccountBalance)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -31,6 +31,9 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionGasFee(moduleInfo *model.BRC20
|
||||
tokenBalance.SwapAccountBalance = tokenBalance.SwapAccountBalance.Sub(gasAmt)
|
||||
gasToBalance.SwapAccountBalance = gasToBalance.SwapAccountBalance.Add(gasAmt)
|
||||
|
||||
tokenBalance.UpdateHeight = g.BestHeight
|
||||
gasToBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
// log.Printf("gas fee[%s]: %s user: %s, gasTo: %s", moduleInfo.GasTick, gasAmt, tokenBalance.SwapAccountBalance, gasToBalance.SwapAccountBalance)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -129,7 +129,13 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionRemoveLiquidity(moduleInfo *mo
|
||||
token0Balance.SwapAccountBalance = token0Balance.SwapAccountBalance.Add(amt0)
|
||||
token1Balance.SwapAccountBalance = token1Balance.SwapAccountBalance.Add(amt1)
|
||||
|
||||
// update at height
|
||||
token0Balance.UpdateHeight = g.BestHeight
|
||||
token1Balance.UpdateHeight = g.BestHeight
|
||||
pool.UpdateHeight = g.BestHeight
|
||||
|
||||
pool.LpBalance = pool.LpBalance.Sub(tokenLpAmt) // fixme
|
||||
|
||||
// Deduct token balance in the pool
|
||||
pool.TickBalance[token0Idx] = pool.TickBalance[token0Idx].Sub(amt0)
|
||||
pool.TickBalance[token1Idx] = pool.TickBalance[token1Idx].Sub(amt1)
|
||||
|
||||
@@ -33,6 +33,9 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionSend(moduleInfo *model.BRC20Mo
|
||||
tokenBalanceFrom.SwapAccountBalance = tokenBalanceFrom.SwapAccountBalance.Sub(tokenAmt)
|
||||
tokenBalanceTo.SwapAccountBalance = tokenBalanceTo.SwapAccountBalance.Add(tokenAmt)
|
||||
|
||||
tokenBalanceFrom.UpdateHeight = g.BestHeight
|
||||
tokenBalanceTo.UpdateHeight = g.BestHeight
|
||||
|
||||
log.Printf("pool send [%s] swappable: %s -> %s", tokenOrPair, tokenBalanceFrom.SwapAccountBalance, tokenBalanceTo.SwapAccountBalance)
|
||||
} else {
|
||||
token0, token1, _ := utils.DecodeTokensFromSwapPair(tokenOrPair)
|
||||
|
||||
@@ -134,6 +134,9 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionSwap(moduleInfo *model.BRC20Mo
|
||||
tokenInBalance := moduleInfo.GetUserTokenBalance(tokenIn, f.PkScript)
|
||||
tokenOutBalance := moduleInfo.GetUserTokenBalance(tokenOut, f.PkScript)
|
||||
|
||||
tokenInBalance.UpdateHeight = g.BestHeight
|
||||
tokenOutBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
if tokenInBalance.SwapAccountBalance.Cmp(tokenInAmt) < 0 {
|
||||
return errors.New(fmt.Sprintf("swap[%s]: user tokenIn balance insufficient: %s < %s",
|
||||
f.ID,
|
||||
@@ -148,6 +151,8 @@ func (g *BRC20ModuleIndexer) ProcessCommitFunctionSwap(moduleInfo *model.BRC20Mo
|
||||
pool.TickBalance[tokenInIdx] = pool.TickBalance[tokenInIdx].Add(amountIn)
|
||||
tokenOutBalance.SwapAccountBalance = tokenOutBalance.SwapAccountBalance.Add(amountOut)
|
||||
|
||||
pool.UpdateHeight = g.BestHeight
|
||||
|
||||
// log.Printf("[%s] pool after swap [%s] %s: %s, %s: %s, lp: %s", moduleInfo.ID, poolPair, pool.Tick[0], pool.TickBalance[0], pool.Tick[1], pool.TickBalance[1], pool.LpBalance)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -196,6 +196,8 @@ func (g *BRC20ModuleIndexer) ProcessConditionalApproveEvents(events []*model.Con
|
||||
fromTokenBalance.CondApproveableBalance = fromTokenBalance.CondApproveableBalance.Sub(event.Amount)
|
||||
// delete(fromTokenBalance.ValidConditionalApproveMap, data.CreateIdxKey)
|
||||
|
||||
fromTokenBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
// fixme: history.Data
|
||||
fromHistory := model.NewBRC20ModuleHistory(true, constant.BRC20_HISTORY_SWAP_TYPE_N_APPROVE_FROM, &event.FromData, &event.ToData, nil, true)
|
||||
fromTokenBalance.History = append(fromTokenBalance.History, fromHistory)
|
||||
@@ -206,6 +208,8 @@ func (g *BRC20ModuleIndexer) ProcessConditionalApproveEvents(events []*model.Con
|
||||
}
|
||||
tokenBalance.SwapAccountBalance = tokenBalance.SwapAccountBalance.Add(event.Amount)
|
||||
|
||||
tokenBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
// fixme: history.Data
|
||||
toHistory := model.NewBRC20ModuleHistory(true, constant.BRC20_HISTORY_SWAP_TYPE_N_APPROVE_TO, &event.FromData, &event.ToData, nil, true)
|
||||
tokenBalance.History = append(tokenBalance.History, toHistory)
|
||||
@@ -220,6 +224,7 @@ func (g *BRC20ModuleIndexer) ProcessConditionalApproveEvents(events []*model.Con
|
||||
}
|
||||
|
||||
for _, event := range events {
|
||||
event.ApproveInfo.UpdateHeight = g.BestHeight
|
||||
event.ApproveInfo.Balance = event.Balance
|
||||
}
|
||||
return nil
|
||||
@@ -270,6 +275,8 @@ func (g *BRC20ModuleIndexer) ProcessInscribeConditionalApprove(data *model.Inscr
|
||||
condApproveInfo := &model.InscriptionBRC20SwapConditionalApproveInfo{
|
||||
Data: data,
|
||||
}
|
||||
condApproveInfo.UpdateHeight = g.BestHeight
|
||||
|
||||
condApproveInfo.Module = body.Module
|
||||
condApproveInfo.Tick = tokenInfo.Ticker
|
||||
condApproveInfo.Amount = balanceCondApprove
|
||||
@@ -303,6 +310,8 @@ func (g *BRC20ModuleIndexer) ProcessInscribeConditionalApprove(data *model.Inscr
|
||||
}
|
||||
moduleTokenBalance.ValidConditionalApproveMap[data.CreateIdxKey] = data
|
||||
|
||||
moduleTokenBalance.UpdateHeight = g.BestHeight
|
||||
|
||||
// Update global approve lookup table
|
||||
g.InscriptionsValidConditionalApproveMap[data.CreateIdxKey] = condApproveInfo
|
||||
// g.InscriptionsValidBRC20DataMap[data.CreateIdxKey] = condApproveInfo.Data // fixme
|
||||
|
||||
@@ -130,6 +130,8 @@ func (g *BRC20ModuleIndexer) ProcessCreateModule(data *model.InscriptionBRC20Dat
|
||||
ConditionalApproveStateBalanceDataMap: make(map[string]*model.BRC20ModuleConditionalApproveStateBalance, 0),
|
||||
}
|
||||
|
||||
m.UpdateHeight = data.Height
|
||||
|
||||
// deployInfo := model.NewInscriptionBRC20SwapInfo(data)
|
||||
// deployInfo.Module = inscriptionId
|
||||
|
||||
|
||||
@@ -169,6 +169,8 @@ func (body *InscriptionBRC20DeployContent) Unmarshal(contentBody []byte) (err er
|
||||
|
||||
// all ticker (state and history)
|
||||
type BRC20TokenInfo struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
Ticker string
|
||||
Deploy *InscriptionBRC20TickInfo
|
||||
|
||||
@@ -306,6 +308,8 @@ type BRC20UserHistory struct {
|
||||
|
||||
// state of address for each tick, (balance and history)
|
||||
type BRC20TokenBalance struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
Ticker string
|
||||
PkScript string
|
||||
AvailableBalance *decimal.Decimal
|
||||
|
||||
@@ -195,6 +195,8 @@ func NewConditionalApproveEvent(senderPkScript, receiverPkScript string, amount,
|
||||
|
||||
// module state
|
||||
type BRC20ModuleSwapInfo struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
ID string // module id
|
||||
Name string // module name
|
||||
DeployerPkScript string // deployer
|
||||
@@ -602,6 +604,8 @@ func (moduleInfo *BRC20ModuleSwapInfo) GenerateApproveEventsByApprove(owner stri
|
||||
|
||||
// state of address for each tick, (balance and history)
|
||||
type BRC20ModuleTokenBalance struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
Tick string
|
||||
PkScript string
|
||||
|
||||
@@ -701,6 +705,8 @@ func (in *BRC20ModuleTokenBalance) CherryPick() *BRC20ModuleTokenBalance {
|
||||
|
||||
// state of address for each tick, (balance and history)
|
||||
type BRC20ModulePoolTotalBalance struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
Tick [2]string
|
||||
TickBalance [2]*decimal.Decimal
|
||||
LpBalance *decimal.Decimal
|
||||
@@ -745,6 +751,8 @@ type InscriptionBRC20SwapInfo struct {
|
||||
}
|
||||
|
||||
type InscriptionBRC20SwapConditionalApproveInfo struct {
|
||||
UpdateHeight uint32
|
||||
|
||||
Module string
|
||||
Tick string
|
||||
Amount *decimal.Decimal // current amt
|
||||
|
||||
Reference in New Issue
Block a user