Chia-Network / chia-blockchain

Chia blockchain python implementation (full node, farmer, harvester, timelord, and wallet)
Apache License 2.0
10.82k stars 2.03k forks source link

[Bug] light wallet transactions from rpc API "get_transactions" do not add up to wallet balance #15006

Closed RudolfAchter closed 1 year ago

RudolfAchter commented 1 year ago

What happened?

For Tax preparation of my Crypto stuff i use https://www.accointing.com/ which does not already support a chia wallet. So i need to create csv Data myself. I need to track XCH bought from OKX -> transferred to my farm wallet -> transferred to my cold wallet and maybe all the way back until selling at OKX

so spacescan.io Tax tool doesn't help me here either because it isn't aware of my OKX Account.

So i thought to use the rpc api and export the transactions to csv. I do this with powershell

When i do something like this. Then the transactions add up to a too high balance. So there something is not right My "own" Get-ChiaTransactions simply gets the whole json Data from "get_transactions" for one wallet

    $txns=Get-ChiaTransactions -Wallet 1 | Sort-Object confirmed_at_height

    $balance=0
    $txns | ForEach-Object {
        $tx=$_ 
        if($tx.type -eq "0"){ #TxIn
            $balance += $tx.amount
        } 
        elseif($tx.type -eq "1"){ #TxOut
            if($tx.to_address -notin $myAddresses){
                $balance -= $tx.amount
            }
        }
        elseif($tx.type -eq "3"){ #Reward
            $balance += $tx.amount
        }
        $tx | Add-Member -MemberType NoteProperty -Name Balance -Value $balance
        $tx | Add-Member -MemberType NoteProperty -Name AddSum -Value ($tx.additions.amount | Measure-Object -Sum).Sum
        $tx
    } | Format-Table -AutoSize created_at_datetime,confirmed_at_height,type,amount,balance
    $balance

It seems like transactions like "getting my coin change back", or "transactions to myself" are somehow double counted. This is also something @steppsr has messed around with, but i think his "workaround" also still has problems:

The nearest i can get to the right wallet balance is something like this: This code brings me the right balance for my USDS wallet (wallet 25 in my case) but the balance for my XCH wallet is too low. At one point my "calculated balance" goes into negative (-282519999996) which is exact the amount missing to get the correct wallet balance shown in the gui.

    $txns=Get-ChiaTransactions -Wallet 1 | Sort-Object confirmed_at_height

    $balance=0
    $txns | ForEach-Object {
        $tx=$_
        if($tx.type -eq "0"){
            foreach($addition in $tx.additions) {
                if($addition.puzzle_hash -notin ($txns | ?{$_.type -eq "1" -and $_.confirmed_at_height -eq $tx.confirmed_at_height}).additions.puzzle_hash){
                    $balance += $addition.amount
                }
                else{
                    Write-Warning("DoubleCount: " + $tx.confirmed_at_height + " puzzle_hash: "+ $addition.puzzle_hash + " amount:" + $addition.amount)
                }
            }

        }
        elseif($tx.type -eq "1"){
            if($tx.to_address -notin $myAddresses){
                $balance -= $tx.amount
            }
        }
        elseif($tx.type -eq "3"){
            $balance += $tx.amount
        }
        $tx | Add-Member -MemberType NoteProperty -Name Balance -Value $balance
        $tx | Add-Member -MemberType NoteProperty -Name AddSum -Value ($tx.additions.amount | Measure-Object -Sum).Sum
        $tx
    } | Format-Table -AutoSize created_at_datetime,confirmed_at_height,type,amount,balance
    $balance

Somehow i need to know how to track the "double counts" in my light wallet, or you change the implementation in a way that the transaction amount add up.

Version

1.7.1

What platform are you using?

Linux

What ui mode are you using?

CLI

Relevant log output

chia wallet get_transactions -f xxxxxx --no-paginate should add up to current wallet balance, which it does not.
MumfMeisterT commented 1 year ago

We use heuristics to determine transactions because of the privacy features of the blockchain, it is not possible to obtain exact details.There may be other products that do a better job of determining the transactions from the raw blockchain data.

github-actions[bot] commented 1 year ago

This issue has not been updated in 14 days and is now flagged as stale. If this issue is still affecting you and in need of further review, please comment on it with an update to keep it from auto closing in 7 days.

emlowe commented 1 year ago

Closing issue