187 lines
4.6 KiB
Go
187 lines
4.6 KiB
Go
package task
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"main/mt"
|
|
"main/constant"
|
|
"jccommon"
|
|
"fmt"
|
|
"strings"
|
|
"math/big"
|
|
)
|
|
|
|
const (
|
|
EVENT_NAME_STAKE_CEC = "StakeCec"
|
|
EVENT_NAME_UNSTAKE_CEC = "UnstakeCec"
|
|
)
|
|
|
|
type stackingCec struct {
|
|
|
|
}
|
|
|
|
func (this *stackingCec) init() {
|
|
mt.Table.Contract.Traverse(func (ele *mt.Contract) bool {
|
|
if ele.GetName() == jccommon.CONTRACT_NAME_RewardRouter {
|
|
go this.process(ele.GetNetId(), ele.GetAddress())
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
|
|
func (this *stackingCec) unInit() {
|
|
}
|
|
|
|
func (this *stackingCec) process(netId int32, contractAddress string) {
|
|
key := fmt.Sprintf("stackingCec.lastIdx.%d.%s", netId, contractAddress)
|
|
if dbLastIdxStr, err := jccommon.GetDbParam(constant.BCEVENT_DB, key); err == nil {
|
|
f5.GetGoStyleDb().IncrementLoad(
|
|
constant.BCEVENT_DB,
|
|
"stackingCec." + q5.ToString(netId) + "." + contractAddress,
|
|
"t_blockchain_event",
|
|
q5.ToInt64(dbLastIdxStr),
|
|
func (lastIdx int64, maxIdx int64) (string, []string) {
|
|
sql := fmt.Sprintf(`
|
|
SELECT * FROM t_blockchain_event
|
|
WHERE idx > %d AND idx <= %d AND net_id = %d AND contract_address = ? AND event_name IN (?, ?)
|
|
ORDER BY idx LIMIT 1000
|
|
`,
|
|
lastIdx,
|
|
maxIdx,
|
|
netId)
|
|
params := []string{
|
|
contractAddress,
|
|
EVENT_NAME_STAKE_CEC,
|
|
EVENT_NAME_UNSTAKE_CEC,
|
|
}
|
|
return sql, params
|
|
},
|
|
func (newLastIdx int64) {
|
|
jccommon.SaveDbParam(constant.BCEVENT_DB, key, q5.ToString(newLastIdx))
|
|
},
|
|
this.saveToDb)
|
|
} else {
|
|
panic(fmt.Sprintf("stackingCec.process getDBParam error %s", err))
|
|
}
|
|
}
|
|
|
|
func (this *stackingCec) saveToDb(ds *f5.DataSet) bool {
|
|
f5.GetSysLog().Info("load stackingCec idx:%s net_id:%s contract_name:%s event_name:%s",
|
|
ds.GetByName("net_id"),
|
|
ds.GetByName("idx"),
|
|
ds.GetByName("contract_name"),
|
|
ds.GetByName("event_name"))
|
|
idx := q5.ToInt64(ds.GetByName("idx"))
|
|
txhash := ds.GetByName("txhash")
|
|
logIndex := ds.GetByName("log_index")
|
|
netId := ds.GetByName("net_id")
|
|
eventName := ds.GetByName("event_name")
|
|
contractAddress := ds.GetByName("contract_address")
|
|
returnValues := ds.GetByName("return_values")
|
|
createTime := ds.GetByName("createtime")
|
|
modifyTime := ds.GetByName("modifytime")
|
|
|
|
decodeJsonOk := false
|
|
p := jccommon.NewContractStakeAndUnstakeCecPo()
|
|
if q5.DecodeJson(returnValues, p) == nil {
|
|
decodeJsonOk = true
|
|
dbErr, _, _ := f5.GetGoStyleDb().NewUpsert(
|
|
constant.BCEVENT_DB,
|
|
"t_staking_cec_record",
|
|
[][]string{
|
|
{"txhash", txhash},
|
|
{"log_index", logIndex},
|
|
{"net_id", netId},
|
|
{"contract_address", contractAddress},
|
|
},
|
|
[][]string{},
|
|
[][]string{
|
|
{"txhash", txhash},
|
|
{"log_index", logIndex},
|
|
{"net_id", netId},
|
|
{"contract_address", strings.ToLower(contractAddress)},
|
|
{"src_idx", q5.ToString(idx)},
|
|
{"event_name", eventName},
|
|
{"account_address", p.Account},
|
|
{"token_address", p.Token},
|
|
{"amount", p.Amount},
|
|
{"createtime", createTime},
|
|
{"modifytime", modifyTime},
|
|
})
|
|
if dbErr != nil {
|
|
return false
|
|
}
|
|
if !this.apply(idx, eventName, p) {
|
|
return false
|
|
}
|
|
}
|
|
if !decodeJsonOk {
|
|
jccommon.AddDbLog(constant.BCEVENT_DB, "stackingCec", "decodeJsonError",
|
|
map[string]string{
|
|
"param1": txhash,
|
|
"param2": netId,
|
|
"param3": eventName,
|
|
"param4": q5.ToString(idx),
|
|
"param5": contractAddress,
|
|
})
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (this *stackingCec) apply(idx int64, eventName string,
|
|
p *jccommon.ContractStakeAndUnstakeCecPo) bool {
|
|
bnAmount, ok := new(big.Int).SetString(p.Amount, 10)
|
|
if !ok {
|
|
return false
|
|
}
|
|
if eventName == EVENT_NAME_STAKE_CEC {
|
|
} else if eventName == EVENT_NAME_UNSTAKE_CEC {
|
|
bnAmount = big.NewInt(0).Sub(big.NewInt(0), bnAmount)
|
|
} else {
|
|
return false
|
|
}
|
|
srcAmount := "0"
|
|
{
|
|
dbErr, ds := f5.GetGoStyleDb().NewOrmSelect(
|
|
constant.BCNFT_DB,
|
|
"t_staking_cec",
|
|
[][]string{
|
|
{"account_address", strings.ToLower(p.Account)},
|
|
{"token_address", strings.ToLower(p.Token)},
|
|
},
|
|
)
|
|
if dbErr != nil {
|
|
return false
|
|
}
|
|
if ds.Next() {
|
|
if idx <= q5.ToInt64(ds.GetByName("last_apply_record_idx")) {
|
|
return true
|
|
}
|
|
srcAmount = ds.GetByName("amount")
|
|
}
|
|
}
|
|
bnNewAmount, ok := new(big.Int).SetString(srcAmount, 10)
|
|
if !ok {
|
|
return false
|
|
}
|
|
bnNewAmount.Add(bnNewAmount, bnAmount)
|
|
{
|
|
nowTime := f5.GetApp().GetRealSeconds()
|
|
dbErr, _, _ := f5.GetGoStyleDb().NewUpsert(
|
|
constant.BCNFT_DB,
|
|
"t_staking_cec",
|
|
[][]string{
|
|
{"account_address", strings.ToLower(p.Account)},
|
|
{"token_address", strings.ToLower(p.Token)},
|
|
},
|
|
[][]string{},
|
|
[][]string{
|
|
{"account_address", strings.ToLower(p.Account)},
|
|
{"token_address", strings.ToLower(p.Token)},
|
|
{"createtime", q5.ToString(nowTime)},
|
|
{"modifytime", q5.ToString(nowTime)},
|
|
})
|
|
return dbErr == nil
|
|
}
|
|
}
|