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

Cannot import CSV file, multi-commodity transaction is unbalanced. #1978

Closed
PSLLSP opened this issue Jan 8, 2023 · 4 comments
Closed

Cannot import CSV file, multi-commodity transaction is unbalanced. #1978

PSLLSP opened this issue Jan 8, 2023 · 4 comments
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. csv The csv file format, csv output format, or generally CSV-related.

Comments

@PSLLSP
Copy link

PSLLSP commented Jan 8, 2023

I try to import CSV file, I think I create valid journal but hledger doesn't like it. Input CSV file:

$ cat demo-test.csv
"Txhash","Blockno","UnixTimestamp","DateTime","From","To","ContractAddress","Value_IN(BNB)","Value_OUT(BNB)","CurrentValue @ $265.11/BNB","TxnFee(BNB)","TxnFee(USD)","Historical $Price/BNB","Status","ErrCode","Method"
"0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b","24073786","1681557699","2022-12-21 17:23:59","0x1f4d772f3cb8c4a9d42a29a2168be552854a23d1","0xa2ef40a673df43157232810d99cd9f73491bd7d5","","0","0.1","26.511","0.00152825","0.4051543575","251.27","","","Invest",""

Import rules:

$ cat demo-test.csv.rules 
# hledger rules

# skip the headings line:
skip 1

# specify the date field's format - not needed here since date is Y/M/D
date-format %Y-%m-%d %H:%M:%S

# since the CSV amounts have no currency symbol, add one:
#currency BNB

fields code, blkid, epoch, date, accfrom, accto, contract, amountin, amountout, value_usd, txfee, txfee_usd, hprice, txstatus, err, method

# DEBUG
#comment \n%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17
comment \n%code,%date,%amountin,%amountout,%txfee,%txstatus,%err,%method

description HF|%method
status *

# set the base account that this CSV file corresponds to
account1 expenses:bnb:hf:tx-fee        %txfee BNB
account2 assets:bnb:acc1

if %amountin ^0$
   amount3    -%amountout BNB
   account3   expenses:bnb:hf

if %amountout ^0$
   amount3    %amountin BNB
   account3   income:bnb:hf

Error during import, the issue:

$ hledger -f demo-test.csv print 
hledger: Error: /home/user/demo-test.csv:1-0:
1 | 2022-12-21 * (0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b) HF|Invest
  |     ; 0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b,2022-12-21 17:23:59,0,0.1,0.00152825,,,Invest
  |     expenses:bnb:hf:tx-fee        0.00152825 BNB
  |     assets:bnb:acc1
  |     expenses:bnb:hf                                     -0.1 BNB

This multi-commodity transaction is unbalanced.
There can't be more than one real posting with no amount.
(Remember to put two or more spaces between account and amount.)

When I create journal file with the same data, hledger processes it without any issue:

$ cat demo-test.journal 
2022-12-21 * (0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b) HF|Invest
     ; 0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b,2022-12-21 17:23:59,0,0.1,0.00152825,,,Invest
     expenses:bnb:hf:tx-fee        0.00152825 BNB
     assets:bnb:acc1
     expenses:bnb:hf                                     -0.1 BNB
$ hledger -f demo-test.journal print
2022-12-21 * (0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b) HF|Invest
    ; 0x34906d1622d7a8ddf5996d95c55a78c18452bb3dc20d82a74915a584a65d4a8b,2022-12-21 17:23:59,0,0.1,0.00152825,,,Invest
    expenses:bnb:hf:tx-fee     0.00152825 BNB
    assets:bnb:acc1
    expenses:bnb:hf           -0.10000000 BNB
$ hledger --version
hledger 1.27.1, linux-x86_64

I wanted to report the issue with simpler CSV file but in this case hledger works without any issue; that is a mystery:

$ cat demo.csv
"Txhash","DateTime","Value_IN(BNB)","Value_OUT(BNB)","TxnFee(BNB)","Status","ErrCode","Method"
"0x34906d","2022-12-21","0","0.1","0.00152825","","","Invest",""
"0x09b8ee","2022-12-30","0.15","0","0.0039526795","","","Withdraw",""
$ cat demo.csv.rules 
# skip the headings line:
skip 1

# specify the date field's format - not needed here since date is Y/M/D
date-format %Y-%m-%d

# since the CSV amounts have no currency symbol, add one:
#currency BNB

fields code, date, amountin, amountout, txfee, txstatus, err, method

description   DEMO|%method
status        *

account1  expenses:bnb:tx-fee
amount1   %txfee BNB

account2  assets:bnb:acc1

# DEBUG
#comment \n%1,%2,%3,%4,%5,%6,%7,%8,%9
#comment \n%1,%2,%3,%4,%5,%6,%7,%8,%9\n%code,%date,%amountin,%amountout,%txfee,%txstatus,%err,%method
comment \n%code,%date,%amountin,%amountout,%txfee,%txstatus,%err,%method

if %amountin ^0$
   amount3    -%amountout BNB
   account3   expenses:bnb:demo

if %amountout ^0$
   amount3    %amountin BNB
   account3   income:bnb:demo

No issue in this case:

$ hledger -f demo.csv print
2022-12-21 * (0x34906d) DEMO|Invest
    ; 0x24906d,2022-12-21,0,0.1,0.00152825,,,Invest
    expenses:bnb:tx-fee     0.0015282500 BNB
    assets:bnb:acc1
    expenses:bnb:demo      -0.1000000000 BNB

2022-12-30 * (0x09b8ee) DEMO|Withdraw
    ; 0x09b8ee,2022-12-30,0.15,0,0.0039526795,,,Withdraw
    expenses:bnb:tx-fee    0.0039526795 BNB
    assets:bnb:acc1
    income:bnb:demo        0.1500000000 BNB

IMPORTANT UPDATE: I have found the source of this issue. Error in rules file, I define account1 as

account1 expenses:bnb:hf:tx-fee        %txfee BNB

and that is wrong, I have to define it as two items:

account1 expenses:bnb:hf:tx-fee
amount1  %txfee BNB

File demo.csv.rules was correct and that is why hledger processed file demo.csv without any issue...

This issue could be closed but I keep it open, maybe diagnostic could be improved...


UPDATE: sign in amount3 is wrong, it should be opposite (income should be negative and expense positive). This accounting error can be ignored, it has no impact to this issue..

@PSLLSP PSLLSP added the A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. label Jan 8, 2023
@PSLLSP
Copy link
Author

PSLLSP commented Jan 8, 2023

One more update, maybe new issue. CSV file cannot be used with hledger-web. I have to use hledger -f file.csv print > file.journal and after that I can start web with hledger-web -f file.journal. When I try hledger-web -f file.csv, there is no error, web server is started but it shows only 0 values, like journal is empty...

UPDATE: It works. I do not know why I met this issue but when I try it with the latest hledger, then hledger-web can work with CSV file, no issue. On the other hand, I was not able to work with CSV file, maybe that another hledger-web server was already running or I tried with older version of hledger...

@simonmichael simonmichael added the csv The csv file format, csv output format, or generally CSV-related. label Jan 11, 2023
@simonmichael
Copy link
Owner

Hi @PSLLSP, are any issues still unresolved here ?

@PSLLSP
Copy link
Author

PSLLSP commented Jan 11, 2023

This issue could be closed, it was user error. I just was fighting with this issue for several hours. See "IMPORTANT UPDATE" in the issue description.

@simonmichael
Copy link
Owner

I agree that the result of a rule like

account1 expenses:bnb:hf:tx-fee        1 BNB

is confusing; I think we should disallow it, since it's not a valid account name (it contains 2-plus-wide whitespace).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-BUG Something wrong, confusing or sub-standard in the software, docs, or user experience. csv The csv file format, csv output format, or generally CSV-related.
Projects
None yet
Development

No branches or pull requests

2 participants