Skip to content

Add binanceReturns: Binance law enforcement returns (.xlsx) - #661

Merged
abrignoni merged 10 commits into
abrignoni:mainfrom
Maxitelmo:feat/binance-returns
Nov 20, 2026
Merged

abrignoni merged 10 commits into
abrignoni:mainfrom
Maxitelmo:feat/binance-returns

Conversation

@Maxitelmo

@Maxitelmo Maxitelmo commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

Adds scripts/artifacts/binanceReturns.py, which parses the .xlsx workbook Binance produces in response to legal process. It reads both layouts seen so far: the 2022 one, with 8 or 18 sheets and an Account Balance sheet, and the 2024 one, with 30 sheets and an Assets Overview sheet. It produces 38 artifacts under the Binance Returns category. A workbook is recognised by its content, a Customer Information sheet plus an Access Logs sheet, not by its file name. It uses openpyxl, which is already in requirements.txt.

What it covers:

  • Customer Information blocks: basic info, API, KYC, address validation and sub-accounts.
  • Balances: holdings, earn products, futures positions, balances and collateral, margin, and Current Assets & Wallets.
  • Transaction history: deposits, withdrawals and attempted withdrawals, order history, asset logs, P2P, fiat history, fiat orders and payment methods, OTC, Binance Pay and Send, cards, gift cards and loans.
  • Access logs.
  • Approved devices, with their key/value attributes in a separate artifact.
  • Images embedded in the workbook. These are registered with check_in_embedded_media, and the format is sniffed from the bytes rather than the extension.

Nothing in a workbook is dropped silently:

  • An unrecognised sheet goes to Other Sheets.
  • An unrecognised block goes to Other Blocks.
  • A column an artifact does not name goes to Other Columns (as produced) on the same row.
  • Parsing Notes records what the parser decided, per workbook.
  • Block Index lists every block it found, with its header and record count.

Rows a balance block lists for an asset or pair the account holds nothing of are not listed. A row counts as empty when every amount is zero, blank or - and it carries no column the artifact does not name. Current Assets keeps such a row if it has a deposit address or a label/memo. Block Index gives the number of rows left out per block, in Rows Not Listed, and Parsing Notes gives the count for Current Assets. In the 2022 returns, compared cell by cell, more than 90% of the futures and margin rows and 96% of the Current Assets rows were empty this way.

For a new or changed artifact

  • Ran the tool against a real extraction and confirmed the row counts, not just that it imports. I ran it against four real Binance returns: three in the 2022 layout and one in the 2024 layout. A separate script compared every non-empty cell of every sheet with the report. Of the 163,254 source cells, every one is either in the report or in one of the 5,659 rows left out for holding no position. All 27 embedded images are in media/ byte for byte, and all 865 device attribute pairs are present.
  • Ran python admin/scripts/check_artifact_output.py <report folder> on that report and fixed or documented every finding. Each empty, constant or identical column was checked against the source cells; none is a parsing defect. Each one is named in that artifact's notes, and the checker now reports nothing to look at.
  • Checked it against a second app data directory. This does not apply: a return is one workbook per account. The four returns are four different accounts.
  • notes and description say only what the data shows, and the numbers were re-derived from the finished run. The module has no sample_data block.

Anything reviewers should know

  • No real return data is in this PR. The test case (caseSYNTH001) is built by admin/test/scripts/gen_binance_synth.py, and every value in it is invented: documentation IP ranges, example.com addresses, SYNTH identifiers and plain colour images. Before pushing, I searched every committed file, the cells and XML inside the test zips, and the commit messages for every value in the real returns. Only generic words and round amounts matched.
  • Margin can show "No data found" even when the return has margin blocks. In the returns tested, every margin row was zero, so none is listed. Block Index still shows the blocks and how many rows were left out of each.
  • Amounts and timestamps are reported as the text the return holds. Columns the return labels UTC are converted. The return does not say at what time the balances were taken, and the notes say so.
  • Related tables are merged into one artifact, with a Sheet or Block column saying where each row came from. Examples are Spot together with the 2024 Funding, staking and Autoinvest blocks, cross and isolated margin, and fiat deposits together with fiat withdrawals. This keeps the module at 38 artifacts instead of 47.
  • Only these two layouts have been seen. A workbook with another shape is still read, and what the parser does not recognise lands in Other Sheets, Other Blocks or Other Columns rather than being lost. More returns, especially other years, would help confirm coverage.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc

Maxitelmo and others added 8 commits November 11, 2026 15:23
Reads the one-workbook-per-account returns Binance produces, recognised by
content (a Customer Information and an Access Logs sheet), not file name.
Layouts observed in three 2022 returns, one of 8 sheets and two of 18, are
handled: plain table sheets, the block-structured Customer Information and
Account Balance sheets, Approved Devices with its Key/Value continuation rows
and bilingual headers, and images embedded on KYC Documents.

Values are reported as the text the workbook holds. Only columns the return
labels UTC are converted; other times carry no zone and stay as produced.
Unknown columns, Account Balance blocks and sheets are kept, and anything the
module cannot place is listed in Parsing Notes.

Verified against the three returns by row count against an independent
structure scan; no values from them are in this change. The generator builds
a fabricated return for the test cases.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
One case, SYNTH001, built from gen_binance_synth.py: an 18-sheet and an
8-sheet workbook plus a decoy .xlsx, with baselines for all 29 artifacts.
Every value is fabricated; the fixture's values were checked against the
three returns the module was written from and share none beyond headers,
block titles, generic vocabulary and round amounts. One invented amount that
happened to coincide with a real one was changed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
check_artifact_output.py run on the three observed returns flagged empty,
constant and identical columns. Each was checked against the source cells:
none is a parsing defect. Margin Cross Total Balance and Order History Type
hold a single space on every row, which is trimmed to blank; the Margin
Isolated balance columns are equal in the source itself. The notes now name
each column and the observation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
- Tabler icon names for the four artifacts that used Feather names
  (layout-grid, stack-2, login, device-mobile).
- A UTC column whose value cannot be read is None, not an empty string, and
  the conversion uses convert_ts_human_to_utc from ilapfuncs.
- A small helper replaces a lambda assigned inside a loop.

The Access Logs baseline is re-recorded: its unreadable timestamp is now null.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
A Binance return produced in 2024 has 30 sheets where the 2022 returns had 8
or 18. The module now reads both layouts:

- Customer Information: the KYC Approved Info, Address Validation and
  Sub-accounts blocks get their own artifacts, and an unrecognised block is
  reported cell for cell under Customer Information - Other Blocks. Before,
  such blocks were only named in Parsing Notes and their rows reached no
  table. Basic Information reads First Name, Last Name, Tax ID and TnC.
- Assets Overview, which replaces Account Balance, is read with the same
  block layouts, plus Overview, Asset Positions and Earn Products. A layout
  is now matched on the columns every observed header holds, so the added
  USDT Equivalent column does not stop 2022 blocks matching.
- New sheets: Spot and Funding Asset Log, P2P, Attempted Withdrawal History,
  Margin Order, Fiat payment methods, OTC Payment ID History, Gift Card
  Creation and Transfer, Loan Order and Repayment History.
- The camel-case Fiat layout of the 2024 return (Fiat Deposit History, Fiat
  Withdrawal History and Fiat Trades) is reported under Fiat Orders; a Fiat
  sheet whose header matches neither layout goes to Other Sheets.
- Columns added in 2024 to sheets the 2022 layout had (Account Type, USDT,
  Port Number, USDT equivalent and others) are reported in their own columns.

Times the 2024 sheets label UTC+8, or carry a zone inside the value, are
reported as produced and not converted.

The three 2022 returns used to write the module give the same row counts
as before. The synthetic case gains a workbook in the 2024 layout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
Account Balance, Assets Overview and Current Assets & Wallets list a row for
every asset or pair whether or not the account holds any. In the 2022
returns compared cell by cell, more than 90% of the Futures Positions,
Futures Balances and Margin rows, and 96% of the Current Assets rows, held
nothing. Those rows are no longer listed:

- A balance row is listed when one of its amounts is not zero (blank, '-'
  and any number equal to zero count as zero) or it carries a column the
  artifact does not name. Block Index gains Rows Not Listed, per block.
- A Current Assets row is also kept for a deposit address, a label or memo,
  or an extra column. Parsing Notes gives how many rows were left out.

Related artifacts are merged, 47 to 38, each with a Sheet or Block column
saying where the row came from:

- Spot and the 2024 Funding, staking and Autoinvest blocks: Holdings
- Margin - Cross and Margin - Isolated: Margin
- Order History and Margin Order: Order History
- the 2022 Fiat Deposit and Fiat Withdrawal History: Fiat History
- Gift Card Creation and Transfer: Gift Cards
- Loan Order and Repayment History: Loans
- KYC Approved Info, Address Validation and Sub-accounts: Customer
  Information - KYC and Address
- the two Other Blocks artifacts: Other Blocks

Checked against the three 2022 returns: every source cell is either in the
report or in a row left out for holding no position, and the synthetic case
now has zero rows to leave out beside zero rows kept for an address or memo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
After the merge, six functions kept the name of one of the artifacts folded
into them. The function name is also the LAVA table name, so it should
match the artifact. Nothing has shipped under the old names.

- binanceBalanceSpot -> binanceBalanceHoldings
- binanceBalanceMarginCross -> binanceBalanceMargin
- binanceFiatDeposits -> binanceFiatHistory
- binanceGiftCardCreation -> binanceGiftCards
- binanceLoanOrders -> binanceLoans
- binanceBalanceOther -> binanceOtherBlocks (it also carries Customer
  Information blocks)

Test data and baselines regenerated; the rows are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
check_artifact_output.py, run on the report from the four returns tested
(three in the 2022 layout, one in the 2024 layout), reported 70 empty,
constant and identical columns across 20 artifacts. Each is now named
in that artifact's notes, in a sentence saying what was observed in the
returns tested, so the checker reads it as documented.

None is a parsing defect: the verifier that compares every source cell
with the report found every cell in place. Other Columns (as produced) is
empty where no return added an unnamed column, and Sheet and Block are
constant where every listed row came from one sheet or block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qn6BCzbC9Y1p1oUTAc
@Maxitelmo
Maxitelmo marked this pull request as ready for review November 12, 2026 00:59
Maxitelmo and others added 2 commits November 12, 2026 02:51
The module matches every .xlsx, so a run over another provider's return put a
Binance Returns category in the report, with a Parsing Notes row for each
workbook that was not a Binance return. Those workbooks, and ones that cannot
be opened, are now named in the run log and produce no row. The Parsing Notes
baseline is re-recorded: the decoy workbook's row is gone, the other 12 match.

A header that repeats an earlier column's name lost the first column's value,
since records are keyed by header name. The repeat now carries its column
letter, so it is reported under Other Columns and listed in Parsing Notes.

Each workbook is read once per run however many are matched. The cache held 16
and did not keep a failed open, so a return of 17 or more workbooks was reread
by each of the 38 artifacts, and an unreadable one was retried by each.

The module docstring said nothing in a recognised workbook is dropped; it now
names the zero-amount rows that are left out and where they are counted.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qmjfBRpcweABqm9Epc
The pattern was *.xlsx, which off Windows does not match a workbook whose
extension is in upper case; the module lower-cased the name only after the
seeker had already left the file out. The extension is now matched with one
bracket class per letter. Checked with fnmatch.fnmatchcase: .xlsx, .XLSX and
.Xlsx match, .xls, .xlsm and .xlsx.bak do not. The case file records the new
pattern; the case zips and baselines are unchanged and all 38 units match.

A block's 'Note:' row was read as its note only when it stood in place of the
header, as in every return observed. Under a header row it became a record.
It is now the block's note in both positions, before any record. The second
position has only been exercised on synthetic rows.

The misplaced indentation of seven "notes" keys and the lines over 120
characters are fixed; the notes and descriptions read the same as before.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Krp9qmjfBRpcweABqm9Epc
@Maxitelmo
Maxitelmo force-pushed the feat/binance-returns branch from 0424767 to ab9244a Compare November 12, 2026 02:51
@abrignoni
abrignoni merged commit c83ad08 into abrignoni:main Nov 20, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants