Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: clarify the type and txType in whole codebase #75

Merged
merged 1 commit into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ title: 测试
huobi:
rules:
- item: BTC/USDT,BTC1S/USDT # multiple keywords with separator
type: 币币交易
txType: 买入
type: 买入
txType: 币币交易
fullMatch: true
sep: ',' # define separator as a comma
cashAccount: Assets:Rule1:Cash
Expand All @@ -432,8 +432,8 @@ huobi:

`huobi` 是火币相关的配置。它提供基于规则的匹配。可以指定:
- `item`(交易对)的完全/包含匹配。
- `type`(交易类型)的完全/包含匹配。
- `txType`(交易方向)的完全/包含匹配。
- `type`(交易方向)的完全/包含匹配。
- `txType`(交易类型)的完全/包含匹配。
- `time`(交易时间)的区间匹配。
> 交易时间可写为以下两种形式:
> - `11:00-13:00`
Expand Down Expand Up @@ -472,7 +472,7 @@ title: 测试
htsec:
rules:
- item: 兴业转债
txType: 卖
type: 卖
sep: ','
cashAccount: Assets:Rule1:Cash
positionAccount: Assets:Rule1:Positions
Expand All @@ -488,7 +488,7 @@ htsec:

`htsec` 是海通证券相关的配置。它提供基于规则的匹配。可以指定:
- `item`(交易方向-证券编码-证券市值)的完全/包含匹配。
- `txType`(交易方向)的完全/包含匹配。
- `type`(交易方向)的完全/包含匹配。
- `time`(交易时间)的区间匹配。
> 交易时间可写为以下两种形式:
> - `11:00-13:00`
Expand Down
2 changes: 1 addition & 1 deletion example/htsec/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: 测试
htsec:
rules:
- item: 兴业转债
txType: 卖
type: 卖
sep: ','
cashAccount: Assets:Rule1:Cash
positionAccount: Assets:Rule1:Positions
Expand Down
4 changes: 2 additions & 2 deletions example/huobi/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ title: 测试
huobi:
rules:
- item: BTC/USDT,BTC1S/USDT
type: 币币交易
txType: 买入
type: 买入
txType: 币币交易
fullMatch: true
sep: ','
cashAccount: Assets:Rule1:Cash
Expand Down
8 changes: 4 additions & 4 deletions pkg/analyser/alipay/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (a Alipay) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
var err error
for _, r := range cfg.Alipay.Rules {
match := true
// get seperator
// get separator
sep := ","
if r.Separator != nil {
sep = *r.Separator
Expand All @@ -66,7 +66,7 @@ func (a Alipay) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
match = matchFunc(*r.Peer, o.Peer, sep, match)
}
if r.Type != nil {
match = matchFunc(*r.Type, o.TxTypeOriginal, sep, match)
match = matchFunc(*r.Type, o.TypeOriginal, sep, match)
}
if r.Item != nil {
match = matchFunc(*r.Item, o.Item, sep, match)
Expand Down Expand Up @@ -94,14 +94,14 @@ func (a Alipay) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
// Support multiple matches, like one rule matches the
// minus account, the other rule matches the plus account.
if r.TargetAccount != nil {
if o.TxType == ir.TxTypeRecv {
if o.Type == ir.TypeRecv {
resMinus = *r.TargetAccount
} else {
resPlus = *r.TargetAccount
}
}
if r.MethodAccount != nil {
if o.TxType == ir.TxTypeRecv {
if o.Type == ir.TypeRecv {
resPlus = *r.MethodAccount
} else {
resMinus = *r.MethodAccount
Expand Down
10 changes: 5 additions & 5 deletions pkg/analyser/htsec/htsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ func (h Htsec) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provi
var err error
for _, r := range cfg.Htsec.Rules {
match := true
// get seperator
// get separator
sep := ","
if r.Seperator != nil {
sep = *r.Seperator
if r.Separator != nil {
sep = *r.Separator
}

matchFunc := util.SplitFindContains
if r.FullMatch {
matchFunc = util.SplitFindEquals
}

if r.TxType != nil {
match = matchFunc(*r.TxType, o.TxTypeOriginal, sep, match)
if r.Type != nil {
match = matchFunc(*r.Type, o.TypeOriginal, sep, match)
}
if r.Item != nil {
match = matchFunc(*r.Item, o.Item, sep, match)
Expand Down
6 changes: 3 additions & 3 deletions pkg/analyser/huobi/huobi.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ func (h Huobi) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, provi
var err error
for _, r := range cfg.Huobi.Rules {
match := true
// get seperator
// get separator
sep := ","
if r.Seperator != nil {
sep = *r.Seperator
if r.Separator != nil {
sep = *r.Separator
}

matchFunc := util.SplitFindContains
Expand Down
15 changes: 7 additions & 8 deletions pkg/analyser/wechat/wechat.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ func (w Wechat) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
var err error
for _, r := range cfg.Wechat.Rules {
match := true
// get seperator
// get separator
sep := ","

if r.Seperator != nil {
sep = *r.Seperator
if r.Separator != nil {
sep = *r.Separator
}

matchFunc := util.SplitFindContains
Expand All @@ -79,10 +78,10 @@ func (w Wechat) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
match = matchFunc(*r.Peer, o.Peer, sep, match)
}
if r.Type != nil {
match = matchFunc(*r.Type, o.TxTypeOriginal, sep, match)
match = matchFunc(*r.Type, o.TypeOriginal, sep, match)
}
if r.TxType != nil {
match = matchFunc(*r.TxType, o.TypeOriginal, sep, match)
match = matchFunc(*r.TxType, o.TxTypeOriginal, sep, match)
}
if r.Method != nil {
match = matchFunc(*r.Method, o.Method, sep, match)
Expand All @@ -106,14 +105,14 @@ func (w Wechat) GetAccountsAndTags(o *ir.Order, cfg *config.Config, target, prov
if match {
// Support multiple matches, like one rule matches the minus accout, the other rule matches the plus account.
if r.TargetAccount != nil {
if o.TxType == ir.TxTypeRecv {
if o.Type == ir.TypeRecv {
resMinus = *r.TargetAccount
} else {
resPlus = *r.TargetAccount
}
}
if r.MethodAccount != nil {
if o.TxType == ir.TxTypeRecv {
if o.Type == ir.TypeRecv {
resPlus = *r.MethodAccount
} else {
resMinus = *r.MethodAccount
Expand Down
22 changes: 11 additions & 11 deletions pkg/compiler/beancount/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
Tags: o.Tags,
})
case ir.OrderTypeHuobiTrade: // Huobi trades
switch o.TxType {
case ir.TxTypeSend: // buy
switch o.Type {
case ir.TypeSend: // buy
isDiffCommissionUnit := false
commissionUnit, ok := o.Units[ir.CommissionUnit]
if !ok {
Expand All @@ -209,8 +209,8 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
err = huobiTradeBuyOrderDiffCommissionUnitTemplate.Execute(&buf, &HuobiTradeBuyOrderVars{
PayTime: o.PayTime,
Peer: o.Peer,
TypeOriginal: o.TypeOriginal,
TxTypeOriginal: o.TxTypeOriginal,
TypeOriginal: o.TypeOriginal,
Item: o.Item,
Amount: o.Amount,
Money: o.Money,
Expand All @@ -228,8 +228,8 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
err = huobiTradeBuyOrderTemplate.Execute(&buf, &HuobiTradeBuyOrderVars{
PayTime: o.PayTime,
Peer: o.Peer,
TypeOriginal: o.TypeOriginal,
TxTypeOriginal: o.TxTypeOriginal,
TypeOriginal: o.TypeOriginal,
Item: o.Item,
Amount: o.Amount,
Money: o.Money,
Expand All @@ -244,12 +244,12 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
CommissionUnit: o.Units[ir.CommissionUnit],
})
}
case ir.TxTypeRecv: // sell
case ir.TypeRecv: // sell
err = huobiTradeSellOrderTemplate.Execute(&buf, &HuobiTradeSellOrderVars{
PayTime: o.PayTime,
Peer: o.Peer,
TypeOriginal: o.TypeOriginal,
TxTypeOriginal: o.TxTypeOriginal,
TypeOriginal: o.TypeOriginal,
Item: o.Item,
Amount: o.Amount,
Money: o.Money,
Expand All @@ -267,13 +267,13 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
err = fmt.Errorf("Failed to get the TxType.")
}
case ir.OrderTypeSecuritiesTrade:
switch o.TxType {
case ir.TxTypeSend: // buy
switch o.Type {
case ir.TypeSend: // buy
err = htsecTradeBuyOrderTemplate.Execute(&buf, &HtsecTradeBuyOrderVars{
PayTime: o.PayTime,
Peer: o.Peer,
TypeOriginal: o.TypeOriginal,
TxTypeOriginal: o.TxTypeOriginal,
TypeOriginal: o.TypeOriginal,
Item: o.Item,
Amount: o.Amount,
Money: o.Money,
Expand All @@ -288,12 +288,12 @@ func (b *BeanCount) writeBill(file *os.File, index int) error {
CommissionUnit: o.Units[ir.CommissionUnit],
Currency: b.Config.DefaultCurrency,
})
case ir.TxTypeRecv: // sell
case ir.TypeRecv: // sell
err = htsecTradeSellOrderTemplate.Execute(&buf, &HtsecTradeSellOrderVars{
PayTime: o.PayTime,
Peer: o.Peer,
TypeOriginal: o.TypeOriginal,
TxTypeOriginal: o.TxTypeOriginal,
TypeOriginal: o.TypeOriginal,
Item: o.Item,
Amount: o.Amount,
Money: o.Money,
Expand Down
23 changes: 12 additions & 11 deletions pkg/compiler/beancount/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type NormalOrderVars struct {
}

// 火币买入模版(手续费单位为购买单位货币)
var huobiTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TypeOriginal }}" "{{ .TxTypeOriginal }}-{{ .Item }}"
var huobiTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TxTypeOriginal }}" "{{ .TypeOriginal }}-{{ .Item }}"
{{ .CashAccount }} -{{ .Money | printf "%.8f" }} {{ .BaseUnit }}
{{ .PositionAccount }} {{ .Amount | printf "%.8f" }} {{ .TargetUnit }} { {{- .Price | printf "%.8f" }} {{ .BaseUnit -}} } @@ {{ .Money | printf "%.8f" }} {{ .BaseUnit }}
{{ .CashAccount }} -{{ .Commission | printf "%.8f" }} {{ .TargetUnit }} @ {{ .Price | printf "%.8f" }} {{ .BaseUnit }}
Expand All @@ -42,7 +42,7 @@ var huobiTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .
`

// 火币买入模版 2(手续费为特定货币)
var huobiTradeBuyOrderDiffCommissionUnit = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TypeOriginal }}" "{{ .TxTypeOriginal }}-{{ .Item }}"
var huobiTradeBuyOrderDiffCommissionUnit = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TxTypeOriginal }}" "{{ .TypeOriginal }}-{{ .Item }}"
{{ .CashAccount }} -{{ .Money | printf "%.8f" }} {{ .BaseUnit }}
{{ .PositionAccount }} {{ .Amount | printf "%.8f" }} {{ .TargetUnit }} { {{- .Price | printf "%.4f" }} {{ .BaseUnit -}} } @@ {{ .Money | printf "%.8f" }} {{ .BaseUnit }}
{{ .PositionAccount }} -{{ .Commission | printf "%.8f" }} {{ .CommissionUnit }}
Expand All @@ -53,8 +53,8 @@ var huobiTradeBuyOrderDiffCommissionUnit = `{{ .PayTime.Format "2006-01-02" }} *
type HuobiTradeBuyOrderVars struct {
PayTime time.Time
Peer string
TypeOriginal string
TxTypeOriginal string
TypeOriginal string
Item string
CashAccount string
PositionAccount string
Expand All @@ -69,7 +69,8 @@ type HuobiTradeBuyOrderVars struct {
CommissionUnit string
}

var huobiTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TypeOriginal }}" "{{ .TxTypeOriginal }}-{{ .Item }}"
// 火币卖出模版
var huobiTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{ .TxTypeOriginal }}" "{{ .TypeOriginal }}-{{ .Item }}"
{{ .PositionAccount }} -{{ .Amount | printf "%.8f" }} {{ .TargetUnit }} {} @ {{ .Price | printf "%.8f" }} {{ .BaseUnit }}
{{ .CashAccount }} {{ .Money | printf "%.8f" }} {{ .BaseUnit }}
{{ .CashAccount }} -{{ .Commission | printf "%.8f" }} {{ .CommissionUnit }}
Expand All @@ -81,8 +82,8 @@ var huobiTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}-{{
type HuobiTradeSellOrderVars struct {
PayTime time.Time
Peer string
TypeOriginal string
TxTypeOriginal string
TypeOriginal string
Item string
CashAccount string
PositionAccount string
Expand All @@ -98,9 +99,9 @@ type HuobiTradeSellOrderVars struct {
}

// 海通买入模版
var htsecTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{ .TxTypeOriginal }}-{{ .Item }}"
var htsecTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{ .TypeOriginal }}-{{ .Item }}"
{{ .CashAccount }} -{{ .Money | printf "%.2f" }} {{ .Currency }}
{{ .PositionAccount }} {{ .Amount | printf "%.2f" }} {{ .TypeOriginal }} { {{- .Price | printf "%.3f" }} {{ .Currency }}} @@ {{ .Money | printf "%.2f" }} {{ .Currency }}
{{ .PositionAccount }} {{ .Amount | printf "%.2f" }} {{ .TxTypeOriginal }} { {{- .Price | printf "%.3f" }} {{ .Currency }}} @@ {{ .Money | printf "%.2f" }} {{ .Currency }}
{{ .CashAccount }} -{{ .Commission | printf "%.2f" }} {{ .Currency }}
{{ .CommissionAccount }} {{ .Commission | printf "%.2f" }} {{ .Currency }}

Expand All @@ -109,8 +110,8 @@ var htsecTradeBuyOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{
type HtsecTradeBuyOrderVars struct {
PayTime time.Time
Peer string
TypeOriginal string
TxTypeOriginal string
TypeOriginal string
Item string
CashAccount string
PositionAccount string
Expand All @@ -126,8 +127,8 @@ type HtsecTradeBuyOrderVars struct {
Currency string
}

var htsecTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{ .TxTypeOriginal }}-{{ .Item }}"
{{ .PositionAccount }} -{{ .Amount | printf "%.2f" }} {{ .TypeOriginal }} {} @ {{ .Price | printf "%.3f" }} {{ .Currency }}
var htsecTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{{ .TypeOriginal }}-{{ .Item }}"
{{ .PositionAccount }} -{{ .Amount | printf "%.2f" }} {{ .TxTypeOriginal }} {} @ {{ .Price | printf "%.3f" }} {{ .Currency }}
{{ .CashAccount }} {{ .Money | printf "%.2f" }} {{ .Currency }}
{{ .CashAccount }} -{{ .Commission | printf "%.2f" }} {{ .Currency }}
{{ .CommissionAccount }} {{ .Commission | printf "%.2f" }} {{ .Currency }}
Expand All @@ -138,8 +139,8 @@ var htsecTradeSellOrder = `{{ .PayTime.Format "2006-01-02" }} * "{{ .Peer }}" "{
type HtsecTradeSellOrderVars struct {
PayTime time.Time
Peer string
TypeOriginal string
TxTypeOriginal string
TypeOriginal string
Item string
CashAccount string
PositionAccount string
Expand Down
14 changes: 7 additions & 7 deletions pkg/ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type Order struct {
Money float64
Note string
PayTime time.Time
TxType TxType // 方向,一般为 收/支
TxTypeOriginal string
Type Type // 方向,一般为 收/支
TypeOriginal string
TxTypeOriginal string // 交易类型
Method string
Amount float64
Price float64
Expand Down Expand Up @@ -73,13 +73,13 @@ const (
MinusAccount = "MinusAccount"
)

// TxType is transanction type defined by alipay.
type TxType string
// Type is transaction type defined by alipay.
type Type string

const (
TxTypeSend TxType = "Send"
TxTypeRecv = "Recv"
TxTypeUnknown = "Unknwon"
TypeSend Type = "Send"
TypeRecv = "Recv"
TypeUnknown = "Unknwon"
)

type OrderType string // 为 IR 设置的交易类别
Expand Down
Loading