Skip to content

Commit

Permalink
Merge branch 'renew'
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpiotzh committed Jan 28, 2024
2 parents b83587a + 90849e6 commit e68c5ab
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 58 deletions.
64 changes: 47 additions & 17 deletions http_server/handle/auto_account_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RespAutoAccountSearch struct {
ExpiredAt uint64 `json:"expired_at"`
PremiumPercentage decimal.Decimal `json:"premium_percentage"`
PremiumBase decimal.Decimal `json:"premium_base"`
DefaultRenewRule bool `json:"default_renew_rule"`
}

type AccStatus int
Expand Down Expand Up @@ -98,7 +99,7 @@ func (h *HttpHandle) doAutoAccountSearch(req *ReqAutoAccountSearch, apiResp *api
}

// check switch
err = h.checkSwitch(parentAccountId, apiResp)
autoDistribution, err := h.checkSwitch(parentAccountId, req.ActionType, apiResp)
if err != nil {
return err
} else if apiResp.ErrNo != api_code.ApiCodeSuccess {
Expand All @@ -108,19 +109,20 @@ func (h *HttpHandle) doAutoAccountSearch(req *ReqAutoAccountSearch, apiResp *api
// get max years
resp.MaxYear = h.getMaxYears(parentAccount)

// get rule price
resp.Price, err = h.getRulePrice(parentAccount.Account, parentAccountId, req.SubAccount, apiResp)
if err != nil {
return err
} else if apiResp.ErrNo != api_code.ApiCodeSuccess {
return nil
}
// check min price 0.99$
builder, err := h.DasCore.ConfigCellDataBuilderByTypeArgsList(common.ConfigCellTypeArgsSubAccount)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeError500, "Failed to get config info")
return fmt.Errorf("ConfigCellDataBuilderByTypeArgsList err: %s", err.Error())
}
// get rule price
resp.Price, resp.DefaultRenewRule, err = h.getRulePrice(parentAccount.Account, parentAccountId, req.SubAccount, apiResp, req.ActionType, builder, autoDistribution)
if err != nil {
return err
} else if apiResp.ErrNo != api_code.ApiCodeSuccess {
return nil
}

newSubAccountPrice, _ := molecule.Bytes2GoU64(builder.ConfigCellSubAccount.NewSubAccountPrice().RawData())
minPrice := decimal.NewFromInt(int64(newSubAccountPrice)).DivRound(decimal.NewFromInt(common.UsdRateBase), 2)
if req.ActionType == tables.ActionTypeRenew {
Expand Down Expand Up @@ -275,7 +277,7 @@ func (h *HttpHandle) checkSubAccount(actionType tables.ActionType, apiResp *api_
return
}
if orderInfo.Id > 0 {
if smtRecord.OrderID == orderInfo.OrderId {
if smtRecord.OrderID == orderInfo.OrderId && orderInfo.OrderStatus != tables.OrderStatusDefault {
accStatus = AccStatusDefault
return
}
Expand All @@ -298,29 +300,38 @@ func (h *HttpHandle) checkSubAccount(actionType tables.ActionType, apiResp *api_
e = fmt.Errorf("GetMintOrderInProgressByAccountIdWithAddr err: %s %s", err.Error(), subAccountId)
return
}
if orderInfo.Id > 0 {
accStatus = AccStatusMinting
if orderInfo.Id > 0 && orderInfo.OrderStatus == tables.OrderStatusDefault {
switch actionType {
case tables.ActionTypeMint:
accStatus = AccStatusMinting
case tables.ActionTypeRenew:
accStatus = AccStatusRenewing
}
}
return
}

func (h *HttpHandle) checkSwitch(parentAccountId string, apiResp *api_code.ApiResp) error {
func (h *HttpHandle) checkSwitch(parentAccountId string, actionType tables.ActionType, apiResp *api_code.ApiResp) (witness.AutoDistribution, error) {
subAccCell, err := h.DasCore.GetSubAccountCell(parentAccountId)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeError500, err.Error())
return fmt.Errorf("GetSubAccountCell err: %s", err.Error())
return witness.AutoDistributionDefault, fmt.Errorf("GetSubAccountCell err: %s", err.Error())
}
subAccTx, err := h.DasCore.Client().GetTransaction(h.Ctx, subAccCell.OutPoint.TxHash)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeError500, err.Error())
return fmt.Errorf("GetTransaction err: %s", err.Error())
return witness.AutoDistributionDefault, fmt.Errorf("GetTransaction err: %s", err.Error())
}
subAccData := witness.ConvertSubAccountCellOutputData(subAccTx.Transaction.OutputsData[subAccCell.OutPoint.Index])
log.Info("checkSwitch:", subAccData.AutoDistribution, subAccData.Flag)
if subAccData.AutoDistribution == witness.AutoDistributionDefault {
if subAccData.Flag == witness.FlagTypeCustomRule && actionType == tables.ActionTypeRenew {
return witness.AutoDistributionDefault, nil
}
apiResp.ApiRespErr(api_code.ApiCodeAutoDistributionClosed, "Automatic allocation is not turned on")
return nil
return witness.AutoDistributionDefault, nil
}
return nil
return witness.AutoDistributionEnable, nil
}

func (h *HttpHandle) getMaxYears(parentAccount *tables.TableAccountInfo) uint64 {
Expand All @@ -339,12 +350,25 @@ func (h *HttpHandle) getMaxYears(parentAccount *tables.TableAccountInfo) uint64
return maxYear
}

func (h *HttpHandle) getRulePrice(parentAcc, parentAccountId, subAccount string, apiResp *api_code.ApiResp) (price decimal.Decimal, e error) {
func (h *HttpHandle) getRulePrice(parentAcc, parentAccountId, subAccount string, apiResp *api_code.ApiResp, actionType tables.ActionType, priceBuilder *witness.ConfigCellDataBuilder, autoDistribution witness.AutoDistribution) (price decimal.Decimal, defaultRenewRule bool, e error) {
if autoDistribution == witness.AutoDistributionDefault && actionType == tables.ActionTypeRenew {
defaultRenewRule = true
renewSubAccountPrice, _ := molecule.Bytes2GoU64(priceBuilder.ConfigCellSubAccount.RenewSubAccountPrice().RawData())
price = decimal.NewFromInt(int64(renewSubAccountPrice)).DivRound(decimal.NewFromInt(common.UsdRateBase), 2)
return
}
ruleConfig, err := h.DbDao.GetRuleConfigByAccountId(parentAccountId)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeDbError, "Failed to search rule config")
e = fmt.Errorf("GetRuleConfigByAccountId err: %s", err.Error())
return
} else if ruleConfig.TxHash == "" {
if actionType == tables.ActionTypeRenew {
defaultRenewRule = true
renewSubAccountPrice, _ := molecule.Bytes2GoU64(priceBuilder.ConfigCellSubAccount.RenewSubAccountPrice().RawData())
price = decimal.NewFromInt(int64(renewSubAccountPrice)).DivRound(decimal.NewFromInt(common.UsdRateBase), 2)
return
}
}
ruleTx, err := h.DasCore.Client().GetTransaction(h.Ctx, types.HexToHash(ruleConfig.TxHash))
if err != nil {
Expand Down Expand Up @@ -380,6 +404,12 @@ func (h *HttpHandle) getRulePrice(parentAcc, parentAccountId, subAccount string,
e = fmt.Errorf("rulePrice.Hit err: %s", err.Error())
return
} else if !hit {
if actionType == tables.ActionTypeRenew {
defaultRenewRule = true
renewSubAccountPrice, _ := molecule.Bytes2GoU64(priceBuilder.ConfigCellSubAccount.RenewSubAccountPrice().RawData())
price = decimal.NewFromInt(int64(renewSubAccountPrice)).DivRound(decimal.NewFromInt(common.UsdRateBase), 2)
return
}
apiResp.ApiRespErr(api_code.ApiCodeNoTSetRules, "not set price rules")
return
}
Expand Down
44 changes: 29 additions & 15 deletions http_server/handle/auto_order_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (h *HttpHandle) doAutoOrderCreate(req *ReqAutoOrderCreate, apiResp *api_cod
}

// check switch
err = h.checkSwitch(parentAccountId, apiResp)
autoDistribution, err := h.checkSwitch(parentAccountId, req.ActionType, apiResp)
if err != nil {
return err
} else if apiResp.ErrNo != api_code.ApiCodeSuccess {
Expand All @@ -123,8 +123,14 @@ func (h *HttpHandle) doAutoOrderCreate(req *ReqAutoOrderCreate, apiResp *api_cod
return nil
}

// check min price 0.99$
builder, err := h.DasCore.ConfigCellDataBuilderByTypeArgsList(common.ConfigCellTypeArgsSubAccount)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeError500, "Failed to get config info")
return fmt.Errorf("ConfigCellDataBuilderByTypeArgsList err: %s", err.Error())
}
// get rule price
usdAmount, err := h.getRulePrice(parentAccount.Account, parentAccountId, req.SubAccount, apiResp)
usdAmount, defaultRenewRule, err := h.getRulePrice(parentAccount.Account, parentAccountId, req.SubAccount, apiResp, req.ActionType, builder, autoDistribution)
if err != nil {
return err
} else if apiResp.ErrNo != api_code.ApiCodeSuccess {
Expand Down Expand Up @@ -207,21 +213,29 @@ func (h *HttpHandle) doAutoOrderCreate(req *ReqAutoOrderCreate, apiResp *api_cod
// usd price -> token price
if actualUsdPrice.GreaterThan(decimal.Zero) {
// check user payment config
userConfig, err := h.DbDao.GetUserPaymentConfig(parentAccountId)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeDbError, "Failed to search payment config")
return fmt.Errorf("GetUserPaymentConfig err: %s", err.Error())
} else if cfg, ok := userConfig.CfgMap[string(req.TokenId)]; !ok || !cfg.Enable {
apiResp.ApiRespErr(api_code.ApiCodeTokenIdNotSupported, "payment method not supported")
return nil
if defaultRenewRule {
find := false
for _, v := range config.Cfg.Das.AutoMint.SupportPaymentToken {
if v == string(req.TokenId) {
find = true
break
}
}
if !find {
apiResp.ApiRespErr(api_code.ApiCodeNoSupportPaymentToken, "payment method not supported")
return nil
}
} else {
userConfig, err := h.DbDao.GetUserPaymentConfig(parentAccountId)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeDbError, "Failed to search payment config")
return fmt.Errorf("GetUserPaymentConfig err: %s", err.Error())
} else if cfg, ok := userConfig.CfgMap[string(req.TokenId)]; !ok || !cfg.Enable {
apiResp.ApiRespErr(api_code.ApiCodeTokenIdNotSupported, "payment method not supported")
return nil
}
}

// check min price 0.99$
builder, err := h.DasCore.ConfigCellDataBuilderByTypeArgsList(common.ConfigCellTypeArgsSubAccount)
if err != nil {
apiResp.ApiRespErr(api_code.ApiCodeError500, "Failed to get config info")
return fmt.Errorf("ConfigCellDataBuilderByTypeArgsList err: %s", err.Error())
}
newSubAccountPrice, _ := molecule.Bytes2GoU64(builder.ConfigCellSubAccount.NewSubAccountPrice().RawData())
minPrice := decimal.NewFromInt(int64(newSubAccountPrice)).DivRound(decimal.NewFromInt(common.UsdRateBase), 2)
if req.ActionType == tables.ActionTypeRenew {
Expand Down
3 changes: 3 additions & 0 deletions http_server/handle/auto_order_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ func (h *HttpHandle) doAutoOrderInfo(req *ReqAutoOrderInfo, apiResp *api_code.Ap
resp.OrderStatus = OrderStatusMintFail
case tables.RecordTypeChain:
resp.OrderStatus = OrderStatusMintOK
if order.OrderStatus == tables.OrderStatusDefault {
resp.OrderStatus = OrderStatusMinting
}
}
}

Expand Down
63 changes: 38 additions & 25 deletions txtool/update_sub_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,33 +247,46 @@ func (s *SubAccountTxTool) getAccountPrice(p *ParamBuildUpdateSubAccountTx) (*Ac
yearlyPrice = p.RenewSubAccountPrice
}
case tables.MintTypeAutoMint:
ruleConfig, err := s.DbDao.GetRuleConfigByAccountId(v.ParentAccountId)
if err != nil {
return nil, err
}
ruleTx, err := s.DasCore.Client().GetTransaction(s.Ctx, types.HexToHash(ruleConfig.TxHash))
if err != nil {
return nil, err
}

parentAccountInfo, err := s.DbDao.GetAccountInfoByAccountId(v.ParentAccountId)
if err != nil {
return nil, err
}
subAccountRule := witness.NewSubAccountRuleEntity(parentAccountInfo.Account)
if err := subAccountRule.ParseFromTx(ruleTx.Transaction, common.ActionDataTypeSubAccountPriceRules); err != nil {
return nil, err
}
hit, idx, err := subAccountRule.Hit(v.Account)
if err != nil {
return nil, err
}
if !hit {
return nil, fmt.Errorf("%s not hit any price rule", v.Account)
subDataDetail := witness.ConvertSubAccountCellOutputData(p.SubAccountOutputsData)
if subDataDetail.AutoDistribution == witness.AutoDistributionDefault && v.SubAction == common.SubActionRenew {
yearlyPrice = p.RenewSubAccountPrice
} else {
ruleConfig, err := s.DbDao.GetRuleConfigByAccountId(v.ParentAccountId)
if err != nil {
return nil, err
} else if ruleConfig.TxHash == "" && v.SubAction == common.SubActionRenew {
yearlyPrice = p.RenewSubAccountPrice
} else {
ruleTx, err := s.DasCore.Client().GetTransaction(s.Ctx, types.HexToHash(ruleConfig.TxHash))
if err != nil {
return nil, err
}

parentAccountInfo, err := s.DbDao.GetAccountInfoByAccountId(v.ParentAccountId)
if err != nil {
return nil, err
}
subAccountRule := witness.NewSubAccountRuleEntity(parentAccountInfo.Account)
if err := subAccountRule.ParseFromTx(ruleTx.Transaction, common.ActionDataTypeSubAccountPriceRules); err != nil {
return nil, err
}
hit, idx, err := subAccountRule.Hit(v.Account)
if err != nil {
return nil, err
}
if !hit {
if v.SubAction == common.SubActionRenew {
yearlyPrice = p.RenewSubAccountPrice
} else {
return nil, fmt.Errorf("%s not hit any price rule", v.Account)
}
} else {
yearlyPrice = uint64(subAccountRule.Rules[idx].Price)
}
}
}
yearlyPrice = uint64(subAccountRule.Rules[idx].Price)
}

log.Info("yearlyPrice:", yearlyPrice)
subAccCapacity := config.PriceToCKB(yearlyPrice, quote, v.RegisterYears+v.RenewYears)

switch v.MintType {
Expand Down
2 changes: 1 addition & 1 deletion unipay/order_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (t *ToolUniPay) RunOrderCheck() {
tickerOrder := time.NewTicker(time.Minute * 5)
tickerOrder := time.NewTicker(time.Minute)
t.Wg.Add(1)
go func() {
defer http_api.RecoverPanic()
Expand Down

0 comments on commit e68c5ab

Please sign in to comment.