Skip to content

Commit

Permalink
Finish implementation of all parsing groups for cl position handling …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
pharr117 committed Jan 28, 2024
1 parent f01400f commit aa1bb43
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 77 deletions.
16 changes: 2 additions & 14 deletions csv/parsers/accointing/osmosis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ import (
"github.com/preichenberger/go-coinbasepro/v2"
)

func addTxToGroupMap(groupedTxs map[uint][]db.TaxableTransaction, tx db.TaxableTransaction) map[uint][]db.TaxableTransaction {
// Add tx to group using the TX ID as key and appending to array
if _, ok := groupedTxs[tx.Message.Tx.ID]; ok {
groupedTxs[tx.Message.Tx.ID] = append(groupedTxs[tx.Message.Tx.ID], tx)
} else {
var txGrouping []db.TaxableTransaction
txGrouping = append(txGrouping, tx)
groupedTxs[tx.Message.Tx.ID] = txGrouping
}
return groupedTxs
}

type OsmosisLpTxGroup struct {
GroupedTxes map[uint][]db.TaxableTransaction // TX db ID to its messages
Rows []parsers.CsvRow
Expand Down Expand Up @@ -51,7 +39,7 @@ func (sf *OsmosisLpTxGroup) AddTxToGroup(tx db.TaxableTransaction) {
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
sf.GroupedTxes = addTxToGroupMap(sf.GroupedTxes, tx)
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

func (sf *OsmosisLpTxGroup) ParseGroup() error {
Expand Down Expand Up @@ -128,7 +116,7 @@ func (sf *OsmosisConcentratedLiquidityTxGroup) AddTxToGroup(tx db.TaxableTransac
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
sf.GroupedTxes = addTxToGroupMap(sf.GroupedTxes, tx)
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

// Concentrated liquidit txs are grouped to be parsed together. Complex analysis may be require later, so group them now for later extension.
Expand Down
41 changes: 7 additions & 34 deletions csv/parsers/cointracker/osmosis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@ import (
"github.com/DefiantLabs/cosmos-tax-cli/util"
)

func addTxToGroupMap(groupedTxs map[uint][]db.TaxableTransaction, tx db.TaxableTransaction) map[uint][]db.TaxableTransaction {
// Add tx to group using the TX ID as key and appending to array
if _, ok := groupedTxs[tx.Message.Tx.ID]; ok {
groupedTxs[tx.Message.Tx.ID] = append(groupedTxs[tx.Message.Tx.ID], tx)
} else {
var txGrouping []db.TaxableTransaction
txGrouping = append(txGrouping, tx)
groupedTxs[tx.Message.Tx.ID] = txGrouping
}
return groupedTxs
}

type OsmosisLpTxGroup struct {
GroupedTxes map[uint][]db.TaxableTransaction // TX db ID to its messages
Rows []parsers.CsvRow
Expand All @@ -45,7 +33,7 @@ func (sf *OsmosisLpTxGroup) AddTxToGroup(tx db.TaxableTransaction) {
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
sf.GroupedTxes = addTxToGroupMap(sf.GroupedTxes, tx)
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

func (sf *OsmosisLpTxGroup) ParseGroup() error {
Expand Down Expand Up @@ -144,13 +132,12 @@ func (sf *OsmosisConcentratedLiquidityTxGroup) AddTxToGroup(tx db.TaxableTransac
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
sf.GroupedTxes = addTxToGroupMap(sf.GroupedTxes, tx)
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

// Concentrated liquidit txs are grouped to be parsed together. Complex analysis may be require later, so group them now for later extension.
func (sf *OsmosisConcentratedLiquidityTxGroup) ParseGroup() error {

txsToFees := getTxToFeesMap(sf.GroupedTxes)
txsToFees := parsers.GetTxToFeesMap(sf.GroupedTxes)
for _, txMessages := range sf.GroupedTxes {
for _, message := range txMessages {

Expand Down Expand Up @@ -187,26 +174,12 @@ func (sf *OsmosisConcentratedLiquidityTxGroup) ParseGroup() error {
for _, fees := range txsToFees {
for _, fee := range fees {
row := Row{}
row.ParseFee(fee.Tx, fee)
err := row.ParseFee(fee.Tx, fee)
if err != nil {
return err
}
sf.Rows = append(sf.Rows, row)
}
}
return nil
}

func getTxToFeesMap(groupedTxes map[uint][]db.TaxableTransaction) map[uint][]db.Fee {

txToFees := make(map[uint][]db.Fee)

for _, txMessages := range groupedTxes {
for _, message := range txMessages {
messageTx := message.Message.Tx
if _, ok := txToFees[messageTx.ID]; !ok {
txToFees[messageTx.ID] = append(txToFees[messageTx.ID], messageTx.Fees...)
break
}
}
}

return txToFees
}
4 changes: 2 additions & 2 deletions csv/parsers/cryptotaxcalculator/cryptotaxcalculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (p *Parser) ProcessTaxableTx(address string, taxableTxs []db.TaxableTransac

for _, fee := range feesWithoutTx {
row := Row{}
err := row.ParseFee(address, fee)
err := row.ParseFee(fee)
if err != nil {
return err
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func ParseMsgRecvPacket(address string, event db.TaxableTransaction) (Row, error
}

func (p *Parser) InitializeParsingGroups() {
p.ParsingGroups = append(p.ParsingGroups, &OsmosisLpTxGroup{})
p.ParsingGroups = append(p.ParsingGroups, &OsmosisLpTxGroup{}, &OsmosisConcentratedLiquidityTxGroup{})
}

func ParseOsmosisReward(event db.TaxableEvent) (Row, error) {
Expand Down
93 changes: 85 additions & 8 deletions csv/parsers/cryptotaxcalculator/osmosis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cryptotaxcalculator
import (
"github.com/DefiantLabs/cosmos-tax-cli/csv/parsers"
"github.com/DefiantLabs/cosmos-tax-cli/db"
"github.com/DefiantLabs/cosmos-tax-cli/osmosis/modules/concentratedliquidity"
"github.com/DefiantLabs/cosmos-tax-cli/osmosis/modules/gamm"
"github.com/DefiantLabs/cosmos-tax-cli/util"
)
Expand Down Expand Up @@ -33,14 +34,7 @@ func (sf *OsmosisLpTxGroup) AddTxToGroup(tx db.TaxableTransaction) {
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
// Add tx to group using the TX ID as key and appending to array
if _, ok := sf.GroupedTxes[tx.Message.Tx.ID]; ok {
sf.GroupedTxes[tx.Message.Tx.ID] = append(sf.GroupedTxes[tx.Message.Tx.ID], tx)
} else {
var txGrouping []db.TaxableTransaction
txGrouping = append(txGrouping, tx)
sf.GroupedTxes[tx.Message.Tx.ID] = txGrouping
}
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

func (sf *OsmosisLpTxGroup) ParseGroup() error {
Expand Down Expand Up @@ -85,3 +79,86 @@ func (sf *OsmosisLpTxGroup) ParseGroup() error {
}
return nil
}

type OsmosisConcentratedLiquidityTxGroup struct {
GroupedTxes map[uint][]db.TaxableTransaction // TX db ID to its messages
Rows []parsers.CsvRow
}

func (sf *OsmosisConcentratedLiquidityTxGroup) GetRowsForParsingGroup() []parsers.CsvRow {
return sf.Rows
}

func (sf *OsmosisConcentratedLiquidityTxGroup) BelongsToGroup(message db.TaxableTransaction) bool {
_, isInGroup := parsers.IsOsmosisConcentratedLiquidity[message.Message.MessageType.MessageType]
return isInGroup
}

func (sf *OsmosisConcentratedLiquidityTxGroup) GetGroupedTxes() map[uint][]db.TaxableTransaction {
return sf.GroupedTxes
}

func (sf *OsmosisConcentratedLiquidityTxGroup) String() string {
return "OsmosisLpTxGroup"
}

func (sf *OsmosisConcentratedLiquidityTxGroup) AddTxToGroup(tx db.TaxableTransaction) {
// Add tx to group using the TX ID as key and appending to array
if sf.GroupedTxes == nil {
sf.GroupedTxes = make(map[uint][]db.TaxableTransaction)
}
sf.GroupedTxes = parsers.AddTxToGroupMap(sf.GroupedTxes, tx)
}

// Concentrated liquidit txs are grouped to be parsed together. Complex analysis may be require later, so group them now for later extension.
func (sf *OsmosisConcentratedLiquidityTxGroup) ParseGroup() error {
txsToFees := parsers.GetTxToFeesMap(sf.GroupedTxes)
for _, txMessages := range sf.GroupedTxes {
for _, message := range txMessages {

row := Row{}
row.Date = message.Message.Tx.Block.TimeStamp.Format(TimeLayout)
row.ID = message.Message.Tx.Hash
switch message.Message.MessageType.MessageType {
case concentratedliquidity.MsgCreatePosition:
parseAndAddSentAmountWithDefault(&row, message)
row.Type = Sell
case concentratedliquidity.MsgWithdrawPosition, concentratedliquidity.MsgTransferPositions:
parseAndAddReceivedAmountWithDefault(&row, message)
row.Type = Buy
case concentratedliquidity.MsgAddToPosition:
if message.DenominationReceivedID != nil {
parseAndAddReceivedAmountWithDefault(&row, message)
row.Type = Buy
} else {
parseAndAddSentAmountWithDefault(&row, message)
row.Type = Sell
}
}

messageFee := txsToFees[message.Message.Tx.ID]
if len(messageFee) > 0 {
fee := messageFee[0]
parseAndAddFeeWithDefault(&row, fee)

// This fee has been processed, pop it off the stack
txsToFees[message.Message.Tx.ID] = txsToFees[message.Message.Tx.ID][1:]

}
sf.Rows = append(sf.Rows, row)
}
}

// If there are any fees left over, add them to the CSV
for _, fees := range txsToFees {
for _, fee := range fees {
row := Row{}
err := row.ParseFee(fee)
if err != nil {
return err
}
sf.Rows = append(sf.Rows, row)
}
}
return nil
}
60 changes: 59 additions & 1 deletion csv/parsers/cryptotaxcalculator/rows.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cryptotaxcalculator

import (
"errors"
"fmt"

"github.com/DefiantLabs/cosmos-tax-cli/db"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (row *Row) ParseBasic(address string, event db.TaxableTransaction) error {
return nil
}

func (row *Row) ParseFee(address string, fee db.Fee) error {
func (row *Row) ParseFee(fee db.Fee) error {
row.Date = fee.Tx.Block.TimeStamp.Format(TimeLayout)
row.ID = fee.Tx.Hash
row.Type = Fee
Expand Down Expand Up @@ -138,3 +139,60 @@ func (row *Row) ParseSwap(event db.TaxableTransaction, address, eventType string

return nil
}

func parseAndAddSentAmount(row *Row, event db.TaxableTransaction) error {
conversionAmount, conversionSymbol, err := db.ConvertUnits(util.FromNumeric(event.AmountSent), event.DenominationSent)
if err != nil {
return errors.New("cannot parse denom units")
}
row.QuoteAmount = conversionAmount.Text('f', -1)
row.QuoteCurrency = conversionSymbol

return nil
}

func parseAndAddSentAmountWithDefault(row *Row, event db.TaxableTransaction) {
err := parseAndAddSentAmount(row, event)
if err != nil {
row.QuoteAmount = util.NumericToString(event.AmountSent)
row.QuoteCurrency = event.DenominationSent.Base
}
}

func parseAndAddReceivedAmount(row *Row, event db.TaxableTransaction) error {
conversionAmount, conversionSymbol, err := db.ConvertUnits(util.FromNumeric(event.AmountReceived), event.DenominationReceived)
if err != nil {
return errors.New("cannot parse denom units")
}
row.BaseAmount = conversionAmount.Text('f', -1)
row.BaseCurrency = conversionSymbol

return nil
}

func parseAndAddReceivedAmountWithDefault(row *Row, event db.TaxableTransaction) {
err := parseAndAddReceivedAmount(row, event)
if err != nil {
row.BaseAmount = util.NumericToString(event.AmountReceived)
row.BaseCurrency = event.DenominationReceived.Base
}
}

func parseAndAddFee(row *Row, fee db.Fee) error {
conversionAmount, conversionSymbol, err := db.ConvertUnits(util.FromNumeric(fee.Amount), fee.Denomination)
if err != nil {
return errors.New("cannot parse denom units")
}
row.FeeAmount = conversionAmount.Text('f', -1)
row.FeeCurrency = conversionSymbol

return nil
}

func parseAndAddFeeWithDefault(row *Row, fee db.Fee) {
err := parseAndAddFee(row, fee)
if err != nil {
row.FeeAmount = util.NumericToString(fee.Amount)
row.FeeCurrency = fee.Denomination.Base
}
}
2 changes: 1 addition & 1 deletion csv/parsers/koinly/koinly.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Parser) ProcessTaxableEvent(taxableEvents []db.TaxableEvent) error {
}

func (p *Parser) InitializeParsingGroups() {
p.ParsingGroups = append(p.ParsingGroups, &OsmosisLpTxGroup{})
p.ParsingGroups = append(p.ParsingGroups, &OsmosisLpTxGroup{}, &OsmosisConcentratedLiquidityTxGroup{})
}

func (p *Parser) GetRows(address string, startDate, endDate *time.Time) ([]parsers.CsvRow, error) {
Expand Down
Loading

0 comments on commit aa1bb43

Please sign in to comment.